@@ -17,6 +17,7 @@ const {
17
17
PredefinedDbPaths,
18
18
StateVersions,
19
19
ValueChangedEventSources,
20
+ TransactionStates,
20
21
} = require ( '../common/constants' ) ;
21
22
const { ConsensusErrorCode } = require ( '../common/result-code' ) ;
22
23
const {
@@ -600,7 +601,8 @@ class Consensus {
600
601
601
602
static validateAndExecuteLastVotes ( lastVotes , lastHash , number , blockTime , db , blockPool , eventSource ) {
602
603
if ( number === 0 ) return ;
603
- if ( ! db . executeTransactionList ( lastVotes , true , false , number , blockTime , eventSource ) ) {
604
+ const txsRes = db . executeTransactionList ( lastVotes , true , false , number , blockTime , eventSource ) ;
605
+ if ( ! txsRes ) {
604
606
throw new ConsensusError ( {
605
607
code : ConsensusErrorCode . EXECUTING_LAST_VOTES_FAILURE ,
606
608
message : `Failed to execute last votes` ,
@@ -628,6 +630,7 @@ class Consensus {
628
630
level : 'error'
629
631
} ) ;
630
632
}
633
+ return txsRes ;
631
634
}
632
635
633
636
// Cross-check the offenses in proposalTx & the evidence in proposalBlock
@@ -726,6 +729,7 @@ class Consensus {
726
729
level : 'error'
727
730
} ) ;
728
731
}
732
+ return txsRes ;
729
733
}
730
734
731
735
static validateStateProofHash ( expectedStateProofHash , block , db , node , takeSnapshot ) {
@@ -774,16 +778,17 @@ class Consensus {
774
778
} ) ;
775
779
}
776
780
// Try executing the proposal tx on the proposal block's db state
777
- const proposalTxExecRes = tempDb . executeTransaction ( executableTx , true , false , number , blockTime ) ;
781
+ const txRes = tempDb . executeTransaction ( executableTx , true , false , number , blockTime ) ;
778
782
tempDb . destroyDb ( ) ;
779
- if ( CommonUtil . isFailedTx ( proposalTxExecRes ) ) {
783
+ if ( CommonUtil . isFailedTx ( txRes ) ) {
780
784
throw new ConsensusError ( {
781
785
code : ConsensusErrorCode . EXECUTING_PROPOSAL_FAILURE ,
782
- message : `Failed to execute the proposal tx: ${ JSON . stringify ( proposalTxExecRes ) } ` ,
786
+ message : `Failed to execute the proposal tx: ${ JSON . stringify ( txRes ) } ` ,
783
787
level : 'error'
784
788
} ) ;
785
789
}
786
790
node . tp . addTransaction ( executableTx ) ;
791
+ return txRes ;
787
792
}
788
793
789
794
static addBlockToBlockPool ( block , proposalTx , db , blockPool ) {
@@ -797,7 +802,54 @@ class Consensus {
797
802
blockPool . addToHashToDbMap ( block . hash , db ) ;
798
803
}
799
804
800
- static validateAndExecuteBlockOnDb ( rawBlock , node , stateVersionPrefix , proposalTx = null , takeSnapshot = false ) {
805
+ static updateTransactionInfoForBlock ( node , voteTxs , proposalTx , transactions , voteTxsRes , proposalTxRes , txsRes ) {
806
+ const trackedAt = Date . now ( ) ;
807
+ for ( let i = 0 ; i < voteTxs . length ; i ++ ) {
808
+ const voteTx = voteTxs [ i ] ;
809
+ const voteResult = voteTxsRes [ i ] ;
810
+ const tracked = node . tp . transactionTracker . get ( voteTx . hash ) || { } ;
811
+ const beforeState = _ . get ( tracked , 'state' , null ) ;
812
+ node . tp . updateTransactionInfo ( voteTx . hash , {
813
+ state : TransactionStates . IN_BLOCK ,
814
+ exec_result : voteResult ,
815
+ tracked_at : trackedAt ,
816
+ } ) ;
817
+ if ( node . eh ) {
818
+ node . eh . emitTxStateChanged ( voteTx , beforeState , TransactionStates . IN_BLOCK ) ;
819
+ }
820
+ }
821
+ // NOTE(platfowner): Genesis block has no porposal transaction.
822
+ if ( proposalTx ) {
823
+ const trackedProposalTx = node . tp . transactionTracker . get ( proposalTx . hash ) || { } ;
824
+ const beforeState = _ . get ( trackedProposalTx , 'state' , null ) ;
825
+ node . tp . updateTransactionInfo ( proposalTx . hash , {
826
+ state : TransactionStates . IN_BLOCK ,
827
+ exec_result : proposalTxRes ,
828
+ tracked_at : trackedAt ,
829
+ } ) ;
830
+ if ( node . eh ) {
831
+ node . eh . emitTxStateChanged ( proposalTx , beforeState , TransactionStates . IN_BLOCK ) ;
832
+ }
833
+ }
834
+ for ( let i = 0 ; i < transactions . length ; i ++ ) {
835
+ const tx = transactions [ i ] ;
836
+ const txResult = txsRes [ i ] ;
837
+ const tracked = node . tp . transactionTracker . get ( tx . hash ) || { } ;
838
+ const beforeState = _ . get ( tracked , 'state' , null ) ;
839
+ const txState =
840
+ CommonUtil . isFailedTx ( txResult ) ? TransactionStates . REVERTED : TransactionStates . IN_BLOCK ;
841
+ node . tp . updateTransactionInfo ( tx . hash , {
842
+ state : txState ,
843
+ exec_result : txResult ,
844
+ tracked_at : trackedAt ,
845
+ } ) ;
846
+ if ( node . eh ) {
847
+ node . eh . emitTxStateChanged ( tx , beforeState , txState ) ;
848
+ }
849
+ }
850
+ }
851
+
852
+ static validateAndExecuteBlockOnDb ( rawBlock , node , stateVersionPrefix , proposalTx , updateTxInfo , takeSnapshot = false ) {
801
853
const block = Block . parse ( rawBlock ) ;
802
854
if ( ! block ) {
803
855
throw new ConsensusError ( {
@@ -822,23 +874,29 @@ class Consensus {
822
874
node . bc . lastBlockNumber ( ) , node . stateManager . getFinalVersion ( ) , node . bp ) ;
823
875
const db = Consensus . getNewDbForProposal ( number , baseVersion , stateVersionPrefix , node ) ;
824
876
877
+ let voteTxsRes = null ;
878
+ let proposalTxRes = null ;
879
+ let txsRes = null ;
825
880
try {
826
881
Consensus . validateBlockNumberAndHashes ( block , prevBlock , node . bc . genesisBlockHash ) ;
827
882
Consensus . validateValidators ( validators , number , baseVersion , node ) ;
828
883
Consensus . validateProposer ( number , prevBlockLastVotesHash , epoch , validators , proposer ) ;
829
- Consensus . validateAndExecuteLastVotes ( last_votes , last_hash , number , timestamp , db , node . bp , ValueChangedEventSources . BLOCK ) ;
884
+ voteTxsRes = Consensus . validateAndExecuteLastVotes ( last_votes , last_hash , number , timestamp , db , node . bp , ValueChangedEventSources . BLOCK ) ;
830
885
Consensus . validateAndExecuteOffensesAndEvidence (
831
886
evidence , validators , prevBlockMajority , number , timestamp , proposalTx , db , ValueChangedEventSources . BLOCK ) ;
832
- Consensus . validateAndExecuteTransactions (
887
+ txsRes = Consensus . validateAndExecuteTransactions (
833
888
transactions , receipts , number , timestamp , gas_amount_total , gas_cost_total , db , node , ValueChangedEventSources . BLOCK ) ;
834
889
db . applyBandagesForBlockNumber ( number ) ;
835
890
Consensus . validateStateProofHash ( state_proof_hash , block , db , node , takeSnapshot ) ;
836
- Consensus . executeProposalTx ( proposalTx , number , timestamp , db , node ) ;
891
+ proposalTxRes = Consensus . executeProposalTx ( proposalTx , number , timestamp , db , node ) ;
837
892
Consensus . addBlockToBlockPool ( block , proposalTx , db , node . bp ) ;
838
893
} catch ( e ) {
839
894
db . destroyDb ( ) ;
840
895
throw e ;
841
896
}
897
+ if ( updateTxInfo ) {
898
+ Consensus . updateTransactionInfoForBlock ( node , last_votes , proposalTx , transactions , voteTxsRes , proposalTxRes , txsRes ) ;
899
+ }
842
900
}
843
901
844
902
/**
@@ -854,7 +912,7 @@ class Consensus {
854
912
`${ proposalBlock . number } / ${ proposalBlock . epoch } / ${ proposalBlock . hash } / ${ proposalBlock . proposer } \n` +
855
913
`${ proposalTx . hash } / ${ proposalTx . address } ` ) ;
856
914
857
- Consensus . validateAndExecuteBlockOnDb ( proposalBlock , this . node , StateVersions . POOL , proposalTx ) ;
915
+ Consensus . validateAndExecuteBlockOnDb ( proposalBlock , this . node , StateVersions . POOL , proposalTx , true ) ;
858
916
859
917
if ( proposalBlock . number > 1 && ! this . node . bp . longestNotarizedChainTips . includes ( proposalBlock . last_hash ) ) {
860
918
throw new ConsensusError ( {
@@ -1144,7 +1202,7 @@ class Consensus {
1144
1202
proposalTx = i < chain . length - 1 ? ConsensusUtil . filterProposalFromVotes ( chain [ i + 1 ] . last_votes ) : null ;
1145
1203
logger . debug ( `[${ LOG_HEADER } ] applying block ${ JSON . stringify ( block ) } ` ) ;
1146
1204
try {
1147
- Consensus . validateAndExecuteBlockOnDb ( block , this . node , StateVersions . SNAP , proposalTx ) ;
1205
+ Consensus . validateAndExecuteBlockOnDb ( block , this . node , StateVersions . SNAP , proposalTx , false ) ;
1148
1206
} catch ( e ) {
1149
1207
logger . error ( `[${ LOG_HEADER } ] Failed to validate and execute block ${ block . number } : ${ e } ` ) ;
1150
1208
return null ;
0 commit comments