mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-27 07:06:42 +00:00
eth/downloader/whitelist: skip future chain validation (#796)
* eth/downloader/whitelist: skip future chain validation * eth/downloader/whitelist: fix tests for future chain import
This commit is contained in:
parent
ef8f0abf78
commit
b39e64c335
2 changed files with 17 additions and 12 deletions
|
|
@ -100,18 +100,20 @@ func (w *Service) IsValidChain(currentHeader *types.Header, chain []*types.Heade
|
||||||
}
|
}
|
||||||
|
|
||||||
// Split the chain into past and future chain
|
// Split the chain into past and future chain
|
||||||
pastChain, futureChain := splitChain(current, chain)
|
pastChain, _ := splitChain(current, chain)
|
||||||
|
|
||||||
|
// Note: Do not act on future chain and allow importing all kinds of future chains.
|
||||||
|
|
||||||
// Add an offset to future chain if it's not in continuity
|
// Add an offset to future chain if it's not in continuity
|
||||||
offset := 0
|
// offset := 0
|
||||||
if len(futureChain) != 0 {
|
// if len(futureChain) != 0 {
|
||||||
offset += int(futureChain[0].Number.Uint64()-currentHeader.Number.Uint64()) - 1
|
// offset += int(futureChain[0].Number.Uint64()-currentHeader.Number.Uint64()) - 1
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Don't accept future chain of unacceptable length (from current block)
|
// Don't accept future chain of unacceptable length (from current block)
|
||||||
if len(futureChain)+offset > int(w.checkpointInterval) {
|
// if len(futureChain)+offset > int(w.checkpointInterval) {
|
||||||
return false, ErrLongFutureChain
|
// return false, ErrLongFutureChain
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Iterate over the chain and validate against the last checkpoint
|
// Iterate over the chain and validate against the last checkpoint
|
||||||
// It will handle all cases where the incoming chain has atleast one checkpoint
|
// It will handle all cases where the incoming chain has atleast one checkpoint
|
||||||
|
|
|
||||||
|
|
@ -166,7 +166,7 @@ func TestIsValidChain(t *testing.T) {
|
||||||
// create a future chain to be imported of length <= `checkpointInterval`
|
// create a future chain to be imported of length <= `checkpointInterval`
|
||||||
chainB := createMockChain(21, 30) // B21->B22...B29->B30
|
chainB := createMockChain(21, 30) // B21->B22...B29->B30
|
||||||
|
|
||||||
// case5: Try importing a future chain of acceptable length
|
// case5: Try importing a future chain (1)
|
||||||
res, err = s.IsValidChain(chainA[len(chainA)-1], chainB)
|
res, err = s.IsValidChain(chainA[len(chainA)-1], chainB)
|
||||||
require.Equal(t, res, true, "expected chain to be valid")
|
require.Equal(t, res, true, "expected chain to be valid")
|
||||||
require.Equal(t, err, nil, "expected error to be nil")
|
require.Equal(t, err, nil, "expected error to be nil")
|
||||||
|
|
@ -174,10 +174,13 @@ func TestIsValidChain(t *testing.T) {
|
||||||
// create a future chain to be imported of length > `checkpointInterval`
|
// create a future chain to be imported of length > `checkpointInterval`
|
||||||
chainB = createMockChain(21, 40) // C21->C22...C39->C40
|
chainB = createMockChain(21, 40) // C21->C22...C39->C40
|
||||||
|
|
||||||
// case5: Try importing a future chain of unacceptable length
|
// Note: Earlier, it used to reject future chains longer than some threshold.
|
||||||
|
// That check is removed for now.
|
||||||
|
|
||||||
|
// case6: Try importing a future chain (2)
|
||||||
res, err = s.IsValidChain(chainA[len(chainA)-1], chainB)
|
res, err = s.IsValidChain(chainA[len(chainA)-1], chainB)
|
||||||
require.Equal(t, res, false, "expected chain to be invalid")
|
require.Equal(t, res, true, "expected chain to be valid")
|
||||||
require.Equal(t, err, ErrLongFutureChain, "expected error")
|
require.Equal(t, err, nil, "expected error to be nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSplitChain(t *testing.T) {
|
func TestSplitChain(t *testing.T) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue