erigon-pulse/cmd/state/generate/regenerate_tx_lookup.go

60 lines
1.5 KiB
Go
Raw Normal View History

package generate
import (
2021-04-02 11:22:25 +00:00
"context"
"os"
"os/signal"
"time"
2021-07-29 11:53:13 +00:00
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/mdbx"
"github.com/ledgerwatch/erigon/common/dbutils"
"github.com/ledgerwatch/erigon/eth/stagedsync"
"github.com/ledgerwatch/erigon/eth/stagedsync/stages"
"github.com/ledgerwatch/erigon/ethdb/prune"
2021-07-29 10:23:23 +00:00
"github.com/ledgerwatch/log/v3"
)
func RegenerateTxLookup(chaindata string) error {
db := mdbx.MustOpen(chaindata)
defer db.Close()
2021-07-07 16:15:49 +00:00
tx, err := db.BeginRw(context.Background())
2021-04-02 11:22:25 +00:00
if err != nil {
return err
}
defer tx.Rollback()
if err := tx.ClearBucket(kv.TxLookup); err != nil {
2021-07-07 16:15:49 +00:00
return err
}
2021-04-02 11:22:25 +00:00
startTime := time.Now()
ch := make(chan os.Signal, 1)
quitCh := make(chan struct{})
signal.Notify(ch, os.Interrupt)
go func() {
<-ch
close(quitCh)
}()
pm, err := prune.Get(tx)
if err != nil {
return err
}
2021-04-02 11:22:25 +00:00
lastExecutedBlock, err := stages.GetStageProgress(tx, stages.Execution)
if err != nil {
//There could be headers without block in the end
log.Error("Cant get last executed block", "err", err)
}
log.Info("TxLookup generation started", "start time", startTime)
err = stagedsync.TxLookupTransform("txlookup", tx,
dbutils.EncodeBlockNumber(pm.TxIndex.PruneTo(lastExecutedBlock)),
dbutils.EncodeBlockNumber(lastExecutedBlock+1),
quitCh,
stagedsync.StageTxLookupCfg(db, pm, os.TempDir()))
if err != nil {
return err
}
log.Info("TxLookup index is successfully regenerated", "it took", time.Since(startTime))
return nil
}