Skip to content

Commit

Permalink
Merge pull request #2653 from planetarium/release/1.13.0
Browse files Browse the repository at this point in the history
Release/1.13.0
  • Loading branch information
ipdae authored Jun 27, 2024
2 parents 01f3829 + e793752 commit 315e97a
Show file tree
Hide file tree
Showing 34 changed files with 1,725 additions and 211 deletions.
2 changes: 1 addition & 1 deletion .Lib9c.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ static void Main(string[] args)
block.Transactions.Count()
);

chain.DetermineBlockStateRootHash(block, out IReadOnlyList<ICommittedActionEvaluation> blockEvals);
chain.DetermineNextBlockStateRootHash(block, out IReadOnlyList<ICommittedActionEvaluation> blockEvals);
txs += block.Transactions.LongCount();
actions += block.Transactions.Sum(tx =>
tx.Actions is { } customActions ? customActions.LongCount() : 0);
Expand Down
82 changes: 0 additions & 82 deletions .Lib9c.Tests/Action/RewardGoldTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -491,88 +491,6 @@ void AssertMinerReward(int blockIndex, string expected)
AssertMinerReward(50457601, "0");
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task Genesis_StateRootHash(bool mainnet)
{
BlockPolicySource blockPolicySource = new BlockPolicySource();
NCStagePolicy stagePolicy = new NCStagePolicy(default, 2);
IBlockPolicy policy = blockPolicySource.GetPolicy();
Block genesis;
if (mainnet)
{
const string genesisBlockPath = "https://release.nine-chronicles.com/genesis-block-9c-main";
var uri = new Uri(genesisBlockPath);
using var client = new HttpClient();
var rawBlock = await client.GetByteArrayAsync(uri);
var blockDict = (Bencodex.Types.Dictionary)new Codec().Decode(rawBlock);
genesis = BlockMarshaler.UnmarshalBlock(blockDict);
}
else
{
var adminPrivateKey = new PrivateKey();
var adminAddress = adminPrivateKey.Address;
var activatedAccounts = ImmutableHashSet<Address>.Empty;
var nonce = new byte[] { 0x00, 0x01, 0x02, 0x03 };
var privateKey = new PrivateKey();
(ActivationKey activationKey, PendingActivationState pendingActivation) =
ActivationKey.Create(privateKey, nonce);
var pendingActivationStates = new List<PendingActivationState>
{
pendingActivation,
};
var initializeStates = new InitializeStates(
rankingState: new RankingState0(),
shopState: new ShopState(),
gameConfigState: new GameConfigState(),
redeemCodeState: new RedeemCodeState(Bencodex.Types.Dictionary.Empty
.Add("address", RedeemCodeState.Address.Serialize())
.Add("map", Bencodex.Types.Dictionary.Empty)
),
adminAddressState: new AdminState(adminAddress, 1500000),
activatedAccountsState: new ActivatedAccountsState(activatedAccounts),
#pragma warning disable CS0618
// Use of obsolete method Currency.Legacy(): https://github.com/planetarium/lib9c/discussions/1319
goldCurrencyState: new GoldCurrencyState(Currency.Legacy("NCG", 2, null)),
#pragma warning restore CS0618
goldDistributions: new GoldDistribution[0],
tableSheets: TableSheetsImporter.ImportSheets(),
pendingActivationStates: pendingActivationStates.ToArray()
);
var tempActionEvaluator = new ActionEvaluator(
policyBlockActionGetter: _ => policy.BlockAction,
stateStore: new TrieStateStore(new MemoryKeyValueStore()),
actionTypeLoader: new NCActionLoader());
genesis = BlockChain.ProposeGenesisBlock(
tempActionEvaluator,
transactions: ImmutableList<Transaction>.Empty
.Add(Transaction.Create(
0,
new PrivateKey(),
null,
new ActionBase[] { initializeStates }.ToPlainValues()))
);
}

var store = new DefaultStore(null);
var stateStore = new TrieStateStore(new DefaultKeyValueStore(null));
var blockChain = BlockChain.Create(
policy: policy,
store: store,
stagePolicy: stagePolicy,
stateStore: stateStore,
genesisBlock: genesis,
actionEvaluator: new ActionEvaluator(
policyBlockActionGetter: _ => policy.BlockAction,
stateStore: stateStore,
actionTypeLoader: new NCActionLoader()
),
renderers: new IRenderer[] { new ActionRenderer(), new BlockRenderer() }
);
Assert.Equal(genesis.StateRootHash, blockChain.Genesis.StateRootHash);
}

