Skip to content

Commit b5f0ca9

Browse files
committed
blockchain -> clique voting: added distinct signer comparison to vote count
1 parent cee640d commit b5f0ca9

File tree

1 file changed

+67
-50
lines changed

1 file changed

+67
-50
lines changed

packages/blockchain/src/index.ts

Lines changed: 67 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -520,63 +520,80 @@ export default class Blockchain implements BlockchainInterface {
520520
let consensus = false
521521

522522
// 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])
531535
}).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])
551538
}
552539
}
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+
}
553561
// 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])
562574
}).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])
578577
}
579578
}
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+
}
580597
if (round === 1) {
581598
// Always add the latest vote to the history no matter if already voted
582599
// the same vote or not

0 commit comments

Comments
 (0)