2020-07-14 01:56:29 +00:00
|
|
|
package commands
|
2020-07-07 04:00:25 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-07-25 07:35:08 +00:00
|
|
|
"os"
|
2020-07-25 17:18:45 +00:00
|
|
|
"path"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2020-07-07 04:00:25 +00:00
|
|
|
|
2020-07-25 07:35:08 +00:00
|
|
|
"github.com/ledgerwatch/lmdb-go/lmdb"
|
2020-07-25 17:18:45 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
2020-07-07 04:00:25 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/common/dbutils"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core"
|
2020-07-17 05:52:09 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/eth/stagedsync"
|
2020-07-07 04:00:25 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/eth/stagedsync/stages"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/ethdb"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/log"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var cmdResetState = &cobra.Command{
|
|
|
|
Use: "reset_state",
|
2020-07-15 11:53:12 +00:00
|
|
|
Short: "Reset StateStages (4,5,6,7,8,9) and buckets",
|
2020-07-07 04:00:25 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
ctx := rootContext()
|
|
|
|
err := resetState(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
return err
|
|
|
|
}
|
2020-07-25 07:35:08 +00:00
|
|
|
if compact {
|
|
|
|
if err := copyCompact(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-07 04:00:25 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
withChaindata(cmdResetState)
|
2020-07-25 07:35:08 +00:00
|
|
|
withCompact(cmdResetState)
|
2020-07-07 04:00:25 +00:00
|
|
|
|
|
|
|
rootCmd.AddCommand(cmdResetState)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resetState(_ context.Context) error {
|
|
|
|
db := ethdb.MustOpen(chaindata)
|
|
|
|
defer db.Close()
|
|
|
|
fmt.Printf("Before reset: \n")
|
|
|
|
if err := printStages(db); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
core.UsePlainStateExecution = true
|
|
|
|
// don't reset senders here
|
|
|
|
if err := resetExec(db); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-17 05:52:09 +00:00
|
|
|
if err := stagedsync.ResetHashState(db); err != nil {
|
2020-07-07 04:00:25 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := resetHistory(db); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-07 10:07:14 +00:00
|
|
|
if err := resetTxLookup(db); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-07 04:00:25 +00:00
|
|
|
|
|
|
|
// set genesis after reset all buckets
|
|
|
|
if _, _, err := core.DefaultGenesisBlock().CommitGenesisState(db, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("After reset: \n")
|
|
|
|
if err := printStages(db); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-07-07 10:07:14 +00:00
|
|
|
|
2020-07-07 07:11:10 +00:00
|
|
|
func resetSenders(db *ethdb.ObjectDatabase) error {
|
|
|
|
if err := db.ClearBuckets(
|
|
|
|
dbutils.Senders,
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-15 11:53:12 +00:00
|
|
|
if err := stages.SaveStageProgress(db, stages.Senders, 0, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := stages.SaveStageUnwind(db, stages.Senders, 0, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2020-07-07 07:11:10 +00:00
|
|
|
}
|
2020-07-07 04:00:25 +00:00
|
|
|
|
|
|
|
func resetExec(db *ethdb.ObjectDatabase) error {
|
|
|
|
if err := db.ClearBuckets(
|
|
|
|
dbutils.CurrentStateBucket,
|
|
|
|
dbutils.AccountChangeSetBucket,
|
|
|
|
dbutils.StorageChangeSetBucket,
|
|
|
|
dbutils.ContractCodeBucket,
|
|
|
|
dbutils.PlainStateBucket,
|
|
|
|
dbutils.PlainAccountChangeSetBucket,
|
|
|
|
dbutils.PlainStorageChangeSetBucket,
|
|
|
|
dbutils.PlainContractCodeBucket,
|
|
|
|
dbutils.IncarnationMapBucket,
|
|
|
|
dbutils.CodeBucket,
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-15 11:53:12 +00:00
|
|
|
if err := stages.SaveStageProgress(db, stages.Execution, 0, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := stages.SaveStageUnwind(db, stages.Execution, 0, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2020-07-07 04:00:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func resetHistory(db *ethdb.ObjectDatabase) error {
|
|
|
|
if err := db.ClearBuckets(
|
|
|
|
dbutils.AccountsHistoryBucket,
|
|
|
|
dbutils.StorageHistoryBucket,
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := stages.SaveStageProgress(db, stages.AccountHistoryIndex, 0, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := stages.SaveStageProgress(db, stages.StorageHistoryIndex, 0, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-15 11:53:12 +00:00
|
|
|
if err := stages.SaveStageUnwind(db, stages.AccountHistoryIndex, 0, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := stages.SaveStageUnwind(db, stages.StorageHistoryIndex, 0, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-07 04:00:25 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-07 10:07:14 +00:00
|
|
|
func resetTxLookup(db *ethdb.ObjectDatabase) error {
|
|
|
|
if err := db.ClearBuckets(
|
|
|
|
dbutils.TxLookupPrefix,
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := stages.SaveStageProgress(db, stages.TxLookup, 0, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-15 11:53:12 +00:00
|
|
|
if err := stages.SaveStageUnwind(db, stages.TxLookup, 0, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-07 10:07:14 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-07-07 04:00:25 +00:00
|
|
|
func printStages(db *ethdb.ObjectDatabase) error {
|
|
|
|
var err error
|
|
|
|
var progress uint64
|
|
|
|
for stage := stages.SyncStage(0); stage < stages.Finish; stage++ {
|
|
|
|
if progress, _, err = stages.GetStageProgress(db, stage); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-04 10:05:27 +00:00
|
|
|
fmt.Printf("Stage: %d, progress: %d\n", stage, progress)
|
2020-07-07 04:00:25 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-07-25 07:35:08 +00:00
|
|
|
|
|
|
|
func copyCompact() error {
|
|
|
|
from := chaindata
|
|
|
|
backup := from + "_backup"
|
|
|
|
to := chaindata + "_copy"
|
|
|
|
|
|
|
|
log.Info("Start db copy-compact")
|
|
|
|
|
|
|
|
env, errOpen := lmdb.NewEnv()
|
|
|
|
if errOpen != nil {
|
|
|
|
return errOpen
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := env.Open(from, lmdb.Readonly, 0644); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-02 11:53:08 +00:00
|
|
|
_ = os.RemoveAll(to)
|
2020-07-25 07:35:08 +00:00
|
|
|
if err := os.MkdirAll(to, 0744); err != nil {
|
|
|
|
return fmt.Errorf("could not create dir: %s, %w", to, err)
|
|
|
|
}
|
|
|
|
if err := env.SetMapSize(ethdb.LMDBMapSize); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-25 17:18:45 +00:00
|
|
|
|
|
|
|
f, err := os.Stat(path.Join(from, "data.mdb"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, stopLogging := context.WithCancel(context.Background())
|
|
|
|
defer stopLogging()
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
for {
|
2020-07-27 13:51:59 +00:00
|
|
|
time.Sleep(20 * time.Second)
|
|
|
|
|
2020-07-25 17:18:45 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
2020-07-27 13:51:59 +00:00
|
|
|
|
2020-07-25 17:18:45 +00:00
|
|
|
f2, err := os.Stat(path.Join(to, "data.mdb"))
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Progress check failed", "err", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Info("Progress", "done", common.StorageSize(f2.Size()), "from", common.StorageSize(f.Size()))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-07-25 07:35:08 +00:00
|
|
|
if err := env.CopyFlag(to, lmdb.CopyCompact); err != nil {
|
|
|
|
return fmt.Errorf("%w, from: %s, to: %s", err, from, to)
|
|
|
|
}
|
2020-07-25 17:18:45 +00:00
|
|
|
|
|
|
|
stopLogging()
|
|
|
|
wg.Wait()
|
2020-07-25 07:35:08 +00:00
|
|
|
if err := os.Rename(from, backup); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.Rename(to, from); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.RemoveAll(backup); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|