diff options
author | Mission Liao <mission.liao@dexon.org> | 2019-01-28 17:56:11 +0800 |
---|---|---|
committer | Mission Liao <mission.liao@dexon.org> | 2019-01-28 17:56:11 +0800 |
commit | b875e23660f61f674b89193ac61812699eeaf321 (patch) | |
tree | e4b529a460ab08266e472ea4973a71c1f48926cc | |
parent | 329c0e8d522ce1e93eb82d46b6d0af361c58cb2e (diff) | |
download | dexon-consensus-b875e23660f61f674b89193ac61812699eeaf321.tar.gz dexon-consensus-b875e23660f61f674b89193ac61812699eeaf321.tar.zst dexon-consensus-b875e23660f61f674b89193ac61812699eeaf321.zip |
Fixup: check forked votes in agreement.votesInfo.add
-rw-r--r-- | core/agreement/vote-cache.go | 35 |
1 files changed, 18 insertions, 17 deletions
diff --git a/core/agreement/vote-cache.go b/core/agreement/vote-cache.go index c840916..d7e2a80 100644 --- a/core/agreement/vote-cache.go +++ b/core/agreement/vote-cache.go @@ -56,8 +56,12 @@ type votesInfo struct { purged bool } -func (info *votesInfo) add(v *types.Vote) { - if _, exist := info.votes[v.ProposerID]; exist { +func (info *votesInfo) add(v *types.Vote) (oldV *types.Vote) { + oldV, exist := info.votes[v.ProposerID] + if exist { + if oldV.BlockHash == v.BlockHash { + oldV = nil + } return } info.votes[v.ProposerID] = v @@ -68,6 +72,7 @@ func (info *votesInfo) add(v *types.Vote) { info.best = v.BlockHash } } + return } func (info *votesInfo) getVotes( @@ -419,12 +424,10 @@ func (a *voteChainCache) checkVote(v *types.Vote) (bool, error) { return false, ErrNotInNotarySet } // Check for forked vote. - if vForPos, exist := a.votes[v.Position]; exist { - if vForPeriod, exist := vForPos[v.Period]; exist { - if oldVote, exist := vForPeriod[v.Type].votes[v.ProposerID]; exist { - if v.BlockHash != oldVote.BlockHash { - return false, &ErrForkVote{v.ProposerID, oldVote, v} - } + if info := a.votesInfo(v, false); info != nil { + if oldVote, exist := info.votes[v.ProposerID]; exist { + if v.BlockHash != oldVote.BlockHash { + return false, &ErrForkVote{v.ProposerID, oldVote, v} } } } @@ -474,7 +477,9 @@ func (a *voteChainCache) processVote(v *types.Vote) error { } fallthrough default: - info.add(v) + if oldV := info.add(v); oldV != nil { + panic(fmt.Errorf("unexpected forked detection: %s %s", oldV, v)) + } if s := a.trigger(v, info); s != nil { if a.updateReferenceSignal(s) { a.purgeBy(s) @@ -498,15 +503,11 @@ func (a *voteChainCache) processResult(result *types.AgreementResult) error { "receive agreement result in purged period: %s", result)) } for _, v := range result.Votes { - oldV, exist := info.votes[v.ProposerID] - if exist { - if v.BlockHash != oldV.BlockHash { - a.pendingSignals = append(a.pendingSignals, NewSignal( - SignalFork, []types.Vote{*oldV, v})) - continue - } + if oldV := info.add(&v); oldV != nil { + a.pendingSignals = append(a.pendingSignals, NewSignal( + SignalFork, []types.Vote{*oldV, v})) + continue } - info.add(&v) } if s := a.trigger(&result.Votes[0], info); s != nil { if a.updateReferenceSignal(s) { |