diff options
author | Jimmy Hu <jimmy.hu@dexon.org> | 2019-01-24 11:49:25 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-01-24 11:49:25 +0800 |
commit | b6ca251bcb6e1a19a7276afe68bf37a4372670fa (patch) | |
tree | b1aac8e9759f150d58009020cd92b579e766895c /core/utils | |
parent | 779f63a9f6fc3f4c628f0b97c822546ac51d0eb6 (diff) | |
download | dexon-consensus-b6ca251bcb6e1a19a7276afe68bf37a4372670fa.tar dexon-consensus-b6ca251bcb6e1a19a7276afe68bf37a4372670fa.tar.gz dexon-consensus-b6ca251bcb6e1a19a7276afe68bf37a4372670fa.tar.bz2 dexon-consensus-b6ca251bcb6e1a19a7276afe68bf37a4372670fa.tar.lz dexon-consensus-b6ca251bcb6e1a19a7276afe68bf37a4372670fa.tar.xz dexon-consensus-b6ca251bcb6e1a19a7276afe68bf37a4372670fa.tar.zst dexon-consensus-b6ca251bcb6e1a19a7276afe68bf37a4372670fa.zip |
core: Add vote filter (#430)
* core: ignore usless vote
* core: export SkipBlockHash and NullBlockHash
* core: add VoteFilter
* Add test
* New VoteFilter for each round
Diffstat (limited to 'core/utils')
-rw-r--r-- | core/utils/vote-filter.go | 61 | ||||
-rw-r--r-- | core/utils/vote-filter_test.go | 92 |
2 files changed, 153 insertions, 0 deletions
diff --git a/core/utils/vote-filter.go b/core/utils/vote-filter.go new file mode 100644 index 0000000..a199027 --- /dev/null +++ b/core/utils/vote-filter.go @@ -0,0 +1,61 @@ +// Copyright 2019 The dexon-consensus Authors +// This file is part of the dexon-consensus library. +// +// The dexon-consensus library is free software: you can redistribute it +// and/or modify it under the terms of the GNU Lesser General Public License as +// published by the Free Software Foundation, either version 3 of the License, +// or (at your option) any later version. +// +// The dexon-consensus library is distributed in the hope that it will be +// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser +// General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the dexon-consensus library. If not, see +// <http://www.gnu.org/licenses/>. + +package utils + +import ( + "github.com/dexon-foundation/dexon-consensus/core/types" +) + +// VoteFilter filters votes that are useless for now. +// To maximize performance, this structure is not thread-safe and will never be. +type VoteFilter struct { + Height uint64 + LockIter uint64 + Period uint64 + Confirm bool +} + +// NewVoteFilter creates a new vote filter instance. +func NewVoteFilter() *VoteFilter { + return &VoteFilter{} +} + +// Filter checks if the vote should be filtered out. +func (vf *VoteFilter) Filter(vote *types.Vote) bool { + if vote.Type == types.VoteInit { + return true + } + if vote.Position.Height < vf.Height { + return true + } else if vote.Position.Height > vf.Height { + // It's impossible to check the vote of other height. + return false + } + if vf.Confirm { + return true + } + if vote.Type == types.VotePreCom && vote.Period < vf.LockIter { + return true + } + if vote.Type == types.VoteCom && + vote.Period < vf.Period && + vote.BlockHash == types.SkipBlockHash { + return true + } + return false +} diff --git a/core/utils/vote-filter_test.go b/core/utils/vote-filter_test.go new file mode 100644 index 0000000..88050e1 --- /dev/null +++ b/core/utils/vote-filter_test.go @@ -0,0 +1,92 @@ +// Copyright 2019 The dexon-consensus Authors +// This file is part of the dexon-consensus library. +// +// The dexon-consensus library is free software: you can redistribute it +// and/or modify it under the terms of the GNU Lesser General Public License as +// published by the Free Software Foundation, either version 3 of the License, +// or (at your option) any later version. +// +// The dexon-consensus library is distributed in the hope that it will be +// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser +// General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the dexon-consensus library. If not, see +// <http://www.gnu.org/licenses/>. + +package utils + +import ( + "testing" + + "github.com/stretchr/testify/suite" + + "github.com/dexon-foundation/dexon-consensus/common" + "github.com/dexon-foundation/dexon-consensus/core/types" +) + +type VoteFilterTestSuite struct { + suite.Suite +} + +func (s *VoteFilterTestSuite) TestFilterVotePass() { + filter := NewVoteFilter() + filter.Height = uint64(6) + filter.Period = uint64(3) + filter.LockIter = uint64(3) + // Pass with higher Height. + vote := types.NewVote(types.VotePreCom, common.NewRandomHash(), uint64(1)) + vote.Position.Height = filter.Height + 1 + s.Require().False(filter.Filter(vote)) + // Pass with VotePreCom. + vote = types.NewVote(types.VotePreCom, common.NewRandomHash(), + filter.LockIter) + vote.Position.Height = filter.Height + s.Require().False(filter.Filter(vote)) + // Pass with VoteCom. + vote = types.NewVote(types.VoteCom, common.NewRandomHash(), + filter.Period) + vote.Position.Height = filter.Height + s.Require().False(filter.Filter(vote)) + vote.Period-- + s.Require().False(filter.Filter(vote)) +} + +func (s *VoteFilterTestSuite) TestFilterVoteInit() { + filter := NewVoteFilter() + vote := types.NewVote(types.VoteInit, common.NewRandomHash(), uint64(1)) + s.True(filter.Filter(vote)) +} + +func (s *VoteFilterTestSuite) TestFilterVotePreCom() { + filter := NewVoteFilter() + filter.LockIter = uint64(3) + vote := types.NewVote(types.VotePreCom, common.NewRandomHash(), uint64(1)) + s.True(filter.Filter(vote)) +} + +func (s *VoteFilterTestSuite) TestFilterVoteCom() { + filter := NewVoteFilter() + filter.Period = uint64(3) + vote := types.NewVote(types.VoteCom, types.SkipBlockHash, uint64(1)) + s.True(filter.Filter(vote)) +} + +func (s *VoteFilterTestSuite) TestFilterConfirm() { + filter := NewVoteFilter() + filter.Confirm = true + vote := types.NewVote(types.VoteCom, common.NewRandomHash(), uint64(1)) + s.True(filter.Filter(vote)) +} +func (s *VoteFilterTestSuite) TestFilterLowerHeight() { + filter := NewVoteFilter() + filter.Height = uint64(10) + vote := types.NewVote(types.VoteCom, common.NewRandomHash(), uint64(1)) + vote.Position.Height = filter.Height - 1 + s.True(filter.Filter(vote)) +} + +func TestVoteFilter(t *testing.T) { + suite.Run(t, new(VoteFilterTestSuite)) +} |