diff --git a/core/vote/vote_manager.go b/core/vote/vote_manager.go index dcab5c38dd..62eb0d6bb3 100644 --- a/core/vote/vote_manager.go +++ b/core/vote/vote_manager.go @@ -32,6 +32,10 @@ type VoteManager struct { chainHeadCh chan core.ChainHeadEvent chainHeadSub event.Subscription + // used for backup validators to sync votes from corresponding mining validator + syncVoteCh chan core.NewVoteEvent + syncVoteSub event.Subscription + pool *VotePool signer *VoteSigner journal *VoteJournal @@ -46,9 +50,9 @@ func NewVoteManager(eth Backend, chainconfig *params.ChainConfig, chain *core.Bl chain: chain, chainconfig: chainconfig, chainHeadCh: make(chan core.ChainHeadEvent, chainHeadChanSize), - - pool: pool, - engine: engine, + syncVoteCh: make(chan core.NewVoteEvent, voteBufferForPut), + pool: pool, + engine: engine, } // Create voteSigner. @@ -69,6 +73,7 @@ func NewVoteManager(eth Backend, chainconfig *params.ChainConfig, chain *core.Bl // Subscribe to chain head event. voteManager.chainHeadSub = voteManager.chain.SubscribeChainHeadEvent(voteManager.chainHeadCh) + voteManager.syncVoteSub = voteManager.pool.SubscribeNewVoteEvent(voteManager.syncVoteCh) go voteManager.loop() @@ -164,6 +169,21 @@ func (voteManager *VoteManager) loop() { voteManager.pool.PutVote(voteMessage) votesManagerCounter.Inc(1) } + case event := <-voteManager.syncVoteCh: + voteMessage := event.Vote + 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.syncVoteSub.Err(): + log.Debug("voteManager subscribed votes failed") + return case <-voteManager.chainHeadSub.Err(): log.Debug("voteManager subscribed chainHead failed") return diff --git a/core/vote/vote_signer.go b/core/vote/vote_signer.go index e6c8010b0b..03d89d53c7 100644 --- a/core/vote/vote_signer.go +++ b/core/vote/vote_signer.go @@ -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 +}