Skip to content

Commit

Permalink
Merge branch 'development' of github.com:planetarium/NineChronicles.H…
Browse files Browse the repository at this point in the history
…eadless into 61-merge-development
  • Loading branch information
ipdae committed Oct 11, 2023
2 parents f252408 + 6cebcf5 commit 1034649
Show file tree
Hide file tree
Showing 17 changed files with 49 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -64,7 +64,7 @@ public void CheckPairs()
class PostActionEvaluator : IActionEvaluator
{
public IActionLoader ActionLoader => throw new NotSupportedException();
public IReadOnlyList<IActionEvaluation> Evaluate(IPreEvaluationBlock block)
public IReadOnlyList<IActionEvaluation> Evaluate(IPreEvaluationBlock block, HashDigest<SHA256>? baseStateroothash)
{
return new IActionEvaluation[]
{
Expand All @@ -79,7 +79,7 @@ public IReadOnlyList<IActionEvaluation> Evaluate(IPreEvaluationBlock block)
0,
false,
new AccountStateDelta(),
new Random(0),
0,
null,
false),
new AccountStateDelta(),
Expand All @@ -91,7 +91,7 @@ public IReadOnlyList<IActionEvaluation> Evaluate(IPreEvaluationBlock block)
class PreActionEvaluator : IActionEvaluator
{
public IActionLoader ActionLoader => throw new NotSupportedException();
public IReadOnlyList<IActionEvaluation> Evaluate(IPreEvaluationBlock block)
public IReadOnlyList<IActionEvaluation> Evaluate(IPreEvaluationBlock block, HashDigest<SHA256>? baseStateRootHash)
{
return new IActionEvaluation[]
{
Expand All @@ -106,7 +106,7 @@ public IReadOnlyList<IActionEvaluation> Evaluate(IPreEvaluationBlock block)
0,
false,
new AccountStateDelta(),
new Random(0),
0,
null,
false),
new AccountStateDelta(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,9 +17,10 @@ public ForkableActionEvaluator(IEnumerable<((long StartIndex, long EndIndex) Ran

public IActionLoader ActionLoader => throw new NotSupportedException();

public IReadOnlyList<IActionEvaluation> Evaluate(IPreEvaluationBlock block)
public IReadOnlyList<IActionEvaluation> Evaluate(
IPreEvaluationBlock block, HashDigest<SHA256>? baseStateRootHash)
{
var actionEvaluator = _router.GetEvaluator(block.Index);
return actionEvaluator.Evaluate(block);
return actionEvaluator.Evaluate(block, baseStateRootHash);
}
}
5 changes: 3 additions & 2 deletions Libplanet.Headless.Tests/Hosting/LibplanetNodeServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion Libplanet.Headless/Hosting/LibplanetNodeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ public partial class ReplayCommand : CoconaLiteConsoleAppBase
/// </summary>
private sealed class ActionContext : IActionContext
{
private readonly int _randomSeed;

public ActionContext(
Address signer,
TxId? txid,
Expand All @@ -48,8 +46,7 @@ public ActionContext(
BlockProtocolVersion = blockProtocolVersion;
Rehearsal = rehearsal;
PreviousState = previousState;
Random = new Random(randomSeed);
_randomSeed = randomSeed;
RandomSeed = randomSeed;
}

public Address Signer { get; }
Expand All @@ -66,28 +63,19 @@ public ActionContext(

public IAccount PreviousState { get; }

public IRandom Random { get; }
public int RandomSeed { get; }

public bool BlockAction => TxId is null;

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
Expand Down
8 changes: 4 additions & 4 deletions NineChronicles.Headless.Executable/Commands/ReplayCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ICommittedActionEvaluation> failedActionEvaluations);
LoggingActionEvaluations(failedActionEvaluations, outputSw);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}

Expand Down
5 changes: 3 additions & 2 deletions NineChronicles.Headless.Executable/Commands/StateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions NineChronicles.Headless.Tests/GraphQLTestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions NineChronicles.Headless.Tests/GraphTypes/StandaloneQueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 1034649

Please sign in to comment.