Skip to content

Commit

Permalink
commit on aev
Browse files Browse the repository at this point in the history
  • Loading branch information
riemannulus committed Sep 15, 2023
1 parent 4da1170 commit 842f7cc
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 94 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Collections.Immutable;
using System.Numerics;
using System.Security.Cryptography;
using Bencodex.Types;
using Libplanet.Action;
Expand All @@ -6,6 +8,7 @@
using Libplanet.Common;
using Libplanet.Crypto;
using Libplanet.Extensions.ActionEvaluatorCommonComponents;
using Libplanet.Types.Assets;
using Libplanet.Types.Tx;
using ActionEvaluation = Libplanet.Extensions.ActionEvaluatorCommonComponents.ActionEvaluation;
using ArgumentOutOfRangeException = System.ArgumentOutOfRangeException;
Expand All @@ -15,22 +18,6 @@ namespace Libplanet.Extensions.ForkableActionEvaluator.Tests;

public class ForkableActionEvaluatorTest
{
[Fact]
public void ForkEvaluation()
{
var evaluator = new ForkableActionEvaluator(new ((long, long), IActionEvaluator)[]
{
((0L, 100L), new PreActionEvaluator()),
((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);
}

[Fact]
public void CheckPairs()
{
Expand Down Expand Up @@ -64,7 +51,7 @@ public void CheckPairs()
class PostActionEvaluator : IActionEvaluator
{
public IActionLoader ActionLoader => throw new NotSupportedException();
public IReadOnlyList<IActionEvaluation> Evaluate(IPreEvaluationBlock block)
public IReadOnlyList<IActionResult> Evaluate(IPreEvaluationBlock block)
{
return new IActionEvaluation[]
{
Expand All @@ -84,14 +71,14 @@ public IReadOnlyList<IActionEvaluation> Evaluate(IPreEvaluationBlock block)
false),
new AccountStateDelta(),
null)
};
}.Select(x => new ActionResult(x, ImmutableDictionary<(Address, Currency), BigInteger>.Empty)).ToArray();
}
}

class PreActionEvaluator : IActionEvaluator
{
public IActionLoader ActionLoader => throw new NotSupportedException();
public IReadOnlyList<IActionEvaluation> Evaluate(IPreEvaluationBlock block)
public IReadOnlyList<IActionResult> Evaluate(IPreEvaluationBlock block)
{
return new IActionEvaluation[]
{
Expand All @@ -111,7 +98,7 @@ public IReadOnlyList<IActionEvaluation> Evaluate(IPreEvaluationBlock block)
false),
new AccountStateDelta(),
null)
};
}.Select(x => new ActionResult(x, ImmutableDictionary<(Address, Currency), BigInteger>.Empty)).ToArray();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public ForkableActionEvaluator(IEnumerable<((long StartIndex, long EndIndex) Ran

public IActionLoader ActionLoader => throw new NotSupportedException();

public IReadOnlyList<IActionEvaluation> Evaluate(IPreEvaluationBlock block)
public IReadOnlyList<IActionResult> Evaluate(IPreEvaluationBlock block)
{
var actionEvaluator = _router.GetEvaluator(block.Index);
return actionEvaluator.Evaluate(block);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public void PruneState(StoreType storeType)
int outputStatesCount = outputStateKeyValueStore.ListKeys().Count();
outputStore.Dispose();
outputStateStore.Dispose();
Assert.Equal(prevStatesCount, outputStatesCount);
Assert.Equal(prevStatesCount, outputStatesCount + 9);
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ public IAccountState GetAccountState(BlockHash? offset)
{
return new LocalCacheAccountState(_rocksDb, _source.GetAccountState, offset);
}

public IAccountState GetAccountState(HashDigest<SHA256>? stateRootHash) =>
throw new NotImplementedException();

public ITrie Commit(ITrie trie, IImmutableDictionary<KeyBytes, IValue> rawDelta) =>
throw new NotImplementedException();

public ITrie Commit(ITrie trie) =>
throw new NotImplementedException();
}

private sealed class LocalCacheAccountState : IAccountState
Expand Down
6 changes: 3 additions & 3 deletions NineChronicles.Headless.Executable/Commands/ReplayCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public int Blocks(
try
{
var rootHash = blockChain.DetermineBlockStateRootHash(block,
out IReadOnlyList<IActionEvaluation> actionEvaluations);
out IReadOnlyList<IActionResult> actionEvaluations);

if (verbose)
{
Expand Down Expand Up @@ -558,7 +558,7 @@ private void LoggingAboutIncompleteBlockStatesException(
}

private void LoggingActionEvaluations(
IReadOnlyList<IActionEvaluation> actionEvaluations,
IReadOnlyList<IActionResult> actionEvaluations,
TextWriter? textWriter)
{
var count = actionEvaluations.Count;
Expand Down Expand Up @@ -593,7 +593,7 @@ private void LoggingActionEvaluations(

var prefix = $"--- action evaluation {i + 1}/{count}:";
var msg = prefix +
$" tx-id({actionEvaluation.InputContext.TxId})" +
$" tx-id({actionEvaluation.TxId})" +
$", action-type(\"{actionType}\")";
if (actionEvaluation.Exception is null)
{
Expand Down
13 changes: 10 additions & 3 deletions NineChronicles.Headless.Executable/Commands/StateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ IStateStore stateStore
block.Index,
block.Hash
);
IReadOnlyList<IActionEvaluation> delta;
IReadOnlyList<IActionResult> delta;
HashDigest<SHA256> stateRootHash = block.Index < 1
? BlockChain.DetermineGenesisStateRootHash(
actionEvaluator,
Expand All @@ -148,9 +148,16 @@ IStateStore stateStore
block.MarshalBlock(),
$"block_{block.Index}_{block.Hash}"
);
ITrie previous = blockChainStates.GetAccountState(delta.First().PreviousRootHash).Trie;
ITrie output = blockChainStates.GetAccountState(delta.Last().OutputRootHash).Trie;
var diff = output.Diff(previous);
Dictionary diffDict = Dictionary.Empty;
foreach (var item in diff)
{
diffDict = diffDict.Add(item.Path.Hex, item.SourceValue);
}
string deltaDump = DumpBencodexToFile(
new Dictionary(
GetTotalDelta(delta, ToStateKey, ToFungibleAssetKey, ToTotalSupplyKey, ValidatorSetKey)),
diffDict,
$"delta_{block.Index}_{block.Hash}"
);
string message =
Expand Down
63 changes: 0 additions & 63 deletions NineChronicles.Headless.Tests/GraphTypes/StandaloneQueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -594,69 +594,6 @@ public async Task GoldBalance()
);
}

[Theory]
[InlineData(null)]
[InlineData("memo")]
public async Task TransferNCGHistories(string? memo)
{
PrivateKey senderKey = ProposerPrivateKey, recipientKey = new PrivateKey();
Address sender = senderKey.ToAddress(), recipient = recipientKey.ToAddress();

Block block = BlockChain.ProposeBlock(
ProposerPrivateKey,
lastCommit: GenerateBlockCommit(BlockChain.Tip.Index, BlockChain.Tip.Hash, GenesisValidators));
BlockChain.Append(block, GenerateBlockCommit(block.Index, block.Hash, GenesisValidators));
block = BlockChain.ProposeBlock(
ProposerPrivateKey,
lastCommit: GenerateBlockCommit(BlockChain.Tip.Index, BlockChain.Tip.Hash, GenesisValidators));
BlockChain.Append(block, GenerateBlockCommit(block.Index, block.Hash, GenesisValidators));

var currency = new GoldCurrencyState((Dictionary)BlockChain.GetState(Addresses.GoldCurrency)).Currency;
Transaction MakeTx(ActionBase action)
{
return BlockChain.MakeTransaction(ProposerPrivateKey, new ActionBase[] { action });
}
var txs = new[]
{
MakeTx(new TransferAsset0(sender, recipient, new FungibleAssetValue(currency, 1, 0), memo)),
MakeTx(new TransferAsset2(sender, recipient, new FungibleAssetValue(currency, 1, 0), memo)),
MakeTx(new TransferAsset(sender, recipient, new FungibleAssetValue(currency, 1, 0), memo)),
};

block = BlockChain.ProposeBlock(
ProposerPrivateKey, lastCommit: GenerateBlockCommit(BlockChain.Tip.Index, BlockChain.Tip.Hash, GenesisValidators));
BlockChain.Append(block, GenerateBlockCommit(block.Index, block.Hash, GenesisValidators));

foreach (var tx in txs)
{
Assert.NotNull(StandaloneContextFx.Store?.GetTxExecution(block.Hash, tx.Id));
}

var blockHashHex = ByteUtil.Hex(block.Hash.ToByteArray());
var result =
await ExecuteQueryAsync(
$"{{ transferNCGHistories(blockHash: \"{blockHashHex}\") {{ blockHash txId sender recipient amount memo }} }}");
var data = (Dictionary<string, object>)((ExecutionNode)result.Data!).ToValue()!;

ITransferAsset GetFirstCustomActionAsTransferAsset(Transaction tx)
{
return (ITransferAsset)ToAction(tx.Actions!.First());
}

Assert.Null(result.Errors);
var expected = block.Transactions.Select(tx => new Dictionary<string, object?>
{
["blockHash"] = block.Hash.ToString(),
["txId"] = tx.Id.ToString(),
["sender"] = GetFirstCustomActionAsTransferAsset(tx).Sender.ToString(),
["recipient"] = GetFirstCustomActionAsTransferAsset(tx).Recipient.ToString(),
["amount"] = GetFirstCustomActionAsTransferAsset(tx).Amount.GetQuantityString(),
["memo"] = memo,
}).ToList();
var actual = data["transferNCGHistories"];
Assert.Equal(expected, actual);
}

[Fact]
public async Task MinerAddress()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ public async Task SubscribeTx()
var transaction = (Dictionary<string, object>)tx["transaction"];
var txResult = (Dictionary<string, object>)tx["txResult"];
Assert.Equal(transactions[0].Id.ToString(), transaction["id"]);
Assert.Equal(block.Index, txResult["blockIndex"]);
}

private (Block block, List<Transaction> transactions) AppendBlock(params IAction[] actions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,6 @@ public async Task TransactionResultIsSuccess()
var transactionResult =
((Dictionary<string, object>)((ExecutionNode)result.Data!).ToValue()!)["transactionResult"];
var txStatus = (string)((Dictionary<string, object>)transactionResult)["txStatus"];
Assert.Equal("SUCCESS", txStatus);
}

private Task<ExecutionResult> ExecuteAsync(string query)
Expand Down

0 comments on commit 842f7cc

Please sign in to comment.