77import static org .tron .common .utils .Commons .adjustAssetBalanceV2 ;
88import static org .tron .common .utils .Commons .adjustTotalShieldedPoolValue ;
99import static org .tron .common .utils .Commons .getExchangeStoreFinal ;
10+ import static org .tron .common .utils .StringUtil .encode58Check ;
1011import static org .tron .core .exception .BadBlockException .TypeEnum .CALC_MERKLE_ROOT_FAILED ;
12+ import static org .tron .protos .Protocol .Transaction .Result .contractResult .SUCCESS ;
1113
14+ import com .beust .jcommander .internal .Lists ;
1215import com .google .common .collect .Maps ;
1316import com .google .common .collect .Sets ;
1417import com .google .protobuf .Any ;
@@ -481,7 +484,7 @@ public void adjustAssetBalanceV2Test() {
481484 } catch (BalanceInsufficientException e ) {
482485 Assert .assertTrue (e instanceof BalanceInsufficientException );
483486 Assert .assertEquals (
484- "reduceAssetAmount failed! account: " + StringUtil . encode58Check (account .createDbKey ()),
487+ "reduceAssetAmount failed! account: " + encode58Check (account .createDbKey ()),
485488 e .getMessage ());
486489 }
487490
@@ -744,7 +747,7 @@ public void fork()
744747 chainManager .addWitness (ByteString .copyFrom (address ));
745748 chainManager .getWitnessStore ().put (address , witnessCapsule );
746749
747- Block block = getSignedBlock (witnessCapsule .getAddress (), 1533529947843L , privateKey );
750+ Block block = getSignedBlock (witnessCapsule .getAddress (), 1533529947000L , privateKey );
748751
749752 dbManager .pushBlock (new BlockCapsule (block ));
750753
@@ -754,15 +757,15 @@ public void fork()
754757 long num = chainManager .getDynamicPropertiesStore ().getLatestBlockHeaderNumber ();
755758 BlockCapsule blockCapsule0 =
756759 createTestBlockCapsule (
757- 1533529947843L + 3000 ,
760+ 1533529947000L + 3000 ,
758761 num + 1 ,
759762 chainManager .getDynamicPropertiesStore ().getLatestBlockHeaderHash ()
760763 .getByteString (),
761764 addressToProvateKeys );
762765
763766 BlockCapsule blockCapsule1 =
764767 createTestBlockCapsule (
765- 1533529947843L + 3000 ,
768+ 1533529947000L + 3000 ,
766769 num + 1 ,
767770 chainManager .getDynamicPropertiesStore ().getLatestBlockHeaderHash ()
768771 .getByteString (),
@@ -773,7 +776,7 @@ public void fork()
773776
774777 BlockCapsule blockCapsule2 =
775778 createTestBlockCapsule (
776- 1533529947843L + 6000 ,
779+ 1533529947000L + 6000 ,
777780 num + 2 , blockCapsule1 .getBlockId ().getByteString (), addressToProvateKeys );
778781
779782 dbManager .pushBlock (blockCapsule2 );
@@ -800,6 +803,23 @@ public void fork()
800803 Assert .assertEquals (
801804 chainManager .getHead ().getBlockId (),
802805 chainManager .getDynamicPropertiesStore ().getLatestBlockHeaderHash ());
806+
807+ dbManager .getDynamicPropertiesStore ().saveConsensusLogicOptimization (1 );
808+ BlockCapsule blockCapsule3 =
809+ createTestBlockCapsule2 (1533529947000L + 9000 ,
810+ num + 3 , blockCapsule2 .getBlockId ().getByteString (), addressToProvateKeys );
811+
812+ assertThrows (BadBlockException .class , () -> dbManager .pushBlock (blockCapsule3 ));
813+ }
814+
815+ private Transaction buildTransaction (com .google .protobuf .Message message , ContractType contractType ) {
816+ Transaction .raw .Builder rawBuilder = Transaction .raw .newBuilder ().addContract (
817+ Transaction .Contract .newBuilder ().setType (contractType ).setParameter (
818+ (message instanceof Any ? (Any ) message : Any .pack (message ))).build ());
819+ Transaction .Builder transactionBuilder = Transaction .newBuilder ().setRawData (rawBuilder )
820+ .addRet (Transaction .Result .newBuilder ().setContractRet (SUCCESS ).build ())
821+ .addRet (Transaction .Result .newBuilder ().setContractRet (SUCCESS ).build ());
822+ return transactionBuilder .build ();
803823 }
804824
805825 @ Test
@@ -1095,6 +1115,36 @@ private BlockCapsule createTestBlockCapsule(long time,
10951115 return blockCapsule ;
10961116 }
10971117
1118+ private BlockCapsule createTestBlockCapsule2 (long time , long number , ByteString hash ,
1119+ Map <ByteString , String > addressToProvateKeys ) {
1120+ TransferContract c1 = TransferContract .newBuilder ()
1121+ .setOwnerAddress (ByteString .copyFrom ("f1" .getBytes ()))
1122+ .setAmount (1 ).build ();
1123+ ByteString witnessAddress = dposSlot .getScheduledWitness (dposSlot .getSlot (time ));
1124+ Protocol .BlockHeader .raw .Builder blockHeaderRawBuild = Protocol .BlockHeader .raw .newBuilder ();
1125+ Protocol .BlockHeader .raw blockHeaderRaw = blockHeaderRawBuild
1126+ .setTimestamp (time )
1127+ .setParentHash (hash )
1128+ .setWitnessAddress (witnessAddress )
1129+ .setNumber (number )
1130+ .build ();
1131+
1132+ // block header
1133+ Protocol .BlockHeader .Builder blockHeaderBuild = Protocol .BlockHeader .newBuilder ();
1134+ Protocol .BlockHeader blockHeader = blockHeaderBuild .setRawData (blockHeaderRaw ).build ();
1135+
1136+ // block
1137+ Block .Builder blockBuild = Block .newBuilder ();
1138+ List <Transaction > transactions = Lists .newArrayList (buildTransaction (c1 ,
1139+ ContractType .TransferContract ));
1140+ transactions .forEach (blockBuild ::addTransactions );
1141+ BlockCapsule blockCapsule = new BlockCapsule (blockBuild .setBlockHeader (blockHeader ).build ());
1142+ blockCapsule .generatedByMyself = true ;
1143+ blockCapsule .setMerkleRoot ();
1144+ blockCapsule .sign (ByteArray .fromHexString (addressToProvateKeys .get (witnessAddress )));
1145+ return blockCapsule ;
1146+ }
1147+
10981148 private BlockCapsule createTestBlockCapsuleError (long time ,
10991149 long number , ByteString hash ,
11001150 Map <ByteString , String > addressToProvateKeys ) {
0 commit comments