diff --git a/Libplanet.Extensions.ForkableActionEvaluator.Tests/ForkableActionEvaluatorTest.cs b/Libplanet.Extensions.ForkableActionEvaluator.Tests/ForkableActionEvaluatorTest.cs index cd5714f01..a28045b56 100644 --- a/Libplanet.Extensions.ForkableActionEvaluator.Tests/ForkableActionEvaluatorTest.cs +++ b/Libplanet.Extensions.ForkableActionEvaluator.Tests/ForkableActionEvaluatorTest.cs @@ -24,11 +24,11 @@ public void ForkEvaluation() ((101L, long.MaxValue), new PostActionEvaluator()), }); - Assert.Equal((Text)"PRE", Assert.Single(evaluator.Evaluate(new MockBlock(0))).Action); - Assert.Equal((Text)"PRE", Assert.Single(evaluator.Evaluate(new MockBlock(99))).Action); - Assert.Equal((Text)"PRE", Assert.Single(evaluator.Evaluate(new MockBlock(100))).Action); - Assert.Equal((Text)"POST", Assert.Single(evaluator.Evaluate(new MockBlock(101))).Action); - Assert.Equal((Text)"POST", Assert.Single(evaluator.Evaluate(new MockBlock(long.MaxValue))).Action); + Assert.Equal((Text)"PRE", Assert.Single(evaluator.Evaluate(new MockBlock(0), null)).Action); + Assert.Equal((Text)"PRE", Assert.Single(evaluator.Evaluate(new MockBlock(99), null)).Action); + Assert.Equal((Text)"PRE", Assert.Single(evaluator.Evaluate(new MockBlock(100), null)).Action); + Assert.Equal((Text)"POST", Assert.Single(evaluator.Evaluate(new MockBlock(101), null)).Action); + Assert.Equal((Text)"POST", Assert.Single(evaluator.Evaluate(new MockBlock(long.MaxValue), null)).Action); } [Fact] @@ -64,7 +64,7 @@ public void CheckPairs() class PostActionEvaluator : IActionEvaluator { public IActionLoader ActionLoader => throw new NotSupportedException(); - public IReadOnlyList Evaluate(IPreEvaluationBlock block) + public IReadOnlyList Evaluate(IPreEvaluationBlock block, HashDigest? baseStateroothash) { return new IActionEvaluation[] { @@ -79,7 +79,7 @@ public IReadOnlyList Evaluate(IPreEvaluationBlock block) 0, false, new AccountStateDelta(), - new Random(0), + 0, null, false), new AccountStateDelta(), @@ -91,7 +91,7 @@ public IReadOnlyList Evaluate(IPreEvaluationBlock block) class PreActionEvaluator : IActionEvaluator { public IActionLoader ActionLoader => throw new NotSupportedException(); - public IReadOnlyList Evaluate(IPreEvaluationBlock block) + public IReadOnlyList Evaluate(IPreEvaluationBlock block, HashDigest? baseStateRootHash) { return new IActionEvaluation[] { @@ -106,7 +106,7 @@ public IReadOnlyList Evaluate(IPreEvaluationBlock block) 0, false, new AccountStateDelta(), - new Random(0), + 0, null, false), new AccountStateDelta(), diff --git a/Libplanet.Extensions.ForkableActionEvaluator/ForkableActionEvaluator.cs b/Libplanet.Extensions.ForkableActionEvaluator/ForkableActionEvaluator.cs index 13c60a5ea..1e5f47214 100644 --- a/Libplanet.Extensions.ForkableActionEvaluator/ForkableActionEvaluator.cs +++ b/Libplanet.Extensions.ForkableActionEvaluator/ForkableActionEvaluator.cs @@ -1,5 +1,7 @@ +using System.Security.Cryptography; using Libplanet.Action; using Libplanet.Action.Loader; +using Libplanet.Common; using Libplanet.Types.Blocks; namespace Libplanet.Extensions.ForkableActionEvaluator; @@ -15,9 +17,10 @@ public ForkableActionEvaluator(IEnumerable<((long StartIndex, long EndIndex) Ran public IActionLoader ActionLoader => throw new NotSupportedException(); - public IReadOnlyList Evaluate(IPreEvaluationBlock block) + public IReadOnlyList Evaluate( + IPreEvaluationBlock block, HashDigest? baseStateRootHash) { var actionEvaluator = _router.GetEvaluator(block.Index); - return actionEvaluator.Evaluate(block); + return actionEvaluator.Evaluate(block, baseStateRootHash); } } diff --git a/Libplanet.Headless.Tests/Hosting/LibplanetNodeServiceTest.cs b/Libplanet.Headless.Tests/Hosting/LibplanetNodeServiceTest.cs index 938e5e305..7ccd5cd94 100644 --- a/Libplanet.Headless.Tests/Hosting/LibplanetNodeServiceTest.cs +++ b/Libplanet.Headless.Tests/Hosting/LibplanetNodeServiceTest.cs @@ -25,13 +25,14 @@ public void Constructor() { var policy = new BlockPolicy(); var stagePolicy = new VolatileStagePolicy(); + var stateStore = new TrieStateStore(new MemoryKeyValueStore()); var blockChainStates = new BlockChainStates( new MemoryStore(), - new TrieStateStore(new MemoryKeyValueStore())); + stateStore); var actionLoader = new SingleActionLoader(typeof(DummyAction)); var actionEvaluator = new ActionEvaluator( _ => policy.BlockAction, - blockChainStates, + stateStore, actionLoader); var genesisBlock = BlockChain.ProposeGenesisBlock(actionEvaluator); var service = new LibplanetNodeService( diff --git a/Libplanet.Headless/Hosting/LibplanetNodeService.cs b/Libplanet.Headless/Hosting/LibplanetNodeService.cs index 25b10b962..2b99920d1 100644 --- a/Libplanet.Headless/Hosting/LibplanetNodeService.cs +++ b/Libplanet.Headless/Hosting/LibplanetNodeService.cs @@ -124,7 +124,7 @@ IActionEvaluator BuildActionEvaluator(IActionEvaluatorConfiguration actionEvalua new Uri(remoteActionEvaluatorConfiguration.StateServiceEndpoint), blockChainStates), DefaultActionEvaluatorConfiguration _ => new ActionEvaluator( _ => blockPolicy.BlockAction, - blockChainStates: blockChainStates, + stateStore: StateStore, actionTypeLoader: actionLoader ), ForkableActionEvaluatorConfiguration forkableActionEvaluatorConfiguration => new diff --git a/NineChronicles.Headless.Executable.Tests/Commands/AccountCommandTest.cs b/NineChronicles.Headless.Executable.Tests/Commands/AccountCommandTest.cs index 08d7a2759..a190c5cdc 100644 --- a/NineChronicles.Headless.Executable.Tests/Commands/AccountCommandTest.cs +++ b/NineChronicles.Headless.Executable.Tests/Commands/AccountCommandTest.cs @@ -49,7 +49,7 @@ public void Balance(StoreType storeType) IBlockPolicy blockPolicy = new BlockPolicySource().GetPolicy(); ActionEvaluator actionEvaluator = new ActionEvaluator( _ => blockPolicy.BlockAction, - new BlockChainStates(store, stateStore), + stateStore, new NCActionLoader()); BlockChain chain = BlockChain.Create( blockPolicy, diff --git a/NineChronicles.Headless.Executable.Tests/Commands/ChainCommandTest.cs b/NineChronicles.Headless.Executable.Tests/Commands/ChainCommandTest.cs index dc21068eb..36f7fbbd9 100644 --- a/NineChronicles.Headless.Executable.Tests/Commands/ChainCommandTest.cs +++ b/NineChronicles.Headless.Executable.Tests/Commands/ChainCommandTest.cs @@ -59,7 +59,7 @@ public void Tip(StoreType storeType) { var actionEvaluator = new ActionEvaluator( _ => new BlockPolicy().BlockAction, - new BlockChainStates(new MemoryStore(), new TrieStateStore(new MemoryKeyValueStore())), + new TrieStateStore(new MemoryKeyValueStore()), new NCActionLoader()); Block genesisBlock = BlockChain.ProposeGenesisBlock(actionEvaluator); IStore store = storeType.CreateStore(_storePath); @@ -93,7 +93,7 @@ public void Inspect(StoreType storeType) IBlockPolicy blockPolicy = new BlockPolicySource().GetTestPolicy(); ActionEvaluator actionEvaluator = new ActionEvaluator( _ => blockPolicy.BlockAction, - new BlockChainStates(store, stateStore), + stateStore, new NCActionLoader()); Block genesisBlock = BlockChain.ProposeGenesisBlock( actionEvaluator, @@ -154,7 +154,7 @@ public void Truncate(StoreType storeType) IBlockPolicy blockPolicy = new BlockPolicySource().GetTestPolicy(); ActionEvaluator actionEvaluator = new ActionEvaluator( _ => blockPolicy.BlockAction, - new BlockChainStates(store, stateStore), + stateStore, new NCActionLoader()); Block genesisBlock = BlockChain.ProposeGenesisBlock( actionEvaluator, @@ -233,7 +233,7 @@ public void PruneState(StoreType storeType) IBlockPolicy blockPolicy = new BlockPolicySource().GetPolicy(); ActionEvaluator actionEvaluator = new ActionEvaluator( _ => blockPolicy.BlockAction, - new BlockChainStates(store, stateStore), + stateStore, new NCActionLoader()); BlockChain chain = BlockChain.Create( blockPolicy, @@ -275,7 +275,7 @@ public void Snapshot(StoreType storeType) IBlockPolicy blockPolicy = new BlockPolicySource().GetPolicy(); ActionEvaluator actionEvaluator = new ActionEvaluator( _ => blockPolicy.BlockAction, - new BlockChainStates(store, stateStore), + stateStore, new NCActionLoader()); BlockChain chain = BlockChain.Create( blockPolicy, diff --git a/NineChronicles.Headless.Executable.Tests/Store/StoreExtensionsTest.cs b/NineChronicles.Headless.Executable.Tests/Store/StoreExtensionsTest.cs index 4626eab45..d5d77b991 100644 --- a/NineChronicles.Headless.Executable.Tests/Store/StoreExtensionsTest.cs +++ b/NineChronicles.Headless.Executable.Tests/Store/StoreExtensionsTest.cs @@ -30,7 +30,7 @@ public void GetGenesisBlock(StoreType storeType) IStore store = storeType.CreateStore(_storePath); IActionEvaluator actionEvaluator = new ActionEvaluator( _ => new BlockPolicy().BlockAction, - new BlockChainStates(new MemoryStore(), new TrieStateStore(new MemoryKeyValueStore())), + new TrieStateStore(new MemoryKeyValueStore()), new NCActionLoader()); Block genesisBlock = BlockChain.ProposeGenesisBlock(actionEvaluator); Guid chainId = Guid.NewGuid(); diff --git a/NineChronicles.Headless.Executable/Commands/ChainCommand.cs b/NineChronicles.Headless.Executable/Commands/ChainCommand.cs index 22ebe8425..c5333094b 100644 --- a/NineChronicles.Headless.Executable/Commands/ChainCommand.cs +++ b/NineChronicles.Headless.Executable/Commands/ChainCommand.cs @@ -131,7 +131,7 @@ public void Inspect( var blockChainStates = new BlockChainStates(store, stateStore); var actionEvaluator = new ActionEvaluator( _ => blockPolicy.BlockAction, - blockChainStates, + stateStore, new NCActionLoader()); BlockChain chain = new BlockChain( blockPolicy, diff --git a/NineChronicles.Headless.Executable/Commands/ReplayCommand.Privates.cs b/NineChronicles.Headless.Executable/Commands/ReplayCommand.Privates.cs index 9395bad78..861b678c0 100644 --- a/NineChronicles.Headless.Executable/Commands/ReplayCommand.Privates.cs +++ b/NineChronicles.Headless.Executable/Commands/ReplayCommand.Privates.cs @@ -29,8 +29,6 @@ public partial class ReplayCommand : CoconaLiteConsoleAppBase /// private sealed class ActionContext : IActionContext { - private readonly int _randomSeed; - public ActionContext( Address signer, TxId? txid, @@ -48,8 +46,7 @@ public ActionContext( BlockProtocolVersion = blockProtocolVersion; Rehearsal = rehearsal; PreviousState = previousState; - Random = new Random(randomSeed); - _randomSeed = randomSeed; + RandomSeed = randomSeed; } public Address Signer { get; } @@ -66,7 +63,7 @@ public ActionContext( public IAccount PreviousState { get; } - public IRandom Random { get; } + public int RandomSeed { get; } public bool BlockAction => TxId is null; @@ -74,20 +71,11 @@ public void UseGas(long gas) { } - public IActionContext GetUnconsumedContext() => - new ActionContext( - Signer, - TxId, - Miner, - BlockIndex, - BlockProtocolVersion, - PreviousState, - _randomSeed, - Rehearsal); - public long GasUsed() => 0; public long GasLimit() => 0; + + public IRandom GetRandom() => new Random(RandomSeed); } private sealed class Random : System.Random, IRandom diff --git a/NineChronicles.Headless.Executable/Commands/ReplayCommand.cs b/NineChronicles.Headless.Executable/Commands/ReplayCommand.cs index 4af166134..9467301f8 100644 --- a/NineChronicles.Headless.Executable/Commands/ReplayCommand.cs +++ b/NineChronicles.Headless.Executable/Commands/ReplayCommand.cs @@ -300,7 +300,7 @@ public int Blocks( _console.Out.WriteLine(msg); outputSw?.WriteLine(msg); - var actionEvaluator = GetActionEvaluator(blockChain); + var actionEvaluator = GetActionEvaluator(stateStore); var actionEvaluations = blockChain.DetermineBlockStateRootHash(block, out IReadOnlyList failedActionEvaluations); LoggingActionEvaluations(failedActionEvaluations, outputSw); @@ -483,7 +483,7 @@ private static (FileStream? fs, StreamWriter? sw) GetOutputFileStream( var blockChainStates = new BlockChainStates(store, stateStore); var actionEvaluator = new ActionEvaluator( _ => policy.BlockAction, - blockChainStates, + stateStore, new NCActionLoader()); return ( store, @@ -524,13 +524,13 @@ private Transaction LoadTx(string txPath) return TxMarshaler.UnmarshalTransaction(txDict); } - private ActionEvaluator GetActionEvaluator(BlockChain blockChain) + private ActionEvaluator GetActionEvaluator(IStateStore stateStore) { var policy = new BlockPolicySource().GetPolicy(); IActionLoader actionLoader = new NCActionLoader(); return new ActionEvaluator( _ => policy.BlockAction, - blockChainStates: blockChain, + stateStore: stateStore, actionTypeLoader: actionLoader); } diff --git a/NineChronicles.Headless.Executable/Commands/StateCommand.cs b/NineChronicles.Headless.Executable/Commands/StateCommand.cs index 664c4c8f8..b88506bb6 100644 --- a/NineChronicles.Headless.Executable/Commands/StateCommand.cs +++ b/NineChronicles.Headless.Executable/Commands/StateCommand.cs @@ -104,12 +104,13 @@ IStateStore stateStore (int)(top.Index - bottom.Index + 1L) ); + var sStore = new TrieStateStore(new Libplanet.Store.Trie.MemoryKeyValueStore()); var blockChainStates = new BlockChainStates( new MemoryStore(), - new TrieStateStore(new Libplanet.Store.Trie.MemoryKeyValueStore())); + sStore); var actionEvaluator = new ActionEvaluator( _ => policy.BlockAction, - blockChainStates, + sStore, new NCActionLoader()); foreach (BlockHash blockHash in blockHashes) diff --git a/NineChronicles.Headless.Tests/GraphQLTestUtils.cs b/NineChronicles.Headless.Tests/GraphQLTestUtils.cs index ce2ad8c6b..67e8eb60a 100644 --- a/NineChronicles.Headless.Tests/GraphQLTestUtils.cs +++ b/NineChronicles.Headless.Tests/GraphQLTestUtils.cs @@ -82,7 +82,7 @@ public static StandaloneContext CreateStandaloneContext() var policy = new BlockPolicy(); var actionEvaluator = new ActionEvaluator( _ => policy.BlockAction, - new BlockChainStates(store, stateStore), + stateStore, new NCActionLoader()); var genesisBlock = BlockChain.ProposeGenesisBlock(actionEvaluator); var blockchain = BlockChain.Create( @@ -113,7 +113,7 @@ PrivateKey minerPrivateKey var policy = new BlockPolicy(); var actionEvaluator = new ActionEvaluator( _ => policy.BlockAction, - new BlockChainStates(store, stateStore), + stateStore, new NCActionLoader()); var genesisBlock = BlockChain.ProposeGenesisBlock( actionEvaluator, diff --git a/NineChronicles.Headless.Tests/GraphTypes/GraphQLTestBase.cs b/NineChronicles.Headless.Tests/GraphTypes/GraphQLTestBase.cs index 02eb0fc12..4643f7e9c 100644 --- a/NineChronicles.Headless.Tests/GraphTypes/GraphQLTestBase.cs +++ b/NineChronicles.Headless.Tests/GraphTypes/GraphQLTestBase.cs @@ -58,7 +58,7 @@ public GraphQLTestBase(ITestOutputHelper output) var blockAction = new RewardGold(); var actionEvaluator = new ActionEvaluator( _ => blockAction, - new BlockChainStates(new MemoryStore(), new TrieStateStore(new MemoryKeyValueStore())), + new TrieStateStore(new MemoryKeyValueStore()), new NCActionLoader()); var genesisBlock = BlockChain.ProposeGenesisBlock( actionEvaluator, diff --git a/NineChronicles.Headless.Tests/GraphTypes/StandaloneMutationTest.cs b/NineChronicles.Headless.Tests/GraphTypes/StandaloneMutationTest.cs index 3f9d1325d..14e1e7f49 100644 --- a/NineChronicles.Headless.Tests/GraphTypes/StandaloneMutationTest.cs +++ b/NineChronicles.Headless.Tests/GraphTypes/StandaloneMutationTest.cs @@ -1007,7 +1007,7 @@ private Block MakeGenesisBlock( { var actionEvaluator = new ActionEvaluator( _ => ServiceBuilder.BlockPolicy.BlockAction, - new BlockChainStates(new MemoryStore(), new TrieStateStore(new MemoryKeyValueStore())), + new TrieStateStore(new MemoryKeyValueStore()), new NCActionLoader()); return BlockChain.ProposeGenesisBlock( actionEvaluator, diff --git a/NineChronicles.Headless.Tests/GraphTypes/StandaloneQueryTest.cs b/NineChronicles.Headless.Tests/GraphTypes/StandaloneQueryTest.cs index 62dad6f21..a7847392f 100644 --- a/NineChronicles.Headless.Tests/GraphTypes/StandaloneQueryTest.cs +++ b/NineChronicles.Headless.Tests/GraphTypes/StandaloneQueryTest.cs @@ -118,7 +118,7 @@ public async Task NodeStatus() var apv = AppProtocolVersion.Sign(apvPrivateKey, 0); var actionEvaluator = new ActionEvaluator( _ => null, - new BlockChainStates(new MemoryStore(), new TrieStateStore(new MemoryKeyValueStore())), + new TrieStateStore(new MemoryKeyValueStore()), new NCActionLoader()); var genesisBlock = BlockChain.ProposeGenesisBlock(actionEvaluator); @@ -446,7 +446,7 @@ public async Task ActivationStatus(bool existsActivatedAccounts) }.ToList()); var actionEvaluator = new ActionEvaluator( _ => null, - new BlockChainStates(new MemoryStore(), new TrieStateStore(new MemoryKeyValueStore())), + new TrieStateStore(new MemoryKeyValueStore()), new NCActionLoader()); Block genesis = BlockChain.ProposeGenesisBlock( @@ -840,7 +840,7 @@ public async Task ActivationKeyNonce(bool trim) }; var actionEvaluator = new ActionEvaluator( _ => null, - new BlockChainStates(new MemoryStore(), new TrieStateStore(new MemoryKeyValueStore())), + new TrieStateStore(new MemoryKeyValueStore()), new NCActionLoader()); Block genesis = BlockChain.ProposeGenesisBlock( @@ -925,7 +925,7 @@ public async Task ActivationKeyNonce_Throw_ExecutionError(string code, string ms var actionEvaluator = new ActionEvaluator( _ => null, - new BlockChainStates(new MemoryStore(), new TrieStateStore(new MemoryKeyValueStore())), + new TrieStateStore(new MemoryKeyValueStore()), new NCActionLoader()); Block genesis = BlockChain.ProposeGenesisBlock( @@ -1006,7 +1006,7 @@ public async Task Balance() var actionEvaluator = new ActionEvaluator( _ => null, - new BlockChainStates(new MemoryStore(), new TrieStateStore(new MemoryKeyValueStore())), + new TrieStateStore(new MemoryKeyValueStore()), new NCActionLoader()); Block genesis = BlockChain.ProposeGenesisBlock( @@ -1111,7 +1111,7 @@ private NineChroniclesNodeService MakeNineChroniclesNodeService(PrivateKey priva }.ToList()); var actionEvaluator = new ActionEvaluator( _ => blockPolicy.BlockAction, - new BlockChainStates(new MemoryStore(), new TrieStateStore(new MemoryKeyValueStore())), + new TrieStateStore(new MemoryKeyValueStore()), new NCActionLoader()); Block genesis = BlockChain.ProposeGenesisBlock( diff --git a/NineChronicles.Headless.Tests/GraphTypes/StandaloneSubscriptionTest.cs b/NineChronicles.Headless.Tests/GraphTypes/StandaloneSubscriptionTest.cs index 27b8cec53..419ec81f7 100644 --- a/NineChronicles.Headless.Tests/GraphTypes/StandaloneSubscriptionTest.cs +++ b/NineChronicles.Headless.Tests/GraphTypes/StandaloneSubscriptionTest.cs @@ -136,7 +136,7 @@ public async Task SubscribePreloadProgress() var apv = AppProtocolVersion.Sign(apvPrivateKey, 0); var actionEvaluator = new ActionEvaluator( _ => null, - new BlockChainStates(new MemoryStore(), new TrieStateStore(new MemoryKeyValueStore())), + new TrieStateStore(new MemoryKeyValueStore()), new SingleActionLoader(typeof(EmptyAction))); var genesisBlock = BlockChain.ProposeGenesisBlock( actionEvaluator, diff --git a/NineChronicles.Headless.Tests/GraphTypes/TransactionHeadlessQueryTest.cs b/NineChronicles.Headless.Tests/GraphTypes/TransactionHeadlessQueryTest.cs index 5716a9479..a3bd08316 100644 --- a/NineChronicles.Headless.Tests/GraphTypes/TransactionHeadlessQueryTest.cs +++ b/NineChronicles.Headless.Tests/GraphTypes/TransactionHeadlessQueryTest.cs @@ -46,7 +46,7 @@ public TransactionHeadlessQueryTest() IBlockPolicy policy = NineChroniclesNodeService.GetTestBlockPolicy(); var actionEvaluator = new ActionEvaluator( _ => policy.BlockAction, - new BlockChainStates(_store, _stateStore), + _stateStore, new NCActionLoader()); Block genesisBlock = BlockChain.ProposeGenesisBlock( actionEvaluator,