Skip to content

Commit

Permalink
Merge pull request #2160 from greymistcube/bump/libplanet-to-3.4.0
Browse files Browse the repository at this point in the history
⬆️ Bump libplanet to 3.4.0
  • Loading branch information
greymistcube authored Oct 4, 2023
2 parents e8313d2 + c453158 commit f162e5d
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 73 deletions.
10 changes: 1 addition & 9 deletions .Lib9c.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,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 Down
9 changes: 4 additions & 5 deletions .Lib9c.Tests/Action/ActionEvaluationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ public void Serialize_With_MessagePack(Type actionType)
action,
_signer,
1234,
_states,
_states.Trie.Hash,
null,
_states,
_states.Trie.Hash,
0,
new Dictionary<string, IValue>()
);
Expand All @@ -110,9 +110,8 @@ public void Serialize_With_MessagePack(Type actionType)
var deserialized = MessagePackSerializer.Deserialize<NCActionEvaluation>(b);
Assert.Equal(evaluation.Signer, deserialized.Signer);
Assert.Equal(evaluation.BlockIndex, deserialized.BlockIndex);
var dict = (Dictionary)deserialized.OutputState.GetState(default)!;
Assert.Equal("value", (Text)dict["key"]);
Assert.Equal(_currency * 10000, deserialized.OutputState.GetBalance(_signer, _currency));
Assert.Equal(_states.Trie.Hash, deserialized.OutputState);
Assert.Equal(_states.Trie.Hash, deserialized.PreviousState);
if (actionType == typeof(RewardGold))
{
Assert.Null(deserialized.Action);
Expand Down
2 changes: 1 addition & 1 deletion .Lib9c.Tests/Action/MockStateDelta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private MockStateDelta(IAccountState baseState, IAccountDelta delta)
TotalUpdatedFungibles = ImmutableDictionary<(Address, Currency), BigInteger>.Empty;
}

public ITrie Trie => throw new NotSupportedException();
public ITrie Trie => new MerkleTrie(new MemoryKeyValueStore());

/// <inheritdoc/>
public IAccountDelta Delta { get; private set; }
Expand Down
3 changes: 3 additions & 0 deletions .Lib9c.Tests/Action/Snapshot/TransferAsset0SnapshotTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Lib9c.Tests.Action.Snapshot
using Libplanet.Action.State;
using Libplanet.Common;
using Libplanet.Crypto;
using Libplanet.Store.Trie;
using Libplanet.Types.Assets;
using Nekoyume.Action;
using Nekoyume.Helper;
Expand Down Expand Up @@ -66,6 +67,7 @@ public Task TransferCrystal()
.Verify(outputState)
.IgnoreMembersWithType<IImmutableSet<(Address, Currency)>>()
.IgnoreMembersWithType<IImmutableDictionary<(Address, Currency), BigInteger>>()
.IgnoreMembersWithType<ITrie>()
.UseTypeName((Text)GetActionTypeId<TransferAsset0>())
.UseMethodName($"{nameof(TransferCrystal)}.summary");
var fungibles = Verifier
Expand Down Expand Up @@ -106,6 +108,7 @@ public Task TransferWithMemo()
.Verify(outputState)
.IgnoreMembersWithType<IImmutableSet<(Address, Currency)>>()
.IgnoreMembersWithType<IImmutableDictionary<(Address, Currency), BigInteger>>()
.IgnoreMembersWithType<ITrie>()
.UseTypeName((Text)GetActionTypeId<TransferAsset0>())
.UseMethodName($"{nameof(TransferWithMemo)}.summary");
var fungibles = Verifier
Expand Down
2 changes: 1 addition & 1 deletion .Lib9c.Tools/SubCommand/Market.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void Query(
.Reverse()
.Where(tx => includeFails ||
!(chain.GetTxExecution(block.Hash, tx.Id) is { } e) ||
e is TxSuccess)
!e.Fail)
.SelectMany(tx => tx.Actions is { } actions
? actions.Reverse().Select(a => (tx, (ActionBase)actionLoader.LoadAction(block.Index, a)))
: Enumerable.Empty<(Transaction, ActionBase)>());
Expand Down
22 changes: 4 additions & 18 deletions .Lib9c.Tools/SubCommand/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ IStateStore stateStore
block.Index,
block.Hash
);
IReadOnlyList<IActionEvaluation> delta;

