Skip to content

Downloader.fetchHead

Source (upstream pre-purge) Current
File eth/downloader/downloader.go downloader_pow.go
Symbol Downloader.fetchHead Downloader.fetchHead
Ref 45baf2111~1 etc/v1.17.3-full-node

Diffs: LightSync fetch mode not needed (#29711)

3-way merge — purge → getc ← upstream

pre-purge≈ adapted (origin inferred by similarity)fork-only
// fetchHead retrieves the head header and optionally the pivot header from
// a remote peer.
func (d *Downloader) fetchHead(p *peerConnection) (head *types.Header, pivot *types.Header, err error) {
p.log.Debug("Retrieving remote chain head")
mode := d.getMode()
// Request the advertised remote head block and wait for the response
latest, _ := p.peer.Head()
fetch := 1
if mode == ethconfig.SnapSync {
fetch = 2 // head + pivot headers
}
headers, hashes, err := d.fetchHeadersByHash(p, latest, fetch, fsMinFullBlocks-1, true)
if err != nil {
return nil, nil, err
}
// Make sure the peer gave us at least one and at most the requested headers
if len(headers) == 0 || len(headers) > fetch {
return nil, nil, fmt.Errorf("%w: returned headers %d != requested %d", errBadPeer, len(headers), fetch)
}
// The first header needs to be the head, validate against the request. If
// only 1 header was returned, make sure there's no pivot or there was not
// one requested.
head = headers[0]
if len(headers) == 1 {
if mode == ethconfig.SnapSync && head.Number.Uint64() > uint64(fsMinFullBlocks) {
return nil, nil, fmt.Errorf("%w: no pivot included along head header", errBadPeer)
}
p.log.Debug("Remote head identified, no pivot", "number", head.Number, "hash", hashes[0])
return head, nil, nil
}
// At this point we have 2 headers in total and the first is the
// validated head of the chain. Check the pivot number and return,
pivot = headers[1]
if pivot.Number.Uint64() != head.Number.Uint64()-uint64(fsMinFullBlocks) {
return nil, nil, fmt.Errorf("%w: remote pivot %d != requested %d", errInvalidChain, pivot.Number, head.Number.Uint64()-uint64(fsMinFullBlocks))
}
return head, pivot, nil
}
core-geth validation — +8 -11 | | | |---|---| | File | [`downloader.go`](https://github.com/etclabscore/core-geth/blob/v1.12.20/eth/downloader/downloader.go) | | Symbol | `Downloader.fetchHead` | | Ref | `v1.12.20` |
--- a/core-geth/eth/downloader/downloader.go
+++ b/etc/eth/downloader/downloader_pow.go
@@ -1,14 +1,14 @@
-// fetchHead retrieves the head header and prior pivot block (if available) from
+// fetchHead retrieves the head header and optionally the pivot header from
 // a remote peer.
 func (d *Downloader) fetchHead(p *peerConnection) (head *types.Header, pivot *types.Header, err error) {
    p.log.Debug("Retrieving remote chain head")
    mode := d.getMode()

    // Request the advertised remote head block and wait for the response
-   latest, peerTd, _ := p.peer.Head()
-   d.td = peerTd
+   latest, _ := p.peer.Head()
+
    fetch := 1
-   if mode == SnapSync {
+   if mode == ethconfig.SnapSync {
        fetch = 2 // head + pivot headers
    }
    headers, hashes, err := d.fetchHeadersByHash(p, latest, fetch, fsMinFullBlocks-1, true)
@@ -19,15 +19,12 @@
    if len(headers) == 0 || len(headers) > fetch {
        return nil, nil, fmt.Errorf("%w: returned headers %d != requested %d", errBadPeer, len(headers), fetch)
    }
-   // The first header needs to be the head, validate against the checkpoint
-   // and request. If only 1 header was returned, make sure there's no pivot
-   // or there was not one requested.
+   // The first header needs to be the head, validate against the request. If
+   // only 1 header was returned, make sure there's no pivot or there was not
+   // one requested.
    head = headers[0]
-   if (mode == SnapSync || mode == LightSync) && head.Number.Uint64() < d.checkpoint {
-       return nil, nil, fmt.Errorf("%w: remote head %d below checkpoint %d", errUnsyncedPeer, head.Number, d.checkpoint)
-   }
    if len(headers) == 1 {
-       if mode == SnapSync && head.Number.Uint64() > uint64(fsMinFullBlocks) {
+       if mode == ethconfig.SnapSync && head.Number.Uint64() > uint64(fsMinFullBlocks) {
            return nil, nil, fmt.Errorf("%w: no pivot included along head header", errBadPeer)
        }
        p.log.Debug("Remote head identified, no pivot", "number", head.Number, "hash", hashes[0])

← Sync & Downloader