[Theory]
[InlineData(5, 4)]
[InlineData(101, 100)]
Expand Down
60 changes: 40 additions & 20 deletions .Lib9c.Tests/Policy/BlockPolicyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ namespace Lib9c.Tests
using System.Collections.Immutable;
using System.Linq;
using System.Numerics;
using System.Security.Cryptography;
using Bencodex.Types;
using Lib9c.Renderers;
using Libplanet.Action;
using Libplanet.Action.State;
using Libplanet.Blockchain;
using Libplanet.Blockchain.Policies;
using Libplanet.Common;
using Libplanet.Crypto;
using Libplanet.Store;
using Libplanet.Store.Trie;
Expand Down Expand Up @@ -395,7 +397,7 @@ public void EarnMiningGoldWhenSuccessMining()
Block block = blockChain.ProposeBlock(adminPrivateKey);
blockChain.Append(block, GenerateBlockCommit(block, adminPrivateKey));
FungibleAssetValue actualBalance = blockChain
.GetWorldState()
.GetNextWorldState()
.GetBalance(adminAddress, _currency);
FungibleAssetValue expectedBalance = new FungibleAssetValue(_currency, 10, 0);
Assert.True(expectedBalance.Equals(actualBalance));
Expand Down Expand Up @@ -424,17 +426,18 @@ public void ValidateNextBlockWithManyTransactions()

using var store = new DefaultStore(null);
var stateStore = new TrieStateStore(new MemoryKeyValueStore());
var actionEvaluator = new ActionEvaluator(
policyBlockActionGetter: _ => policy.BlockAction,
stateStore: stateStore,
actionTypeLoader: new NCActionLoader()
);
var blockChain = BlockChain.Create(
policy,
stagePolicy,
store,
stateStore,
genesis,
new ActionEvaluator(
policyBlockActionGetter: _ => policy.BlockAction,
stateStore: stateStore,
actionTypeLoader: new NCActionLoader()
)
actionEvaluator
);

int nonce = 0;
Expand Down Expand Up @@ -465,7 +468,7 @@ List<Transaction> GenerateTransactions(int count)
txHash: BlockContent.DeriveTxHash(txs),
lastCommit: null),
transactions: txs).Propose();
Block block1 = EvaluateAndSign(blockChain, preEvalBlock1, adminPrivateKey);
Block block1 = EvaluateAndSign(store, actionEvaluator, preEvalBlock1, adminPrivateKey);
blockChain.Append(block1, GenerateBlockCommit(block1, adminPrivateKey));
Assert.Equal(2, blockChain.Count);
Assert.True(blockChain.ContainsBlock(block1.Hash));
Expand All @@ -479,7 +482,7 @@ List<Transaction> GenerateTransactions(int count)
txHash: BlockContent.DeriveTxHash(txs),
lastCommit: GenerateBlockCommit(blockChain.Tip, adminPrivateKey)),
transactions: txs).Propose();
Block block2 = EvaluateAndSign(blockChain, preEvalBlock2, adminPrivateKey);
Block block2 = EvaluateAndSign(store, actionEvaluator, preEvalBlock2, adminPrivateKey);
blockChain.Append(block2, GenerateBlockCommit(block2, adminPrivateKey));
Assert.Equal(3, blockChain.Count);
Assert.True(blockChain.ContainsBlock(block2.Hash));
Expand All @@ -493,7 +496,7 @@ List<Transaction> GenerateTransactions(int count)
txHash: BlockContent.DeriveTxHash(txs),
lastCommit: GenerateBlockCommit(blockChain.Tip, adminPrivateKey)),
transactions: txs).Propose();
Block block3 = EvaluateAndSign(blockChain, preEvalBlock3, adminPrivateKey);
Block block3 = EvaluateAndSign(store, actionEvaluator, preEvalBlock3, adminPrivateKey);
Assert.Throws<InvalidBlockTxCountException>(
() => blockChain.Append(block3, GenerateBlockCommit(block3, adminPrivateKey)));
Assert.Equal(3, blockChain.Count);
Expand Down Expand Up @@ -525,17 +528,18 @@ public void ValidateNextBlockWithManyTransactionsPerSigner()

