Skip to content

Add offset support to MemoryFreezer#1965

Open
LarryArnault45 wants to merge 3 commits into0xPolygon:developfrom
LarryArnault45:memory-freezer-offset
Open

Add offset support to MemoryFreezer#1965
LarryArnault45 wants to merge 3 commits into0xPolygon:developfrom
LarryArnault45:memory-freezer-offset

Conversation

@LarryArnault45
Copy link
Contributor

MemoryFreezer was missing offset handling, causing issues when used with ancient database pruning.

@github-actions
Copy link

This PR is stale because it has been open 21 days with no activity. Remove stale label or comment or this will be closed in 14 days.

@sonarqubecloud
Copy link

@LarryArnault45
Copy link
Contributor Author

Hi @pratikspatil024, I fixed the CI issues. Could you approve the workflow runs when you get a chance? Thanks!

@github-actions
Copy link

This PR is stale because it has been open 21 days with no activity. Remove stale label or comment or this will be closed in 14 days.

@github-actions github-actions bot added the Stale label Feb 22, 2026
@pratikspatil024
Copy link
Member

@LarryArnault45 - could you push an empty commit?

@github-actions github-actions bot removed the Stale label Mar 3, 2026
@marcello33 marcello33 requested a review from Copilot March 18, 2026 19:33
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds pruning-offset support to the in-memory ancient store (MemoryFreezer) so it can represent an ancient DB that starts at a non-zero block number (e.g., after pruning older ancients), aligning behavior with the file-based freezer’s offset concept.

Changes:

  • Extend NewMemoryFreezer to accept an offset and initialize internal counters accordingly.
  • Apply the offset in memory freezer write sequencing and in Ancient, TruncateHead, and TruncateTail.
  • Update call sites and tests to use the new NewMemoryFreezer signature.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
core/rawdb/freezer_memory.go Adds offset parameter + uses it in read/write/truncation paths for the in-memory freezer.
core/rawdb/freezer_memory_test.go Updates tests to the new constructor signature.
core/rawdb/chain_freezer.go Passes the pruning offset into the in-memory freezer when running without a datadir.
core/rawdb/ancient_scheme.go Updates state freezer’s in-memory path to pass offset 0.
Comments suppressed due to low confidence (1)

core/rawdb/freezer_memory.go:268

  • Ancient now subtracts f.offset from number without guarding number < offset. With unsigned arithmetic this underflows and relies on downstream bounds checks to fail; it also makes the error cause less clear. Consider explicitly returning errOutOfBounds (or a dedicated error) when number < f.offset.Load(), and ensure AncientRange applies the same offset semantics for consistency (currently it passes start through unchanged).
func (f *MemoryFreezer) Ancient(kind string, number uint64) ([]byte, error) {
	f.lock.RLock()
	defer f.lock.RUnlock()

	t := f.tables[kind]
	if t == nil {
		return nil, errUnknownTable
	}
	data, err := t.retrieve(number-f.offset.Load(), 1, 0)
	if err != nil {
		return nil, err
	}
	return data[0], nil
}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

return NewMemoryFreezer(false, tables)
return NewMemoryFreezer(false, 0, tables)
})
}
Comment on lines 217 to 236
// NewMemoryFreezer initializes an in-memory freezer instance.
func NewMemoryFreezer(readonly bool, tableName map[string]freezerTableConfig) *MemoryFreezer {
func NewMemoryFreezer(readonly bool, offset uint64, tableName map[string]freezerTableConfig) *MemoryFreezer {
tables := make(map[string]*memoryTable)
for name, cfg := range tableName {
tables[name] = newMemoryTable(name, cfg)
}
return &MemoryFreezer{
freezer := &MemoryFreezer{
writeBatch: newMemoryBatch(),
readonly: readonly,
tables: tables,
}

freezer.offset.Store(offset)

// Some blocks in ancientDB may have already been frozen and been pruned, so adding the offset to
// represent the absolute number of blocks already frozen.
freezer.items += offset

return freezer
}
Comment on lines 370 to 376
return old, nil
}
for _, table := range f.tables {
if err := table.truncateHead(items); err != nil {
if err := table.truncateHead(items - f.offset.Load()); err != nil {
return 0, err
}
}
Comment on lines 394 to 400
}
for _, table := range f.tables {
if table.config.prunable {
if err := table.truncateTail(tail); err != nil {
if err := table.truncateTail(tail - f.offset.Load()); err != nil {
return 0, err
}
}
@sonarqubecloud
Copy link

@LarryArnault45
Copy link
Contributor Author

@pratikspatil024 , did it, sir.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants