Skip to content

Commit

Permalink
Cherry-pick from migrate/iaccount-to-itrie
Browse files Browse the repository at this point in the history
  • Loading branch information
greymistcube committed Sep 26, 2023
1 parent 97d53a8 commit c94ea14
Show file tree
Hide file tree
Showing 16 changed files with 114 additions and 315 deletions.
9 changes: 2 additions & 7 deletions Libplanet.Headless/Hosting/LibplanetNodeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ public LibplanetNodeService(
(Store, StateStore) = LoadStore(
Properties.StorePath,
Properties.StoreType,
Properties.StoreStatesCacheSize,
Properties.NoReduceStore);
Properties.StoreStatesCacheSize);

var chainIds = Store.ListChainIds().ToList();
Log.Debug($"Number of chain ids: {chainIds.Count()}");
Expand Down Expand Up @@ -303,7 +302,7 @@ public override async Task StopAsync(CancellationToken cancellationToken)
}
}

protected (IStore, IStateStore) LoadStore(string path, string type, int statesCacheSize, bool noReduceStore = false)
protected (IStore, IStateStore) LoadStore(string path, string type, int statesCacheSize)
{
IStore store = null;
if (type == "rocksdb")
Expand Down Expand Up @@ -344,10 +343,6 @@ public override async Task StopAsync(CancellationToken cancellationToken)
}

store ??= new DefaultStore(path, flush: false);
if (!noReduceStore)
{
store = new ReducedStore(store);
}

IKeyValueStore stateKeyValueStore = new RocksDBKeyValueStore(Path.Combine(path, "states"));
IStateStore stateStore = new TrieStateStore(stateKeyValueStore);
Expand Down
162 changes: 0 additions & 162 deletions Libplanet.Headless/ReducedStore.cs

This file was deleted.

4 changes: 2 additions & 2 deletions NineChronicles.Headless.Executable/Commands/MarketCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ public void Query(
IEnumerable<(Transaction, ActionBase)> actions = block.Transactions
.Reverse()
.Where(tx => includeFails ||
!(chain.GetTxExecution(block.Hash, tx.Id) is { } e) ||
e is TxSuccess)
!(chain.GetTxExecution(block.Hash, tx.Id) is { } e) ||
!e.Fail)
.SelectMany(tx => tx.Actions is { } ca
? ca.Reverse().Select(a => (tx, ToAction(a)))
: Enumerable.Empty<(Transaction, ActionBase)>());
Expand Down
14 changes: 8 additions & 6 deletions NineChronicles.Headless.Executable/Commands/ReplayCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public int Tx(

// Evaluate tx.
IAccountState previousBlockStates = blockChain.GetAccountState(previousBlock.Hash);
IAccount previousStates = new Account(previousBlockStates);
IAccount previousStates = AccountStateDelta.Create(previousBlockStates);
var actions = tx.Actions.Select(a => ToAction(a));
var actionEvaluations = EvaluateActions(
preEvaluationHash: targetBlock.PreEvaluationHash,
Expand Down Expand Up @@ -271,7 +271,7 @@ public int Blocks(
try
{
var rootHash = blockChain.DetermineBlockStateRootHash(block,
out IReadOnlyList<IActionEvaluation> actionEvaluations);
out IReadOnlyList<ICommittedActionEvaluation> actionEvaluations);

if (verbose)
{
Expand Down Expand Up @@ -301,8 +301,9 @@ public int Blocks(
outputSw?.WriteLine(msg);

var actionEvaluator = GetActionEvaluator(blockChain);
var actionEvaluations = actionEvaluator.Evaluate(block);
LoggingActionEvaluations(actionEvaluations, outputSw);
var actionEvaluations = blockChain.DetermineBlockStateRootHash(block,
out IReadOnlyList<ICommittedActionEvaluation> failedActionEvaluations);
LoggingActionEvaluations(failedActionEvaluations, outputSw);

msg = $"- block #{block.Index} evaluating failed with ";
_console.Out.Write(msg);
Expand Down Expand Up @@ -398,7 +399,8 @@ public int RemoteTx(
cacheDirectory ?? Path.Join(Path.GetTempPath(), "ncd-replay-remote-tx-cache"));

var previousBlockHash = BlockHash.FromString(previousBlockHashValue);
var previousStates = new Account(blockChainStates.GetAccountState(previousBlockHash));
var previousStates =
AccountStateDelta.Create(blockChainStates.GetAccountState(previousBlockHash));

var actions = transaction.Actions
.Select(ToAction)
Expand Down Expand Up @@ -558,7 +560,7 @@ private void LoggingAboutIncompleteBlockStatesException(
}

private void LoggingActionEvaluations(
IReadOnlyList<IActionEvaluation> actionEvaluations,
IReadOnlyList<ICommittedActionEvaluation> actionEvaluations,
TextWriter? textWriter)
{
var count = actionEvaluations.Count;
Expand Down
17 changes: 3 additions & 14 deletions NineChronicles.Headless.Executable/Commands/StateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,31 +132,20 @@ IStateStore stateStore
block.Index,
block.Hash
);
IReadOnlyList<IActionEvaluation> delta;
HashDigest<SHA256> stateRootHash = block.Index < 1
? 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
1 change: 1 addition & 0 deletions NineChronicles.Headless.Executable/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ IActionLoader MakeSingleActionLoader()
standaloneContext.NineChroniclesNodeService!.ActionRenderer,
standaloneContext.NineChroniclesNodeService!.ExceptionRenderer,
standaloneContext.NineChroniclesNodeService!.NodeStatusRenderer,
standaloneContext.NineChroniclesNodeService!.BlockChain,
IPAddress.Loopback.ToString(),
rpcProperties.RpcListenPort,
context,
Expand Down
5 changes: 4 additions & 1 deletion NineChronicles.Headless.Tests/Common/MockState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ private MockState(

public ValidatorSet ValidatorSet => _validatorSet;

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

public IValue? GetState(Address address) => _states.TryGetValue(address, out IValue? value)
? value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public GraphQLTestBase(ITestOutputHelper output)
ncService.ActionRenderer,
ncService.ExceptionRenderer,
ncService.NodeStatusRenderer,
ncService.BlockChain,
"",
0,
new RpcContext(),
Expand Down
Loading

0 comments on commit c94ea14

Please sign in to comment.