Skip to content

Commit 315e97a

Browse files
authored
Merge pull request #2653 from planetarium/release/1.13.0
Release/1.13.0
2 parents 01f3829 + e793752 commit 315e97a

34 files changed

+1725
-211
lines changed

.Lib9c.Benchmarks/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ static void Main(string[] args)
121121
block.Transactions.Count()
122122
);
123123

124-
chain.DetermineBlockStateRootHash(block, out IReadOnlyList<ICommittedActionEvaluation> blockEvals);
124+
chain.DetermineNextBlockStateRootHash(block, out IReadOnlyList<ICommittedActionEvaluation> blockEvals);
125125
txs += block.Transactions.LongCount();
126126
actions += block.Transactions.Sum(tx =>
127127
tx.Actions is { } customActions ? customActions.LongCount() : 0);

.Lib9c.Tests/Action/RewardGoldTest.cs

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -491,88 +491,6 @@ void AssertMinerReward(int blockIndex, string expected)
491491
AssertMinerReward(50457601, "0");
492492
}
493493

494-
[Theory]
495-
[InlineData(true)]
496-
[InlineData(false)]
497-
public async Task Genesis_StateRootHash(bool mainnet)
498-
{
499-
BlockPolicySource blockPolicySource = new BlockPolicySource();
500-
NCStagePolicy stagePolicy = new NCStagePolicy(default, 2);
501-
IBlockPolicy policy = blockPolicySource.GetPolicy();
502-
Block genesis;
503-
if (mainnet)
504-
{
505-
const string genesisBlockPath = "https://release.nine-chronicles.com/genesis-block-9c-main";
506-
var uri = new Uri(genesisBlockPath);
507-
using var client = new HttpClient();
508-
var rawBlock = await client.GetByteArrayAsync(uri);
509-
var blockDict = (Bencodex.Types.Dictionary)new Codec().Decode(rawBlock);
510-
genesis = BlockMarshaler.UnmarshalBlock(blockDict);
511-
}
512-
else
513-
{
514-
var adminPrivateKey = new PrivateKey();
515-
var adminAddress = adminPrivateKey.Address;
516-
var activatedAccounts = ImmutableHashSet<Address>.Empty;
517-
var nonce = new byte[] { 0x00, 0x01, 0x02, 0x03 };
518-
var privateKey = new PrivateKey();
519-
(ActivationKey activationKey, PendingActivationState pendingActivation) =
520-
ActivationKey.Create(privateKey, nonce);
521-
var pendingActivationStates = new List<PendingActivationState>
522-
{
523-
pendingActivation,
524-
};
525-
var initializeStates = new InitializeStates(
526-
rankingState: new RankingState0(),
527-
shopState: new ShopState(),
528-
gameConfigState: new GameConfigState(),
529-
redeemCodeState: new RedeemCodeState(Bencodex.Types.Dictionary.Empty
530-
.Add("address", RedeemCodeState.Address.Serialize())
531-
.Add("map", Bencodex.Types.Dictionary.Empty)
532-
),
533-
adminAddressState: new AdminState(adminAddress, 1500000),
534-
activatedAccountsState: new ActivatedAccountsState(activatedAccounts),
535-
#pragma warning disable CS0618
536-
// Use of obsolete method Currency.Legacy(): https://github.com/planetarium/lib9c/discussions/1319
537-
goldCurrencyState: new GoldCurrencyState(Currency.Legacy("NCG", 2, null)),
538-
#pragma warning restore CS0618
539-
goldDistributions: new GoldDistribution[0],
540-
tableSheets: TableSheetsImporter.ImportSheets(),
541-
pendingActivationStates: pendingActivationStates.ToArray()
542-
);
543-
var tempActionEvaluator = new ActionEvaluator(
544-
policyBlockActionGetter: _ => policy.BlockAction,
545-
stateStore: new TrieStateStore(new MemoryKeyValueStore()),
546-
actionTypeLoader: new NCActionLoader());
547-
genesis = BlockChain.ProposeGenesisBlock(
548-
tempActionEvaluator,
549-
transactions: ImmutableList<Transaction>.Empty
550-
.Add(Transaction.Create(
551-
0,
552-
new PrivateKey(),
553-
null,
554-
new ActionBase[] { initializeStates }.ToPlainValues()))
555-
);
556-
}
557-
558-
var store = new DefaultStore(null);
559-
var stateStore = new TrieStateStore(new DefaultKeyValueStore(null));
560-
var blockChain = BlockChain.Create(
561-
policy: policy,
562-
store: store,
563-
stagePolicy: stagePolicy,
564-
stateStore: stateStore,
565-
genesisBlock: genesis,
566-
actionEvaluator: new ActionEvaluator(
567-
policyBlockActionGetter: _ => policy.BlockAction,
568-
stateStore: stateStore,
569-
actionTypeLoader: new NCActionLoader()
570-
),
571-
renderers: new IRenderer[] { new ActionRenderer(), new BlockRenderer() }
572-
);
573-
Assert.Equal(genesis.StateRootHash, blockChain.Genesis.StateRootHash);
574-
}
575-
576494
[Theory]
577495
[InlineData(5, 4)]
578496
[InlineData(101, 100)]