var actionLoader = TypedActionLoader.Create(
typeof(ActionBase).Assembly, typeof(ActionBase));
var actionEvaluator = new ActionEvaluator(
Expand All @@ -126,30 +126,16 @@ IStateStore stateStore
? BlockChain.DetermineGenesisStateRootHash(
actionEvaluator,
preEvalBlock,
out delta)
out _)
: chain.DetermineBlockStateRootHash(
preEvalBlock,
out delta);
out _);
DateTimeOffset now = DateTimeOffset.Now;
if (invalidStateRootHashBlock is null && !stateRootHash.Equals(block.StateRootHash))
{
string blockDump = DumpBencodexToFile(
block.MarshalBlock(),
$"block_{block.Index}_{block.Hash}"
);
string deltaDump = DumpBencodexToFile(
new Dictionary(GetTotalDelta(
delta,
ToStateKey,
ToFungibleAssetKey,
ToTotalSupplyKey,
ValidatorSetKey)),
$"delta_{block.Index}_{block.Hash}"
);
string message =
$"Unexpected state root hash for block #{block.Index} {block.Hash}.\n" +
$" Expected: {block.StateRootHash}\n Actual: {stateRootHash}\n" +
$" Block file: {blockDump}\n Evaluated delta file: {deltaDump}\n";
$" Expected: {block.StateRootHash}\n Actual: {stateRootHash}\n";
if (!bypassStateRootHashCheck)
{
throw new CommandExitedException(message, 1);
Expand Down
2 changes: 1 addition & 1 deletion .Libplanet
Submodule .Libplanet updated 57 files
+43 −0 CHANGES.md
+3 −3 Libplanet.Action.Tests/Sys/InitializeTest.cs
+1 −0 Libplanet.Action/ActionContext.cs
+96 −0 Libplanet.Action/CommittedActionContext.cs
+60 −0 Libplanet.Action/CommittedActionEvaluation.cs
+88 −0 Libplanet.Action/ICommittedActionContext.cs
+35 −0 Libplanet.Action/ICommittedActionEvaluation.cs
+0 −3 Libplanet.Action/Random.cs
+351 −0 Libplanet.Action/State/AccountDiff.cs
+17 −0 Libplanet.Action/State/IBlockChainStates.cs
+5 −53 Libplanet.Explorer.Tests/GraphTypes/TxResultTypeTest.cs
+4 −0 Libplanet.Explorer.Tests/Queries/StateQueryTest.cs
+10 −8 Libplanet.Explorer.Tests/Queries/TransactionQueryGeneratedTest.cs
+4 −19 Libplanet.Explorer/GraphTypes/TxResult.cs
+3 −54 Libplanet.Explorer/GraphTypes/TxResultType.cs
+6 −32 Libplanet.Explorer/Queries/TransactionQuery.cs
+3 −7 Libplanet.Explorer/Store/LiteDBRichStore.cs
+3 −7 Libplanet.Explorer/Store/MySQLRichStore.cs
+9 −6 Libplanet.Net/Consensus/Context.cs
+4 −11 Libplanet.RocksDBStore/RocksDBStore.cs
+4 −51 Libplanet.Store/BaseStore.cs
+4 −15 Libplanet.Store/DefaultStore.cs
+5 −20 Libplanet.Store/IStore.cs
+2 −5 Libplanet.Store/MemoryStore.cs
+172 −0 Libplanet.Tests/Action/AccountDiffTest.cs
+2 −2 Libplanet.Tests/Action/ActionEvaluatorTest.cs
+75 −72 Libplanet.Tests/Blockchain/BlockChainTest.Append.cs
+27 −42 Libplanet.Tests/Blockchain/BlockChainTest.Internals.cs
+1 −1 Libplanet.Tests/Blockchain/BlockChainTest.ProposeBlock.cs
+2 −2 Libplanet.Tests/Blockchain/BlockChainTest.cs
+13 −11 Libplanet.Tests/Blockchain/Renderers/AnonymousActionRendererTest.cs
+21 −21 Libplanet.Tests/Blockchain/Renderers/LoggedActionRendererTest.cs
+3 −7 Libplanet.Tests/Store/ProxyStore.cs
+29 −46 Libplanet.Tests/Store/StoreTest.cs
+3 −9 Libplanet.Tests/Store/StoreTracker.cs
+2 −2 Libplanet.Tests/Store/TrieStateStoreTest.cs
+144 −0 Libplanet.Tests/Tx/TxExecutionTest.cs
+0 −74 Libplanet.Tests/Tx/TxFailureTest.cs
+0 −91 Libplanet.Tests/Tx/TxSuccessTest.cs
+181 −16 Libplanet.Types/Tx/TxExecution.cs
+0 −86 Libplanet.Types/Tx/TxFailure.cs
+0 −143 Libplanet.Types/Tx/TxSuccess.cs
+68 −27 Libplanet/Blockchain/BlockChain.Evaluate.cs
+15 −25 Libplanet/Blockchain/BlockChain.Swap.cs
+54 −77 Libplanet/Blockchain/BlockChain.TxExecution.cs
+1 −1 Libplanet/Blockchain/BlockChain.Validate.cs
+7 −2 Libplanet/Blockchain/BlockChain.cs
+18 −12 Libplanet/Blockchain/BlockChainStates.cs
+5 −0 Libplanet/Blockchain/NullChainStates.cs
+18 −14 Libplanet/Blockchain/Renderers/AnonymousActionRenderer.cs
+15 −13 Libplanet/Blockchain/Renderers/AtomicActionRenderer.cs
+6 −5 Libplanet/Blockchain/Renderers/Debug/RecordingActionRenderer.cs
+10 −9 Libplanet/Blockchain/Renderers/Debug/RenderRecord.cs
+8 −9 Libplanet/Blockchain/Renderers/Debug/ValidatingActionRenderer.cs
+13 −10 Libplanet/Blockchain/Renderers/IActionRenderer.cs
+9 −10 Libplanet/Blockchain/Renderers/LoggedActionRenderer.cs
+1 −1 Libplanet/Libplanet.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Security.Cryptography;
using Bencodex.Types;
using Libplanet.Action.State;
using Libplanet.Common;
using Libplanet.Crypto;
using Libplanet.Types.Assets;
using Libplanet.Types.Blocks;
Expand Down Expand Up @@ -39,9 +41,15 @@ public ValidatorSet GetValidatorSet(BlockHash? offset)
return new RemoteBlockState(_explorerEndpoint, offset).GetValidatorSet();
}

public IAccountState GetAccountState(BlockHash? offset)
public IAccountState GetAccountState(BlockHash? offset) =>
throw new NotImplementedException();

public IAccountState GetBlockState(BlockHash? offset)
{
return new RemoteBlockState(_explorerEndpoint, offset);
}

public IAccountState GetAccountState(HashDigest<SHA256>? hash) =>
throw new NotImplementedException();
}
}
15 changes: 8 additions & 7 deletions Lib9c.MessagePack/Action/NCActionEvaluation.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using Bencodex.Types;
using Lib9c.Formatters;
using Lib9c.Renderers;
using Libplanet.Crypto;
using Libplanet.Action.State;
using Libplanet.Common;
using MessagePack;

