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