Skip to content

Commit

Permalink
Merge pull request #2491 from planetarium/main-merge-150
Browse files Browse the repository at this point in the history
Main merge 150
  • Loading branch information
ipdae authored Jun 27, 2024
2 parents a11565e + 26404cb commit e8faa14
Show file tree
Hide file tree
Showing 22 changed files with 227 additions and 139 deletions.
2 changes: 1 addition & 1 deletion Lib9c
Submodule Lib9c updated 34 files
+1 −1 .Lib9c.Benchmarks/Program.cs
+0 −82 .Lib9c.Tests/Action/RewardGoldTest.cs
+40 −20 .Lib9c.Tests/Policy/BlockPolicyTest.cs
+10 −8 .Lib9c.Tools/SubCommand/State.cs
+1 −1 .Libplanet
+1 −0 @planetarium/lib9c/docs/.gitignore
+74 −0 @planetarium/lib9c/docs/.vitepress/config.ts
+91 −0 @planetarium/lib9c/docs/docs/actions.md
+27 −0 @planetarium/lib9c/docs/docs/index.md
+31 −0 @planetarium/lib9c/docs/docs/installation.md
+70 −0 @planetarium/lib9c/docs/docs/utility.md
+19 −0 @planetarium/lib9c/docs/index.md
+91 −0 @planetarium/lib9c/docs/ko/docs/actions.md
+27 −0 @planetarium/lib9c/docs/ko/docs/index.md
+31 −0 @planetarium/lib9c/docs/ko/docs/installation.md
+70 −0 @planetarium/lib9c/docs/ko/docs/utility.md
+19 −0 @planetarium/lib9c/docs/ko/index.md
+5 −1 @planetarium/lib9c/package.json
+62 −12 @planetarium/lib9c/src/actions/claim_items.ts
+20 −9 @planetarium/lib9c/src/actions/claim_stake_reward.ts
+12 −1 @planetarium/lib9c/src/actions/common.ts
+17 −9 @planetarium/lib9c/src/actions/daily_reward.ts
+33 −9 @planetarium/lib9c/src/actions/deliver_to_others_garages.ts
+49 −9 @planetarium/lib9c/src/actions/load_into_my_garages.ts
+20 −9 @planetarium/lib9c/src/actions/stake.ts
+44 −15 @planetarium/lib9c/src/actions/transfer_asset.ts
+39 −9 @planetarium/lib9c/src/actions/transfer_assets.ts
+29 −9 @planetarium/lib9c/src/index.ts
+31 −0 @planetarium/lib9c/src/models/currencies.ts
+23 −1 @planetarium/lib9c/src/models/hashdigest.ts
+6 −0 @planetarium/lib9c/src/models/networks.ts
+726 −0 @planetarium/pnpm-lock.yaml
+5 −4 Lib9c.Policy/Policy/BlockPolicySource.cs
+1 −2 Lib9c.Utils/BlockHelper.cs
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
26 changes: 22 additions & 4 deletions NineChronicles.Headless.Executable.Tests/ProgramTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Grpc.Core;
using Libplanet.Action;
using Libplanet.Action.Loader;
using Libplanet.Blockchain;
using Libplanet.Blockchain.Policies;
using Libplanet.Common;
using Libplanet.Crypto;
using Libplanet.Net;
using Libplanet.Store;
using Libplanet.Store.Trie;
using Libplanet.Types.Blocks;
using MagicOnion.Client;
using Nekoyume.Action.Loader;
using Nekoyume.Blockchain.Policy;
using Nekoyume.Shared.Services;
using Xunit;

Expand All @@ -22,14 +31,23 @@ public class ProgramTest
private readonly string _storePath;
private readonly ushort _rpcPort;
private readonly ushort _graphqlPort;
private readonly string _genesisBlockHash;
private readonly byte[] _genesisEncoded;

public ProgramTest()
{
var privateKey = new PrivateKey();
_apvString = AppProtocolVersion.Sign(privateKey, 1000).Token;

_genesisBlockPath = "https://release.nine-chronicles.com/genesis-block-9c-main";
_storePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(_storePath);
_genesisBlockPath = Path.Combine(_storePath, "./genesis");
var genesis = BlockChain.ProposeGenesisBlock();

Bencodex.Codec codec = new Bencodex.Codec();
_genesisBlockHash = ByteUtil.Hex(genesis.Hash.ByteArray);
_genesisEncoded = codec.Encode(BlockMarshaler.MarshalBlock(genesis));
File.WriteAllBytes(_genesisBlockPath, _genesisEncoded);

_rpcPort = 41234;
_graphqlPort = 41238;
Expand Down Expand Up @@ -72,7 +90,7 @@ public async Task Run()
var response = await client.PostAsync($"http://localhost:{_graphqlPort}/graphql", content);
var responseString = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("\"data\":{\"chainQuery\":{\"blockQuery\":{\"block\":{\"hash\":\"4582250d0da33b06779a8475d283d5dd210c683b9b999d74d03fac4f58fa6bce\"}}}}", responseString);
Assert.Contains($"\"data\":{{\"chainQuery\":{{\"blockQuery\":{{\"block\":{{\"hash\":\"{_genesisBlockHash}\"}}}}}}}}", responseString);

var channel = new Channel(
$"localhost:{_rpcPort}",
Expand All @@ -87,7 +105,7 @@ public async Task Run()

var service = MagicOnionClient.Create<IBlockChainService>(channel, Array.Empty<IClientFilter>())
.WithCancellationToken(channel.ShutdownToken);
Assert.Equal(11085612, (await service.GetTip()).Length);
Assert.Equal(_genesisEncoded.Length, (await service.GetTip()).Length);
}
finally
{
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);
}
}
Loading

0 comments on commit e8faa14

Please sign in to comment.