From f98d513f88081daff94465735a216698dccc06bc Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 26 Sep 2022 22:44:20 +0800 Subject: [PATCH] fix skip snapshot block (#178) --- eth/bft/bft_handler.go | 8 ++++---- eth/bft/bft_handler_test.go | 20 +++++++++++--------- eth/handler.go | 2 +- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/eth/bft/bft_handler.go b/eth/bft/bft_handler.go index 27b0b92989..4291649b9b 100644 --- a/eth/bft/bft_handler.go +++ b/eth/bft/bft_handler.go @@ -20,7 +20,7 @@ type broadcastSyncInfoFn func(*types.SyncInfo) type chainHeightFn func() uint64 type Bfter struct { - gapNumber uint64 + epoch uint64 blockChainReader consensus.ChainReader broadcastCh chan interface{} @@ -59,8 +59,8 @@ func New(broadcasts BroadcastFns, blockChainReader *core.BlockChain, chainHeight } // Create this function to avoid massive test change -func (b *Bfter) InitGapNumber() { - b.gapNumber = b.blockChainReader.Config().XDPoS.Gap +func (b *Bfter) InitEpochNumber() { + b.epoch = b.blockChainReader.Config().XDPoS.Epoch } func (b *Bfter) SetConsensusFuns(engine consensus.Engine) { @@ -113,7 +113,7 @@ func (b *Bfter) Timeout(peer string, timeout *types.Timeout) error { log.Debug("Receive Timeout", "timeout", timeout) gapNum := timeout.GapNumber - if dist := int64(gapNum) - int64(b.chainHeight()); dist < -int64(b.gapNumber)*2 || dist > int64(b.gapNumber)*2 { // times 2 is to avoid miscalculation on cross epoch case + if dist := int64(gapNum) - int64(b.chainHeight()); dist < -int64(b.epoch)*2 || dist > int64(b.epoch)*2 { // times 2 is to avoid cross epoch case, ex: timeout block between 901 to 1799, gapnumber is 450 log.Debug("Discarded propagated timeout, too far away", "peer", peer, "gapNumber", gapNum, "hash", timeout.Hash, "distance", dist) return nil } diff --git a/eth/bft/bft_handler_test.go b/eth/bft/bft_handler_test.go index d68bfb5333..a0f2b4dd65 100644 --- a/eth/bft/bft_handler_test.go +++ b/eth/bft/bft_handler_test.go @@ -24,9 +24,9 @@ func makeVotes(n int) []types.Vote { var votes []types.Vote for i := 0; i < n; i++ { votes = append(votes, types.Vote{ - ProposedBlockInfo: &types.BlockInfo{Number: big.NewInt(1)}, + ProposedBlockInfo: &types.BlockInfo{Number: big.NewInt(1350)}, Signature: []byte{byte(i)}, - GapNumber: 0, + GapNumber: 450, }) } return votes @@ -44,12 +44,12 @@ func newTester() *bfterTester { blockChain := &core.BlockChain{} blockChain.SetConfig(params.TestXDPoSMockChainConfig) chainHeight := func() uint64 { - return 1 + return 1351 } tester := &bfterTester{} tester.bfter = New(broadcasts, blockChain, chainHeight) - tester.bfter.InitGapNumber() + tester.bfter.InitEpochNumber() tester.bfter.SetConsensusFuns(testConsensus) tester.bfter.broadcastCh = make(chan interface{}) tester.bfter.Start() @@ -140,7 +140,7 @@ func TestBoardcastButNotProcessDisqualifiedVotes(t *testing.T) { atomic.AddUint32(&broadcastCounter, 1) } - vote := types.Vote{ProposedBlockInfo: &types.BlockInfo{Number: big.NewInt(1)}} + vote := types.Vote{ProposedBlockInfo: &types.BlockInfo{Number: big.NewInt(1350)}} tester.bfter.Vote(peerID, &vote) time.Sleep(50 * time.Millisecond) @@ -194,7 +194,7 @@ func TestBoardcastButNotProcessDisqualifiedSyncInfo(t *testing.T) { atomic.AddUint32(&broadcastCounter, 1) } - syncInfo := types.SyncInfo{HighestQuorumCert: &types.QuorumCert{ProposedBlockInfo: &types.BlockInfo{Number: big.NewInt(1)}}} + syncInfo := types.SyncInfo{HighestQuorumCert: &types.QuorumCert{ProposedBlockInfo: &types.BlockInfo{Number: big.NewInt(1350)}}} tester.bfter.SyncInfo(peerID, &syncInfo) time.Sleep(50 * time.Millisecond) @@ -281,7 +281,7 @@ func TestSyncInfoHandler(t *testing.T) { atomic.AddUint32(&broadcastCounter, 1) } - syncInfo := types.SyncInfo{HighestQuorumCert: &types.QuorumCert{ProposedBlockInfo: &types.BlockInfo{Number: big.NewInt(1)}}} + syncInfo := types.SyncInfo{HighestQuorumCert: &types.QuorumCert{ProposedBlockInfo: &types.BlockInfo{Number: big.NewInt(1350)}}} tester.bfter.SyncInfo(peerID, &syncInfo) time.Sleep(50 * time.Millisecond) @@ -312,7 +312,7 @@ func TestTooFarVotes(t *testing.T) { atomic.AddUint32(&broadcastCounter, 1) } - tester.bfter.chainHeight = func() uint64 { return 100 } + tester.bfter.chainHeight = func() uint64 { return 10000 } votes := makeVotes(numberVotes) for _, vote := range votes { @@ -349,7 +349,9 @@ func TestTooFarTimeout(t *testing.T) { atomic.AddUint32(&broadcastCounter, 1) } - timeoutMsg := &types.Timeout{GapNumber: 10000} + tester.bfter.chainHeight = func() uint64 { return 2400 } + + timeoutMsg := &types.Timeout{GapNumber: 450} err := tester.bfter.Timeout(peerID, timeoutMsg) if err != nil { diff --git a/eth/handler.go b/eth/handler.go index 8b89a984d8..f9f75ef259 100644 --- a/eth/handler.go +++ b/eth/handler.go @@ -256,7 +256,7 @@ func NewProtocolManager(config *params.ChainConfig, mode downloader.SyncMode, ne } manager.bft = bft.New(broadcasts, blockchain, heighter) if blockchain.Config().XDPoS != nil { - manager.bft.InitGapNumber() + manager.bft.InitEpochNumber() manager.bft.SetConsensusFuns(engine) }