From 7824a0c56f590f4a4ec5bc537300c5b76caff4ba Mon Sep 17 00:00:00 2001 From: Marius van der Wijden Date: Wed, 12 Mar 2025 18:36:48 +0100 Subject: [PATCH] core/rawdb: add truncateHeadBlocks to interface --- core/rawdb/database.go | 5 +++++ core/rawdb/freezer.go | 15 +++++++-------- core/rawdb/freezer_memory.go | 24 ++++++++++++++++++++++++ core/rawdb/freezer_resettable.go | 9 +++++++++ core/rawdb/table.go | 6 ++++++ ethdb/database.go | 7 +++++++ ethdb/remotedb/remotedb.go | 4 ++++ 7 files changed, 62 insertions(+), 8 deletions(-) diff --git a/core/rawdb/database.go b/core/rawdb/database.go index bc29347324..4ea1e9d798 100644 --- a/core/rawdb/database.go +++ b/core/rawdb/database.go @@ -126,6 +126,11 @@ func (db *nofreezedb) TruncateTail(items uint64) (uint64, error) { return 0, errNotSupported } +// TruncateTailBlock returns an error as we don't have a backing chain freezer. +func (db *nofreezedb) TruncateTailBlocks(items uint64) (uint64, error) { + return 0, errNotSupported +} + // Sync returns an error as we don't have a backing chain freezer. func (db *nofreezedb) Sync() error { return errNotSupported diff --git a/core/rawdb/freezer.go b/core/rawdb/freezer.go index a021effc56..57c9afc3da 100644 --- a/core/rawdb/freezer.go +++ b/core/rawdb/freezer.go @@ -301,9 +301,9 @@ func (f *Freezer) TruncateHead(items uint64) (uint64, error) { return oitems, nil } -// TruncateTailNoHeads discards any recent data below the provided threshold number. -// Does not truncate the header and hash table -func (f *Freezer) TruncateTailNoHeads(tail uint64) (uint64, error) { +// TruncateTailBlocks discards any recent data below the provided threshold number. +// Only truncates blocks and receipts. +func (f *Freezer) TruncateTailBlocks(tail uint64) (uint64, error) { if f.readonly { return 0, errReadOnly } @@ -315,11 +315,10 @@ func (f *Freezer) TruncateTailNoHeads(tail uint64) (uint64, error) { return old, nil } for kind, table := range f.tables { - if kind == ChainFreezerHeaderTable || kind == ChainFreezerHashTable { - continue - } - if err := table.truncateTail(tail); err != nil { - return 0, err + if kind == ChainFreezerBodiesTable || kind == ChainFreezerReceiptTable { + if err := table.truncateTail(tail); err != nil { + return 0, err + } } } f.tail.Store(tail) diff --git a/core/rawdb/freezer_memory.go b/core/rawdb/freezer_memory.go index 2d3dbb07dd..7841686d4c 100644 --- a/core/rawdb/freezer_memory.go +++ b/core/rawdb/freezer_memory.go @@ -389,6 +389,30 @@ func (f *MemoryFreezer) TruncateTail(tail uint64) (uint64, error) { return old, nil } +// TruncateTailBlocks discards any recent data below the provided threshold number. +// Only truncates blocks and receipts. +func (f *MemoryFreezer) TruncateTailBlocks(tail uint64) (uint64, error) { + f.lock.Lock() + defer f.lock.Unlock() + + if f.readonly { + return 0, errReadOnly + } + old := f.tail + if old >= tail { + return old, nil + } + for kind, table := range f.tables { + if kind == ChainFreezerBodiesTable || kind == ChainFreezerReceiptTable { + if err := table.truncateTail(tail); err != nil { + return 0, err + } + } + } + f.tail = tail + return old, nil +} + // Sync flushes all data tables to disk. func (f *MemoryFreezer) Sync() error { return nil diff --git a/core/rawdb/freezer_resettable.go b/core/rawdb/freezer_resettable.go index 7c77a06efc..bb36037c9e 100644 --- a/core/rawdb/freezer_resettable.go +++ b/core/rawdb/freezer_resettable.go @@ -194,6 +194,15 @@ func (f *resettableFreezer) TruncateTail(tail uint64) (uint64, error) { return f.freezer.TruncateTail(tail) } +// TruncateTailBlocks discards any recent data below the provided threshold number. +// It returns the previous value +func (f *resettableFreezer) TruncateTailBlocks(tail uint64) (uint64, error) { + f.lock.RLock() + defer f.lock.RUnlock() + + return f.freezer.TruncateTailBlocks(tail) +} + // Sync flushes all data tables to disk. func (f *resettableFreezer) Sync() error { f.lock.RLock() diff --git a/core/rawdb/table.go b/core/rawdb/table.go index 1a9060b636..96452534c6 100644 --- a/core/rawdb/table.go +++ b/core/rawdb/table.go @@ -107,6 +107,12 @@ func (t *table) TruncateTail(items uint64) (uint64, error) { return t.db.TruncateTail(items) } +// TruncateTailBlocks is a noop passthrough that just forwards the request to the underlying +// database. +func (t *table) TruncateTailBlocks(items uint64) (uint64, error) { + return t.db.TruncateTailBlocks(items) +} + // Sync is a noop passthrough that just forwards the request to the underlying // database. func (t *table) Sync() error { diff --git a/ethdb/database.go b/ethdb/database.go index 323f8f5d6f..c14eea198c 100644 --- a/ethdb/database.go +++ b/ethdb/database.go @@ -130,6 +130,13 @@ type AncientWriter interface { // will be removed all together. TruncateTail(n uint64) (uint64, error) + // TruncateTailBlocks discards the first n ancient data from the ancient store. The already + // deleted items are ignored. After the truncation, the earliest item can be accessed + // is item_n(start from 0). The deleted items may not be removed from the ancient store + // immediately, but only when the accumulated deleted data reach the threshold then + // will be removed all together. Only deletes bodies and receipts. + TruncateTailBlocks(n uint64) (uint64, error) + // Sync flushes all in-memory ancient store data to disk. Sync() error } diff --git a/ethdb/remotedb/remotedb.go b/ethdb/remotedb/remotedb.go index 247a4392db..5734fda500 100644 --- a/ethdb/remotedb/remotedb.go +++ b/ethdb/remotedb/remotedb.go @@ -110,6 +110,10 @@ func (db *Database) TruncateTail(n uint64) (uint64, error) { panic("not supported") } +func (db *Database) TruncateTailBlocks(n uint64) (uint64, error) { + panic("not supported") +} + func (db *Database) Sync() error { return nil }