erigon-pulse/cmd/downloader/generator/commands/verify_state_snapshot.go

87 lines
2.6 KiB
Go
Raw Normal View History

package commands
import (
"context"
"fmt"
"io/ioutil"
"os"
"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/core/rawdb"
"github.com/ledgerwatch/erigon/eth/stagedsync"
"github.com/ledgerwatch/erigon/ethdb/snapshotdb"
"github.com/ledgerwatch/erigon/turbo/snapshotsync"
2021-07-29 10:23:23 +00:00
"github.com/ledgerwatch/log/v3"
"github.com/spf13/cobra"
)
func init() {
withDatadir(verifyStateSnapshotCmd)
withSnapshotFile(verifyStateSnapshotCmd)
withBlock(verifyStateSnapshotCmd)
rootCmd.AddCommand(verifyStateSnapshotCmd)
}
//
var verifyStateSnapshotCmd = &cobra.Command{
Use: "verify_state",
Short: "Verify state snapshot",
Example: "go run cmd/snapshots/generator/main.go verify_state --block 11000000 --snapshot /media/b00ris/nvme/snapshots/state/ --datadir /media/b00ris/nvme/backup/snapshotsync/",
RunE: func(cmd *cobra.Command, args []string) error {
logger := log.New()
return VerifyStateSnapshot(cmd.Context(), logger, chaindata, snapshotFile, block)
},
}
func VerifyStateSnapshot(ctx context.Context, logger log.Logger, dbPath, snapshotPath string, block uint64) error {
var snkv, tmpDB kv.RwDB
tmpPath, err := ioutil.TempDir(os.TempDir(), "vrf*")
if err != nil {
return err
}
2021-07-28 03:43:51 +00:00
snkv = mdbx.NewMDBX(logger).WithTablessCfg(func(defaultBuckets kv.TableCfg) kv.TableCfg {
return kv.TableCfg{
2021-07-28 03:43:51 +00:00
kv.PlainState: kv.ChaindataTablesCfg[kv.PlainState],
kv.PlainContractCode: kv.ChaindataTablesCfg[kv.PlainContractCode],
kv.Code: kv.ChaindataTablesCfg[kv.Code],
2021-06-16 10:57:58 +00:00
}
}).Path(snapshotPath).Readonly().MustOpen()
tmpDB = mdbx.NewMDBX(logger).Path(tmpPath).MustOpen()
2021-06-16 10:57:58 +00:00
defer os.RemoveAll(tmpPath)
defer tmpDB.Close()
snkv = snapshotdb.NewSnapshotKV().StateSnapshot(snkv).DB(tmpDB).Open()
tx, err := snkv.BeginRw(context.Background())
if err != nil {
return err
}
defer tx.Rollback()
2021-04-02 11:22:25 +00:00
hash, err := rawdb.ReadCanonicalHash(tx, block)
if err != nil {
return err
}
2021-04-02 11:22:25 +00:00
syncHeadHeader := rawdb.ReadHeader(tx, hash, block)
if syncHeadHeader == nil {
return fmt.Errorf("empty header")
}
expectedRootHash := syncHeadHeader.Root
tt := time.Now()
err = stagedsync.PromoteHashedStateCleanly("", tx, stagedsync.StageHashStateCfg(snkv, os.TempDir()), ctx.Done())
fmt.Println("Promote took", time.Since(tt))
if err != nil {
return fmt.Errorf("promote state err: %w", err)
}
blockReader := snapshotsync.NewBlockReader()
_, err = stagedsync.RegenerateIntermediateHashes("", tx, stagedsync.StageTrieCfg(snkv, true, true, os.TempDir(), blockReader), expectedRootHash, ctx.Done())
if err != nil {
return fmt.Errorf("regenerateIntermediateHashes err: %w", err)
}
return nil
}