.Lib9c.Tests/Policy/BlockPolicyTest.cs

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ namespace Lib9c.Tests
55
using System.Collections.Immutable;
66
using System.Linq;
77
using System.Numerics;
8+
using System.Security.Cryptography;
89
using Bencodex.Types;
910
using Lib9c.Renderers;
1011
using Libplanet.Action;
1112
using Libplanet.Action.State;
1213
using Libplanet.Blockchain;
1314
using Libplanet.Blockchain.Policies;
15+
using Libplanet.Common;
1416
using Libplanet.Crypto;
1517
using Libplanet.Store;
1618
using Libplanet.Store.Trie;
@@ -395,7 +397,7 @@ public void EarnMiningGoldWhenSuccessMining()
395397
Block block = blockChain.ProposeBlock(adminPrivateKey);
396398
blockChain.Append(block, GenerateBlockCommit(block, adminPrivateKey));
397399
FungibleAssetValue actualBalance = blockChain
398-
.GetWorldState()
400+
.GetNextWorldState()
399401
.GetBalance(adminAddress, _currency);
400402
FungibleAssetValue expectedBalance = new FungibleAssetValue(_currency, 10, 0);
401403
Assert.True(expectedBalance.Equals(actualBalance));
@@ -424,17 +426,18 @@ public void ValidateNextBlockWithManyTransactions()
424426

425427
using var store = new DefaultStore(null);
426428
var stateStore = new TrieStateStore(new MemoryKeyValueStore());
429+
var actionEvaluator = new ActionEvaluator(
430+
policyBlockActionGetter: _ => policy.BlockAction,
431+
stateStore: stateStore,
432+
actionTypeLoader: new NCActionLoader()
433+
);
427434
var blockChain = BlockChain.Create(
428435
policy,
429436
stagePolicy,
430437
store,
431438
stateStore,
432439
genesis,
433-
new ActionEvaluator(
434-
policyBlockActionGetter: _ => policy.BlockAction,
435-
stateStore: stateStore,
436-
actionTypeLoader: new NCActionLoader()
437-
)
440+
actionEvaluator
438441
);
439442

440443
int nonce = 0;
@@ -465,7 +468,7 @@ List<Transaction> GenerateTransactions(int count)
465468
txHash: BlockContent.DeriveTxHash(txs),
466469
lastCommit: null),
467470
transactions: txs).Propose();
468-
Block block1 = EvaluateAndSign(blockChain, preEvalBlock1, adminPrivateKey);
471+
Block block1 = EvaluateAndSign(store, actionEvaluator, preEvalBlock1, adminPrivateKey);
469472
blockChain.Append(block1, GenerateBlockCommit(block1, adminPrivateKey));
470473
Assert.Equal(2, blockChain.Count);
471474
Assert.True(blockChain.ContainsBlock(block1.Hash));
@@ -479,7 +482,7 @@ List<Transaction> GenerateTransactions(int count)
479482
txHash: BlockContent.DeriveTxHash(txs),
480483
lastCommit: GenerateBlockCommit(blockChain.Tip, adminPrivateKey)),
481484
transactions: txs).Propose();
482-
Block block2 = EvaluateAndSign(blockChain, preEvalBlock2, adminPrivateKey);
485+
Block block2 = EvaluateAndSign(store, actionEvaluator, preEvalBlock2, adminPrivateKey);
483486
blockChain.Append(block2, GenerateBlockCommit(block2, adminPrivateKey));
484487
Assert.Equal(3, blockChain.Count);
485488
Assert.True(blockChain.ContainsBlock(block2.Hash));
@@ -493,7 +496,7 @@ List<Transaction> GenerateTransactions(int count)
493496
txHash: BlockContent.DeriveTxHash(txs),
494497
lastCommit: GenerateBlockCommit(blockChain.Tip, adminPrivateKey)),
495498
transactions: txs).Propose();
496-
Block block3 = EvaluateAndSign(blockChain, preEvalBlock3, adminPrivateKey);
499+
Block block3 = EvaluateAndSign(store, actionEvaluator, preEvalBlock3, adminPrivateKey);
497500
Assert.Throws<InvalidBlockTxCountException>(
498501
() => blockChain.Append(block3, GenerateBlockCommit(block3, adminPrivateKey)));
499502
Assert.Equal(3, blockChain.Count);
@@ -525,17 +528,18 @@ public void ValidateNextBlockWithManyTransactionsPerSigner()
525528

526529
using var store = new DefaultStore(null);
527530
var stateStore = new TrieStateStore(new MemoryKeyValueStore());
531+
var actionEvaluator = new ActionEvaluator(
532+
policyBlockActionGetter: _ => policy.BlockAction,
533+
stateStore: stateStore,
534+
actionTypeLoader: new NCActionLoader()
535+
);
528536
var blockChain = BlockChain.Create(
529537
policy,
530538
stagePolicy,
531539
store,
532540
stateStore,
533541
genesis,
534-
new ActionEvaluator(
535-
policyBlockActionGetter: _ => policy.BlockAction,
536-
stateStore: stateStore,
537-
actionTypeLoader: new NCActionLoader()
538-
)
542+
actionEvaluator
539543
);
540544

541545
int nonce = 0;
@@ -566,7 +570,7 @@ List<Transaction> GenerateTransactions(int count)
566570
txHash: BlockContent.DeriveTxHash(txs),
567571
lastCommit: null),
568572
transactions: txs).Propose();
569-
Block block1 = EvaluateAndSign(blockChain, preEvalBlock1, adminPrivateKey);
573+
Block block1 = EvaluateAndSign(store, actionEvaluator, preEvalBlock1, adminPrivateKey);
570574

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

588592
// Subpolicy kicks in.
589593
Assert.Throws<InvalidBlockTxCountPerSignerException>(
@@ -604,7 +608,7 @@ List<Transaction> GenerateTransactions(int count)
604608
txHash: BlockContent.DeriveTxHash(txs),
605609
lastCommit: GenerateBlockCommit(blockChain.Tip, adminPrivateKey)),
606610
transactions: txs).Propose();
607-
Block block3 = EvaluateAndSign(blockChain, preEvalBlock3, adminPrivateKey);
611+
Block block3 = EvaluateAndSign(store, actionEvaluator, preEvalBlock3, adminPrivateKey);
608612
blockChain.Append(block3, GenerateBlockCommit(block3, adminPrivateKey));
609613
Assert.Equal(3, blockChain.Count);
610614
Assert.True(blockChain.ContainsBlock(block3.Hash));
@@ -663,13 +667,29 @@ private Block MakeGenesisBlock(
663667
}
664668

665669
private Block EvaluateAndSign(
666-
BlockChain blockChain,
670+
IStore store,
671+
ActionEvaluator actionEvaluator,
667672
PreEvaluationBlock preEvaluationBlock,
668673
PrivateKey privateKey
669674
)
670675
{
671-
var stateRootHash = blockChain.DetermineBlockStateRootHash(preEvaluationBlock, out _);
672-
return preEvaluationBlock.Sign(privateKey, stateRootHash);
676+
if (preEvaluationBlock.Index < 1)
677+
{
678+
throw new ArgumentException(
679+
$"Given {nameof(preEvaluationBlock)} must have block index " +
680+
$"higher than 0");
681+
}
682+
683+
if (preEvaluationBlock.ProtocolVersion < BlockMetadata.SlothProtocolVersion)
684+
{
685+
throw new ArgumentException(
686+
$"{nameof(preEvaluationBlock)} of which protocol version less than" +
687+
$"{BlockMetadata.SlothProtocolVersion} is not acceptable");
688+
}
689+
690+
var stateRootHash = store.GetNextStateRootHash((BlockHash)preEvaluationBlock.PreviousHash);
691+
692+
return preEvaluationBlock.Sign(privateKey, (HashDigest<SHA256>)stateRootHash);
673693
}
674694
}
675695
}

