From 99d15b2ae3996db2aadf62651700fb140b6f860a Mon Sep 17 00:00:00 2001 From: Mostafa Date: Tue, 28 May 2024 04:49:28 +0800 Subject: [PATCH] chore: fix linting issues --- consensus/config.go | 2 +- consensus/config_test.go | 4 +- consensus/cp.go | 2 +- fastconsensus/commit.go | 8 +-- fastconsensus/config.go | 4 +- fastconsensus/config_test.go | 6 +- fastconsensus/consensus.go | 10 ++-- fastconsensus/consensus_test.go | 30 ++++------ fastconsensus/cp.go | 4 +- fastconsensus/cp_decide.go | 2 +- fastconsensus/cp_mainvote.go | 6 +- fastconsensus/cp_prevote.go | 6 +- fastconsensus/cp_test.go | 4 +- fastconsensus/height.go | 4 +- fastconsensus/interface.go | 8 +-- fastconsensus/manager.go | 2 +- fastconsensus/manager_test.go | 75 +++++++++++++------------ fastconsensus/mediator.go | 4 +- fastconsensus/mock.go | 4 +- fastconsensus/precommit.go | 4 +- fastconsensus/prepare.go | 2 +- fastconsensus/propose.go | 8 +-- fastconsensus/voteset/binary_voteset.go | 7 ++- fastconsensus/voteset/block_voteset.go | 7 ++- types/vote/cp_just.go | 8 +-- util/testsuite/testsuite.go | 8 +-- 26 files changed, 112 insertions(+), 117 deletions(-) diff --git a/consensus/config.go b/consensus/config.go index 8ba67136c..275cc689c 100644 --- a/consensus/config.go +++ b/consensus/config.go @@ -20,7 +20,7 @@ func DefaultConfig() *Config { func (conf *Config) BasicCheck() error { if conf.ChangeProposerTimeout <= 0 { return ConfigError{ - Reason: "timeout for change proposer must be greater than zero", + Reason: "change proposer timeout must be greater than zero", } } if conf.ChangeProposerDelta <= 0 { diff --git a/consensus/config_test.go b/consensus/config_test.go index f75318d46..d612c3651 100644 --- a/consensus/config_test.go +++ b/consensus/config_test.go @@ -19,10 +19,10 @@ func TestDefaultConfigCheck(t *testing.T) { assert.ErrorIs(t, c2.BasicCheck(), ConfigError{Reason: "change proposer delta must be greater than zero"}) c3.ChangeProposerTimeout = 0 * time.Second - assert.ErrorIs(t, c3.BasicCheck(), ConfigError{Reason: "timeout for change proposer must be greater than zero"}) + assert.ErrorIs(t, c3.BasicCheck(), ConfigError{Reason: "change proposer timeout must be greater than zero"}) c4.ChangeProposerTimeout = -1 * time.Second - assert.ErrorIs(t, c4.BasicCheck(), ConfigError{Reason: "timeout for change proposer must be greater than zero"}) + assert.ErrorIs(t, c4.BasicCheck(), ConfigError{Reason: "change proposer timeout must be greater than zero"}) c5.MinimumAvailabilityScore = 1.5 assert.ErrorIs(t, c5.BasicCheck(), ConfigError{Reason: "minimum availability score can't be negative or more than 1"}) diff --git a/consensus/cp.go b/consensus/cp.go index 436506503..7292b9b86 100644 --- a/consensus/cp.go +++ b/consensus/cp.go @@ -56,7 +56,7 @@ func (cp *changeProposer) checkJustInitZero(just vote.Just, blockHash hash.Hash) return nil } -func (cp *changeProposer) checkJustInitOne(just vote.Just) error { +func (*changeProposer) checkJustInitOne(just vote.Just) error { _, ok := just.(*vote.JustInitYes) if !ok { return invalidJustificationError{ diff --git a/fastconsensus/commit.go b/fastconsensus/commit.go index e10517f3a..65088fac2 100644 --- a/fastconsensus/commit.go +++ b/fastconsensus/commit.go @@ -29,18 +29,18 @@ func (s *commitState) decide() { s.enterNewState(s.newHeightState) } -func (s *commitState) onAddVote(_ *vote.Vote) { +func (*commitState) onAddVote(_ *vote.Vote) { panic("Unreachable") } -func (s *commitState) onSetProposal(_ *proposal.Proposal) { +func (*commitState) onSetProposal(_ *proposal.Proposal) { panic("Unreachable") } -func (s *commitState) onTimeout(_ *ticker) { +func (*commitState) onTimeout(_ *ticker) { panic("Unreachable") } -func (s *commitState) name() string { +func (*commitState) name() string { return "commit" } diff --git a/fastconsensus/config.go b/fastconsensus/config.go index 380888ba1..805a94294 100644 --- a/fastconsensus/config.go +++ b/fastconsensus/config.go @@ -20,12 +20,12 @@ func DefaultConfig() *Config { func (conf *Config) BasicCheck() error { if conf.ChangeProposerTimeout <= 0 { return ConfigError{ - Reason: "timeout for change proposer can't be negative", + Reason: "change proposer timeout must be greater than zero", } } if conf.ChangeProposerDelta <= 0 { return ConfigError{ - Reason: "change proposer delta can't be negative", + Reason: "change proposer delta must be greater than zero", } } if conf.MinimumAvailabilityScore < 0 || conf.MinimumAvailabilityScore > 1 { diff --git a/fastconsensus/config_test.go b/fastconsensus/config_test.go index 008fea4eb..356238ccc 100644 --- a/fastconsensus/config_test.go +++ b/fastconsensus/config_test.go @@ -16,13 +16,13 @@ func TestDefaultConfigCheck(t *testing.T) { assert.NoError(t, c1.BasicCheck()) c2.ChangeProposerDelta = 0 * time.Second - assert.ErrorIs(t, c2.BasicCheck(), ConfigError{Reason: "change proposer delta can't be negative"}) + assert.ErrorIs(t, c2.BasicCheck(), ConfigError{Reason: "change proposer delta must be greater than zero"}) c3.ChangeProposerTimeout = 0 * time.Second - assert.ErrorIs(t, c3.BasicCheck(), ConfigError{Reason: "timeout for change proposer can't be negative"}) + assert.ErrorIs(t, c3.BasicCheck(), ConfigError{Reason: "change proposer timeout must be greater than zero"}) c4.ChangeProposerTimeout = -1 * time.Second - assert.ErrorIs(t, c4.BasicCheck(), ConfigError{Reason: "timeout for change proposer can't be negative"}) + assert.ErrorIs(t, c4.BasicCheck(), ConfigError{Reason: "change proposer timeout must be greater than zero"}) c5.MinimumAvailabilityScore = 1.5 assert.ErrorIs(t, c5.BasicCheck(), ConfigError{Reason: "minimum availability score can't be negative or more than 1"}) diff --git a/fastconsensus/consensus.go b/fastconsensus/consensus.go index b38853029..46f9cb3b2 100644 --- a/fastconsensus/consensus.go +++ b/fastconsensus/consensus.go @@ -65,11 +65,11 @@ func NewConsensus( broadcastCh <- msg } - return newConsensus(conf, bcState, + return makeConsensus(conf, bcState, valKey, rewardAddr, broadcaster, mediator) } -func newConsensus( +func makeConsensus( conf *Config, bcState state.Facade, valKey *bls.ValidatorKey, @@ -119,7 +119,7 @@ func (cs *consensus) Start() { cs.lk.Lock() defer cs.lk.Unlock() - cs.moveToNewHeight() + cs.doMoveToNewHeight() // We have just started the consensus (possibly restarting the node). // Therefore, let's query the votes and proposals in case we missed any. if cs.active { @@ -187,10 +187,10 @@ func (cs *consensus) MoveToNewHeight() { cs.lk.Lock() defer cs.lk.Unlock() - cs.moveToNewHeight() + cs.doMoveToNewHeight() } -func (cs *consensus) moveToNewHeight() { +func (cs *consensus) doMoveToNewHeight() { stateHeight := cs.bcState.LastBlockHeight() if cs.height != stateHeight+1 { cs.enterNewState(cs.newHeightState) diff --git a/fastconsensus/consensus_test.go b/fastconsensus/consensus_test.go index 9432d7d96..d571dbd51 100644 --- a/fastconsensus/consensus_test.go +++ b/fastconsensus/consensus_test.go @@ -119,7 +119,7 @@ func setupWithSeed(t *testing.T, seed int64) *testData { store.MockingStore(ts), txPool, nil) require.NoError(t, err) - instances[i] = newConsensus(testConfig(), bcState, valKey, + instances[i] = makeConsensus(testConfig(), bcState, valKey, valKey.PublicKey().AccountAddress(), broadcasterFunc, newConcreteMediator()) } @@ -252,7 +252,7 @@ func (td *testData) shouldPublishVote(t *testing.T, cons *consensus, voteType vo return nil } -func checkHeightRound(t *testing.T, cons *consensus, height uint32, round int16) { +func (*testData) checkHeightRound(t *testing.T, cons *consensus, height uint32, round int16) { t.Helper() h, r := cons.HeightRound() @@ -260,12 +260,6 @@ func checkHeightRound(t *testing.T, cons *consensus, height uint32, round int16) assert.Equal(t, r, round) } -func (td *testData) checkHeightRound(t *testing.T, cons *consensus, height uint32, round int16) { - t.Helper() - - checkHeightRound(t, cons, height, round) -} - func (td *testData) addPrepareVote(cons *consensus, blockHash hash.Hash, height uint32, round int16, valID int, ) *vote.Vote { @@ -313,23 +307,19 @@ func (td *testData) addVote(cons *consensus, v *vote.Vote, valID int) *vote.Vote return v } -func newHeightTimeout(cons *consensus) { +func (*testData) newHeightTimeout(cons *consensus) { cons.lk.Lock() cons.currentState.onTimeout(&ticker{0, cons.height, cons.round, tickerTargetNewHeight}) cons.lk.Unlock() } -func (td *testData) newHeightTimeout(cons *consensus) { - newHeightTimeout(cons) -} - -func (td *testData) queryProposalTimeout(cons *consensus) { +func (*testData) queryProposalTimeout(cons *consensus) { cons.lk.Lock() cons.currentState.onTimeout(&ticker{0, cons.height, cons.round, tickerTargetQueryProposal}) cons.lk.Unlock() } -func (td *testData) changeProposerTimeout(cons *consensus) { +func (*testData) changeProposerTimeout(cons *consensus) { cons.lk.Lock() cons.currentState.onTimeout(&ticker{0, cons.height, cons.round, tickerTargetChangeProposer}) cons.lk.Unlock() @@ -346,7 +336,7 @@ func (td *testData) enterNewHeight(cons *consensus) { } // enterNextRound helps tests to enter next round safely. -func (td *testData) enterNextRound(cons *consensus) { +func (*testData) enterNextRound(cons *consensus) { cons.lk.Lock() cons.round++ cons.enterNewState(cons.proposeState) @@ -506,9 +496,9 @@ func TestNotInCommittee(t *testing.T) { str := store.MockingStore(td.TestSuite) st, _ := state.LoadOrNewState(td.genDoc, []*bls.ValidatorKey{valKey}, str, td.txPool, nil) - Cons := NewConsensus(testConfig(), st, valKey, valKey.Address(), make(chan message.Message, 100), + consInst := NewConsensus(testConfig(), st, valKey, valKey.Address(), make(chan message.Message, 100), newConcreteMediator()) - cons := Cons.(*consensus) + cons := consInst.(*consensus) td.enterNewHeight(cons) td.newHeightTimeout(cons) @@ -746,9 +736,9 @@ func TestNonActiveValidator(t *testing.T) { td := setup(t) valKey := td.RandValKey() - Cons := NewConsensus(testConfig(), state.MockingState(td.TestSuite), + consInst := NewConsensus(testConfig(), state.MockingState(td.TestSuite), valKey, valKey.Address(), make(chan message.Message, 100), newConcreteMediator()) - nonActiveCons := Cons.(*consensus) + nonActiveCons := consInst.(*consensus) t.Run("non-active instances should be in new-height state", func(t *testing.T) { nonActiveCons.MoveToNewHeight() diff --git a/fastconsensus/cp.go b/fastconsensus/cp.go index b51af01ed..ee5ef01a1 100644 --- a/fastconsensus/cp.go +++ b/fastconsensus/cp.go @@ -12,7 +12,7 @@ type changeProposer struct { *consensus } -func (cp *changeProposer) onSetProposal(_ *proposal.Proposal) { +func (*changeProposer) onSetProposal(_ *proposal.Proposal) { // Ignore proposal } @@ -23,7 +23,7 @@ func (cp *changeProposer) onTimeout(t *ticker) { } } -func (cp *changeProposer) cpCheckCPValue(value vote.CPValue, allowedValues ...vote.CPValue) error { +func (*changeProposer) cpCheckCPValue(value vote.CPValue, allowedValues ...vote.CPValue) error { for _, v := range allowedValues { if value == v { return nil diff --git a/fastconsensus/cp_decide.go b/fastconsensus/cp_decide.go index d8182bc2f..53abbb51a 100644 --- a/fastconsensus/cp_decide.go +++ b/fastconsensus/cp_decide.go @@ -54,6 +54,6 @@ func (s *cpDecideState) onAddVote(_ *vote.Vote) { s.decide() } -func (s *cpDecideState) name() string { +func (*cpDecideState) name() string { return "cp:decide" } diff --git a/fastconsensus/cp_mainvote.go b/fastconsensus/cp_mainvote.go index 58513753f..602762501 100644 --- a/fastconsensus/cp_mainvote.go +++ b/fastconsensus/cp_mainvote.go @@ -90,14 +90,14 @@ func (s *cpMainVoteState) onAddVote(_ *vote.Vote) { s.decide() } -func (s *cpMainVoteState) onSetProposal(_ *proposal.Proposal) { +func (*cpMainVoteState) onSetProposal(_ *proposal.Proposal) { // Ignore proposal } -func (s *cpMainVoteState) onTimeout(_ *ticker) { +func (*cpMainVoteState) onTimeout(_ *ticker) { // Ignore timeouts } -func (s *cpMainVoteState) name() string { +func (*cpMainVoteState) name() string { return "cp:main-vote" } diff --git a/fastconsensus/cp_prevote.go b/fastconsensus/cp_prevote.go index d7c13ac43..0ed758092 100644 --- a/fastconsensus/cp_prevote.go +++ b/fastconsensus/cp_prevote.go @@ -91,12 +91,12 @@ func (s *cpPreVoteState) onAddVote(_ *vote.Vote) { s.decide() } -func (s *cpPreVoteState) onSetProposal(_ *proposal.Proposal) { +func (*cpPreVoteState) onSetProposal(_ *proposal.Proposal) { } -func (s *cpPreVoteState) onTimeout(_ *ticker) { +func (*cpPreVoteState) onTimeout(_ *ticker) { } -func (s *cpPreVoteState) name() string { +func (*cpPreVoteState) name() string { return "cp:pre-vote" } diff --git a/fastconsensus/cp_test.go b/fastconsensus/cp_test.go index fb3d3cde4..377b6fed3 100644 --- a/fastconsensus/cp_test.go +++ b/fastconsensus/cp_test.go @@ -53,7 +53,7 @@ func TestChangeProposerAgreement1(t *testing.T) { td.addCPMainVote(td.consP, hash.UndefHash, h, r, vote.CPValueYes, mainVote0.CPJust(), tIndexY) td.shouldPublishVote(t, td.consP, vote.VoteTypeCPDecided, hash.UndefHash) - checkHeightRound(t, td.consP, h, r+1) + td.checkHeightRound(t, td.consP, h, r+1) } func TestChangeProposerAgreement0(t *testing.T) { @@ -88,7 +88,7 @@ func TestChangeProposerAgreement0(t *testing.T) { td.shouldPublishVote(t, td.consP, vote.VoteTypeCPDecided, blockHash) td.addPrecommitVote(td.consP, blockHash, h, r, tIndexX) td.addPrecommitVote(td.consP, blockHash, h, r, tIndexY) - checkHeightRound(t, td.consP, h, r) + td.checkHeightRound(t, td.consP, h, r) } // ConsP receives all PRE-VOTE:0 votes before receiving a proposal or prepare votes. diff --git a/fastconsensus/height.go b/fastconsensus/height.go index 0c1915bf7..c5a4012d2 100644 --- a/fastconsensus/height.go +++ b/fastconsensus/height.go @@ -43,7 +43,7 @@ func (s *newHeightState) onAddVote(_ *vote.Vote) { } } -func (s *newHeightState) onSetProposal(_ *proposal.Proposal) { +func (*newHeightState) onSetProposal(_ *proposal.Proposal) { // Ignore proposal } @@ -55,6 +55,6 @@ func (s *newHeightState) onTimeout(t *ticker) { } } -func (s *newHeightState) name() string { +func (*newHeightState) name() string { return "new-height" } diff --git a/fastconsensus/interface.go b/fastconsensus/interface.go index cbcbe3a01..adae51b8e 100644 --- a/fastconsensus/interface.go +++ b/fastconsensus/interface.go @@ -22,8 +22,8 @@ type Consensus interface { Start() MoveToNewHeight() - AddVote(vote *vote.Vote) - SetProposal(proposal *proposal.Proposal) + AddVote(vte *vote.Vote) + SetProposal(prop *proposal.Proposal) } type ManagerReader interface { @@ -40,6 +40,6 @@ type Manager interface { Start() error Stop() MoveToNewHeight() - AddVote(vote *vote.Vote) - SetProposal(proposal *proposal.Proposal) + AddVote(vot *vote.Vote) + SetProposal(prop *proposal.Proposal) } diff --git a/fastconsensus/manager.go b/fastconsensus/manager.go index 4aeced92b..62f177fb0 100644 --- a/fastconsensus/manager.go +++ b/fastconsensus/manager.go @@ -60,7 +60,7 @@ func (mgr *manager) Start() error { } // Stop stops the manager. -func (mgr *manager) Stop() { +func (*manager) Stop() { } // Instances return all consensus instances that are read-only and diff --git a/fastconsensus/manager_test.go b/fastconsensus/manager_test.go index 029106a20..3b6c69e89 100644 --- a/fastconsensus/manager_test.go +++ b/fastconsensus/manager_test.go @@ -25,12 +25,12 @@ func TestManager(t *testing.T) { valKeys := []*bls.ValidatorKey{st.TestValKeys[0], ts.RandValKey()} broadcastCh := make(chan message.Message, 500) - stateHeight := ts.RandHeight() - blk, cert := ts.GenerateTestBlock(stateHeight) + randomHeight := ts.RandHeight() + blk, cert := ts.GenerateTestBlock(randomHeight) st.TestStore.SaveBlock(blk, cert) - Mgr := NewManager(testConfig(), st, valKeys, rewardAddrs, broadcastCh) - mgr := Mgr.(*manager) + mgrInst := NewManager(testConfig(), st, valKeys, rewardAddrs, broadcastCh) + mgr := mgrInst.(*manager) consA := mgr.instances[0].(*consensus) // active consB := mgr.instances[1].(*consensus) // inactive @@ -41,17 +41,20 @@ func TestManager(t *testing.T) { }) t.Run("Check if all instances move to new height", func(t *testing.T) { + stateHeight := mgr.state.LastBlockHeight() assert.False(t, mgr.HasActiveInstance()) + mgr.MoveToNewHeight() - h, r := mgr.HeightRound() + consHeight, consRound := mgr.HeightRound() assert.True(t, mgr.HasActiveInstance()) - assert.Equal(t, stateHeight+1, h) - assert.Zero(t, r) + assert.Equal(t, consHeight, stateHeight+1) + assert.Zero(t, consRound) }) t.Run("Testing add vote", func(t *testing.T) { - v := vote.NewPrepareVote(ts.RandHash(), stateHeight+1, 0, valKeys[0].Address()) + consHeight, _ := mgr.HeightRound() + v := vote.NewPrepareVote(ts.RandHash(), consHeight, 0, valKeys[0].Address()) ts.HelperSignVote(valKeys[0], v) mgr.AddVote(v) @@ -61,8 +64,9 @@ func TestManager(t *testing.T) { }) t.Run("Testing set proposal", func(t *testing.T) { + consHeight, _ := mgr.HeightRound() b, _ := st.ProposeBlock(valKeys[0], valKeys[0].Address()) - p := proposal.NewProposal(stateHeight+1, 0, b) + p := proposal.NewProposal(consHeight, 0, b) ts.HelperSignProposal(valKeys[0], p) mgr.SetProposal(p) @@ -72,7 +76,8 @@ func TestManager(t *testing.T) { }) t.Run("Check discarding old votes", func(t *testing.T) { - v := vote.NewPrepareVote(ts.RandHash(), stateHeight-1, 0, st.TestValKeys[2].Address()) + consHeight, _ := mgr.HeightRound() + v := vote.NewPrepareVote(ts.RandHash(), consHeight-1, 0, st.TestValKeys[2].Address()) ts.HelperSignVote(st.TestValKeys[2], v) mgr.AddVote(v) @@ -80,8 +85,9 @@ func TestManager(t *testing.T) { }) t.Run("Check discarding old proposals", func(t *testing.T) { + consHeight, _ := mgr.HeightRound() b, _ := st.ProposeBlock(valKeys[0], valKeys[0].Address()) - p := proposal.NewProposal(stateHeight-1, 1, b) + p := proposal.NewProposal(consHeight-1, 1, b) ts.HelperSignProposal(valKeys[0], p) mgr.SetProposal(p) @@ -89,9 +95,10 @@ func TestManager(t *testing.T) { }) t.Run("Processing upcoming votes", func(t *testing.T) { - v1 := vote.NewPrepareVote(ts.RandHash(), stateHeight+2, 0, valKeys[0].Address()) - v2 := vote.NewPrepareVote(ts.RandHash(), stateHeight+3, 0, valKeys[0].Address()) - v3 := vote.NewPrepareVote(ts.RandHash(), stateHeight+4, 0, valKeys[0].Address()) + consHeight, _ := mgr.HeightRound() + v1 := vote.NewPrepareVote(ts.RandHash(), consHeight+1, 0, valKeys[0].Address()) + v2 := vote.NewPrepareVote(ts.RandHash(), consHeight+2, 0, valKeys[0].Address()) + v3 := vote.NewPrepareVote(ts.RandHash(), consHeight+3, 0, valKeys[0].Address()) ts.HelperSignVote(valKeys[0], v1) ts.HelperSignVote(valKeys[0], v2) @@ -103,15 +110,13 @@ func TestManager(t *testing.T) { assert.Len(t, mgr.upcomingVotes, 3) - blk, cert := ts.GenerateTestBlock(stateHeight + 1) - err := st.CommitBlock(blk, cert) + blk1, cert1 := ts.GenerateTestBlock(consHeight) + err := st.CommitBlock(blk1, cert1) assert.NoError(t, err) - stateHeight++ - blk, cert = ts.GenerateTestBlock(stateHeight + 1) - err = st.CommitBlock(blk, cert) + blk2, cert2 := ts.GenerateTestBlock(consHeight + 1) + err = st.CommitBlock(blk2, cert2) assert.NoError(t, err) - stateHeight++ mgr.MoveToNewHeight() @@ -119,14 +124,15 @@ func TestManager(t *testing.T) { }) t.Run("Processing upcoming proposal", func(t *testing.T) { + consHeight, _ := mgr.HeightRound() b1, _ := st.ProposeBlock(valKeys[0], valKeys[0].Address()) - p1 := proposal.NewProposal(stateHeight+2, 0, b1) + p1 := proposal.NewProposal(consHeight+1, 0, b1) b2, _ := st.ProposeBlock(valKeys[0], valKeys[0].Address()) - p2 := proposal.NewProposal(stateHeight+3, 0, b2) + p2 := proposal.NewProposal(consHeight+2, 0, b2) b3, _ := st.ProposeBlock(valKeys[0], valKeys[0].Address()) - p3 := proposal.NewProposal(stateHeight+4, 0, b3) + p3 := proposal.NewProposal(consHeight+3, 0, b3) ts.HelperSignProposal(valKeys[0], p1) ts.HelperSignProposal(valKeys[0], p2) @@ -138,15 +144,13 @@ func TestManager(t *testing.T) { assert.Len(t, mgr.upcomingProposals, 3) - blk, cert := ts.GenerateTestBlock(stateHeight + 1) - err := st.CommitBlock(blk, cert) + blk1, cert1 := ts.GenerateTestBlock(consHeight) + err := st.CommitBlock(blk1, cert1) assert.NoError(t, err) - stateHeight++ - blk, cert = ts.GenerateTestBlock(stateHeight + 1) - err = st.CommitBlock(blk, cert) + blk2, cert2 := ts.GenerateTestBlock(consHeight + 1) + err = st.CommitBlock(blk2, cert2) assert.NoError(t, err) - stateHeight++ mgr.MoveToNewHeight() @@ -157,15 +161,14 @@ func TestManager(t *testing.T) { func TestMediator(t *testing.T) { ts := testsuite.NewTestSuite(t) - committeeSize := 6 st := state.MockingState(ts) - cmt, valKeys := ts.GenerateTestCommittee(committeeSize) + cmt, valKeys := ts.GenerateTestCommittee(4) st.TestCommittee = cmt st.TestParams.BlockIntervalInSecond = 1 - rewardAddrs := []crypto.Address{} - for i := 0; i < committeeSize; i++ { - rewardAddrs = append(rewardAddrs, ts.RandAccAddress()) + rewardAddrs := []crypto.Address{ + ts.RandAccAddress(), ts.RandAccAddress(), + ts.RandAccAddress(), ts.RandAccAddress(), } broadcastCh := make(chan message.Message, 500) @@ -173,8 +176,8 @@ func TestMediator(t *testing.T) { blk, cert := ts.GenerateTestBlock(stateHeight) st.TestStore.SaveBlock(blk, cert) - Mgr := NewManager(testConfig(), st, valKeys, rewardAddrs, broadcastCh) - mgr := Mgr.(*manager) + mgrInst := NewManager(testConfig(), st, valKeys, rewardAddrs, broadcastCh) + mgr := mgrInst.(*manager) mgr.MoveToNewHeight() diff --git a/fastconsensus/mediator.go b/fastconsensus/mediator.go index d527babd7..29ba371f4 100644 --- a/fastconsensus/mediator.go +++ b/fastconsensus/mediator.go @@ -8,8 +8,8 @@ import ( // The `mediator“ interface defines a mechanism for setting proposals and votes // between independent consensus instances. type mediator interface { - OnPublishProposal(from Consensus, proposal *proposal.Proposal) - OnPublishVote(from Consensus, vote *vote.Vote) + OnPublishProposal(from Consensus, prop *proposal.Proposal) + OnPublishVote(from Consensus, vte *vote.Vote) OnBlockAnnounce(from Consensus) Register(cons Consensus) } diff --git a/fastconsensus/mock.go b/fastconsensus/mock.go index 876b06ffd..f7dbcc6ca 100644 --- a/fastconsensus/mock.go +++ b/fastconsensus/mock.go @@ -59,7 +59,7 @@ func (m *MockConsensus) MoveToNewHeight() { m.Height++ } -func (m *MockConsensus) Start() {} +func (*MockConsensus) Start() {} func (m *MockConsensus) AddVote(v *vote.Vote) { m.lk.Lock() @@ -109,7 +109,7 @@ func (m *MockConsensus) HeightRound() (uint32, int16) { return m.Height, m.Round } -func (m *MockConsensus) String() string { +func (*MockConsensus) String() string { return "" } diff --git a/fastconsensus/precommit.go b/fastconsensus/precommit.go index 0ca57b20b..726b88207 100644 --- a/fastconsensus/precommit.go +++ b/fastconsensus/precommit.go @@ -67,10 +67,10 @@ func (s *precommitState) onSetProposal(_ *proposal.Proposal) { s.decide() } -func (s *precommitState) onTimeout(_ *ticker) { +func (*precommitState) onTimeout(_ *ticker) { // Ignore timeouts } -func (s *precommitState) name() string { +func (*precommitState) name() string { return "precommit" } diff --git a/fastconsensus/prepare.go b/fastconsensus/prepare.go index 96b0d17db..5c3317ef3 100644 --- a/fastconsensus/prepare.go +++ b/fastconsensus/prepare.go @@ -78,6 +78,6 @@ func (s *prepareState) onSetProposal(_ *proposal.Proposal) { s.decide() } -func (s *prepareState) name() string { +func (*prepareState) name() string { return "prepare" } diff --git a/fastconsensus/propose.go b/fastconsensus/propose.go index 81eb7d011..99dacabb2 100644 --- a/fastconsensus/propose.go +++ b/fastconsensus/propose.go @@ -64,18 +64,18 @@ func (s *proposeState) createProposal(height uint32, round int16) { s.logger.Info("proposal signed and broadcasted", "proposal", prop) } -func (s *proposeState) onAddVote(_ *vote.Vote) { +func (*proposeState) onAddVote(_ *vote.Vote) { panic("Unreachable") } -func (s *proposeState) onSetProposal(_ *proposal.Proposal) { +func (*proposeState) onSetProposal(_ *proposal.Proposal) { panic("Unreachable") } -func (s *proposeState) onTimeout(_ *ticker) { +func (*proposeState) onTimeout(_ *ticker) { panic("Unreachable") } -func (s *proposeState) name() string { +func (*proposeState) name() string { return "propose" } diff --git a/fastconsensus/voteset/binary_voteset.go b/fastconsensus/voteset/binary_voteset.go index 0c528a7c9..8221d9032 100644 --- a/fastconsensus/voteset/binary_voteset.go +++ b/fastconsensus/voteset/binary_voteset.go @@ -99,12 +99,13 @@ func (vs *BinaryVoteSet) AddVote(v *vote.Vote) (bool, error) { roundVotes := vs.mustGetRoundVotes(v.CPRound()) existingVote, ok := roundVotes.allVotes[v.Signer()] if ok { - if existingVote.Hash() != v.Hash() { - err = errors.Error(errors.ErrDuplicateVote) - } else { + if existingVote.Hash() == v.Hash() { // The vote is already added return false, nil } + + // It is a duplicated vote + err = errors.Error(errors.ErrDuplicateVote) } else { roundVotes.allVotes[v.Signer()] = v roundVotes.votedPower += power diff --git a/fastconsensus/voteset/block_voteset.go b/fastconsensus/voteset/block_voteset.go index 09a617548..6a4ab2f1e 100644 --- a/fastconsensus/voteset/block_voteset.go +++ b/fastconsensus/voteset/block_voteset.go @@ -78,12 +78,13 @@ func (vs *BlockVoteSet) AddVote(v *vote.Vote) (bool, error) { existingVote, ok := vs.allVotes[v.Signer()] if ok { - if existingVote.Hash() != v.Hash() { - err = errors.Error(errors.ErrDuplicateVote) - } else { + if existingVote.Hash() == v.Hash() { // The vote is already added return false, nil } + + // It is a duplicated vote + err = errors.Error(errors.ErrDuplicateVote) } else { vs.allVotes[v.Signer()] = v } diff --git a/types/vote/cp_just.go b/types/vote/cp_just.go index 24877af9b..95d81c5e3 100644 --- a/types/vote/cp_just.go +++ b/types/vote/cp_just.go @@ -90,11 +90,11 @@ type JustDecided struct { QCert *certificate.VoteCertificate `cbor:"1,keyasint"` } -func (j *JustInitNo) Type() JustType { +func (*JustInitNo) Type() JustType { return JustTypeInitNo } -func (j *JustInitYes) Type() JustType { +func (*JustInitYes) Type() JustType { return JustTypeInitYes } @@ -118,11 +118,11 @@ func (*JustDecided) Type() JustType { return JustTypeDecided } -func (j *JustInitNo) BasicCheck() error { +func (*JustInitNo) BasicCheck() error { return nil } -func (j *JustInitYes) BasicCheck() error { +func (*JustInitYes) BasicCheck() error { return nil } diff --git a/util/testsuite/testsuite.go b/util/testsuite/testsuite.go index 176bea1e2..c62bce128 100644 --- a/util/testsuite/testsuite.go +++ b/util/testsuite/testsuite.go @@ -307,21 +307,21 @@ func (ts *TestSuite) GenerateTestValidator(number int32) (*validator.Validator, // GenerateTestBlockWithProposer generates a block with the give proposer address for testing purposes. func (ts *TestSuite) GenerateTestBlockWithProposer(height uint32, proposer crypto.Address, ) (*block.Block, *certificate.BlockCertificate) { - return ts.generateTestBlock(height, proposer, util.Now()) + return ts.makeTestBlock(height, proposer, util.Now()) } // GenerateTestBlockWithTime generates a block with the given time for testing purposes. func (ts *TestSuite) GenerateTestBlockWithTime(height uint32, tme time.Time, ) (*block.Block, *certificate.BlockCertificate) { - return ts.generateTestBlock(height, ts.RandValAddress(), tme) + return ts.makeTestBlock(height, ts.RandValAddress(), tme) } // GenerateTestBlock generates a block for testing purposes. func (ts *TestSuite) GenerateTestBlock(height uint32) (*block.Block, *certificate.BlockCertificate) { - return ts.generateTestBlock(height, ts.RandValAddress(), util.Now()) + return ts.makeTestBlock(height, ts.RandValAddress(), util.Now()) } -func (ts *TestSuite) generateTestBlock(height uint32, proposer crypto.Address, tme time.Time, +func (ts *TestSuite) makeTestBlock(height uint32, proposer crypto.Address, tme time.Time, ) (*block.Block, *certificate.BlockCertificate) { txs := block.NewTxs() tx1, _ := ts.GenerateTestTransferTx()