Skip to content

Commit

Permalink
vote: backup validator sync votes from corresponding mining validator
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanBSC committed Jul 24, 2023
1 parent 83cc950 commit adb2548
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
11 changes: 11 additions & 0 deletions core/vote/vote_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ func (voteManager *VoteManager) loop() {
voteManager.pool.PutVote(voteMessage)
votesManagerCounter.Inc(1)
}
case voteMessage := <-voteManager.pool.BasicVerifiedVotesCh():
if voteManager.eth.IsMining() || !voteManager.signer.UsingKey(&voteMessage.VoteAddress) {
continue
}
if err := voteManager.journal.WriteVote(voteMessage); err != nil {
log.Error("Failed to write vote into journal", "err", err)
voteJournalErrorCounter.Inc(1)
continue
}
log.Debug("vote manager synced vote", "votedBlockNumber", voteMessage.Data.TargetNumber, "votedBlockHash", voteMessage.Data.TargetHash, "voteMessageHash", voteMessage.Hash())
votesManagerCounter.Inc(1)
case <-voteManager.chainHeadSub.Err():
log.Debug("voteManager subscribed chainHead failed")
return
Expand Down
29 changes: 19 additions & 10 deletions core/vote/vote_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,27 @@ type VotePool struct {

votesCh chan *types.VoteEnvelope

// used for backup validators to sync votes from corresponding mining validator
basicVerifiedVotesCh chan *types.VoteEnvelope

engine consensus.PoSA
}

type votesPriorityQueue []*types.VoteData

func NewVotePool(chainconfig *params.ChainConfig, chain *core.BlockChain, engine consensus.PoSA) *VotePool {
votePool := &VotePool{
chain: chain,
chainconfig: chainconfig,
receivedVotes: mapset.NewSet(),
curVotes: make(map[common.Hash]*VoteBox),
futureVotes: make(map[common.Hash]*VoteBox),
curVotesPq: &votesPriorityQueue{},
futureVotesPq: &votesPriorityQueue{},
chainHeadCh: make(chan core.ChainHeadEvent, chainHeadChanSize),
votesCh: make(chan *types.VoteEnvelope, voteBufferForPut),
engine: engine,
chain: chain,
chainconfig: chainconfig,
receivedVotes: mapset.NewSet(),
curVotes: make(map[common.Hash]*VoteBox),
futureVotes: make(map[common.Hash]*VoteBox),
curVotesPq: &votesPriorityQueue{},
futureVotesPq: &votesPriorityQueue{},
chainHeadCh: make(chan core.ChainHeadEvent, chainHeadChanSize),
votesCh: make(chan *types.VoteEnvelope, voteBufferForPut),
basicVerifiedVotesCh: make(chan *types.VoteEnvelope, voteBufferForPut),
engine: engine,
}

// Subscribe events from blockchain and start the main event loop.
Expand Down Expand Up @@ -151,6 +155,8 @@ func (pool *VotePool) putIntoVotePool(vote *types.VoteEnvelope) bool {
return false
}

pool.basicVerifiedVotesCh <- vote

if !isFutureVote {
// Verify if the vote comes from valid validators based on voteAddress (BLSPublicKey), only verify curVotes here, will verify futureVotes in transfer process.
if pool.engine.VerifyVote(pool.chain, vote) != nil {
Expand All @@ -166,6 +172,9 @@ func (pool *VotePool) putIntoVotePool(vote *types.VoteEnvelope) bool {

return true
}
func (pool *VotePool) BasicVerifiedVotesCh() <-chan *types.VoteEnvelope {
return pool.basicVerifiedVotesCh
}

func (pool *VotePool) SubscribeNewVoteEvent(ch chan<- core.NewVoteEvent) event.Subscription {
return pool.scope.Track(pool.votesFeed.Subscribe(ch))
Expand Down
4 changes: 4 additions & 0 deletions core/vote/vote_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,7 @@ func (signer *VoteSigner) SignVote(vote *types.VoteEnvelope) error {
copy(vote.Signature[:], signature.Marshal()[:])
return nil
}

func (signer *VoteSigner) UsingKey(bLSPublicKey *types.BLSPublicKey) bool {
return types.BLSPublicKey(signer.pubKey) == *bLSPublicKey
}

0 comments on commit adb2548

Please sign in to comment.