erigon-pulse/cmd/state/generate/regenerate_index.go
Alex Sharov 57777e7c60
Prepare codebase for future default DB change (#670)
* Add kv.tx.bucket.Clear() and db.ClearBuckets() methods

* Add kv.tx.bucket.Clear() and db.ClearBuckets() methods

* choose db based on file suffix

* implement db.id method

* implement db.id method

* use ethdb.NewDatabase method

* use ethb.MustOpen method

* cleanup

* support TEST_DB env flag

* create db path automatically needed

* bolt - don't change prefix on happy path
2020-06-16 14:36:16 +01:00

50 lines
1.1 KiB
Go

package generate
import (
"errors"
"os"
"os/signal"
"time"
"github.com/ledgerwatch/turbo-geth/core"
"github.com/ledgerwatch/turbo-geth/eth/stagedsync/stages"
"github.com/ledgerwatch/turbo-geth/ethdb"
"github.com/ledgerwatch/turbo-geth/log"
)
func RegenerateIndex(chaindata string, csBucket []byte) error {
db := ethdb.MustOpen(chaindata)
ch := make(chan os.Signal, 1)
quitCh := make(chan struct{})
signal.Notify(ch, os.Interrupt)
go func() {
<-ch
close(quitCh)
}()
lastExecutedBlock, _, err := stages.GetStageProgress(db, stages.Execution)
if err != nil {
//There could be headers without block in the end
log.Error("Cant get last executed block", "err", err)
}
ig := core.NewIndexGenerator(db, quitCh)
cs, ok := core.CSMapper[string(csBucket)]
if !ok {
return errors.New("unknown changeset")
}
err = ig.DropIndex(cs.IndexBucket)
if err != nil {
return err
}
startTime := time.Now()
log.Info("Index generation started", "start time", startTime)
err = ig.GenerateIndex(0, lastExecutedBlock, csBucket)
if err != nil {
return err
}
log.Info("Index is successfully regenerated", "it took", time.Since(startTime))
return nil
}