@@ -11,7 +11,6 @@ const Transaction = require('../tx-pool/transaction');
1111const PushId = require ( '../db/push-id' ) ;
1212const ChainUtil = require ( '../chain-util' ) ;
1313const {
14- DEBUG ,
1514 WriteDbOperations,
1615 ReadDbOperations,
1716 PredefinedDbPaths,
@@ -124,9 +123,7 @@ class Consensus {
124123 // adjust time
125124 try {
126125 const iNTPData = await ntpsync . ntpLocalClockDeltaPromise ( ) ;
127- if ( DEBUG ) {
128- logger . debug ( `(Local Time - NTP Time) Delta = ${ iNTPData . minimalNTPLatencyDelta } ms` ) ;
129- }
126+ logger . debug ( `(Local Time - NTP Time) Delta = ${ iNTPData . minimalNTPLatencyDelta } ms` ) ;
130127 this . timeAdjustment = iNTPData . minimalNTPLatencyDelta ;
131128 } catch ( e ) {
132129 logger . error ( `ntpsync error: ${ e } ` ) ;
@@ -135,17 +132,11 @@ class Consensus {
135132 currentTime -= this . timeAdjustment ;
136133 const absEpoch = Math . floor ( ( currentTime - this . startingTime ) / ConsensusConsts . EPOCH_MS ) ;
137134 if ( this . state . epoch + 1 < absEpoch ) {
138- if ( DEBUG ) {
139- logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] Epoch is too low: ${ this . state . epoch } / ${ absEpoch } ` ) ;
140- }
135+ logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] Epoch is too low: ${ this . state . epoch } / ${ absEpoch } ` ) ;
141136 } else if ( this . state . epoch + 1 > absEpoch ) {
142- if ( DEBUG ) {
143- logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] Epoch is too high: ${ this . state . epoch } / ${ absEpoch } ` ) ;
144- }
145- }
146- if ( DEBUG ) {
147- logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] Updating epoch at ${ currentTime } : ${ this . state . epoch } => ${ absEpoch } ` ) ;
137+ logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] Epoch is too high: ${ this . state . epoch } / ${ absEpoch } ` ) ;
148138 }
139+ logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] Updating epoch at ${ currentTime } : ${ this . state . epoch } => ${ absEpoch } ` ) ;
149140 // re-adjust and update epoch
150141 this . state . epoch = absEpoch ;
151142 if ( this . state . epoch > 1 ) {
@@ -186,9 +177,7 @@ class Consensus {
186177 const LOG_SUFFIX = 'handleConsensusMessage' ;
187178
188179 if ( this . status !== ConsensusStatus . RUNNING ) {
189- if ( DEBUG ) {
190- logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] Consensus status (${ this . status } ) is not RUNNING (${ ConsensusStatus . RUNNING } )` ) ;
191- }
180+ logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] Consensus status (${ this . status } ) is not RUNNING (${ ConsensusStatus . RUNNING } )` ) ;
192181 return ;
193182 }
194183 if ( msg . type !== ConsensusMessageTypes . PROPOSE && msg . type !== ConsensusMessageTypes . VOTE ) {
@@ -200,20 +189,16 @@ class Consensus {
200189 return ;
201190 }
202191 logger . info ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] Consensus state - finalized number: ${ this . node . bc . lastBlockNumber ( ) } / epoch: ${ this . state . epoch } ` ) ;
203- if ( DEBUG ) {
204- logger . debug ( `Message: ${ JSON . stringify ( msg . value , null , 2 ) } ` ) ;
205- }
192+ logger . debug ( `Message: ${ JSON . stringify ( msg . value , null , 2 ) } ` ) ;
206193 if ( msg . type === ConsensusMessageTypes . PROPOSE ) {
207194 const lastNotarizedBlock = this . getLastNotarizedBlock ( ) ;
208- const { proposalBlock, proposalTx } = msg . value ;
195+ const { proposalBlock, proposalTx } = msg . value ;
209196 if ( ! proposalBlock || ! proposalTx ) {
210197 logger . error ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] Proposal is missing required fields: ${ msg . value } ` ) ;
211198 return ;
212199 }
213200 if ( this . node . tp . transactionTracker [ proposalTx . hash ] ) {
214- if ( DEBUG ) {
215- logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] Already have the proposal in my tx tracker` ) ;
216- }
201+ logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] Already have the proposal in my tx tracker` ) ;
217202 return ;
218203 }
219204 if ( proposalBlock . number > lastNotarizedBlock . number + 1 ) {
@@ -234,9 +219,7 @@ class Consensus {
234219 }
235220 } else {
236221 if ( this . node . tp . transactionTracker [ msg . value . hash ] ) {
237- if ( DEBUG ) {
238- logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] Already have the vote in my tx tracker` ) ;
239- }
222+ logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] Already have the vote in my tx tracker` ) ;
240223 return ;
241224 }
242225 if ( Consensus . isValidConsensusTx ( msg . value ) && this . checkVote ( msg . value ) ) {
@@ -264,13 +247,9 @@ class Consensus {
264247 this . node . bc . backupDb : this . blockPool . hashToState . get ( lastBlock . hash ) ;
265248 const tempState = new DB ( null , lastBlock . number - 1 ) ;
266249 tempState . setDbToSnapshot ( prevState ) ;
267- if ( DEBUG ) {
268- logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] Created a temp state for tx checks` ) ;
269- }
250+ logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] Created a temp state for tx checks` ) ;
270251 const lastBlockInfo = this . blockPool . hashToBlockInfo [ lastBlock . hash ] ;
271- if ( DEBUG ) {
272- logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] lastBlockInfo: ${ JSON . stringify ( lastBlockInfo , null , 2 ) } ` ) ;
273- }
252+ logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] lastBlockInfo: ${ JSON . stringify ( lastBlockInfo , null , 2 ) } ` ) ;
274253 // FIXME(minsu or lia): When I am behind and a newly coming node is ahead of me, then I cannot
275254 // get lastBlockInfo from the block-pool. So that, it is not able to create a proper block
276255 // proposal and also cannot pass checkProposal() where checking prevBlockInfo.notarized.
@@ -287,9 +266,7 @@ class Consensus {
287266 } )
288267
289268 transactions . forEach ( tx => {
290- if ( DEBUG ) {
291- logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] Checking tx ${ JSON . stringify ( tx , null , 2 ) } ` ) ;
292- }
269+ logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] Checking tx ${ JSON . stringify ( tx , null , 2 ) } ` ) ;
293270 if ( ! ChainUtil . transactionFailed ( tempState . executeTransaction ( tx ) ) ) {
294271 logger . info ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] tx result: success!` ) ;
295272 validTransactions . push ( tx ) ;
@@ -373,13 +350,9 @@ class Consensus {
373350 const { proposer, number, epoch, last_hash } = proposalBlock ;
374351 if ( number <= this . node . bc . lastBlockNumber ( ) ) {
375352 logger . info ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] There already is a finalized block of the number` ) ;
376- if ( DEBUG ) {
377- logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] corresponding block info: ${ JSON . stringify ( this . blockPool . hashToBlockInfo [ proposalBlock . hash ] , null , 2 ) } ` ) ;
378- }
353+ logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] corresponding block info: ${ JSON . stringify ( this . blockPool . hashToBlockInfo [ proposalBlock . hash ] , null , 2 ) } ` ) ;
379354 if ( ! this . blockPool . hasSeenBlock ( proposalBlock ) ) {
380- if ( DEBUG ) {
381- logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] Adding the proposal to the blockPool for later use` ) ;
382- }
355+ logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] Adding the proposal to the blockPool for later use` ) ;
383356 this . blockPool . addSeenBlock ( proposalBlock , proposalTx ) ;
384357 }
385358 return false ;
@@ -388,13 +361,9 @@ class Consensus {
388361 // those can notarize the prevBlock (verify, execute and add the missing votes)
389362 let prevBlockInfo = number === 1 ? this . node . bc . getBlockByNumber ( 0 ) : this . blockPool . hashToBlockInfo [ last_hash ] ;
390363 const prevBlock = number > 1 ? prevBlockInfo . block : prevBlockInfo ;
391- if ( DEBUG ) {
392- logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] prevBlockInfo: ${ JSON . stringify ( prevBlockInfo , null , 2 ) } ` ) ;
393- }
364+ logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] prevBlockInfo: ${ JSON . stringify ( prevBlockInfo , null , 2 ) } ` ) ;
394365 if ( number !== 1 && ( ! prevBlockInfo || ! prevBlockInfo . block ) ) {
395- if ( DEBUG ) {
396- logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] No notarized block at number ${ number - 1 } with hash ${ last_hash } ` ) ;
397- }
366+ logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] No notarized block at number ${ number - 1 } with hash ${ last_hash } ` ) ;
398367 return ;
399368 }
400369 const validators = prevBlock . validators ;
@@ -431,9 +400,7 @@ class Consensus {
431400 // TODO(lia): do more checks on the prevBlockProposal
432401 this . blockPool . addSeenBlock ( prevBlockInfo . block , prevBlockProposal ) ;
433402 } else {
434- if ( DEBUG ) {
435- logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] Prev block is missing its proposal` ) ;
436- }
403+ logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] Prev block is missing its proposal` ) ;
437404 return false ;
438405 }
439406 }
@@ -607,13 +574,9 @@ class Consensus {
607574 tryFinalize ( ) {
608575 const LOG_SUFFIX = 'tryFinalize' ;
609576 let finalizableChain = this . blockPool . getFinalizableChain ( ) ;
610- if ( DEBUG ) {
611- logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] finalizableChain: ${ JSON . stringify ( finalizableChain , null , 2 ) } ` ) ;
612- }
577+ logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] finalizableChain: ${ JSON . stringify ( finalizableChain , null , 2 ) } ` ) ;
613578 if ( ! finalizableChain || ! finalizableChain . length ) {
614- if ( DEBUG ) {
615- logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] No notarized chain with 3 consecutive epochs yet` ) ;
616- }
579+ logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] No notarized chain with 3 consecutive epochs yet` ) ;
617580 return ;
618581 }
619582 // Discard the last block (but save it for a future finalization)
@@ -639,9 +602,7 @@ class Consensus {
639602 if ( ! blockList || ! blockList . length ) return ;
640603 let lastVerifiedBlock ;
641604 blockList . forEach ( blockInfo => {
642- if ( DEBUG ) {
643- logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] Adding notarized chain's block: ${ JSON . stringify ( blockInfo , null , 2 ) } ` ) ;
644- }
605+ logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] Adding notarized chain's block: ${ JSON . stringify ( blockInfo , null , 2 ) } ` ) ;
645606 let lastNotarizedBlock = this . getLastNotarizedBlock ( ) ;
646607 logger . info ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] Current lastNotarizedBlock: ${ lastNotarizedBlock . number } / ${ lastNotarizedBlock . epoch } ` ) ;
647608 if ( ! blockInfo . block || ! blockInfo . proposal || blockInfo . block . number < lastNotarizedBlock . number ) {
@@ -676,9 +637,7 @@ class Consensus {
676637 getLastNotarizedBlock ( ) {
677638 const LOG_SUFFIX = 'getLastNotarizedBlock' ;
678639 let candidate = this . node . bc . lastBlock ( ) ;
679- if ( DEBUG ) {
680- logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] longestNotarizedChainTips: ${ JSON . stringify ( this . blockPool . longestNotarizedChainTips , null , 2 ) } ` ) ;
681- }
640+ logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] longestNotarizedChainTips: ${ JSON . stringify ( this . blockPool . longestNotarizedChainTips , null , 2 ) } ` ) ;
682641 this . blockPool . longestNotarizedChainTips . forEach ( chainTip => {
683642 const block = _ . get ( this . blockPool . hashToBlockInfo [ chainTip ] , 'block' ) ;
684643 if ( ! block ) return ;
@@ -724,9 +683,7 @@ class Consensus {
724683 while ( chain . length ) {
725684 // apply last_votes and transactions
726685 const block = chain . shift ( ) ;
727- if ( DEBUG ) {
728- logger . debug ( `[[${ LOG_PREFIX } :${ LOG_SUFFIX } ] applying block ${ JSON . stringify ( block ) } ` ) ;
729- }
686+ logger . debug ( `[[${ LOG_PREFIX } :${ LOG_SUFFIX } ] applying block ${ JSON . stringify ( block ) } ` ) ;
730687 snapshot . executeTransactionList ( block . last_votes ) ;
731688 snapshot . executeTransactionList ( block . transactions ) ;
732689 snapshot . blockNumberSnapshot = block . number ;
@@ -742,9 +699,7 @@ class Consensus {
742699 logger . error ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] No validators voted` ) ;
743700 throw Error ( 'No validators voted' ) ;
744701 }
745- if ( DEBUG ) {
746- logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] current epoch: ${ this . state . epoch } \nblock hash: ${ blockHash } \nvotes: ${ JSON . stringify ( blockInfo . votes , null , 2 ) } ` ) ;
747- }
702+ logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] current epoch: ${ this . state . epoch } \nblock hash: ${ blockHash } \nvotes: ${ JSON . stringify ( blockInfo . votes , null , 2 ) } ` ) ;
748703 const validators = { } ;
749704 blockInfo . votes . forEach ( vote => {
750705 validators [ vote . address ] = _ . get ( vote , 'operation.value.stake' ) ;
@@ -958,9 +913,7 @@ class Consensus {
958913
959914 static selectProposer ( seed , validators ) {
960915 const LOG_SUFFIX = 'selectProposer' ;
961- if ( DEBUG ) {
962- logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] seed: ${ seed } , validators: ${ JSON . stringify ( validators ) } ` ) ;
963- }
916+ logger . debug ( `[${ LOG_PREFIX } :${ LOG_SUFFIX } ] seed: ${ seed } , validators: ${ JSON . stringify ( validators ) } ` ) ;
964917 const alphabeticallyOrderedValidators = Object . keys ( validators ) . sort ( ) ;
965918 const totalAtStake = Object . values ( validators ) . reduce ( ( a , b ) => { return a + b ; } , 0 ) ;
966919 const randomNumGenerator = seedrandom ( seed ) ;
0 commit comments