namespace Nekoyume.Action
Expand All @@ -27,16 +28,16 @@ public struct NCActionEvaluation
public long BlockIndex { get; set; }

[Key(3)]
[MessagePackFormatter(typeof(AccountStateDeltaFormatter))]
public IAccount OutputState { get; set; }
[MessagePackFormatter(typeof(HashDigestFormatter))]
public HashDigest<SHA256> OutputState { get; set; }

[Key(4)]
[MessagePackFormatter(typeof(ExceptionFormatter<Exception>))]
public Exception? Exception { get; set; }

[Key(5)]
[MessagePackFormatter(typeof(AccountStateDeltaFormatter))]
public IAccount PreviousState { get; set; }
[MessagePackFormatter(typeof(HashDigestFormatter))]
public HashDigest<SHA256> PreviousState { get; set; }

[Key(6)]
public int RandomSeed { get; set; }
Expand All @@ -50,9 +51,9 @@ public NCActionEvaluation(
ActionBase? action,
Address signer,
long blockIndex,
IAccount outputStates,
HashDigest<SHA256> outputStates,
Exception? exception,
IAccount previousStates,
HashDigest<SHA256> previousStates,
int randomSeed,
Dictionary<string, IValue> extra
)
Expand Down
35 changes: 35 additions & 0 deletions Lib9c.MessagePack/Formatters/HashDigestFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Buffers;
using System.Security.Cryptography;
using Libplanet.Common;
using MessagePack;
using MessagePack.Formatters;

