eth/downloader: dynamically move pivot even during chain sync

parent faba018b
This diff is collapsed.
...@@ -427,11 +427,7 @@ func (dlp *downloadTesterPeer) Head() (common.Hash, *big.Int) { ...@@ -427,11 +427,7 @@ func (dlp *downloadTesterPeer) Head() (common.Hash, *big.Int) {
// origin; associated with a particular peer in the download tester. The returned // origin; associated with a particular peer in the download tester. The returned
// function can be used to retrieve batches of headers from the particular peer. // function can be used to retrieve batches of headers from the particular peer.
func (dlp *downloadTesterPeer) RequestHeadersByHash(origin common.Hash, amount int, skip int, reverse bool) error { func (dlp *downloadTesterPeer) RequestHeadersByHash(origin common.Hash, amount int, skip int, reverse bool) error {
if reverse { result := dlp.chain.headersByHash(origin, amount, skip, reverse)
panic("reverse header requests not supported")
}
result := dlp.chain.headersByHash(origin, amount, skip)
go dlp.dl.downloader.DeliverHeaders(dlp.id, result) go dlp.dl.downloader.DeliverHeaders(dlp.id, result)
return nil return nil
} }
...@@ -440,11 +436,7 @@ func (dlp *downloadTesterPeer) RequestHeadersByHash(origin common.Hash, amount i ...@@ -440,11 +436,7 @@ func (dlp *downloadTesterPeer) RequestHeadersByHash(origin common.Hash, amount i
// origin; associated with a particular peer in the download tester. The returned // origin; associated with a particular peer in the download tester. The returned
// function can be used to retrieve batches of headers from the particular peer. // function can be used to retrieve batches of headers from the particular peer.
func (dlp *downloadTesterPeer) RequestHeadersByNumber(origin uint64, amount int, skip int, reverse bool) error { func (dlp *downloadTesterPeer) RequestHeadersByNumber(origin uint64, amount int, skip int, reverse bool) error {
if reverse { result := dlp.chain.headersByNumber(origin, amount, skip, reverse)
panic("reverse header requests not supported")
}
result := dlp.chain.headersByNumber(origin, amount, skip)
go dlp.dl.downloader.DeliverHeaders(dlp.id, result) go dlp.dl.downloader.DeliverHeaders(dlp.id, result)
return nil return nil
} }
...@@ -1698,7 +1690,7 @@ func testCheckpointEnforcement(t *testing.T, protocol int, mode SyncMode) { ...@@ -1698,7 +1690,7 @@ func testCheckpointEnforcement(t *testing.T, protocol int, mode SyncMode) {
if mode == FastSync || mode == LightSync { if mode == FastSync || mode == LightSync {
expect = errUnsyncedPeer expect = errUnsyncedPeer
} }
if err := tester.sync("peer", nil, mode); err != expect { if err := tester.sync("peer", nil, mode); !errors.Is(err, expect) {
t.Fatalf("block sync error mismatch: have %v, want %v", err, expect) t.Fatalf("block sync error mismatch: have %v, want %v", err, expect)
} }
if mode == FastSync || mode == LightSync { if mode == FastSync || mode == LightSync {
......
...@@ -170,18 +170,27 @@ func (tc *testChain) td(hash common.Hash) *big.Int { ...@@ -170,18 +170,27 @@ func (tc *testChain) td(hash common.Hash) *big.Int {
return tc.tdm[hash] return tc.tdm[hash]
} }
// headersByHash returns headers in ascending order from the given hash. // headersByHash returns headers in order from the given hash.
func (tc *testChain) headersByHash(origin common.Hash, amount int, skip int) []*types.Header { func (tc *testChain) headersByHash(origin common.Hash, amount int, skip int, reverse bool) []*types.Header {
num, _ := tc.hashToNumber(origin) num, _ := tc.hashToNumber(origin)
return tc.headersByNumber(num, amount, skip) return tc.headersByNumber(num, amount, skip, reverse)
} }
// headersByNumber returns headers in ascending order from the given number. // headersByNumber returns headers from the given number.
func (tc *testChain) headersByNumber(origin uint64, amount int, skip int) []*types.Header { func (tc *testChain) headersByNumber(origin uint64, amount int, skip int, reverse bool) []*types.Header {
result := make([]*types.Header, 0, amount) result := make([]*types.Header, 0, amount)
for num := origin; num < uint64(len(tc.chain)) && len(result) < amount; num += uint64(skip) + 1 {
if header, ok := tc.headerm[tc.chain[int(num)]]; ok { if !reverse {
result = append(result, header) for num := origin; num < uint64(len(tc.chain)) && len(result) < amount; num += uint64(skip) + 1 {
if header, ok := tc.headerm[tc.chain[int(num)]]; ok {
result = append(result, header)
}
}
} else {
for num := int64(origin); num >= 0 && len(result) < amount; num -= int64(skip) + 1 {
if header, ok := tc.headerm[tc.chain[int(num)]]; ok {
result = append(result, header)
}
} }
} }
return result return result
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment