From cf79ace3c1b2e4181960273967c27e69c973d2ed Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Fri, 14 Mar 2025 18:10:15 +0100 Subject: [PATCH] core/rawdb: track table config in memory freezer --- core/rawdb/freezer_memory.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/core/rawdb/freezer_memory.go b/core/rawdb/freezer_memory.go index 159ac486bb..0f94f5d459 100644 --- a/core/rawdb/freezer_memory.go +++ b/core/rawdb/freezer_memory.go @@ -30,17 +30,19 @@ import ( // memoryTable is used to store a list of sequential items in memory. type memoryTable struct { - name string // Table name items uint64 // Number of stored items in the table, including the deleted ones offset uint64 // Number of deleted items from the table data [][]byte // List of rlp-encoded items, sort in order size uint64 // Total memory size occupied by the table lock sync.RWMutex + + name string + config freezerTableConfig } // newMemoryTable initializes the memory table. -func newMemoryTable(name string) *memoryTable { - return &memoryTable{name: name} +func newMemoryTable(name string, config freezerTableConfig) *memoryTable { + return &memoryTable{name: name, config: config} } // has returns an indicator whether the specified data exists. @@ -220,8 +222,8 @@ type MemoryFreezer struct { // NewMemoryFreezer initializes an in-memory freezer instance. func NewMemoryFreezer(readonly bool, tableName map[string]freezerTableConfig) *MemoryFreezer { tables := make(map[string]*memoryTable) - for name := range tableName { - tables[name] = newMemoryTable(name) + for name, cfg := range tableName { + tables[name] = newMemoryTable(name, cfg) } return &MemoryFreezer{ writeBatch: newMemoryBatch(), @@ -389,7 +391,7 @@ func (f *MemoryFreezer) TruncateTail(tail uint64) (uint64, error) { return old, nil } -// TruncateTailBlocks discards any recent data below the provided threshold number. +// TruncateTailBlocks discards all data below the provided block. // Only truncates blocks and receipts. func (f *MemoryFreezer) TruncateTailBlocks(tail uint64) (uint64, error) { f.lock.Lock() @@ -402,8 +404,8 @@ func (f *MemoryFreezer) TruncateTailBlocks(tail uint64) (uint64, error) { if old >= tail { return old, nil } - for kind, table := range f.tables { - if kind == ChainFreezerBodiesTable || kind == ChainFreezerReceiptTable { + for _, table := range f.tables { + if table.config.prunable { if err := table.truncateTail(tail); err != nil { return 0, err } @@ -436,8 +438,8 @@ func (f *MemoryFreezer) Reset() error { defer f.lock.Unlock() tables := make(map[string]*memoryTable) - for name := range f.tables { - tables[name] = newMemoryTable(name) + for name, table := range f.tables { + tables[name] = newMemoryTable(name, table.config) } f.tables = tables f.items, f.tail = 0, 0