From 3906d7e51f79e7c1f19e2f603884ed5fe9ae33b5 Mon Sep 17 00:00:00 2001 From: Alex Sharov Date: Tue, 26 Apr 2022 12:04:38 +0700 Subject: [PATCH] Snapshots: allow stage_headers --unwind behind available snapshots (#3971) * save * save * save --- cmd/integration/commands/stages.go | 2 +- core/rawdb/accessors_chain.go | 18 +++--------------- turbo/snapshotsync/block_snapshots.go | 2 +- 3 files changed, 5 insertions(+), 17 deletions(-) diff --git a/cmd/integration/commands/stages.go b/cmd/integration/commands/stages.go index 1285c242854..9155d5e8892 100644 --- a/cmd/integration/commands/stages.go +++ b/cmd/integration/commands/stages.go @@ -477,7 +477,7 @@ func stageHeaders(db kv.RwDB, ctx context.Context) error { return err } if err = tx.Put(kv.HeadHeaderKey, []byte(kv.HeadHeaderKey), hash[:]); err != nil { - log.Error("ReadHeadHeaderHash failed", "err", err) + return err } log.Info("Progress", "headers", progress) return nil diff --git a/core/rawdb/accessors_chain.go b/core/rawdb/accessors_chain.go index 4c6a1e646ce..eaab7fc45c8 100644 --- a/core/rawdb/accessors_chain.go +++ b/core/rawdb/accessors_chain.go @@ -498,18 +498,6 @@ func RawTransactionsRange(db kv.Getter, from, to uint64) (res [][]byte, err erro // ResetSequence - allow set arbitrary value to sequence (for example to decrement it to exact value) func ResetSequence(tx kv.RwTx, bucket string, newValue uint64) error { - c, err := tx.Cursor(bucket) - if err != nil { - return err - } - k, _, err := c.Last() - if err != nil { - return err - } - if k != nil && binary.BigEndian.Uint64(k) >= newValue { - panic(fmt.Sprintf("must not happen. ResetSequence: %s, %d < lastInDB: %d\n", bucket, newValue, binary.BigEndian.Uint64(k))) - } - newVBytes := make([]byte, 8) binary.BigEndian.PutUint64(newVBytes, newValue) if err := tx.Put(kv.Sequence, []byte(bucket), newVBytes); err != nil { @@ -1111,13 +1099,13 @@ func DeleteAncientBlocks(db kv.RwTx, blockTo uint64, blocksDeleteLimit int) erro } defer c.Close() - var stopAtBlock uint64 + var stopAtBlock, firstNonGenesisInDB uint64 { k, _, err := c.First() if err != nil { return err } - firstNonGenesisInDB := binary.BigEndian.Uint64(k) + firstNonGenesisInDB = binary.BigEndian.Uint64(k) if firstNonGenesisInDB == 0 { // keep genesis in DB k, _, err := c.Next() if err != nil { @@ -1133,7 +1121,7 @@ func DeleteAncientBlocks(db kv.RwTx, blockTo uint64, blocksDeleteLimit int) erro } n := binary.BigEndian.Uint64(k) - if n >= stopAtBlock { + if n >= stopAtBlock { // [from, to) break } diff --git a/turbo/snapshotsync/block_snapshots.go b/turbo/snapshotsync/block_snapshots.go index 96f66b30c87..9123648e454 100644 --- a/turbo/snapshotsync/block_snapshots.go +++ b/turbo/snapshotsync/block_snapshots.go @@ -1265,7 +1265,7 @@ func DumpTxs(ctx context.Context, db kv.RoDB, segmentFile, tmpDir string, blockF var lastBody types.BodyForStorage if err := kv.BigChunks(db, kv.HeaderCanonical, from, func(tx kv.Tx, k, v []byte) (bool, error) { blockNum := binary.BigEndian.Uint64(k) - if blockNum >= blockTo { + if blockNum >= blockTo { // [from, to) return false, nil }