From ca336f6029bf3f3c504b40a1718e943e2751f7f0 Mon Sep 17 00:00:00 2001 From: Jerome Date: Sun, 22 May 2022 14:39:04 +1000 Subject: [PATCH] update the remaining consensus v2 related types to core (#94) --- eth/bft/bft_handler.go | 31 ++++++++--------- eth/bft/bft_handler_test.go | 67 +++++++++++++++++++------------------ eth/handler.go | 13 ++++--- eth/peer.go | 7 ++-- 4 files changed, 59 insertions(+), 59 deletions(-) diff --git a/eth/bft/bft_handler.go b/eth/bft/bft_handler.go index 9cdeb1452f16..61c73ab03c14 100644 --- a/eth/bft/bft_handler.go +++ b/eth/bft/bft_handler.go @@ -5,13 +5,14 @@ import ( "github.com/XinFinOrg/XDPoSChain/consensus/XDPoS" "github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/utils" "github.com/XinFinOrg/XDPoSChain/core" + "github.com/XinFinOrg/XDPoSChain/core/types" "github.com/XinFinOrg/XDPoSChain/log" ) //Define Boradcast Group functions -type broadcastVoteFn func(*utils.Vote) -type broadcastTimeoutFn func(*utils.Timeout) -type broadcastSyncInfoFn func(*utils.SyncInfo) +type broadcastVoteFn func(*types.Vote) +type broadcastTimeoutFn func(*types.Timeout) +type broadcastSyncInfoFn func(*types.SyncInfo) type Bfter struct { blockChainReader consensus.ChainReader @@ -22,14 +23,14 @@ type Bfter struct { } type ConsensusFns struct { - verifyVote func(consensus.ChainReader, *utils.Vote) (bool, error) - voteHandler func(consensus.ChainReader, *utils.Vote) error + verifyVote func(consensus.ChainReader, *types.Vote) (bool, error) + voteHandler func(consensus.ChainReader, *types.Vote) error - verifyTimeout func(consensus.ChainReader, *utils.Timeout) (bool, error) - timeoutHandler func(consensus.ChainReader, *utils.Timeout) error + verifyTimeout func(consensus.ChainReader, *types.Timeout) (bool, error) + timeoutHandler func(consensus.ChainReader, *types.Timeout) error - verifySyncInfo func(consensus.ChainReader, *utils.SyncInfo) (bool, error) - syncInfoHandler func(consensus.ChainReader, *utils.SyncInfo) error + verifySyncInfo func(consensus.ChainReader, *types.SyncInfo) (bool, error) + syncInfoHandler func(consensus.ChainReader, *types.SyncInfo) error } type BroadcastFns struct { @@ -62,7 +63,7 @@ func (b *Bfter) SetConsensusFuns(engine consensus.Engine) { } } -func (b *Bfter) Vote(vote *utils.Vote) error { +func (b *Bfter) Vote(vote *types.Vote) error { log.Trace("Receive Vote", "hash", vote.Hash().Hex(), "voted block hash", vote.ProposedBlockInfo.Hash.Hex(), "number", vote.ProposedBlockInfo.Number, "round", vote.ProposedBlockInfo.Round) verified, err := b.consensus.verifyVote(b.blockChainReader, vote) @@ -88,7 +89,7 @@ func (b *Bfter) Vote(vote *utils.Vote) error { return nil } -func (b *Bfter) Timeout(timeout *utils.Timeout) error { +func (b *Bfter) Timeout(timeout *types.Timeout) error { log.Debug("Receive Timeout", "timeout", timeout) verified, err := b.consensus.verifyTimeout(b.blockChainReader, timeout) @@ -112,7 +113,7 @@ func (b *Bfter) Timeout(timeout *utils.Timeout) error { return nil } -func (b *Bfter) SyncInfo(syncInfo *utils.SyncInfo) error { +func (b *Bfter) SyncInfo(syncInfo *types.SyncInfo) error { log.Debug("Receive SyncInfo", "syncInfo", syncInfo) verified, err := b.consensus.verifySyncInfo(b.blockChainReader, syncInfo) @@ -147,11 +148,11 @@ func (b *Bfter) loop() { return case obj := <-b.broadcastCh: switch v := obj.(type) { - case *utils.Vote: + case *types.Vote: go b.broadcast.Vote(v) - case *utils.Timeout: + case *types.Timeout: go b.broadcast.Timeout(v) - case *utils.SyncInfo: + case *types.SyncInfo: go b.broadcast.SyncInfo(v) default: log.Error("Unknown message type received", "value", v) diff --git a/eth/bft/bft_handler_test.go b/eth/bft/bft_handler_test.go index 2f785af448d9..c74d0a2bb3f7 100644 --- a/eth/bft/bft_handler_test.go +++ b/eth/bft/bft_handler_test.go @@ -11,15 +11,16 @@ import ( "github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/engines/engine_v2" "github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/utils" "github.com/XinFinOrg/XDPoSChain/core" + "github.com/XinFinOrg/XDPoSChain/core/types" "github.com/stretchr/testify/assert" ) // make different votes based on Signatures -func makeVotes(n int) []utils.Vote { - var votes []utils.Vote +func makeVotes(n int) []types.Vote { + var votes []types.Vote for i := 0; i < n; i++ { - votes = append(votes, utils.Vote{ - ProposedBlockInfo: &utils.BlockInfo{}, + votes = append(votes, types.Vote{ + ProposedBlockInfo: &types.BlockInfo{}, Signature: []byte{byte(i)}, GapNumber: 0, }) @@ -55,17 +56,17 @@ func TestSequentialVotes(t *testing.T) { broadcastCounter := uint32(0) targetVotes := 10 - tester.bfter.consensus.verifyVote = func(chain consensus.ChainReader, vote *utils.Vote) (bool, error) { + tester.bfter.consensus.verifyVote = func(chain consensus.ChainReader, vote *types.Vote) (bool, error) { atomic.AddUint32(&verifyCounter, 1) return true, nil } - tester.bfter.consensus.voteHandler = func(chain consensus.ChainReader, vote *utils.Vote) error { + tester.bfter.consensus.voteHandler = func(chain consensus.ChainReader, vote *types.Vote) error { atomic.AddUint32(&handlerCounter, 1) return nil } - tester.bfter.broadcast.Vote = func(*utils.Vote) { + tester.bfter.broadcast.Vote = func(*types.Vote) { atomic.AddUint32(&broadcastCounter, 1) } @@ -91,19 +92,19 @@ func TestNotBoardcastInvalidVote(t *testing.T) { broadcastCounter := uint32(0) targetVotes := 0 - tester.bfter.consensus.verifyVote = func(chain consensus.ChainReader, vote *utils.Vote) (bool, error) { + tester.bfter.consensus.verifyVote = func(chain consensus.ChainReader, vote *types.Vote) (bool, error) { return false, fmt.Errorf("This is invalid vote") } - tester.bfter.consensus.voteHandler = func(chain consensus.ChainReader, vote *utils.Vote) error { + tester.bfter.consensus.voteHandler = func(chain consensus.ChainReader, vote *types.Vote) error { atomic.AddUint32(&handlerCounter, 1) return nil } - tester.bfter.broadcast.Vote = func(*utils.Vote) { + tester.bfter.broadcast.Vote = func(*types.Vote) { atomic.AddUint32(&broadcastCounter, 1) } - vote := utils.Vote{ProposedBlockInfo: &utils.BlockInfo{}} + vote := types.Vote{ProposedBlockInfo: &types.BlockInfo{}} tester.bfter.Vote(&vote) time.Sleep(50 * time.Millisecond) @@ -118,19 +119,19 @@ func TestBoardcastButNotProcessDisqualifiedVotes(t *testing.T) { broadcastCounter := uint32(0) targetVotes := 0 - tester.bfter.consensus.verifyVote = func(chain consensus.ChainReader, vote *utils.Vote) (bool, error) { + tester.bfter.consensus.verifyVote = func(chain consensus.ChainReader, vote *types.Vote) (bool, error) { return false, nil // return false but with nil in error means the message is valid but disqualified } - tester.bfter.consensus.voteHandler = func(chain consensus.ChainReader, vote *utils.Vote) error { + tester.bfter.consensus.voteHandler = func(chain consensus.ChainReader, vote *types.Vote) error { atomic.AddUint32(&handlerCounter, 1) return nil } - tester.bfter.broadcast.Vote = func(*utils.Vote) { + tester.bfter.broadcast.Vote = func(*types.Vote) { atomic.AddUint32(&broadcastCounter, 1) } - vote := utils.Vote{ProposedBlockInfo: &utils.BlockInfo{}} + vote := types.Vote{ProposedBlockInfo: &types.BlockInfo{}} tester.bfter.Vote(&vote) time.Sleep(50 * time.Millisecond) @@ -145,19 +146,19 @@ func TestBoardcastButNotProcessDisqualifiedTimeout(t *testing.T) { broadcastCounter := uint32(0) targetTimeout := 0 - tester.bfter.consensus.verifyTimeout = func(chain consensus.ChainReader, timeout *utils.Timeout) (bool, error) { + tester.bfter.consensus.verifyTimeout = func(chain consensus.ChainReader, timeout *types.Timeout) (bool, error) { return false, nil // return false but with nil in error means the message is valid but disqualified } - tester.bfter.consensus.timeoutHandler = func(chain consensus.ChainReader, timeout *utils.Timeout) error { + tester.bfter.consensus.timeoutHandler = func(chain consensus.ChainReader, timeout *types.Timeout) error { atomic.AddUint32(&handlerCounter, 1) return nil } - tester.bfter.broadcast.Timeout = func(*utils.Timeout) { + tester.bfter.broadcast.Timeout = func(*types.Timeout) { atomic.AddUint32(&broadcastCounter, 1) } - timeout := utils.Timeout{} + timeout := types.Timeout{} tester.bfter.Timeout(&timeout) time.Sleep(50 * time.Millisecond) @@ -172,19 +173,19 @@ func TestBoardcastButNotProcessDisqualifiedSyncInfo(t *testing.T) { broadcastCounter := uint32(0) targetSyncInfo := 0 - tester.bfter.consensus.verifySyncInfo = func(chain consensus.ChainReader, syncInfo *utils.SyncInfo) (bool, error) { + tester.bfter.consensus.verifySyncInfo = func(chain consensus.ChainReader, syncInfo *types.SyncInfo) (bool, error) { return false, nil // return false but with nil in error means the message is valid but disqualified } - tester.bfter.consensus.syncInfoHandler = func(chain consensus.ChainReader, syncInfo *utils.SyncInfo) error { + tester.bfter.consensus.syncInfoHandler = func(chain consensus.ChainReader, syncInfo *types.SyncInfo) error { atomic.AddUint32(&handlerCounter, 1) return nil } - tester.bfter.broadcast.SyncInfo = func(*utils.SyncInfo) { + tester.bfter.broadcast.SyncInfo = func(*types.SyncInfo) { atomic.AddUint32(&broadcastCounter, 1) } - syncInfo := utils.SyncInfo{} + syncInfo := types.SyncInfo{} tester.bfter.SyncInfo(&syncInfo) time.Sleep(50 * time.Millisecond) @@ -203,21 +204,21 @@ func TestTimeoutHandler(t *testing.T) { broadcastCounter := uint32(0) targetVotes := 1 - tester.bfter.consensus.verifyTimeout = func(consensus.ChainReader, *utils.Timeout) (bool, error) { + tester.bfter.consensus.verifyTimeout = func(consensus.ChainReader, *types.Timeout) (bool, error) { atomic.AddUint32(&verifyCounter, 1) return true, nil } - tester.bfter.consensus.timeoutHandler = func(chain consensus.ChainReader, timeout *utils.Timeout) error { + tester.bfter.consensus.timeoutHandler = func(chain consensus.ChainReader, timeout *types.Timeout) error { atomic.AddUint32(&handlerCounter, 1) return nil } - tester.bfter.broadcast.Timeout = func(*utils.Timeout) { + tester.bfter.broadcast.Timeout = func(*types.Timeout) { atomic.AddUint32(&broadcastCounter, 1) } - timeoutMsg := &utils.Timeout{} + timeoutMsg := &types.Timeout{} err := tester.bfter.Timeout(timeoutMsg) if err != nil { @@ -234,21 +235,21 @@ func TestTimeoutHandler(t *testing.T) { func TestTimeoutHandlerRoundNotEqual(t *testing.T) { tester := newTester() - tester.bfter.consensus.verifyTimeout = func(consensus.ChainReader, *utils.Timeout) (bool, error) { + tester.bfter.consensus.verifyTimeout = func(consensus.ChainReader, *types.Timeout) (bool, error) { return true, nil } - tester.bfter.consensus.timeoutHandler = func(chain consensus.ChainReader, timeout *utils.Timeout) error { + tester.bfter.consensus.timeoutHandler = func(chain consensus.ChainReader, timeout *types.Timeout) error { return &utils.ErrIncomingMessageRoundNotEqualCurrentRound{ Type: "timeout", - IncomingRound: utils.Round(1), - CurrentRound: utils.Round(2), + IncomingRound: types.Round(1), + CurrentRound: types.Round(2), } } - tester.bfter.broadcast.Timeout = func(*utils.Timeout) {} + tester.bfter.broadcast.Timeout = func(*types.Timeout) {} - timeoutMsg := &utils.Timeout{} + timeoutMsg := &types.Timeout{} err := tester.bfter.Timeout(timeoutMsg) assert.Equal(t, "timeout message round number: 1 does not match currentRound: 2", err.Error()) diff --git a/eth/handler.go b/eth/handler.go index 6b21d5f9e183..e920555e68dc 100644 --- a/eth/handler.go +++ b/eth/handler.go @@ -30,7 +30,6 @@ import ( "github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/consensus" "github.com/XinFinOrg/XDPoSChain/consensus/XDPoS" - "github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/utils" "github.com/XinFinOrg/XDPoSChain/consensus/misc" "github.com/XinFinOrg/XDPoSChain/core" "github.com/XinFinOrg/XDPoSChain/core/types" @@ -848,7 +847,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error { pm.lendingpool.AddRemotes(txs) } case msg.Code == VoteMsg: - var vote utils.Vote + var vote types.Vote if err := msg.Decode(&vote); err != nil { return errResp(ErrDecode, "msg %v: %v", msg, err) } @@ -865,7 +864,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error { } case msg.Code == TimeoutMsg: - var timeout utils.Timeout + var timeout types.Timeout if err := msg.Decode(&timeout); err != nil { return errResp(ErrDecode, "msg %v: %v", msg, err) } @@ -884,7 +883,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error { } case msg.Code == SyncInfoMsg: - var syncInfo utils.SyncInfo + var syncInfo types.SyncInfo if err := msg.Decode(&syncInfo); err != nil { return errResp(ErrDecode, "msg %v: %v", msg, err) } @@ -952,7 +951,7 @@ func (pm *ProtocolManager) BroadcastTx(hash common.Hash, tx *types.Transaction) // BroadcastVote will propagate a Vote to all peers which are not known to // already have the given vote. -func (pm *ProtocolManager) BroadcastVote(vote *utils.Vote) { +func (pm *ProtocolManager) BroadcastVote(vote *types.Vote) { hash := vote.Hash() peers := pm.peers.PeersWithoutVote(hash) if len(peers) > 0 { @@ -970,7 +969,7 @@ func (pm *ProtocolManager) BroadcastVote(vote *utils.Vote) { // BroadcastTimeout will propagate a Timeout to all peers which are not known to // already have the given timeout. -func (pm *ProtocolManager) BroadcastTimeout(timeout *utils.Timeout) { +func (pm *ProtocolManager) BroadcastTimeout(timeout *types.Timeout) { hash := timeout.Hash() peers := pm.peers.PeersWithoutTimeout(hash) if len(peers) > 0 { @@ -988,7 +987,7 @@ func (pm *ProtocolManager) BroadcastTimeout(timeout *utils.Timeout) { // BroadcastSyncInfo will propagate a SyncInfo to all peers which are not known to // already have the given SyncInfo. -func (pm *ProtocolManager) BroadcastSyncInfo(syncInfo *utils.SyncInfo) { +func (pm *ProtocolManager) BroadcastSyncInfo(syncInfo *types.SyncInfo) { hash := syncInfo.Hash() peers := pm.peers.PeersWithoutSyncInfo(hash) if len(peers) > 0 { diff --git a/eth/peer.go b/eth/peer.go index 190af49effef..62345b79366b 100644 --- a/eth/peer.go +++ b/eth/peer.go @@ -24,7 +24,6 @@ import ( "time" "github.com/XinFinOrg/XDPoSChain/common" - "github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/utils" "github.com/XinFinOrg/XDPoSChain/core/types" "github.com/XinFinOrg/XDPoSChain/p2p" "github.com/XinFinOrg/XDPoSChain/rlp" @@ -299,7 +298,7 @@ func (p *peer) SendReceiptsRLP(receipts []rlp.RawValue) error { } } -func (p *peer) SendVote(vote *utils.Vote) error { +func (p *peer) SendVote(vote *types.Vote) error { p.knownVote.Add(vote.Hash()) if p.pairRw != nil { return p2p.Send(p.pairRw, VoteMsg, vote) @@ -313,7 +312,7 @@ func (p *peer) AsyncSendVote() { } */ -func (p *peer) SendTimeout(timeout *utils.Timeout) error { +func (p *peer) SendTimeout(timeout *types.Timeout) error { p.knownTimeout.Add(timeout.Hash()) if p.pairRw != nil { return p2p.Send(p.pairRw, TimeoutMsg, timeout) @@ -327,7 +326,7 @@ func (p *peer) AsyncSendTimeout() { } */ -func (p *peer) SendSyncInfo(syncInfo *utils.SyncInfo) error { +func (p *peer) SendSyncInfo(syncInfo *types.SyncInfo) error { p.knownSyncInfo.Add(syncInfo.Hash()) if p.pairRw != nil { return p2p.Send(p.pairRw, SyncInfoMsg, syncInfo)