@@ -271,10 +271,10 @@ class BlockPool {
271271 } ) ;
272272 this . tryUpdateNotarized ( blockHash ) ;
273273 }
274- logger . info (
274+ logger . debug (
275275 `[${ LOG_HEADER } ] Block added to the block pool: ${ block . number } / ${ block . epoch } ` ) ;
276276 } else {
277- logger . info (
277+ logger . debug (
278278 `[${ LOG_HEADER } ] Block already in the block pool: ${ block . number } / ${ block . epoch } ` ) ;
279279 }
280280 }
@@ -289,10 +289,10 @@ class BlockPool {
289289 if ( CommonUtil . isEmpty ( blockInfo . block ) ) {
290290 this . hashToInvalidBlockInfo [ blockHash ] . block = block ;
291291 this . hashToInvalidBlockInfo [ blockHash ] . proposal = proposalTx ;
292- logger . info (
292+ logger . debug (
293293 `[${ LOG_HEADER } ] Invalid block added to the block pool: ${ block . number } / ${ block . epoch } ` ) ;
294294 } else {
295- logger . info (
295+ logger . debug (
296296 `[${ LOG_HEADER } ] Invalid block already in the block pool: ${ block . number } / ${ block . epoch } ` ) ;
297297 }
298298 }
@@ -361,9 +361,11 @@ class BlockPool {
361361 addSeenVote ( voteTx ) {
362362 const LOG_HEADER = 'addSeenVote' ;
363363 const blockHash = ConsensusUtil . getBlockHashFromConsensusTx ( voteTx ) ;
364+ const blockNumber = ConsensusUtil . getBlockNumberFromConsensusTx ( voteTx ) ;
364365 const stake = ConsensusUtil . getStakeFromVoteTx ( voteTx ) ;
365366 logger . debug ( `[${ LOG_HEADER } ] voteTx: ${ JSON . stringify ( voteTx , null , 2 ) } , ` +
366- `blockHash: ${ blockHash } , stake: ${ stake } ` ) ;
367+ `blockHash: ${ blockHash } , blockNumber: ${ blockNumber } , stake: ${ stake } ` ) ;
368+ this . addToNumberToBlockSet ( { number : blockNumber , hash : blockHash } ) ;
367369 if ( ConsensusUtil . isProposalTx ( voteTx ) ) {
368370 this . addProposal ( voteTx , blockHash ) ;
369371 } else if ( ConsensusUtil . isAgainstVoteTx ( voteTx ) ) {
@@ -435,6 +437,9 @@ class BlockPool {
435437 logger . info ( `[${ LOG_HEADER } ] Current block is unavailable` ) ;
436438 return ;
437439 }
440+ if ( currentBlockInfo . notarized ) {
441+ return ;
442+ }
438443 if ( currentBlockInfo . block . number === 0 ) {
439444 this . hashToBlockInfo [ currentBlockInfo . block . hash ] . notarized = true ;
440445 this . updateLongestNotarizedChains ( this . hashToBlockInfo [ currentBlockInfo . block . hash ] ) ;
@@ -468,6 +473,20 @@ class BlockPool {
468473 }
469474
470475 cleanUpForBlockHash ( blockHash ) {
476+ const block = _get ( this . hashToBlockInfo [ blockHash ] , 'block' , null ) ;
477+ const blockProposal = _get ( this . hashToBlockInfo [ blockHash ] , 'proposal' , null ) ;
478+ const blockConsensusTxs = _get ( this . hashToBlockInfo [ blockHash ] , 'votes' , [ ] ) ;
479+ if ( blockProposal ) {
480+ blockConsensusTxs . push ( blockProposal ) ;
481+ }
482+ const invalidBlock = _get ( this . hashToInvalidBlockInfo [ blockHash ] , 'block' , null ) ;
483+ const invalidBlockProposal = _get ( this . hashToInvalidBlockInfo [ blockHash ] , 'proposal' , null ) ;
484+ const invalidBlockConsensusTxs = _get ( this . hashToInvalidBlockInfo [ blockHash ] , 'votes' , [ ] ) ;
485+ if ( invalidBlockProposal ) {
486+ invalidBlockConsensusTxs . push ( invalidBlockProposal ) ;
487+ }
488+ this . node . tp . cleanUpConsensusTxs ( block , blockConsensusTxs ) ;
489+ this . node . tp . cleanUpConsensusTxs ( invalidBlock , invalidBlockConsensusTxs ) ;
471490 delete this . hashToBlockInfo [ blockHash ] ;
472491 delete this . hashToInvalidBlockInfo [ blockHash ] ;
473492 delete this . hashToNextBlockSet [ blockHash ] ;
@@ -481,13 +500,14 @@ class BlockPool {
481500 // Remove everything that came before lastBlock.
482501 cleanUpAfterFinalization ( lastBlock , recordedInvalidBlocks ) {
483502 const targetNumber = lastBlock . number ;
503+ const maxInvalidBlocksOnMem = this . node . getBlockchainParam ( 'consensus/max_invalid_blocks_on_mem' ) ;
484504 for ( const blockNumber of Object . keys ( this . numberToBlockSet ) ) {
485- if ( blockNumber < targetNumber ) {
505+ const number = Number ( blockNumber ) ;
506+ if ( number < targetNumber ) {
486507 const blockHashList = this . numberToBlockSet [ blockNumber ] ;
487508 for ( const blockHash of blockHashList ) {
488509 if ( this . hashToInvalidBlockInfo [ blockHash ] ) {
489- if ( recordedInvalidBlocks . has ( blockHash ) ||
490- blockNumber < targetNumber - ConsensusConsts . MAX_CONSENSUS_LOGS_IN_STATES ) {
510+ if ( recordedInvalidBlocks . has ( blockHash ) || number < targetNumber - maxInvalidBlocksOnMem ) {
491511 this . cleanUpForBlockHash ( blockHash ) ;
492512 this . numberToBlockSet [ blockNumber ] . delete ( blockHash ) ;
493513 }
@@ -502,7 +522,7 @@ class BlockPool {
502522 }
503523 }
504524 for ( const epoch of Object . keys ( this . epochToBlock ) ) {
505- if ( epoch < lastBlock . epoch ) {
525+ if ( Number ( epoch ) < lastBlock . epoch ) {
506526 const blockHash = this . epochToBlock [ epoch ] ;
507527 this . cleanUpForBlockHash ( blockHash ) ;
508528 delete this . epochToBlock [ epoch ] ;
@@ -521,6 +541,7 @@ class BlockPool {
521541 */
522542 getValidLastVotes ( lastBlock , blockNumber , blockTime , tempDb ) {
523543 const LOG_HEADER = 'getValidLastVotes' ;
544+ const chainId = this . node . getBlockchainParam ( 'genesis/chain_id' ) ;
524545 const lastBlockInfo = this . hashToBlockInfo [ lastBlock . hash ] ;
525546 logger . debug ( `[${ LOG_HEADER } ] lastBlockInfo: ${ JSON . stringify ( lastBlockInfo , null , 2 ) } ` ) ;
526547 // FIXME(minsulee2 or liayoo): When I am behind and a newly coming node is ahead of me,
@@ -537,7 +558,7 @@ class BlockPool {
537558 let tallied = 0 ;
538559 for ( const vote of lastVotes ) {
539560 if ( CommonUtil . isFailedTx ( tempDb . executeTransaction (
540- Transaction . toExecutable ( vote ) , true , true , 0 , blockTime ) ) ) {
561+ Transaction . toExecutable ( vote , chainId ) , true , true , 0 , blockTime ) ) ) {
541562 logger . debug ( `[${ LOG_HEADER } ] failed to execute last vote: ${ JSON . stringify ( vote , null , 2 ) } ` ) ;
542563 } else {
543564 tallied += _get ( lastBlock . validators , `${ vote . address } .stake` , 0 ) ;
@@ -566,6 +587,7 @@ class BlockPool {
566587 const LOG_HEADER = 'getOffensesAndEvidence' ;
567588 const totalAtStake = ConsensusUtil . getTotalAtStake ( validators ) ;
568589 const blockNumber = baseDb . blockNumberSnapshot ;
590+ const chainId = this . node . getBlockchainParam ( 'genesis/chain_id' ) ;
569591 let backupDb = this . node . createTempDb (
570592 baseDb . stateVersion , `${ StateVersions . SNAP } :${ blockNumber } ` , blockNumber ) ;
571593 const majority = totalAtStake * ConsensusConsts . MAJORITY ;
@@ -589,7 +611,8 @@ class BlockPool {
589611 for ( const vote of blockInfo . votes ) {
590612 const stake = _get ( validators , `${ vote . address } .stake` , 0 ) ;
591613 if ( stake > 0 ) {
592- const res = baseDb . executeTransaction ( Transaction . toExecutable ( vote ) , true , true , 0 , blockTime ) ;
614+ const res = baseDb . executeTransaction (
615+ Transaction . toExecutable ( vote , chainId ) , true , true , 0 , blockTime ) ;
593616 if ( CommonUtil . isFailedTx ( res ) ) {
594617 logger . debug ( `[${ LOG_HEADER } ] Failed to execute evidence vote:\n${ JSON . stringify ( vote , null , 2 ) } \n${ JSON . stringify ( res , null , 2 ) } )` ) ;
595618 } else {
0 commit comments