@@ -22,13 +22,15 @@ const {
2222const PathUtil = require ( '../common/path-util' ) ;
2323
2424class Block {
25- constructor ( lastHash , lastVotes , transactions , number , epoch , timestamp ,
25+ constructor ( lastHash , lastVotes , evidence , transactions , number , epoch , timestamp ,
2626 stateProofHash , proposer , validators , gasAmountTotal , gasCostTotal ) {
2727 this . last_votes = lastVotes ;
28+ this . evidence = evidence ;
2829 this . transactions = Block . sanitizeTransactions ( transactions ) ;
2930 // Block's header
3031 this . last_hash = lastHash ;
3132 this . last_votes_hash = CommonUtil . hashString ( stringify ( lastVotes ) ) ;
33+ this . evidence_hash = CommonUtil . hashString ( stringify ( this . evidence ) ) ;
3234 this . transactions_hash = CommonUtil . hashString ( stringify ( this . transactions ) ) ;
3335 this . number = number ;
3436 this . epoch = epoch ;
@@ -48,6 +50,7 @@ class Block {
4850 last_hash : this . last_hash ,
4951 last_votes_hash : this . last_votes_hash ,
5052 transactions_hash : this . transactions_hash ,
53+ evidence_hash : this . evidence_hash ,
5154 number : this . number ,
5255 epoch : this . epoch ,
5356 timestamp : this . timestamp ,
@@ -62,6 +65,7 @@ class Block {
6265 get body ( ) {
6366 return {
6467 last_votes : this . last_votes ,
68+ evidence : this . evidence ,
6569 transactions : this . transactions ,
6670 } ;
6771 }
@@ -88,25 +92,26 @@ class Block {
8892 return sizeof ( { ...block . header , ...block . body } ) ;
8993 }
9094
91- static create ( lastHash , lastVotes , transactions , number , epoch ,
92- stateProofHash , proposer , validators , gasAmountTotal , gasCostTotal ) {
93- return new Block ( lastHash , lastVotes , transactions , number , epoch , Date . now ( ) ,
94- stateProofHash , proposer , validators , gasAmountTotal , gasCostTotal ) ;
95+ static create ( lastHash , lastVotes , evidence , transactions , number , epoch ,
96+ stateProofHash , proposer , validators , gasAmountTotal , gasCostTotal , timestamp ) {
97+ return new Block ( lastHash , lastVotes , evidence , transactions , number , epoch ,
98+ timestamp ? timestamp : Date . now ( ) , stateProofHash , proposer , validators , gasAmountTotal ,
99+ gasCostTotal ) ;
95100 }
96101
97102 static parse ( blockInfo ) {
98103 if ( ! Block . hasRequiredFields ( blockInfo ) ) return null ;
99104 if ( blockInfo instanceof Block ) return blockInfo ;
100- return new Block ( blockInfo . last_hash , blockInfo . last_votes ,
105+ return new Block ( blockInfo . last_hash , blockInfo . last_votes , blockInfo . evidence ,
101106 blockInfo . transactions , blockInfo . number , blockInfo . epoch , blockInfo . timestamp ,
102107 blockInfo . state_proof_hash , blockInfo . proposer , blockInfo . validators ,
103108 blockInfo . gas_amount_total , blockInfo . gas_cost_total ) ;
104109 }
105110
106111 static hasRequiredFields ( block ) {
107112 return ( block && block . last_hash !== undefined && block . last_votes !== undefined &&
108- block . transactions !== undefined && block . number !== undefined &&
109- block . epoch !== undefined && block . timestamp !== undefined &&
113+ block . evidence !== undefined && block . transactions !== undefined &&
114+ block . number !== undefined && block . epoch !== undefined && block . timestamp !== undefined &&
110115 block . state_proof_hash !== undefined && block . proposer !== undefined &&
111116 block . validators !== undefined && block . gas_amount_total !== undefined &&
112117 block . gas_cost_total !== undefined ) ;
@@ -129,6 +134,11 @@ class Block {
129134 `[${ LOG_HEADER } ] Last votes or last_votes_hash is incorrect for block ${ block . hash } ` ) ;
130135 return false ;
131136 }
137+ if ( block . evidence_hash !== CommonUtil . hashString ( stringify ( block . evidence ) ) ) {
138+ logger . error (
139+ `[${ LOG_HEADER } ] Evidence or evidence_hash is incorrect for block ${ block . hash } ` ) ;
140+ return false ;
141+ }
132142 return true ;
133143 }
134144
@@ -138,8 +148,8 @@ class Block {
138148 if ( ! CommonUtil . isCksumAddr ( address ) ) {
139149 return false ;
140150 }
141- if ( ! CommonUtil . isDict ( info ) || ! CommonUtil . isNumber ( info [ PredefinedDbPaths . STAKE ] ) ||
142- ! CommonUtil . isBool ( info [ PredefinedDbPaths . PROPOSAL_RIGHT ] ) ) {
151+ if ( ! CommonUtil . isDict ( info ) || ! CommonUtil . isNumber ( info [ PredefinedDbPaths . CONSENSUS_STAKE ] ) ||
152+ ! CommonUtil . isBool ( info [ PredefinedDbPaths . CONSENSUS_PROPOSAL_RIGHT ] ) ) {
143153 return false ;
144154 }
145155 }
@@ -292,7 +302,7 @@ class Block {
292302 operation : {
293303 type : 'SET_VALUE' ,
294304 ref : PathUtil . getStakingStakeRecordValuePath ( PredefinedDbPaths . CONSENSUS , address , 0 , timestamp ) ,
295- value : info [ PredefinedDbPaths . STAKE ]
305+ value : info [ PredefinedDbPaths . CONSENSUS_STAKE ]
296306 }
297307 } ;
298308 txs . push ( Transaction . fromTxBody ( txBody , privateKey ) ) ;
@@ -315,13 +325,13 @@ class Block {
315325 return [ firstTx , secondTx , thirdTx , ...stakingTxs ] ;
316326 }
317327
318- static executeGenesisTxsAndGetData ( genesisTxs ) {
328+ static executeGenesisTxsAndGetData ( genesisTxs , genesisTime ) {
319329 const tempGenesisDb = new DB (
320330 new StateNode ( StateVersions . EMPTY ) , StateVersions . EMPTY , null , null , false , - 1 , null ) ;
321331 tempGenesisDb . initDbStates ( ) ;
322332 const resList = [ ] ;
323333 for ( const tx of genesisTxs ) {
324- const res = tempGenesisDb . executeTransaction ( Transaction . toExecutable ( tx ) , true ) ;
334+ const res = tempGenesisDb . executeTransaction ( Transaction . toExecutable ( tx ) , true , false , 0 , genesisTime ) ;
325335 if ( CommonUtil . isFailedTx ( res ) ) {
326336 logger . error ( `Genesis transaction failed:\n${ JSON . stringify ( tx , null , 2 ) } ` +
327337 `\nRESULT: ${ JSON . stringify ( res ) } ` )
@@ -345,13 +355,14 @@ class Block {
345355 const genesisTime = GenesisAccounts [ AccountProperties . TIMESTAMP ] ;
346356 const lastHash = '' ;
347357 const lastVotes = [ ] ;
358+ const evidence = { } ;
348359 const transactions = Block . getGenesisBlockTxs ( genesisTime ) ;
349360 const number = 0 ;
350361 const epoch = 0 ;
351362 const proposer = ownerAddress ;
352363 const validators = GENESIS_VALIDATORS ;
353- const { stateProofHash, gasAmountTotal, gasCostTotal } = Block . executeGenesisTxsAndGetData ( transactions ) ;
354- return new Block ( lastHash , lastVotes , transactions , number , epoch , genesisTime ,
364+ const { stateProofHash, gasAmountTotal, gasCostTotal } = Block . executeGenesisTxsAndGetData ( transactions , genesisTime ) ;
365+ return new Block ( lastHash , lastVotes , evidence , transactions , number , epoch , genesisTime ,
355366 stateProofHash , proposer , validators , gasAmountTotal , gasCostTotal ) ;
356367 }
357368}
0 commit comments