Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eth/downloader/whitelist: skip future chain validation #796

Merged
merged 2 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions eth/downloader/whitelist/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,20 @@ func (w *Service) IsValidChain(currentHeader *types.Header, chain []*types.Heade
}

// 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
offset := 0
if len(futureChain) != 0 {
offset += int(futureChain[0].Number.Uint64()-currentHeader.Number.Uint64()) - 1
}
// offset := 0
// if len(futureChain) != 0 {
// offset += int(futureChain[0].Number.Uint64()-currentHeader.Number.Uint64()) - 1
// }

// Don't accept future chain of unacceptable length (from current block)
if len(futureChain)+offset > int(w.checkpointInterval) {
return false, ErrLongFutureChain
}
// if len(futureChain)+offset > int(w.checkpointInterval) {
// return false, ErrLongFutureChain
// }

// Iterate over the chain and validate against the last checkpoint
// It will handle all cases where the incoming chain has atleast one checkpoint
Expand Down
11 changes: 7 additions & 4 deletions eth/downloader/whitelist/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,21 @@ func TestIsValidChain(t *testing.T) {
// create a future chain to be imported of length <= `checkpointInterval`
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)
require.Equal(t, res, true, "expected chain to be valid")
require.Equal(t, err, nil, "expected error to be nil")

// create a future chain to be imported of length > `checkpointInterval`
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)
require.Equal(t, res, false, "expected chain to be invalid")
require.Equal(t, err, ErrLongFutureChain, "expected error")
require.Equal(t, res, true, "expected chain to be valid")
require.Equal(t, err, nil, "expected error to be nil")
}

func TestSplitChain(t *testing.T) {
Expand Down