Skip to content

Commit cd88f69

Browse files
authored
Merge pull request #14596 from markya0616/valid_clique_vote
consensus/clique: choose valid votes
2 parents 46d0d04 + 514659a commit cd88f69

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

consensus/clique/clique.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -503,13 +503,24 @@ func (c *Clique) Prepare(chain consensus.ChainReader, header *types.Header) erro
503503
header.Nonce = types.BlockNonce{}
504504

505505
number := header.Number.Uint64()
506+
507+
// Assemble the voting snapshot to check which votes make sense
508+
snap, err := c.snapshot(chain, number-1, header.ParentHash, nil)
509+
if err != nil {
510+
return err
511+
}
506512
if number%c.config.Epoch != 0 {
507513
c.lock.RLock()
508-
if len(c.proposals) > 0 {
509-
addresses := make([]common.Address, 0, len(c.proposals))
510-
for address := range c.proposals {
514+
515+
// Gather all the proposals that make sense voting on
516+
addresses := make([]common.Address, 0, len(c.proposals))
517+
for address, authorize := range c.proposals {
518+
if snap.validVote(address, authorize) {
511519
addresses = append(addresses, address)
512520
}
521+
}
522+
// If there's pending proposals, cast a vote on them
523+
if len(addresses) > 0 {
513524
header.Coinbase = addresses[rand.Intn(len(addresses))]
514525
if c.proposals[header.Coinbase] {
515526
copy(header.Nonce[:], nonceAuthVote)
@@ -519,11 +530,7 @@ func (c *Clique) Prepare(chain consensus.ChainReader, header *types.Header) erro
519530
}
520531
c.lock.RUnlock()
521532
}
522-
// Assemble the voting snapshot and set the correct difficulty
523-
snap, err := c.snapshot(chain, number-1, header.ParentHash, nil)
524-
if err != nil {
525-
return err
526-
}
533+
// Set the correct difficulty
527534
header.Difficulty = diffNoTurn
528535
if snap.inturn(header.Number.Uint64(), c.signer) {
529536
header.Difficulty = diffInTurn

consensus/clique/snapshot.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,17 @@ func (s *Snapshot) copy() *Snapshot {
126126
return cpy
127127
}
128128

129+
// validVote returns whether it makes sense to cast the specified vote in the
130+
// given snapshot context (e.g. don't try to add an already authorized signer).
131+
func (s *Snapshot) validVote(address common.Address, authorize bool) bool {
132+
_, signer := s.Signers[address]
133+
return (signer && !authorize) || (!signer && authorize)
134+
}
135+
129136
// cast adds a new vote into the tally.
130137
func (s *Snapshot) cast(address common.Address, authorize bool) bool {
131138
// Ensure the vote is meaningful
132-
_, signer := s.Signers[address]
133-
if (signer && authorize) || (!signer && !authorize) {
139+
if !s.validVote(address, authorize) {
134140
return false
135141
}
136142
// Cast the vote into an existing or new tally

0 commit comments

Comments
 (0)