Skip to content

Commit

Permalink
Merge pull request #2209 from planetarium/release/1.4.0
Browse files Browse the repository at this point in the history
🚀 release: 1.4.0
  • Loading branch information
riemannulus authored Nov 7, 2023
2 parents 2cd67a8 + f8d681a commit 1969277
Show file tree
Hide file tree
Showing 584 changed files with 10,455 additions and 6,929 deletions.
84 changes: 2 additions & 82 deletions .Lib9c.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Action.State;
using Libplanet.Blockchain;
using Libplanet.Blockchain.Policies;
using Libplanet.Common;
using Libplanet.Crypto;
using Libplanet.RocksDBStore;
using Libplanet.Store;
using Libplanet.Store.Trie;
using Libplanet.Types.Assets;
using Libplanet.Types.Blocks;
using Nekoyume.Action.Loader;
using Nekoyume.Blockchain;
using Nekoyume.Blockchain.Policy;
using Serilog;
using Serilog.Events;

namespace Lib9c.Benchmarks
{
Expand Down Expand Up @@ -89,7 +82,7 @@ static void Main(string[] args)
var stateStore = new TrieStateStore(stateKeyValueStore);
var actionEvaluator = new ActionEvaluator(
_ => policy.BlockAction,
new BlockChainStates(store, stateStore),
stateStore,
new NCActionLoader());
var chain = new BlockChain(
policy,
Expand Down Expand Up @@ -128,15 +121,7 @@ static void Main(string[] args)
block.Transactions.Count()
);

chain.DetermineBlockStateRootHash(block, out IReadOnlyList<IActionEvaluation> blockEvals);
SetStates(
chain.Id,
store,
stateStore,
block,
blockEvals.ToArray(),
buildStateReferences: true
);
chain.DetermineBlockStateRootHash(block, out IReadOnlyList<ICommittedActionEvaluation> blockEvals);
txs += block.Transactions.LongCount();
actions += block.Transactions.Sum(tx =>
tx.Actions is { } customActions ? customActions.LongCount() : 0);
Expand All @@ -151,70 +136,5 @@ static void Main(string[] args)
Console.WriteLine("Average per action\t{0}ms", execActionsTotalMilliseconds / actions);
Console.WriteLine("Total elapsed\t{0}", ended - started);
}

// Copied from BlockChain.DetermineBlockStateRootHash().
private static void SetStates(
Guid chainId,
IStore store,
IStateStore stateStore,
Block block,
IReadOnlyList<IActionEvaluation> actionEvaluations,
bool buildStateReferences
)
{
IImmutableDictionary<KeyBytes, IValue> totalDelta = actionEvaluations.GetRawTotalDelta();

if (!stateStore.ContainsStateRoot(block.StateRootHash))
{
HashDigest<SHA256>? prevStateRootHash = store.GetStateRootHash(block.PreviousHash);
ITrie stateRoot = stateStore.Commit(prevStateRootHash, totalDelta);
}
}

// Copied from ActionEvaluationsExtensions.GetTotalDelta().
private static ImmutableDictionary<string, IValue> GetTotalDelta(
IReadOnlyList<IActionEvaluation> actionEvaluations,
Func<Address, string> toStateKey,
Func<(Address, Currency), string> toFungibleAssetKey)
{
IImmutableSet<Address> stateUpdatedAddresses = actionEvaluations
.SelectMany(a => a.OutputState.Delta.StateUpdatedAddresses)
.ToImmutableHashSet();
IImmutableSet<(Address, Currency)> updatedFungibleAssets = actionEvaluations
.SelectMany(a => a.OutputState.Delta.UpdatedFungibleAssets)
.ToImmutableHashSet();

IAccount lastStates = actionEvaluations.Count > 0
? actionEvaluations[actionEvaluations.Count - 1].OutputState
: null;
ImmutableDictionary<string, IValue> totalDelta =
stateUpdatedAddresses.ToImmutableDictionary(
toStateKey,
a => lastStates?.GetState(a)
).SetItems(
updatedFungibleAssets.Select(pair =>
new KeyValuePair<string, IValue>(
toFungibleAssetKey(pair),
new Bencodex.Types.Integer(
lastStates?.GetBalance(pair.Item1, pair.Item2).RawValue ?? 0
)
)
)
);

return totalDelta;
}

