diff --git a/core/blockchain_reader.go b/core/blockchain_reader.go index 8c22d66d11..d440590b8b 100644 --- a/core/blockchain_reader.go +++ b/core/blockchain_reader.go @@ -511,3 +511,12 @@ func (bc *BlockChain) SubscribeBlockProcessingEvent(ch chan<- bool) event.Subscr func (bc *BlockChain) SubscribeFinalizedHeaderEvent(ch chan<- FinalizedHeaderEvent) event.Subscription { return bc.scope.Track(bc.finalizedHeaderFeed.Subscribe(ch)) } + +// AncientTail retrieves the tail the ancients blocks +func (bc *BlockChain) AncientTail() (uint64, error) { + tail, err := bc.db.Tail() + if err != nil { + return 0, err + } + return tail, nil +} diff --git a/core/rawdb/freezer.go b/core/rawdb/freezer.go index ba1542294d..e1dc46bde3 100644 --- a/core/rawdb/freezer.go +++ b/core/rawdb/freezer.go @@ -239,7 +239,7 @@ func (f *Freezer) Ancient(kind string, number uint64) ([]byte, error) { // - if maxBytes is not specified, 'count' items will be returned if they are present. func (f *Freezer) AncientRange(kind string, start, count, maxBytes uint64) ([][]byte, error) { if table := f.tables[kind]; table != nil { - return table.RetrieveItems(start, count, maxBytes) + return table.RetrieveItems(start-f.offset, count, maxBytes) } return nil, errUnknownTable } @@ -252,7 +252,7 @@ func (f *Freezer) Ancients() (uint64, error) { func (f *Freezer) TableAncients(kind string) (uint64, error) { f.writeLock.RLock() defer f.writeLock.RUnlock() - return f.tables[kind].items.Load(), nil + return f.tables[kind].items.Load() + f.offset, nil } // ItemAmountInAncient returns the actual length of current ancientDB. diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go index 7e57153c28..664f775052 100644 --- a/eth/downloader/downloader.go +++ b/eth/downloader/downloader.go @@ -209,6 +209,9 @@ type BlockChain interface { // UpdateChasingHead update remote best chain head, used by DA check now. UpdateChasingHead(head *types.Header) + + // AncientTail retrieves the tail the ancients blocks + AncientTail() (uint64, error) } type DownloadOption func(downloader *Downloader) *Downloader @@ -797,6 +800,11 @@ func (d *Downloader) findAncestor(p *peerConnection, localHeight uint64, remoteH // We're above the max reorg threshold, find the earliest fork point floor = int64(localHeight - maxForkAncestry) } + // if we have pruned too much history, reset the floor + if tail, err := d.blockchain.AncientTail(); err == nil && tail > uint64(floor) { + floor = int64(tail) + } + // If we're doing a light sync, ensure the floor doesn't go below the CHT, as // all headers before that point will be missing. if mode == LightSync {