Skip to content

Commit

Permalink
Merge pull request #2622 from OnedgeLee/bump/sloth
Browse files Browse the repository at this point in the history
Bump Libplanet:sloth
  • Loading branch information
OnedgeLee authored Jun 12, 2024
2 parents 51faf09 + 66f821b commit e793752
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 36 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
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 52 files
+86 −0 CHANGES.md
+1 −1 Docs/articles/overview.md
+11 −9 Libplanet.Action/State/IBlockChainStates.cs
+0 −2 Libplanet.Explorer.Tests/GeneratedBlockChainFixture.cs
+5 −0 Libplanet.Explorer.Tests/GraphTypes/BlockTypeTest.cs
+4 −0 Libplanet.Explorer/GraphTypes/BlockType.cs
+5 −5 Libplanet.Explorer/Queries/StateQuery.cs
+1 −6 Libplanet.Extensions.Cocona/Commands/BlockCommand.cs
+6 −13 Libplanet.Mocks/MockBlockChainStates.cs
+1 −1 Libplanet.Mocks/MockWorldState.cs
+9 −0 Libplanet.Net.Tests/Consensus/ConsensusContextTest.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
+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
+5 −4 Libplanet.Tests/Action/ActionEvaluatorTest.Migration.cs
+14 −17 Libplanet.Tests/Action/ActionEvaluatorTest.cs
+13 −11 Libplanet.Tests/Action/WorldTest.cs
+82 −50 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
+1 −1 Libplanet.Tests/Blocks/BlockMetadataTest.cs
+10 −10 Libplanet.Tests/Blocks/PreEvaluationBlockHeaderTest.cs
+14 −22 Libplanet.Tests/Blocks/PreEvaluationBlockTest.cs
+1 −5 Libplanet.Tests/Fixtures/IntegerSet.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 −10 Libplanet.Tests/TestUtils.cs
+10 −1 Libplanet.Types/Blocks/BlockMetadata.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
+259 −22 Libplanet/Blockchain/BlockChain.cs
+5 −9 Libplanet/Blockchain/BlockChainStates.cs
+1 −1 Libplanet/Libplanet.csproj
9 changes: 5 additions & 4 deletions Lib9c.Policy/Policy/BlockPolicySource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ internal IBlockPolicy GetPolicy(
#endif
}

// TODO: Remove BlockChain.GetNextWorldState() from below method.
internal static TxPolicyViolationException? ValidateNextBlockTxRaw(
BlockChain blockChain,
IActionLoader actionLoader,
Expand All @@ -173,7 +174,7 @@ internal IBlockPolicy GetPolicy(
try
{
if (blockChain
.GetWorldState()
.GetNextWorldState()!
.GetBalance(MeadConfig.PatronAddress, Currencies.Mead) < 1 * Currencies.Mead)
{
// Check Activation
Expand All @@ -186,7 +187,7 @@ internal IBlockPolicy GetPolicy(
{
return transaction.Nonce == 0 &&
blockChain
.GetWorldState()
.GetNextWorldState()!
.GetAccountState(ReservedAddresses.LegacyAccount)
.GetState(activate.PendingAddress) is Dictionary rawPending &&
new PendingActivationState(rawPending).Verify(activate.Signature)
Expand All @@ -211,14 +212,14 @@ internal IBlockPolicy GetPolicy(
}

switch (blockChain
.GetWorldState()
.GetNextWorldState()!
.GetAccountState(ReservedAddresses.LegacyAccount)
.GetState(transaction.Signer.Derive(ActivationKey.DeriveKey)))
{
case null:
// Fallback for pre-migration.
if (blockChain
.GetWorldState()
.GetNextWorldState()!
.GetAccountState(ReservedAddresses.LegacyAccount)
.GetState(ActivatedAccountsState.Address) is Dictionary asDict)
{
Expand Down
3 changes: 1 addition & 2 deletions Lib9c.Utils/BlockHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,13 @@ public static Block ProposeGenesisBlock(
actionLoader);
return
BlockChain.ProposeGenesisBlock(
actionEvaluator,
privateKey: privateKey,
transactions: ImmutableList<Transaction>.Empty
.Add(Transaction.Create(
0, privateKey, null, actions.ToPlainValues()))
.AddRange(systemActions.Select((sa, index) =>
Transaction.Create(
index + 1, privateKey, null, new [] { sa.PlainValue }))),
privateKey: privateKey,
timestamp: timestamp);
}
}
Expand Down

0 comments on commit e793752

Please sign in to comment.