From 66bcd6aa343f29e9b4addcfcb9934af15a24a432 Mon Sep 17 00:00:00 2001 From: Jimmy Hu Date: Sat, 3 Nov 2018 16:02:33 +0800 Subject: core: Fix fork vote (#290) --- core/agreement-state.go | 2 +- core/agreement.go | 24 +++++++++++++----------- core/consensus.go | 8 ++++---- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/core/agreement-state.go b/core/agreement-state.go index 77569d5..fe329f8 100644 --- a/core/agreement-state.go +++ b/core/agreement-state.go @@ -118,9 +118,9 @@ func newCommitState(a *agreementData) *commitState { func (s *commitState) state() agreementStateType { return stateCommit } func (s *commitState) clocks() int { return 2 } func (s *commitState) nextState() (agreementState, error) { - hash, ok := s.a.countVote(s.a.period, types.VotePreCom) s.a.lock.Lock() defer s.a.lock.Unlock() + hash, ok := s.a.countVoteNoLock(s.a.period, types.VotePreCom) if ok && hash != skipBlockHash { s.a.lockValue = hash s.a.lockRound = s.a.period diff --git a/core/agreement.go b/core/agreement.go index d6875bc..b922cae 100644 --- a/core/agreement.go +++ b/core/agreement.go @@ -21,6 +21,7 @@ import ( "fmt" "math" "sync" + "sync/atomic" "time" "github.com/dexon-foundation/dexon-consensus/common" @@ -103,7 +104,7 @@ type agreementData struct { type agreement struct { state agreementState data *agreementData - aID types.Position + aID *atomic.Value notarySet map[types.NodeID]struct{} hasOutput bool lock sync.RWMutex @@ -126,6 +127,7 @@ func newAgreement( ID: ID, leader: leader, }, + aID: &atomic.Value{}, candidateBlock: make(map[common.Hash]*types.Block), fastForward: make(chan uint64, 1), authModule: authModule, @@ -158,7 +160,7 @@ func (a *agreement) restart( a.state = newInitialState(a.data) a.notarySet = notarySet a.candidateBlock = make(map[common.Hash]*types.Block) - a.aID = *aID.Clone() + a.aID.Store(aID) }() if isStop(aID) { @@ -231,9 +233,7 @@ func (a *agreement) pullVotes() bool { // agreementID returns the current agreementID. func (a *agreement) agreementID() types.Position { - a.lock.RLock() - defer a.lock.RUnlock() - return a.aID + return a.aID.Load().(types.Position) } // nextState is called at the specific clock time. @@ -288,10 +288,11 @@ func (a *agreement) processVote(vote *types.Vote) error { if err := a.sanityCheck(vote); err != nil { return err } - if vote.Position != a.aID { + aID := a.agreementID() + if vote.Position != aID { // Agreement module has stopped. - if !isStop(a.aID) { - if a.aID.Newer(&vote.Position) { + if !isStop(aID) { + if aID.Newer(&vote.Position) { return nil } } @@ -403,10 +404,11 @@ func (a *agreement) processBlock(block *types.Block) error { a.data.blocksLock.Lock() defer a.data.blocksLock.Unlock() - if block.Position != a.aID { + aID := a.agreementID() + if block.Position != aID { // Agreement module has stopped. - if !isStop(a.aID) { - if a.aID.Newer(&block.Position) { + if !isStop(aID) { + if aID.Newer(&block.Position) { return nil } } diff --git a/core/consensus.go b/core/consensus.go index 29d4aa2..c781de1 100644 --- a/core/consensus.go +++ b/core/consensus.go @@ -71,11 +71,11 @@ type consensusBAReceiver struct { } func (recv *consensusBAReceiver) ProposeVote(vote *types.Vote) { + if err := recv.agreementModule.prepareVote(vote); err != nil { + recv.consensus.logger.Error("Failed to prepare vote", "error", err) + return + } go func() { - if err := recv.agreementModule.prepareVote(vote); err != nil { - recv.consensus.logger.Error("Failed to prepare vote", "error", err) - return - } if err := recv.agreementModule.processVote(vote); err != nil { recv.consensus.logger.Error("Failed to process vote", "error", err) return -- cgit v1.2.3