erigon-pulse/cmd/integration/commands/reset_state.go

135 lines
3.7 KiB
Go
Raw Normal View History

2020-07-14 01:56:29 +00:00
package commands
import (
2022-05-19 05:28:33 +00:00
"encoding/binary"
"fmt"
"os"
"text/tabwriter"
"github.com/ledgerwatch/erigon-lib/common"
2021-07-29 11:53:13 +00:00
"github.com/ledgerwatch/erigon-lib/kv"
2022-05-19 05:28:33 +00:00
"github.com/ledgerwatch/erigon/core/rawdb"
reset2 "github.com/ledgerwatch/erigon/core/rawdb/rawdbreset"
"github.com/ledgerwatch/erigon/eth/stagedsync/stages"
"github.com/ledgerwatch/erigon/ethdb/prune"
"github.com/ledgerwatch/erigon/turbo/snapshotsync"
2021-07-29 10:23:23 +00:00
"github.com/ledgerwatch/log/v3"
"github.com/spf13/cobra"
)
var cmdResetState = &cobra.Command{
Use: "reset_state",
Short: "Reset StateStages (5,6,7,8,9,10) and buckets",
RunE: func(cmd *cobra.Command, args []string) error {
ctx, _ := common.RootContext()
2022-08-12 14:45:09 +00:00
db := openDB(dbCfg(kv.ChainDB, chaindata), true)
2020-10-28 03:18:10 +00:00
defer db.Close()
if err := db.View(ctx, func(tx kv.Tx) error { return printStages(tx, allSnapshots(db)) }); err != nil {
return err
}
2020-10-28 03:18:10 +00:00
2022-08-12 14:45:09 +00:00
err := reset2.ResetState(db, ctx, chain)
if err != nil {
log.Error(err.Error())
return err
}
2020-08-10 23:38:19 +00:00
// set genesis after reset all buckets
fmt.Printf("After reset: \n")
if err := db.View(ctx, func(tx kv.Tx) error { return printStages(tx, allSnapshots(db)) }); err != nil {
return err
}
2020-08-10 23:38:19 +00:00
return nil
},
}
func init() {
withDataDir(cmdResetState)
2021-06-16 04:31:25 +00:00
withChain(cmdResetState)
rootCmd.AddCommand(cmdResetState)
}
func printStages(db kv.Tx, snapshots *snapshotsync.RoSnapshots) error {
var err error
var progress uint64
w := new(tabwriter.Writer)
defer w.Flush()
w.Init(os.Stdout, 8, 8, 0, '\t', 0)
fmt.Fprintf(w, "Note: prune_at doesn't mean 'all data before were deleted' - it just mean stage.Prune function were run to this block. Because 1 stage may prune multiple data types to different prune distance.\n")
fmt.Fprint(w, "\n \t\t stage_at \t prune_at\n")
for _, stage := range stages.AllStages {
2020-12-21 11:01:37 +00:00
if progress, err = stages.GetStageProgress(db, stage); err != nil {
return err
}
prunedTo, err := stages.GetStagePruneProgress(db, stage)
if err != nil {
return err
}
fmt.Fprintf(w, "%s \t\t %d \t %d\n", string(stage), progress, prunedTo)
}
pm, err := prune.Get(db)
if err != nil {
return err
}
fmt.Fprintf(w, "--\n")
2021-07-24 09:50:42 +00:00
fmt.Fprintf(w, "prune distance: %s\n\n", pm.String())
s1, err := db.ReadSequence(kv.EthTx)
if err != nil {
return err
}
s2, err := db.ReadSequence(kv.NonCanonicalTxs)
if err != nil {
return err
}
fmt.Fprintf(w, "sequence: EthTx=%d, NonCanonicalTx=%d\n\n", s1, s2)
2022-05-19 05:28:33 +00:00
{
firstNonGenesis, err := rawdb.SecondKey(db, kv.Headers)
if err != nil {
return err
}
if firstNonGenesis != nil {
fmt.Fprintf(w, "first header in db: %d\n", binary.BigEndian.Uint64(firstNonGenesis))
} else {
fmt.Fprintf(w, "no headers in db\n")
}
firstNonGenesis, err = rawdb.SecondKey(db, kv.BlockBody)
if err != nil {
return err
}
if firstNonGenesis != nil {
fmt.Fprintf(w, "first body in db: %d\n\n", binary.BigEndian.Uint64(firstNonGenesis))
} else {
fmt.Fprintf(w, "no bodies in db\n\n")
}
2022-05-19 05:28:33 +00:00
}
fmt.Fprintf(w, "--\n")
fmt.Fprintf(w, "snapsthos: blocks=%d, segments=%d, indices=%d\n\n", snapshots.BlocksAvailable(), snapshots.SegmentsMax(), snapshots.IndicesMax())
//fmt.Printf("==== state =====\n")
//db.ForEach(kv.PlainState, nil, func(k, v []byte) error {
// fmt.Printf("st: %x, %x\n", k, v)
// return nil
//})
//fmt.Printf("==== code =====\n")
//db.ForEach(kv.Code, nil, func(k, v []byte) error {
// fmt.Printf("code: %x, %x\n", k, v)
// return nil
//})
//fmt.Printf("==== PlainContractCode =====\n")
//db.ForEach(kv.PlainContractCode, nil, func(k, v []byte) error {
// fmt.Printf("code2: %x, %x\n", k, v)
// return nil
//})
//fmt.Printf("==== IncarnationMap =====\n")
//db.ForEach(kv.IncarnationMap, nil, func(k, v []byte) error {
// fmt.Printf("IncarnationMap: %x, %x\n", k, v)
// return nil
//})
return nil
}