Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nil ptr in delete ancient #7081

Merged
merged 2 commits into from
Mar 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions core/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1241,33 +1241,30 @@ func WriteBlock(db kv.RwTx, block *types.Block) error {
// keeps genesis in db: [1, to)
// doesn't change sequences of kv.EthTx and kv.NonCanonicalTxs
// doesn't delete Receipts, Senders, Canonical markers, TotalDifficulty
// returns [deletedFrom, deletedTo)
func DeleteAncientBlocks(tx kv.RwTx, blockTo uint64, blocksDeleteLimit int) (deletedFrom, deletedTo uint64, err error) {
func DeleteAncientBlocks(tx kv.RwTx, blockTo uint64, blocksDeleteLimit int) error {
c, err := tx.Cursor(kv.Headers)
if err != nil {
return
return err
}
defer c.Close()

// find first non-genesis block
firstK, _, err := c.Seek(hexutility.EncodeTs(1))
if err != nil {
return
return err
}
if firstK == nil { //nothing to delete
return
return err
}
blockFrom := binary.BigEndian.Uint64(firstK)
stopAtBlock := cmp.Min(blockTo, blockFrom+uint64(blocksDeleteLimit))
k, _, _ := c.Current()
deletedFrom = binary.BigEndian.Uint64(k)

var canonicalHash libcommon.Hash
var b *types.BodyForStorage

for k, _, err = c.Current(); k != nil; k, _, err = c.Next() {
for k, _, err := c.Current(); k != nil; k, _, err = c.Next() {
if err != nil {
return
return err
}

n := binary.BigEndian.Uint64(k)
Expand All @@ -1277,13 +1274,13 @@ func DeleteAncientBlocks(tx kv.RwTx, blockTo uint64, blocksDeleteLimit int) (del

canonicalHash, err = ReadCanonicalHash(tx, n)
if err != nil {
return
return err
}
isCanonical := bytes.Equal(k[8:], canonicalHash[:])

b, err = ReadBodyForStorageByKey(tx, k)
if err != nil {
return
return err
}
if b == nil {
log.Debug("DeleteAncientBlocks: block body not found", "height", n)
Expand All @@ -1296,25 +1293,22 @@ func DeleteAncientBlocks(tx kv.RwTx, blockTo uint64, blocksDeleteLimit int) (del
bucket = kv.NonCanonicalTxs
}
if err = tx.Delete(bucket, txIDBytes); err != nil {
return
return err
}
}
}
// Copying k because otherwise the same memory will be reused
// for the next key and Delete below will end up deleting 1 more record than required
kCopy := common.CopyBytes(k)
if err = tx.Delete(kv.Headers, kCopy); err != nil {
return
return err
}
if err = tx.Delete(kv.BlockBody, kCopy); err != nil {
return
return err
}
}

k, _, _ = c.Current()
deletedTo = binary.BigEndian.Uint64(k)

return
return nil
}

// TruncateBlocks - delete block >= blockFrom
Expand Down
4 changes: 2 additions & 2 deletions core/rawdb/accessors_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func TestBlockStorage(t *testing.T) {
if err := WriteBlock(tx, block); err != nil {
t.Fatalf("Could not write block: %v", err)
}
if _, _, err := DeleteAncientBlocks(tx, 0, 1); err != nil {
if err := DeleteAncientBlocks(tx, 0, 1); err != nil {
t.Fatal(err)
}
}
Expand Down Expand Up @@ -551,7 +551,7 @@ func TestBlockWithdrawalsStorage(t *testing.T) {
if err := WriteBlock(tx, block); err != nil {
t.Fatalf("Could not write block: %v", err)
}
if _, _, err := DeleteAncientBlocks(tx, 0, 1); err != nil {
if err := DeleteAncientBlocks(tx, 0, 1); err != nil {
t.Fatal(err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion turbo/snapshotsync/block_snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@ func (br *BlockRetire) PruneAncientBlocks(tx kv.RwTx, limit int) error {
return err
}
canDeleteTo := CanDeleteTo(currentProgress, br.snapshots)
if _, _, err := rawdb.DeleteAncientBlocks(tx, canDeleteTo, limit); err != nil {
if err := rawdb.DeleteAncientBlocks(tx, canDeleteTo, limit); err != nil {
return nil
}
if err := rawdb.PruneTable(tx, kv.Senders, canDeleteTo, context.Background(), limit); err != nil {
Expand Down