namespace Lib9c.Formatters
{
public class HashDigestFormatter : IMessagePackFormatter<HashDigest<SHA256>>
{
public void Serialize(
ref MessagePackWriter writer,
HashDigest<SHA256> value,
MessagePackSerializerOptions options)
{
writer.Write(value.ToByteArray());
}

public HashDigest<SHA256> Deserialize(
ref MessagePackReader reader,
MessagePackSerializerOptions options)
{
options.Security.DepthStep(ref reader);

var bytes = reader.ReadBytes()?.ToArray();
if (bytes is null)
{
throw new InvalidOperationException();
}

return new HashDigest<SHA256>(bytes);
}
}
}
7 changes: 4 additions & 3 deletions Lib9c.Renderers/ActionEvaluation.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using Bencodex.Types;
using Libplanet.Action.State;
using Libplanet.Common;
using Libplanet.Crypto;
using Libplanet.Types.Tx;
using Nekoyume.Action;
Expand All @@ -19,11 +20,11 @@ public struct ActionEvaluation<T>

public TxId? TxId { get; set; }

public IAccount OutputState { get; set; }
public HashDigest<SHA256> OutputState { get; set; }

public Exception? Exception { get; set; }

public IAccount PreviousState { get; set; }
public HashDigest<SHA256> PreviousState { get; set; }

public int RandomSeed { get; set; }

Expand Down
31 changes: 5 additions & 26 deletions Lib9c.Renderers/Renderers/ActionRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System;
using Libplanet.Crypto;
using System.Security.Cryptography;
using Libplanet.Action;
using Libplanet.Action.Loader;
using Libplanet.Blockchain.Renderers;
using Libplanet.Types.Blocks;
using Libplanet.Action.State;
using Nekoyume.Action;
using Serilog;
using Bencodex.Types;
using Libplanet.Common;
using Nekoyume.Action.Loader;
#if UNITY_EDITOR || UNITY_STANDALONE
using UniRx;
Expand All @@ -33,7 +33,7 @@ public ActionRenderer()
_actionLoader = new NCActionLoader();
}

public void RenderAction(IValue action, IActionContext context, IAccount nextStates) =>
public void RenderAction(IValue action, ICommittedActionContext context, HashDigest<SHA256> nextState) =>
ActionRenderSubject.OnNext(new ActionEvaluation<ActionBase>
{
Action = context.BlockAction
Expand All @@ -42,16 +42,12 @@ public void RenderAction(IValue action, IActionContext context, IAccount nextSta
Signer = context.Signer,
BlockIndex = context.BlockIndex,
TxId = context.TxId,
OutputState = nextStates,
OutputState = nextState,
PreviousState = context.PreviousState,
RandomSeed = context.Random.Seed
});

public void RenderActionError(
IValue action,
IActionContext context,
Exception exception
)
public void RenderActionError(IValue action, ICommittedActionContext context, Exception exception)
{
Log.Error(exception, "{action} execution failed.", action);
ActionRenderSubject.OnNext(new ActionEvaluation<ActionBase>
Expand Down Expand Up @@ -96,22 +92,5 @@ public IObservable<ActionEvaluation<T>> EveryRender<T>() where T : ActionBase =>
RandomSeed = eval.RandomSeed,
Extra = eval.Extra,
});

public IObservable<ActionEvaluation<ActionBase>> EveryRender(Address updatedAddress) =>
ActionRenderSubject
.AsObservable()
.Where(eval => eval.OutputState.Delta.UpdatedAddresses.Contains(updatedAddress))
.Select(eval => new ActionEvaluation<ActionBase>
{
Action = eval.Action,
Signer = eval.Signer,
BlockIndex = eval.BlockIndex,
TxId = eval.TxId,
OutputState = eval.OutputState,
Exception = eval.Exception,
PreviousState = eval.PreviousState,
RandomSeed = eval.RandomSeed,
Extra = eval.Extra,
});
}
}
2 changes: 1 addition & 1 deletion Lib9c/Action/Stake.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public override IAccount Execute(IActionContext context)
{
var started = DateTimeOffset.UtcNow;
context.UseGas(1);
var states = context.PreviousState;
IAccount states = context.PreviousState;

// NOTE: Restrict staking if there is a monster collection until now.
if (states.GetAgentState(context.Signer) is { } agentState &&
Expand Down

0 comments on commit f162e5d

Please sign in to comment.