@@ -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}
0 commit comments