Skip to content

Commit

Permalink
Merge pull request #2486 from OnedgeLee/bump/sloth
Browse files Browse the repository at this point in the history
Bump Lib9c:sloth
  • Loading branch information
OnedgeLee authored Jun 14, 2024
2 parents f66e7b3 + 9ae42c0 commit d3f1eeb
Show file tree
Hide file tree
Showing 22 changed files with 206 additions and 144 deletions.
6 changes: 1 addition & 5 deletions Libplanet.Headless.Tests/Hosting/LibplanetNodeServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ public void Constructor()
new MemoryStore(),
stateStore);
var actionLoader = new SingleActionLoader(typeof(DummyAction));
var actionEvaluator = new ActionEvaluator(
_ => policy.BlockAction,
stateStore,
actionLoader);
var genesisBlock = BlockChain.ProposeGenesisBlock(actionEvaluator);
var genesisBlock = BlockChain.ProposeGenesisBlock();
var service = new LibplanetNodeService(
new LibplanetNodeServiceProperties()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public void Balance(StoreType storeType)
stateStore,
genesisBlock,
actionEvaluator);
GenesisHelper.AppendEmptyBlock(chain);
Guid chainId = chain.Id;
store.Dispose();
stateStore.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@ public ChainCommandTest()
[InlineData(StoreType.RocksDb)]
public void Tip(StoreType storeType)
{
var actionEvaluator = new ActionEvaluator(
_ => new BlockPolicy().BlockAction,
new TrieStateStore(new MemoryKeyValueStore()),
new NCActionLoader());
Block genesisBlock = BlockChain.ProposeGenesisBlock(actionEvaluator);
Block genesisBlock = BlockChain.ProposeGenesisBlock();
IStore store = storeType.CreateStore(_storePath);
Guid chainId = Guid.NewGuid();
store.SetCanonicalChainId(chainId);
Expand Down Expand Up @@ -96,7 +92,6 @@ public void Inspect(StoreType storeType)
stateStore,
new NCActionLoader());
Block genesisBlock = BlockChain.ProposeGenesisBlock(
actionEvaluator,
transactions: new IAction[]
{
new Initialize(
Expand Down Expand Up @@ -157,7 +152,6 @@ public void Truncate(StoreType storeType)
stateStore,
new NCActionLoader());
Block genesisBlock = BlockChain.ProposeGenesisBlock(
actionEvaluator,
transactions: new IAction[]
{
new Initialize(
Expand Down
18 changes: 18 additions & 0 deletions NineChronicles.Headless.Executable.Tests/Commands/GenesisHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
using System.Linq;
using System.Numerics;
using System.Text.Json;
using Libplanet.Blockchain;
using Libplanet.Common;
using Libplanet.Crypto;
using Libplanet.Types.Blocks;
using Libplanet.Types.Consensus;
using Nekoyume;
using Nekoyume.Action;
using Nekoyume.Model;
using Nekoyume.Model.State;
using static Humanizer.In;
using Lib9cUtils = Lib9c.DevExtensions.Utils;

namespace NineChronicles.Headless.Executable.Tests.Commands
Expand Down Expand Up @@ -96,6 +99,21 @@ public static Block MineGenesisBlock(
return genesisBlock;
}

public static void AppendEmptyBlock(BlockChain blockChain)
{
var lastHeight = blockChain.Tip.Index;
var block = blockChain.ProposeBlock(ValidatorKey, blockChain.GetBlockCommit(lastHeight));
var blockCommit = new BlockCommit(
block.Index,
0,
block.Hash,
new[]
{
new VoteMetadata(block.Index, 0, block.Hash, block.Timestamp, ValidatorKey.PublicKey, VoteFlag.PreCommit).Sign(ValidatorKey),
}.ToImmutableArray());
blockChain.Append(block, blockCommit);
}

[Serializable]
private struct AuthorizedMinerConfig
{
Expand Down
10 changes: 1 addition & 9 deletions NineChronicles.Headless.Executable.Tests/ProgramTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,7 @@ public ProgramTest()
_storePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(_storePath);
_genesisBlockPath = Path.Combine(_storePath, "./genesis");

IStateStore stateStore = new TrieStateStore(new MemoryKeyValueStore());
IActionLoader actionLoader = new NCActionLoader();
IBlockPolicy blockPolicy = new BlockPolicySource(actionLoader, 1).GetPolicy(Nekoyume.Planet.Odin);
IActionEvaluator actionEvaluator = new ActionEvaluator(
_ => blockPolicy.BlockAction,
stateStore: stateStore,
actionTypeLoader: actionLoader);
var genesis = BlockChain.ProposeGenesisBlock(actionEvaluator);
var genesis = BlockChain.ProposeGenesisBlock();

Bencodex.Codec codec = new Bencodex.Codec();
_genesisBlockHash = ByteUtil.Hex(genesis.Hash.ByteArray);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ public StoreExtensionsTest()
public void GetGenesisBlock(StoreType storeType)
{
IStore store = storeType.CreateStore(_storePath);
IActionEvaluator actionEvaluator = new ActionEvaluator(
_ => new BlockPolicy().BlockAction,
new TrieStateStore(new MemoryKeyValueStore()),
new NCActionLoader());
Block genesisBlock = BlockChain.ProposeGenesisBlock(actionEvaluator);
Block genesisBlock = BlockChain.ProposeGenesisBlock();
Guid chainId = Guid.NewGuid();
store.SetCanonicalChainId(chainId);
store.PutBlock(genesisBlock);
Expand Down
7 changes: 6 additions & 1 deletion NineChronicles.Headless.Executable/Commands/ChainCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ public class ChainCommand : CoconaLiteConsoleAppBase

public enum SnapshotType
{
#pragma warning disable SA1602 // Enumeration items should be documented
Full,
Partition,
All
#pragma warning restore SA1602 // Enumeration items should be documented
}

public ChainCommand(IConsole console)
Expand Down Expand Up @@ -118,7 +120,9 @@ public void Inspect(
IStagePolicy stagePolicy = new VolatileStagePolicy();
IBlockPolicy blockPolicy = new BlockPolicySource().GetPolicy();
IStore store = storeType.CreateStore(storePath);
var stateStore = new TrieStateStore(new DefaultKeyValueStore(null));
var statesPath = Path.Combine(storePath, "states");
IKeyValueStore stateKeyValueStore = new RocksDBKeyValueStore(statesPath);
var stateStore = new TrieStateStore(stateKeyValueStore);
if (!(store.GetCanonicalChainId() is { } chainId))
{
throw new CommandExitedException($"There is no canonical chain: {storePath}", -1);
Expand Down Expand Up @@ -215,6 +219,7 @@ public void Inspect(
}

store.Dispose();
stateStore.Dispose();
}

[Command(Description = "Truncate the chain from the tip by the input value (in blocks)")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ query GetBlockData($hash: ID!)
stateRootHash
}
stateRootHash
protocolVersion
}
}}
}
Expand Down Expand Up @@ -90,6 +91,7 @@ private sealed class BlockType
public string? PreEvaluationHash { get; set; }
public BlockType? PreviousBlock { get; set; }
public string? StateRootHash { get; set; }
public int? ProtocolVersion { get; set; }
}

private sealed class TransactionQueryType
Expand Down
16 changes: 12 additions & 4 deletions NineChronicles.Headless.Executable/Commands/ReplayCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ public int Blocks(
var currentBlockIndex = startIndex.Value;
while (currentBlockIndex <= endIndex)
{
var previousBlock = blockChain[currentBlockIndex];
var block = blockChain[currentBlockIndex++];
if (verbose)
{
Expand All @@ -270,7 +271,8 @@ public int Blocks(

try
{
var rootHash = blockChain.DetermineBlockStateRootHash(block,
var rootHash = blockChain.DetermineNextBlockStateRootHash(
previousBlock,
out IReadOnlyList<ICommittedActionEvaluation> actionEvaluations);

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

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

Expand Down Expand Up @@ -385,7 +388,12 @@ public int RemoteTx(
var previousBlockHashStateRootHash = block?.PreviousBlock?.StateRootHash;
var preEvaluationHashValue = block?.PreEvaluationHash;
var minerValue = block?.Miner;
if (previousBlockHashValue is null || preEvaluationHashValue is null || minerValue is null || previousBlockHashStateRootHash is null)
var protocolVersion = block?.ProtocolVersion;
if (previousBlockHashValue is null
|| preEvaluationHashValue is null
|| minerValue is null
|| previousBlockHashStateRootHash is null
|| protocolVersion is null)
{
throw new CommandExitedException("Failed to get block from query", -1);
}
Expand Down Expand Up @@ -425,7 +433,7 @@ public int RemoteTx(
var actionEvaluations = EvaluateActions(
preEvaluationHash: HashDigest<SHA256>.FromString(preEvaluationHashValue),
blockIndex: transactionResult.BlockIndex ?? 0,
blockProtocolVersion: 0,
blockProtocolVersion: (int)protocolVersion,
txid: transaction.Id,
previousStates: previousStates,
miner: miner,
Expand Down
19 changes: 11 additions & 8 deletions NineChronicles.Headless.Executable/Commands/StateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,17 @@ IStateStore stateStore
block.Index,
block.Hash
);
HashDigest<SHA256> stateRootHash = block.Index < 1
? BlockChain.DetermineGenesisStateRootHash(
actionEvaluator,
preEvalBlock,
out _)
: chain.DetermineBlockStateRootHash(
preEvalBlock,
out _);
HashDigest<SHA256>? refSrh = block.ProtocolVersion < BlockMetadata.SlothProtocolVersion
? store.GetStateRootHash(block.PreviousHash)
: store.GetStateRootHash(block.Hash);
refSrh = refSrh is { } prevSrh
? prevSrh
: MerkleTrie.EmptyRootHash;

IReadOnlyList<ICommittedActionEvaluation> evals = actionEvaluator.Evaluate(block, refSrh);
HashDigest<SHA256> stateRootHash = evals.Count > 0
? evals[evals.Count - 1].OutputState
: (HashDigest<SHA256>)refSrh;
DateTimeOffset now = DateTimeOffset.Now;
if (invalidStateRootHashBlock is null && !stateRootHash.Equals(block.StateRootHash))
{
Expand Down
20 changes: 20 additions & 0 deletions NineChronicles.Headless.Executable/Store/AnonymousStore.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using Libplanet.Common;
using Libplanet.Crypto;
using Libplanet.Store;
using Libplanet.Types.Blocks;
Expand Down Expand Up @@ -47,6 +49,9 @@ public class AnonymousStore : IStore
public Action<BlockCommit> PutBlockCommit { get; set; }
public Action<BlockHash> DeleteBlockCommit { get; set; }
public Func<IEnumerable<BlockHash>> GetBlockCommitHashes { get; set; }
public Func<BlockHash, HashDigest<SHA256>?> GetNextStateRootHash { get; set; }
public Action<BlockHash, HashDigest<SHA256>> PutNextStateRootHash { get; set; }
public Action<BlockHash> DeleteNextStateRootHash { get; set; }
#pragma warning restore CS8618

void IDisposable.Dispose()
Expand Down Expand Up @@ -237,4 +242,19 @@ IEnumerable<BlockHash> IStore.GetBlockCommitHashes()
{
return GetBlockCommitHashes();
}

HashDigest<SHA256>? IStore.GetNextStateRootHash(BlockHash blockHash)
{
return GetNextStateRootHash(blockHash);
}

void IStore.PutNextStateRootHash(BlockHash blockHash, HashDigest<SHA256> nextStateRootHash)
{
PutNextStateRootHash(blockHash, nextStateRootHash);
}

void IStore.DeleteNextStateRootHash(BlockHash blockHash)
{
DeleteNextStateRootHash(blockHash);
}
}
63 changes: 53 additions & 10 deletions NineChronicles.Headless.Tests/GraphQLTestUtils.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Numerics;
using System.Threading.Tasks;
using Bencodex.Types;
using GraphQL;
using GraphQL.Types;
using Libplanet.Action;
using Libplanet.Action.Loader;
using Libplanet.Action.Sys;
using Libplanet.Blockchain;
using Libplanet.Blockchain.Policies;
using Libplanet.Crypto;
using Libplanet.Store;
using Libplanet.Store.Trie;
using Libplanet.Types.Blocks;
using Libplanet.Types.Consensus;
using Libplanet.Types.Tx;
using Microsoft.Extensions.DependencyInjection;
using Nekoyume;
Expand All @@ -26,6 +31,19 @@ namespace NineChronicles.Headless.Tests
public static class GraphQLTestUtils
{
private static readonly IActionLoader _actionLoader = new NCActionLoader();
private static readonly List<PrivateKey> ValidatorPrivateKeys = new List<PrivateKey>
{
PrivateKey.FromString(
"e5792a1518d9c7f7ecc35cd352899211a05164c9dde059c9811e0654860549ef"),
PrivateKey.FromString(
"91d61834be824c952754510fcf545180eca38e036d3d9b66564f0667b30d5b93"),
PrivateKey.FromString(
"b17c919b07320edfb3e6da2f1cfed75910322de2e49377d6d4d226505afca550"),
PrivateKey.FromString(
"91602d7091c5c7837ac8e71a8d6b1ed1355cfe311914d9a76107899add0ad56a"),
};

public static PrivateKey MinerPrivateKey => ValidatorPrivateKeys.First();

public static Task<ExecutionResult> ExecuteQueryAsync<TObjectGraphType>(
string query,
Expand Down Expand Up @@ -86,7 +104,7 @@ public static StandaloneContext CreateStandaloneContext()
_ => policy.BlockAction,
stateStore,
new NCActionLoader());
var genesisBlock = BlockChain.ProposeGenesisBlock(actionEvaluator);
var genesisBlock = BlockChain.ProposeGenesisBlock();
var blockchain = BlockChain.Create(
new BlockPolicy(),
new VolatileStagePolicy(),
Expand All @@ -106,8 +124,7 @@ public static StandaloneContext CreateStandaloneContext()
}

public static StandaloneContext CreateStandaloneContext(
InitializeStates initializeStates,
PrivateKey minerPrivateKey
InitializeStates initializeStates
)
{
var store = new DefaultStore(null);
Expand All @@ -118,13 +135,29 @@ PrivateKey minerPrivateKey
stateStore,
new NCActionLoader());
var genesisBlock = BlockChain.ProposeGenesisBlock(
actionEvaluator,
transactions: ImmutableList<Transaction>.Empty.Add(Transaction.Create(
0, minerPrivateKey, null, new ActionBase[]
{
initializeStates,
}.ToPlainValues())),
privateKey: minerPrivateKey
transactions: ImmutableList<Transaction>.Empty
.Add(
Transaction.Create(
0, MinerPrivateKey,
null,
new IAction[]
{
new Initialize(
new ValidatorSet(
ValidatorPrivateKeys.Select(
v => new Validator(v.PublicKey, BigInteger.One)).ToList()),
ImmutableDictionary.Create<Address, IValue>())
}.Select(a => a.PlainValue)))
.Add(
Transaction.Create(
1,
MinerPrivateKey,
null,
new ActionBase[]
{
initializeStates,
}.ToPlainValues())),
privateKey: MinerPrivateKey
);
var blockchain = BlockChain.Create(
new BlockPolicy(),
Expand All @@ -133,6 +166,16 @@ PrivateKey minerPrivateKey
stateStore,
genesisBlock,
actionEvaluator);
var block = blockchain.ProposeBlock(MinerPrivateKey, null);
var blockCommit = new BlockCommit(
block.Index,
0,
block.Hash,
ValidatorPrivateKeys.Select(
k => new VoteMetadata(block.Index, 0, block.Hash, block.Timestamp, k.PublicKey, VoteFlag.PreCommit).Sign(k))
.ToImmutableArray());

blockchain.Append(block, blockCommit);
var ncg = new GoldCurrencyState((Dictionary)blockchain.GetWorldState().GetLegacyState(Addresses.GoldCurrency))
.Currency;
var currencyFactory = new CurrencyFactory(() => blockchain.GetWorldState(blockchain.Tip.Hash), ncg);
Expand Down
Loading

0 comments on commit d3f1eeb

Please sign in to comment.