@@ -520,63 +520,80 @@ export default class Blockchain implements BlockchainInterface {
520
520
let consensus = false
521
521
522
522
// AUTH vote analysis
523
- if ( round === 1 || ( round === 2 && nonce . equals ( CLIQUE_NONCE_AUTH ) ) ) {
524
- let beneficiaryVotesAUTH = this . _cliqueLatestVotes . filter ( ( vote ) => {
525
- return (
526
- vote [ 0 ] . gte ( lastEpochBlockNumber ) &&
527
- ! vote [ 1 ] [ 0 ] . equals ( signer ) &&
528
- vote [ 1 ] [ 1 ] . equals ( beneficiary ) &&
529
- vote [ 1 ] [ 2 ] . equals ( CLIQUE_NONCE_AUTH )
530
- )
523
+ let votes = this . _cliqueLatestVotes . filter ( ( vote ) => {
524
+ return (
525
+ vote [ 0 ] . gte ( lastEpochBlockNumber ) &&
526
+ ! vote [ 1 ] [ 0 ] . equals ( signer ) &&
527
+ vote [ 1 ] [ 1 ] . equals ( beneficiary ) &&
528
+ vote [ 1 ] [ 2 ] . equals ( CLIQUE_NONCE_AUTH )
529
+ )
530
+ } )
531
+ const beneficiaryVotesAUTH : Address [ ] = [ ]
532
+ for ( const vote of votes ) {
533
+ const num = beneficiaryVotesAUTH . filter ( ( voteCMP ) => {
534
+ return voteCMP . equals ( vote [ 1 ] [ 0 ] )
531
535
} ) . length
532
- if ( round === 2 && nonce . equals ( CLIQUE_NONCE_AUTH ) ) {
533
- beneficiaryVotesAUTH += 1
534
- }
535
- // Majority consensus
536
- if ( beneficiaryVotesAUTH >= limit ) {
537
- consensus = true
538
- // Authorize new signer
539
- activeSigners . push ( beneficiary )
540
- activeSigners . sort ( ( a , b ) => {
541
- // Sort by buffer size
542
- return a . toBuffer ( ) . compare ( b . toBuffer ( ) )
543
- } )
544
- // Discard votes for added signer
545
- this . _cliqueLatestVotes = this . _cliqueLatestVotes . filter (
546
- ( vote ) => ! vote [ 1 ] [ 1 ] . equals ( beneficiary )
547
- )
548
- debug (
549
- `[Block ${ header . number . toNumber ( ) } ] Clique majority consensus (AUTH ${ beneficiary } )`
550
- )
536
+ if ( num === 0 ) {
537
+ beneficiaryVotesAUTH . push ( vote [ 1 ] [ 0 ] )
551
538
}
552
539
}
540
+ let numBeneficiaryVotesAUTH = beneficiaryVotesAUTH . length
541
+ if ( round === 2 && nonce . equals ( CLIQUE_NONCE_AUTH ) ) {
542
+ numBeneficiaryVotesAUTH += 1
543
+ }
544
+ // Majority consensus
545
+ if ( numBeneficiaryVotesAUTH >= limit ) {
546
+ consensus = true
547
+ // Authorize new signer
548
+ activeSigners . push ( beneficiary )
549
+ activeSigners . sort ( ( a , b ) => {
550
+ // Sort by buffer size
551
+ return a . toBuffer ( ) . compare ( b . toBuffer ( ) )
552
+ } )
553
+ // Discard votes for added signer
554
+ this . _cliqueLatestVotes = this . _cliqueLatestVotes . filter (
555
+ ( vote ) => ! vote [ 1 ] [ 1 ] . equals ( beneficiary )
556
+ )
557
+ debug (
558
+ `[Block ${ header . number . toNumber ( ) } ] Clique majority consensus (AUTH ${ beneficiary } )`
559
+ )
560
+ }
553
561
// DROP vote
554
- if ( round === 1 || ( round === 2 && nonce . equals ( CLIQUE_NONCE_DROP ) ) ) {
555
- let beneficiaryVotesDROP = this . _cliqueLatestVotes . filter ( ( vote ) => {
556
- return (
557
- vote [ 0 ] . gte ( lastEpochBlockNumber ) &&
558
- ! vote [ 1 ] [ 0 ] . equals ( signer ) &&
559
- vote [ 1 ] [ 1 ] . equals ( beneficiary ) &&
560
- vote [ 1 ] [ 2 ] . equals ( CLIQUE_NONCE_DROP )
561
- )
562
+ votes = this . _cliqueLatestVotes . filter ( ( vote ) => {
563
+ return (
564
+ vote [ 0 ] . gte ( lastEpochBlockNumber ) &&
565
+ ! vote [ 1 ] [ 0 ] . equals ( signer ) &&
566
+ vote [ 1 ] [ 1 ] . equals ( beneficiary ) &&
567
+ vote [ 1 ] [ 2 ] . equals ( CLIQUE_NONCE_DROP )
568
+ )
569
+ } )
570
+ const beneficiaryVotesDROP : Address [ ] = [ ]
571
+ for ( const vote of votes ) {
572
+ const num = beneficiaryVotesDROP . filter ( ( voteCMP ) => {
573
+ return voteCMP . equals ( vote [ 1 ] [ 0 ] )
562
574
} ) . length
563
- if ( round === 2 && nonce . equals ( CLIQUE_NONCE_DROP ) ) {
564
- beneficiaryVotesDROP += 1
565
- }
566
- // Majority consensus
567
- if ( beneficiaryVotesDROP >= limit ) {
568
- consensus = true
569
- // Drop signer
570
- activeSigners = activeSigners . filter ( ( signer ) => ! signer . equals ( beneficiary ) )
571
- this . _cliqueLatestVotes = this . _cliqueLatestVotes . filter (
572
- // Discard votes from removed signer and for removed signer
573
- ( vote ) => ! vote [ 1 ] [ 0 ] . equals ( beneficiary ) && ! vote [ 1 ] [ 1 ] . equals ( beneficiary )
574
- )
575
- debug (
576
- `[Block ${ header . number . toNumber ( ) } ] Clique majority consensus (DROP ${ beneficiary } )`
577
- )
575
+ if ( num === 0 ) {
576
+ beneficiaryVotesDROP . push ( vote [ 1 ] [ 0 ] )
578
577
}
579
578
}
579
+ let numBeneficiaryVotesDROP = beneficiaryVotesDROP . length
580
+
581
+ if ( round === 2 && nonce . equals ( CLIQUE_NONCE_DROP ) ) {
582
+ numBeneficiaryVotesDROP += 1
583
+ }
584
+ // Majority consensus
585
+ if ( numBeneficiaryVotesDROP >= limit ) {
586
+ consensus = true
587
+ // Drop signer
588
+ activeSigners = activeSigners . filter ( ( signer ) => ! signer . equals ( beneficiary ) )
589
+ this . _cliqueLatestVotes = this . _cliqueLatestVotes . filter (
590
+ // Discard votes from removed signer and for removed signer
591
+ ( vote ) => ! vote [ 1 ] [ 0 ] . equals ( beneficiary ) && ! vote [ 1 ] [ 1 ] . equals ( beneficiary )
592
+ )
593
+ debug (
594
+ `[Block ${ header . number . toNumber ( ) } ] Clique majority consensus (DROP ${ beneficiary } )`
595
+ )
596
+ }
580
597
if ( round === 1 ) {
581
598
// Always add the latest vote to the history no matter if already voted
582
599
// the same vote or not
0 commit comments