fix skip snapshot block (#178)

This commit is contained in:
Liam 2022-09-26 22:44:20 +08:00 committed by GitHub
parent e9a4601de4
commit f98d513f88
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 14 deletions

View file

@ -20,7 +20,7 @@ type broadcastSyncInfoFn func(*types.SyncInfo)
type chainHeightFn func() uint64 type chainHeightFn func() uint64
type Bfter struct { type Bfter struct {
gapNumber uint64 epoch uint64
blockChainReader consensus.ChainReader blockChainReader consensus.ChainReader
broadcastCh chan interface{} broadcastCh chan interface{}
@ -59,8 +59,8 @@ func New(broadcasts BroadcastFns, blockChainReader *core.BlockChain, chainHeight
} }
// Create this function to avoid massive test change // Create this function to avoid massive test change
func (b *Bfter) InitGapNumber() { func (b *Bfter) InitEpochNumber() {
b.gapNumber = b.blockChainReader.Config().XDPoS.Gap b.epoch = b.blockChainReader.Config().XDPoS.Epoch
} }
func (b *Bfter) SetConsensusFuns(engine consensus.Engine) { 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) log.Debug("Receive Timeout", "timeout", timeout)
gapNum := timeout.GapNumber 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) log.Debug("Discarded propagated timeout, too far away", "peer", peer, "gapNumber", gapNum, "hash", timeout.Hash, "distance", dist)
return nil return nil
} }

View file

@ -24,9 +24,9 @@ func makeVotes(n int) []types.Vote {
var votes []types.Vote var votes []types.Vote
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
votes = append(votes, types.Vote{ votes = append(votes, types.Vote{
ProposedBlockInfo: &types.BlockInfo{Number: big.NewInt(1)}, ProposedBlockInfo: &types.BlockInfo{Number: big.NewInt(1350)},
Signature: []byte{byte(i)}, Signature: []byte{byte(i)},
GapNumber: 0, GapNumber: 450,
}) })
} }
return votes return votes
@ -44,12 +44,12 @@ func newTester() *bfterTester {
blockChain := &core.BlockChain{} blockChain := &core.BlockChain{}
blockChain.SetConfig(params.TestXDPoSMockChainConfig) blockChain.SetConfig(params.TestXDPoSMockChainConfig)
chainHeight := func() uint64 { chainHeight := func() uint64 {
return 1 return 1351
} }
tester := &bfterTester{} tester := &bfterTester{}
tester.bfter = New(broadcasts, blockChain, chainHeight) tester.bfter = New(broadcasts, blockChain, chainHeight)
tester.bfter.InitGapNumber() tester.bfter.InitEpochNumber()
tester.bfter.SetConsensusFuns(testConsensus) tester.bfter.SetConsensusFuns(testConsensus)
tester.bfter.broadcastCh = make(chan interface{}) tester.bfter.broadcastCh = make(chan interface{})
tester.bfter.Start() tester.bfter.Start()
@ -140,7 +140,7 @@ func TestBoardcastButNotProcessDisqualifiedVotes(t *testing.T) {
atomic.AddUint32(&broadcastCounter, 1) 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) tester.bfter.Vote(peerID, &vote)
time.Sleep(50 * time.Millisecond) time.Sleep(50 * time.Millisecond)
@ -194,7 +194,7 @@ func TestBoardcastButNotProcessDisqualifiedSyncInfo(t *testing.T) {
atomic.AddUint32(&broadcastCounter, 1) 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) tester.bfter.SyncInfo(peerID, &syncInfo)
time.Sleep(50 * time.Millisecond) time.Sleep(50 * time.Millisecond)
@ -281,7 +281,7 @@ func TestSyncInfoHandler(t *testing.T) {
atomic.AddUint32(&broadcastCounter, 1) 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) tester.bfter.SyncInfo(peerID, &syncInfo)
time.Sleep(50 * time.Millisecond) time.Sleep(50 * time.Millisecond)
@ -312,7 +312,7 @@ func TestTooFarVotes(t *testing.T) {
atomic.AddUint32(&broadcastCounter, 1) atomic.AddUint32(&broadcastCounter, 1)
} }
tester.bfter.chainHeight = func() uint64 { return 100 } tester.bfter.chainHeight = func() uint64 { return 10000 }
votes := makeVotes(numberVotes) votes := makeVotes(numberVotes)
for _, vote := range votes { for _, vote := range votes {
@ -349,7 +349,9 @@ func TestTooFarTimeout(t *testing.T) {
atomic.AddUint32(&broadcastCounter, 1) 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) err := tester.bfter.Timeout(peerID, timeoutMsg)
if err != nil { if err != nil {

View file

@ -256,7 +256,7 @@ func NewProtocolManager(config *params.ChainConfig, mode downloader.SyncMode, ne
} }
manager.bft = bft.New(broadcasts, blockchain, heighter) manager.bft = bft.New(broadcasts, blockchain, heighter)
if blockchain.Config().XDPoS != nil { if blockchain.Config().XDPoS != nil {
manager.bft.InitGapNumber() manager.bft.InitEpochNumber()
manager.bft.SetConsensusFuns(engine) manager.bft.SetConsensusFuns(engine)
} }