Skip to content

Commit

Permalink
Prune: determenistic TxnLookup prune time (#12535)
Browse files Browse the repository at this point in the history
For: #12502
  • Loading branch information
AskAlexSharov authored Oct 30, 2024
1 parent 5498f85 commit d1e6f72
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions eth/stagedsync/stage_txlookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/binary"
"fmt"
"math/big"
"time"

"github.com/erigontech/erigon-lib/log/v3"

Expand Down Expand Up @@ -254,18 +255,30 @@ func PruneTxLookup(s *PruneState, tx kv.RwTx, cfg TxLookupCfg, ctx context.Conte
// can't prune much here: because tx_lookup index has crypto-hashed-keys, and 1 block producing hundreds of deletes
blockTo = min(blockTo, blockFrom+10)

pruneTimeout := 250 * time.Millisecond
if s.CurrentSyncCycle.IsInitialCycle {
pruneTimeout = time.Hour
}

if blockFrom < blockTo {
if err = deleteTxLookupRange(tx, logPrefix, blockFrom, blockTo, ctx, cfg, logger); err != nil {
return fmt.Errorf("prune TxLookUp: %w", err)
}
t := time.Now()
var pruneBlockNum = blockFrom
for ; pruneBlockNum < blockTo; pruneBlockNum++ {
err = deleteTxLookupRange(tx, logPrefix, pruneBlockNum, pruneBlockNum+1, ctx, cfg, logger)
if err != nil {
return fmt.Errorf("prune TxLookUp: %w", err)
}
if cfg.borConfig != nil && pruneBor {
if err = deleteBorTxLookupRange(tx, logPrefix, pruneBlockNum, pruneBlockNum+1, ctx, cfg, logger); err != nil {
return fmt.Errorf("prune BorTxLookUp: %w", err)
}
}

if cfg.borConfig != nil && pruneBor {
if err = deleteBorTxLookupRange(tx, logPrefix, blockFrom, blockTo, ctx, cfg, logger); err != nil {
return fmt.Errorf("prune BorTxLookUp: %w", err)
if time.Since(t) > pruneTimeout {
break
}
}

if err = s.DoneAt(tx, blockTo); err != nil {
if err = s.DoneAt(tx, pruneBlockNum); err != nil {
return err
}
}
Expand All @@ -279,8 +292,8 @@ func PruneTxLookup(s *PruneState, tx kv.RwTx, cfg TxLookupCfg, ctx context.Conte
}

// deleteTxLookupRange - [blockFrom, blockTo)
func deleteTxLookupRange(tx kv.RwTx, logPrefix string, blockFrom, blockTo uint64, ctx context.Context, cfg TxLookupCfg, logger log.Logger) error {
return etl.Transform(logPrefix, tx, kv.HeaderCanonical, kv.TxLookup, cfg.tmpdir, func(k, v []byte, next etl.ExtractNextFunc) error {
func deleteTxLookupRange(tx kv.RwTx, logPrefix string, blockFrom, blockTo uint64, ctx context.Context, cfg TxLookupCfg, logger log.Logger) (err error) {
err = etl.Transform(logPrefix, tx, kv.HeaderCanonical, kv.TxLookup, cfg.tmpdir, func(k, v []byte, next etl.ExtractNextFunc) error {
blocknum, blockHash := binary.BigEndian.Uint64(k), libcommon.CastToHash(v)
body, err := cfg.blockReader.BodyWithTransactions(ctx, tx, blockHash, blocknum)
if err != nil {
Expand All @@ -306,6 +319,10 @@ func deleteTxLookupRange(tx kv.RwTx, logPrefix string, blockFrom, blockTo uint64
return []interface{}{"block", binary.BigEndian.Uint64(k)}
},
}, logger)
if err != nil {
return err
}
return nil
}

// deleteTxLookupRange - [blockFrom, blockTo)
Expand Down

0 comments on commit d1e6f72

Please sign in to comment.