2020-05-23 09:19:56 +00:00
|
|
|
package generate
|
2020-04-20 10:35:33 +00:00
|
|
|
|
|
|
|
import (
|
2020-06-10 20:07:14 +00:00
|
|
|
"errors"
|
2020-07-09 06:13:45 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/common/changeset"
|
2020-06-16 13:36:16 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"time"
|
|
|
|
|
2020-04-20 10:35:33 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/core"
|
2020-06-10 20:07:14 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/eth/stagedsync/stages"
|
2020-04-20 10:35:33 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/ethdb"
|
2020-05-31 06:57:47 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/log"
|
2020-04-20 10:35:33 +00:00
|
|
|
)
|
|
|
|
|
2020-05-31 06:57:47 +00:00
|
|
|
func RegenerateIndex(chaindata string, csBucket []byte) error {
|
2020-06-16 13:36:16 +00:00
|
|
|
db := ethdb.MustOpen(chaindata)
|
2020-06-10 20:07:14 +00:00
|
|
|
ch := make(chan os.Signal, 1)
|
|
|
|
quitCh := make(chan struct{})
|
|
|
|
signal.Notify(ch, os.Interrupt)
|
|
|
|
go func() {
|
|
|
|
<-ch
|
|
|
|
close(quitCh)
|
|
|
|
}()
|
2020-04-20 10:35:33 +00:00
|
|
|
|
2020-06-10 20:07:14 +00:00
|
|
|
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)
|
2020-07-09 06:13:45 +00:00
|
|
|
cs, ok := changeset.Mapper[string(csBucket)]
|
2020-06-10 20:07:14 +00:00
|
|
|
if !ok {
|
|
|
|
return errors.New("unknown changeset")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ig.DropIndex(cs.IndexBucket)
|
2020-05-31 06:57:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-04-20 10:35:33 +00:00
|
|
|
}
|
2020-05-31 06:57:47 +00:00
|
|
|
startTime := time.Now()
|
|
|
|
log.Info("Index generation started", "start time", startTime)
|
2020-06-10 20:07:14 +00:00
|
|
|
err = ig.GenerateIndex(0, lastExecutedBlock, csBucket)
|
2020-04-20 10:35:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-31 06:57:47 +00:00
|
|
|
log.Info("Index is successfully regenerated", "it took", time.Since(startTime))
|
2020-04-20 10:35:33 +00:00
|
|
|
return nil
|
|
|
|
}
|