diff options
author | Jimmy Hu <jimmy.hu@dexon.org> | 2019-04-08 10:57:18 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-08 10:57:18 +0800 |
commit | 1ea06aaeda901ed8c5bfa0dd8341f232038b4bd9 (patch) | |
tree | 071a5d3b6af93b853c604008c982c97636a6f78f | |
parent | 99e3dfee0eb2ffbb4fb0afb8e65664a7c9d91bcd (diff) | |
download | dexon-consensus-1ea06aaeda901ed8c5bfa0dd8341f232038b4bd9.tar dexon-consensus-1ea06aaeda901ed8c5bfa0dd8341f232038b4bd9.tar.gz dexon-consensus-1ea06aaeda901ed8c5bfa0dd8341f232038b4bd9.tar.bz2 dexon-consensus-1ea06aaeda901ed8c5bfa0dd8341f232038b4bd9.tar.lz dexon-consensus-1ea06aaeda901ed8c5bfa0dd8341f232038b4bd9.tar.xz dexon-consensus-1ea06aaeda901ed8c5bfa0dd8341f232038b4bd9.tar.zst dexon-consensus-1ea06aaeda901ed8c5bfa0dd8341f232038b4bd9.zip |
core: filter vote if already processed (#552)
* core: more aggresive vote filter
* add test
-rw-r--r-- | core/agreement-mgr.go | 1 | ||||
-rw-r--r-- | core/utils/vote-filter.go | 13 | ||||
-rw-r--r-- | core/utils/vote-filter_test.go | 8 |
3 files changed, 21 insertions, 1 deletions
diff --git a/core/agreement-mgr.go b/core/agreement-mgr.go index 4e6f230..af0adf2 100644 --- a/core/agreement-mgr.go +++ b/core/agreement-mgr.go @@ -257,6 +257,7 @@ func (mgr *agreementMgr) processVote(v *types.Vote) (err error) { } if err = mgr.baModule.processVote(v); err == nil { mgr.baModule.updateFilter(mgr.voteFilter) + mgr.voteFilter.AddVote(v) } return } diff --git a/core/utils/vote-filter.go b/core/utils/vote-filter.go index a199027..2fc18bb 100644 --- a/core/utils/vote-filter.go +++ b/core/utils/vote-filter.go @@ -24,6 +24,7 @@ import ( // VoteFilter filters votes that are useless for now. // To maximize performance, this structure is not thread-safe and will never be. type VoteFilter struct { + Voted map[types.VoteHeader]struct{} Height uint64 LockIter uint64 Period uint64 @@ -32,7 +33,9 @@ type VoteFilter struct { // NewVoteFilter creates a new vote filter instance. func NewVoteFilter() *VoteFilter { - return &VoteFilter{} + return &VoteFilter{ + Voted: make(map[types.VoteHeader]struct{}), + } } // Filter checks if the vote should be filtered out. @@ -57,5 +60,13 @@ func (vf *VoteFilter) Filter(vote *types.Vote) bool { vote.BlockHash == types.SkipBlockHash { return true } + if _, exist := vf.Voted[vote.VoteHeader]; exist { + return true + } return false } + +// AddVote to the filter so the same vote will be filtered. +func (vf *VoteFilter) AddVote(vote *types.Vote) { + vf.Voted[vote.VoteHeader] = struct{}{} +} diff --git a/core/utils/vote-filter_test.go b/core/utils/vote-filter_test.go index 88050e1..443efff 100644 --- a/core/utils/vote-filter_test.go +++ b/core/utils/vote-filter_test.go @@ -87,6 +87,14 @@ func (s *VoteFilterTestSuite) TestFilterLowerHeight() { s.True(filter.Filter(vote)) } +func (s *VoteFilterTestSuite) TestFilterSameVote() { + filter := NewVoteFilter() + vote := types.NewVote(types.VoteCom, common.NewRandomHash(), uint64(5)) + s.False(filter.Filter(vote)) + filter.AddVote(vote) + s.True(filter.Filter(vote)) +} + func TestVoteFilter(t *testing.T) { suite.Run(t, new(VoteFilterTestSuite)) } |