// Copied from KeyConverters.ToStateKey().
private static string ToStateKey(Address address) => address.ToHex().ToLowerInvariant();

// Copied from KeyConverters.ToFungibleAssetKey().
private static string ToFungibleAssetKey(Address address, Currency currency) =>
"_" + address.ToHex().ToLowerInvariant() +
"_" + ByteUtil.Hex(currency.Hash.ByteArray).ToLowerInvariant();

// Copied from KeyConverters.ToFungibleAssetKey().
private static string ToFungibleAssetKey((Address, Currency) pair) =>
ToFungibleAssetKey(pair.Item1, pair.Item2);
}
}
49 changes: 32 additions & 17 deletions .Lib9c.DevExtensions.Tests/Action/CreateOrReplaceAvatarTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using Bencodex.Types;
using Lib9c.DevExtensions.Action;
using Lib9c.Tests;
using Lib9c.Tests.Action;
using Libplanet.Action.State;
using Libplanet.Crypto;
Expand All @@ -24,10 +25,11 @@ namespace Lib9c.DevExtensions.Tests.Action
public class CreateOrReplaceAvatarTest
{
private readonly IAccount _initialStates;
private readonly TableSheets _tableSheets;

public CreateOrReplaceAvatarTest()
{
_initialStates = new Lib9c.Tests.Action.MockStateDelta();
_initialStates = new Account(MockState.Empty);

#pragma warning disable CS0618
var ncgCurrency = Currency.Legacy("NCG", 2, null);
Expand All @@ -36,6 +38,7 @@ public CreateOrReplaceAvatarTest()
GoldCurrencyState.Address,
new GoldCurrencyState(ncgCurrency).Serialize());
var sheets = TableSheetsImporter.ImportSheets();
_tableSheets = new TableSheets(sheets);
foreach (var (key, value) in sheets)
{
_initialStates = _initialStates.SetState(
Expand Down Expand Up @@ -69,16 +72,8 @@ public CreateOrReplaceAvatarTest()
int.MaxValue,
new[]
{
(10111000, 0),
(10111000, 21), // 21: See also EnhancementCostSheetV2.csv
(10211000, 0),
(10211000, 21),
(10311000, 0),
(10311000, 21),
(10411000, 0),
(10411000, 21),
(10511000, 0),
(10511000, 21),
(ItemSubType.Weapon, 0),
(ItemSubType.Weapon, 21), // 21: See also EnhancementCostSheetV2.csv
},
new[]
{
Expand Down Expand Up @@ -291,12 +286,22 @@ public void Serialize(
int ear,
int tail,
int level,
(int equipmentId, int level)[]? equipments,
(ItemSubType itemSubType, int level)[]? equipmentData,
(int consumableId, int count)[]? foods,
int[]? costumeIds,
(int runeId, int level)[]? runes,
(int stageId, int[] crystalRandomBuffIds)? crystalRandomBuff)
{
var equipments = new List<(int, int)>();
if (!(equipmentData is null))
{
foreach (var data in equipmentData)
{
var row = _tableSheets.EquipmentItemRecipeSheet.Values.First(r =>
r.ItemSubType == data.itemSubType);
equipments.Add((row.ResultEquipmentId, data.level));
}
}
var action = new CreateOrReplaceAvatar(
avatarIndex,
name,
Expand All @@ -305,7 +310,7 @@ public void Serialize(
ear,
tail,
level,
equipments,
equipments.ToArray(),
foods,
costumeIds,
runes,
Expand Down Expand Up @@ -348,12 +353,22 @@ public void Execute_Success(
int ear,
int tail,
int level,
(int equipmentId, int level)[]? equipments,
(ItemSubType itemSubType, int level)[]? equipmentData,
(int consumableId, int count)[]? foods,
int[]? costumeIds,
(int runeId, int level)[]? runes,
(int stageId, int[] crystalRandomBuffIds)? crystalRandomBuff)
{
var equipments = new List<(int, int)>();
if (!(equipmentData is null))
{
foreach (var data in equipmentData)
{
var row = _tableSheets.EquipmentItemRecipeSheet.Values.First(r =>
r.ItemSubType == data.itemSubType);
equipments.Add((row.ResultEquipmentId, data.level));
}
}
var agentAddr = new PrivateKey().ToAddress();
Execute(
_initialStates,
Expand All @@ -366,7 +381,7 @@ public void Execute_Success(
ear,
tail,
level,
equipments,
equipments.ToArray(),
foods,
costumeIds,
runes,
Expand Down Expand Up @@ -444,7 +459,7 @@ private static void Execute(
{
PreviousState = previousStates,
Signer = agentAddr,
Random = new TestRandom(),
RandomSeed = 0,
Rehearsal = false,
BlockIndex = blockIndex,
});
Expand Down Expand Up @@ -478,7 +493,7 @@ private static void Execute(
var recipe = equipmentItemRecipeSheet.OrderedList!.First(r =>
r.ResultEquipmentId == equipment.Id);
Assert.NotNull(recipe);
Assert.Equal(3, recipe.SubRecipeIds.Count);
Assert.Equal(2, recipe.SubRecipeIds.Count);
Assert.True(equipmentItemSubRecipeSheetV2.TryGetValue(
recipe.SubRecipeIds[1],
out var subRecipe));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
#nullable enable

using System;
using System.Collections.Generic;
using System.Linq;
using Lib9c.DevExtensions.Action.Factory;
using Lib9c.Tests;
using Nekoyume.Model.Item;
using Xunit;

namespace Lib9c.DevExtensions.Tests.Action.Factory
{
public class CreateOrReplaceAvatarFactoryTest
{
private readonly TableSheets _tableSheets;

public CreateOrReplaceAvatarFactoryTest()
{
_tableSheets = new TableSheets(TableSheetsImporter.ImportSheets());
}

[Theory]
[MemberData(
nameof(CreateOrReplaceAvatarTest.Get_Execute_Success_MemberData),
Expand All @@ -22,12 +32,23 @@ public void TryGetByBlockIndex_Success(
int ear,
int tail,
int level,
(int equipmentId, int level)[]? equipments,
(ItemSubType itemSubType, int level)[]? equipmentData,
(int consumableId, int count)[]? foods,
int[]? costumeIds,
(int runeId, int level)[]? runes,
(int stageId, int[] crystalRandomBuffIds)? crystalRandomBuff)
{
var equipments = new List<(int, int)>();
if (!(equipmentData is null))
{
foreach (var data in equipmentData)
{
var row = _tableSheets.EquipmentItemRecipeSheet.Values.First(r =>
r.ItemSubType == data.itemSubType);
equipments.Add((row.ResultEquipmentId, data.level));
}
}

var (e, r) = CreateOrReplaceAvatarFactory
.TryGetByBlockIndex(
blockIndex,
Expand All @@ -38,7 +59,7 @@ public void TryGetByBlockIndex_Success(
ear,
tail,
level,
equipments,
equipments.ToArray(),
foods,
costumeIds,
runes,
Expand Down
2 changes: 1 addition & 1 deletion .Lib9c.DevExtensions.Tests/Action/FaucetCurrencyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public FaucetCurrencyTest(ITestOutputHelper outputHelper)
_crystal = Currency.Legacy("CRYSTAL", 18, null);
#pragma warning restore CS0618

_initialState = new MockStateDelta(
_initialState = new Account(
MockState.Empty
.AddBalance(GoldCurrencyState.Address, _ncg * int.MaxValue));

Expand Down
2 changes: 1 addition & 1 deletion .Lib9c.DevExtensions.Tests/Action/FaucetRuneTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public FaucetRuneTest(ITestOutputHelper outputHelper)
.WriteTo.TestOutput(outputHelper)
.CreateLogger();

_initialState = new Lib9c.Tests.Action.MockStateDelta();
_initialState = new Account(MockState.Empty);
var sheets = TableSheetsImporter.ImportSheets();
foreach (var (key, value) in sheets)
{
Expand Down
2 changes: 2 additions & 0 deletions .Lib9c.StateService.Shared/RemoteEvaluationRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ namespace Lib9c.StateService.Shared;
public class RemoteEvaluationRequest
{
public byte[] PreEvaluationBlock { get; set; }

public byte[] BaseStateRootHash { get; set; }
}
Loading

0 comments on commit 1969277

Please sign in to comment.