diff --git a/core/rawdb/freezer.go b/core/rawdb/freezer.go index 33603b973f..aa0fc3ad67 100644 --- a/core/rawdb/freezer.go +++ b/core/rawdb/freezer.go @@ -348,7 +348,7 @@ func (f *Freezer) TruncateHead(items uint64) (uint64, error) { if kind != ChainFreezerBlobSidecarTable { return 0, err } - nt, err := table.resetItems(items-f.offset, items-f.offset) + nt, err := table.resetItems(items - f.offset) if err != nil { return 0, err } @@ -489,7 +489,7 @@ func (f *Freezer) repair() error { if kind != ChainFreezerBlobSidecarTable { return err } - nt, err := table.resetItems(head, head) + nt, err := table.resetItems(head) if err != nil { return err } @@ -720,11 +720,20 @@ func (f *Freezer) ResetTable(kind string, tail, head uint64, onlyEmpty bool) err return nil } - nt, err := f.tables[kind].resetItems(tail-f.offset, head-f.offset) - if err != nil { - return err + if tail != head { + if err := f.tables[kind].truncateHead(head); err != nil { + return err + } + if err := f.tables[kind].truncateTail(tail); err != nil { + return err + } + } else { + nt, err := f.tables[kind].resetItems(head - f.offset) + if err != nil { + return err + } + f.tables[kind] = nt } - f.tables[kind] = nt if err := f.repair(); err != nil { for _, table := range f.tables { diff --git a/core/rawdb/freezer_table.go b/core/rawdb/freezer_table.go index 3fc313e749..68021b3bb1 100644 --- a/core/rawdb/freezer_table.go +++ b/core/rawdb/freezer_table.go @@ -1029,34 +1029,22 @@ func (t *freezerTable) ResetItemsOffset(virtualTail uint64) error { return nil } -// resetItems reset freezer table head & tail +// resetItems reset freezer table to 0 items with head `headAndTail` // only used for ChainFreezerBlobSidecarTable now -func (t *freezerTable) resetItems(tail, head uint64) (*freezerTable, error) { +func (t *freezerTable) resetItems(headAndTail uint64) (*freezerTable, error) { if t.readonly { return nil, errors.New("resetItems in readonly mode") } itemHidden := t.itemHidden.Load() items := t.items.Load() - if tail > head || (tail < head && (itemHidden > tail || items < head)) { + if itemHidden > headAndTail || items < headAndTail { return nil, errors.New("cannot reset to non-exist range") } - var err error - if tail < head { - if err = t.truncateHead(head); err != nil { - return nil, err - } - if err = t.truncateTail(tail); err != nil { - return nil, err - } - return t, nil - } - t.lock.Lock() defer t.lock.Unlock() - // if tail == head, it means table reset to 0 item // remove all data files t.head.Close() t.releaseFilesAfter(0, true) @@ -1086,7 +1074,7 @@ func (t *freezerTable) resetItems(tail, head uint64) (*freezerTable, error) { } tailIndex := indexEntry{ filenum: 0, - offset: uint32(tail), + offset: uint32(headAndTail), } if _, err = index.Write(tailIndex.append(nil)); err != nil { return nil, err