using var store = new DefaultStore(null);
var stateStore = new TrieStateStore(new MemoryKeyValueStore());
var actionEvaluator = new ActionEvaluator(
policyBlockActionGetter: _ => policy.BlockAction,
stateStore: stateStore,
actionTypeLoader: new NCActionLoader()
);
var blockChain = BlockChain.Create(
policy,
stagePolicy,
store,
stateStore,
genesis,
new ActionEvaluator(
policyBlockActionGetter: _ => policy.BlockAction,
stateStore: stateStore,
actionTypeLoader: new NCActionLoader()
)
actionEvaluator
);

int nonce = 0;
Expand Down Expand Up @@ -566,7 +570,7 @@ List<Transaction> GenerateTransactions(int count)
txHash: BlockContent.DeriveTxHash(txs),
lastCommit: null),
transactions: txs).Propose();
Block block1 = EvaluateAndSign(blockChain, preEvalBlock1, adminPrivateKey);
Block block1 = EvaluateAndSign(store, actionEvaluator, preEvalBlock1, adminPrivateKey);

// Should be fine since policy hasn't kicked in yet.
blockChain.Append(block1, GenerateBlockCommit(block1, adminPrivateKey));
Expand All @@ -583,7 +587,7 @@ List<Transaction> GenerateTransactions(int count)
txHash: BlockContent.DeriveTxHash(txs),
lastCommit: GenerateBlockCommit(blockChain.Tip, adminPrivateKey)),
transactions: txs).Propose();
Block block2 = EvaluateAndSign(blockChain, preEvalBlock2, adminPrivateKey);
Block block2 = EvaluateAndSign(store, actionEvaluator, preEvalBlock2, adminPrivateKey);

// Subpolicy kicks in.
Assert.Throws<InvalidBlockTxCountPerSignerException>(
Expand All @@ -604,7 +608,7 @@ List<Transaction> GenerateTransactions(int count)
txHash: BlockContent.DeriveTxHash(txs),
lastCommit: GenerateBlockCommit(blockChain.Tip, adminPrivateKey)),
transactions: txs).Propose();
Block block3 = EvaluateAndSign(blockChain, preEvalBlock3, adminPrivateKey);
Block block3 = EvaluateAndSign(store, actionEvaluator, preEvalBlock3, adminPrivateKey);
blockChain.Append(block3, GenerateBlockCommit(block3, adminPrivateKey));
Assert.Equal(3, blockChain.Count);
Assert.True(blockChain.ContainsBlock(block3.Hash));
Expand Down Expand Up @@ -663,13 +667,29 @@ private Block MakeGenesisBlock(
}

private Block EvaluateAndSign(
BlockChain blockChain,
IStore store,
ActionEvaluator actionEvaluator,
PreEvaluationBlock preEvaluationBlock,
PrivateKey privateKey
)
{
var stateRootHash = blockChain.DetermineBlockStateRootHash(preEvaluationBlock, out _);
return preEvaluationBlock.Sign(privateKey, stateRootHash);
if (preEvaluationBlock.Index < 1)
{
throw new ArgumentException(
$"Given {nameof(preEvaluationBlock)} must have block index " +
$"higher than 0");
}

if (preEvaluationBlock.ProtocolVersion < BlockMetadata.SlothProtocolVersion)
{
throw new ArgumentException(
$"{nameof(preEvaluationBlock)} of which protocol version less than" +
$"{BlockMetadata.SlothProtocolVersion} is not acceptable");
}

var stateRootHash = store.GetNextStateRootHash((BlockHash)preEvaluationBlock.PreviousHash);

return preEvaluationBlock.Sign(privateKey, (HashDigest<SHA256>)stateRootHash);
}
}
}
18 changes: 10 additions & 8 deletions .Lib9c.Tools/SubCommand/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,16 @@ IStateStore stateStore
_ => policy.BlockAction,
stateStore,
actionLoader);
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);

IReadOnlyList<ICommittedActionEvaluation> evals = actionEvaluator.Evaluate(block, refSrh);
HashDigest<SHA256> stateRootHash = evals.Count > 0
? evals[evals.Count - 1].OutputState
: refSrh is { } prevSrh ? prevSrh : MerkleTrie.EmptyRootHash;

DateTimeOffset now = DateTimeOffset.Now;
if (invalidStateRootHashBlock is null && !stateRootHash.Equals(block.StateRootHash))
{
Expand Down
2 changes: 1 addition & 1 deletion .Libplanet
Submodule .Libplanet updated 85 files
+141 −0 CHANGES.md
+1 −1 Docs/articles/overview.md
+26 −0 Libplanet.Action.Tests/BlockProtocolVersionNotSupportedExceptionTest.cs
+10 −0 Libplanet.Action/ActionEvaluator.cs
+0 −1 Libplanet.Action/AssemblyInfo.cs
+52 −0 Libplanet.Action/BlockProtocolVersionNotSupportedException.cs
+4 −0 Libplanet.Action/IActionEvaluator.cs
+11 −9 Libplanet.Action/State/IBlockChainStates.cs
+0 −62 Libplanet.Action/State/IStateStoreExtensions.cs
+0 −130 Libplanet.Common/Nonce.cs
+0 −4 Libplanet.Explorer.Executable/Options.cs
+0 −8 Libplanet.Explorer.Executable/Program.cs
+80 −0 Libplanet.Explorer.Tests/Fixtures/BlockChainStatesFixture.cs
+0 −2 Libplanet.Explorer.Tests/GeneratedBlockChainFixture.cs
+5 −0 Libplanet.Explorer.Tests/GraphTypes/BlockTypeTest.cs
+18 −31 Libplanet.Explorer.Tests/Queries/RawStateQueryTest.cs
+213 −159 Libplanet.Explorer.Tests/Queries/StateQueryTest.Legacy.cs
+0 −77 Libplanet.Explorer.Tests/Queries/StateQueryTest.Mocks.cs
+142 −149 Libplanet.Explorer.Tests/Queries/StateQueryTest.cs
+4 −0 Libplanet.Explorer/GraphTypes/AccountStateType.cs
+4 −0 Libplanet.Explorer/GraphTypes/BlockType.cs
+41 −0 Libplanet.Explorer/GraphTypes/WorldStateType.cs
+5 −14 Libplanet.Explorer/Queries/StateQuery.cs
+1 −6 Libplanet.Extensions.Cocona/Commands/BlockCommand.cs
+73 −0 Libplanet.Mocks/MockBlockChainStates.cs
+1 −1 Libplanet.Mocks/MockWorldState.cs
+9 −0 Libplanet.Net.Tests/Consensus/ConsensusContextTest.cs
+1 −2 Libplanet.Net.Tests/Consensus/ContextNonProposerTest.cs
+117 −3 Libplanet.Net.Tests/Consensus/ContextTest.cs
+1 −1 Libplanet.Net.Tests/Messages/MessageTest.cs
+3 −3 Libplanet.Net.Tests/SwarmTest.Preload.cs
+17 −14 Libplanet.Net.Tests/SwarmTest.cs
+1 −1 Libplanet.Net.Tests/TestUtils.cs
+22 −6 Libplanet.Net/Consensus/ConsensusContext.cs
+5 −0 Libplanet.Net/Consensus/Context.Async.cs
+6 −11 Libplanet.Net/Consensus/Context.Mutate.cs
+9 −26 Libplanet.Net/Consensus/Context.cs
+10 −1 Libplanet.Net/Swarm.BlockCandidate.cs
+0 −12 Libplanet.Net/Swarm.cs
+95 −0 Libplanet.RocksDBStore/RocksDBStore.cs
+12 −0 Libplanet.Store/BaseStore.cs
+59 −0 Libplanet.Store/DefaultStore.cs
+29 −0 Libplanet.Store/IStore.cs
+18 −0 Libplanet.Store/MemoryStore.cs
+0 −7 Libplanet.Store/TrieStateStore.cs
+5 −4 Libplanet.Tests/Action/ActionEvaluatorTest.Migration.cs
+27 −17 Libplanet.Tests/Action/ActionEvaluatorTest.cs
+105 −121 Libplanet.Tests/Action/WorldTest.cs
+15 −0 Libplanet.Tests/Action/WorldV0Test.cs
+15 −0 Libplanet.Tests/Action/WorldV1Test.cs
+83 −52 Libplanet.Tests/Blockchain/BlockChainTest.Append.cs
+2 −1 Libplanet.Tests/Blockchain/BlockChainTest.Internals.cs
+23 −23 Libplanet.Tests/Blockchain/BlockChainTest.ProposeBlock.cs
+178 −9 Libplanet.Tests/Blockchain/BlockChainTest.ValidateNextBlock.cs
+41 −41 Libplanet.Tests/Blockchain/BlockChainTest.cs
+0 −89 Libplanet.Tests/Blockchain/Renderers/AtomicActionRendererTest.cs
+65 −0 Libplanet.Tests/Blocks/BlockMarshalerTest.Legacy.cs
+1 −1 Libplanet.Tests/Blocks/BlockMarshalerTest.cs
+12 −72 Libplanet.Tests/Blocks/BlockMetadataTest.cs
+23 −23 Libplanet.Tests/Blocks/PreEvaluationBlockHeaderTest.cs
+14 −22 Libplanet.Tests/Blocks/PreEvaluationBlockTest.cs
+1 −5 Libplanet.Tests/Fixtures/IntegerSet.cs
+109 −0 Libplanet.Tests/Fixtures/LegacyBlocks.cs
+0 −58 Libplanet.Tests/NonceTest.cs
+14 −0 Libplanet.Tests/Store/ProxyStore.cs
+13 −8 Libplanet.Tests/Store/StoreFixture.cs
+33 −1 Libplanet.Tests/Store/StoreTest.cs
+20 −0 Libplanet.Tests/Store/StoreTracker.cs
+7 −16 Libplanet.Tests/TestUtils.cs
+3 −5 Libplanet.Types/Blocks/BlockContent.cs
+19 −48 Libplanet.Types/Blocks/BlockMarshaler.cs
+27 −10 Libplanet.Types/Blocks/BlockMetadata.cs
+1 −2 Libplanet.Types/Blocks/IBlockHeader.cs
+3 −4 Libplanet.Types/Blocks/IPreEvaluationBlockHeader.cs
+2 −2 Libplanet.Types/Blocks/PreEvaluationBlockHeader.cs
+129 −74 Libplanet/Blockchain/BlockChain.Evaluate.cs
+21 −18 Libplanet/Blockchain/BlockChain.ProposeBlock.cs
+45 −5 Libplanet/Blockchain/BlockChain.States.cs
+1 −1 Libplanet/Blockchain/BlockChain.TxExecution.cs
+65 −7 Libplanet/Blockchain/BlockChain.Validate.cs
+260 −24 Libplanet/Blockchain/BlockChain.cs
+5 −9 Libplanet/Blockchain/BlockChainStates.cs
+0 −123 Libplanet/Blockchain/Renderers/AtomicActionRenderer.cs
+0 −3 Libplanet/Blockchain/Renderers/IActionRenderer.cs
+1 −1 Libplanet/Libplanet.csproj
1 change: 1 addition & 0 deletions @planetarium/lib9c/docs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vitepress/cache
74 changes: 74 additions & 0 deletions @planetarium/lib9c/docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { defineConfig } from 'vitepress'

// https://vitepress.dev/reference/site-config
export default defineConfig({
title: "@planetarium/lib9c",
description: "Documentation for @planetarium/lib9c package",
themeConfig: {
// https://vitepress.dev/reference/default-theme-config
nav: [
{ text: 'Docs', link: '/docs/index.md' },
{ text: 'API Reference', link: 'https://jsr.io/@planetarium/lib9c' },
],

socialLinks: [
{ icon: 'github', link: 'https://github.com/planetarium/lib9c/tree/development/%40planetarium/lib9c' }
],

i18nRouting: true,
sidebar: [
{
link: '/docs/index.md',
text: "Introduction"
},
{
link: '/docs/installation.md',
text: "Installation"
},
{
link: '/docs/actions.md',
text: "Actions"
},
{
link: '/docs/utility.md',
text: "Utility"
}
],
outline: 'deep'
},
locales: {
root: {
label: 'English',
lang: 'en',
},
ko: {
label: 'Korean',
lang: 'ko',
themeConfig: {
i18nRouting: true,
nav: [
{ text: '문서', link: '/ko/docs/index.md' },
{ text: 'API Reference', link: 'https://jsr.io/@planetarium/lib9c' },
],
sidebar: [
{
link: '/ko/docs/index.md',
text: "소개"
},
{
link: '/ko/docs/installation.md',
text: "설치"
},
{
link: '/ko/docs/actions.md',
text: "액션"
},
{
link: '/ko/docs/utility.md',
text: "유틸리티"
}
]
}
},
}
})
Loading

0 comments on commit 315e97a

Please sign in to comment.