.Lib9c.Tools/SubCommand/State.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,16 @@ IStateStore stateStore
118118
_ => policy.BlockAction,
119119
stateStore,
120120
actionLoader);
121-
HashDigest<SHA256> stateRootHash = block.Index < 1
122-
? BlockChain.DetermineGenesisStateRootHash(
123-
actionEvaluator,
124-
preEvalBlock,
125-
out _)
126-
: chain.DetermineBlockStateRootHash(
127-
preEvalBlock,
128-
out _);
121+
122+
HashDigest<SHA256>? refSrh = block.ProtocolVersion < BlockMetadata.SlothProtocolVersion
123+
? store.GetStateRootHash(block.PreviousHash)
124+
: store.GetStateRootHash(block.Hash);
125+
126+
IReadOnlyList<ICommittedActionEvaluation> evals = actionEvaluator.Evaluate(block, refSrh);
127+
HashDigest<SHA256> stateRootHash = evals.Count > 0
128+
? evals[evals.Count - 1].OutputState
129+
: refSrh is { } prevSrh ? prevSrh : MerkleTrie.EmptyRootHash;
130+
129131
DateTimeOffset now = DateTimeOffset.Now;
130132
if (invalidStateRootHashBlock is null && !stateRootHash.Equals(block.StateRootHash))
131133
{

.Libplanet

Submodule .Libplanet updated 85 files

@planetarium/lib9c/docs/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vitepress/cache
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { defineConfig } from 'vitepress'
2+
3+
// https://vitepress.dev/reference/site-config
4+
export default defineConfig({
5+
title: "@planetarium/lib9c",
6+
description: "Documentation for @planetarium/lib9c package",
7+
themeConfig: {
8+
// https://vitepress.dev/reference/default-theme-config
9+
nav: [
10+
{ text: 'Docs', link: '/docs/index.md' },
11+
{ text: 'API Reference', link: 'https://jsr.io/@planetarium/lib9c' },
12+
],
13+
14+
socialLinks: [
15+
{ icon: 'github', link: 'https://github.com/planetarium/lib9c/tree/development/%40planetarium/lib9c' }
16+
],
17+
18+
i18nRouting: true,
19+
sidebar: [
20+
{
21+
link: '/docs/index.md',
22+
text: "Introduction"
23+
},
24+
{
25+
link: '/docs/installation.md',
26+
text: "Installation"
27+
},
28+
{
29+
link: '/docs/actions.md',
30+
text: "Actions"
31+
},
32+
{
33+
link: '/docs/utility.md',
34+
text: "Utility"
35+
}
36+
],
37+
outline: 'deep'
38+
},
39+
locales: {
40+
root: {
41+
label: 'English',
42+
lang: 'en',
43+
},
44+
ko: {
45+
label: 'Korean',
46+
lang: 'ko',
47+
themeConfig: {
48+
i18nRouting: true,
49+
nav: [
50+
{ text: '문서', link: '/ko/docs/index.md' },
51+
{ text: 'API Reference', link: 'https://jsr.io/@planetarium/lib9c' },
52+
],
53+
sidebar: [
54+
{
55+
link: '/ko/docs/index.md',
56+
text: "소개"
57+
},
58+
{
59+
link: '/ko/docs/installation.md',
60+
text: "설치"
61+
},
62+
{
63+
link: '/ko/docs/actions.md',
64+
text: "액션"
65+
},
66+
{
67+
link: '/ko/docs/utility.md',
68+
text: "유틸리티"
69+
}
70+
]
71+
}
72+
},
73+
}
74+
})

0 commit comments

Comments
 (0)