erigon-pulse/cmd/snapshots/generator/commands/generate_body_snapshot.go
b00ris 565a4250d6
Snapshot sync headers stage (#1836)
* save state

* snapshot update works

* save state

* snapshot migrator

* tx test

* save state

* migrations stages refactor

* refactor snapshot migrator

* compilation fixed

* integrate snapshot migrator

* goerli sync headers

* debug async snapshotter on goerly

* move verify headers, remove experiments, fix remove old snapshot

* save state

* refactor snapshotsync injection

* fix deadlock

* replace snapshot generation stage logic to migrate method

* change done for body snapshot

* clean

* clean&&change deleted value

* clean

* fix hash len

* fix hash len

* remove one of wrap methods, add remove snapshots on start

* add err check

* fix shadowing

* stages unwind order debug

* matryoshka experiments

* steam test

* fix build

* fix test

* fix lint

* fix test

* fix test datarace

* add get test

* return timeout

* fix mdbx overlap

* fix after merge

* change epoch size

* clean todo

* fix

* return testdata

* added return from sndownloader gorutine

* fix review comments

* Fix

* More info

Co-authored-by: Alex Sharp <alexsharp@Alexs-MacBook-Pro.local>
2021-05-08 09:45:40 +01:00

107 lines
3.1 KiB
Go

package commands
import (
"context"
"fmt"
"math/big"
"os"
"time"
"github.com/spf13/cobra"
"github.com/ledgerwatch/turbo-geth/common"
"github.com/ledgerwatch/turbo-geth/common/dbutils"
"github.com/ledgerwatch/turbo-geth/core/rawdb"
"github.com/ledgerwatch/turbo-geth/ethdb"
"github.com/ledgerwatch/turbo-geth/log"
)
func init() {
withDatadir(generateBodiesSnapshotCmd)
withSnapshotFile(generateBodiesSnapshotCmd)
withBlock(generateBodiesSnapshotCmd)
rootCmd.AddCommand(generateBodiesSnapshotCmd)
}
var generateBodiesSnapshotCmd = &cobra.Command{
Use: "bodies",
Short: "Generate bodies snapshot",
Example: "go run cmd/snapshots/generator/main.go bodies --block 11000000 --datadir /media/b00ris/nvme/snapshotsync/ --snapshotDir /media/b00ris/nvme/snapshotsync/tg/snapshots/ --snapshotMode \"hb\" --snapshot /media/b00ris/nvme/snapshots/bodies_test",
RunE: func(cmd *cobra.Command, args []string) error {
return BodySnapshot(cmd.Context(), chaindata, snapshotFile, block, snapshotDir, snapshotMode)
},
}
func BodySnapshot(ctx context.Context, dbPath, snapshotPath string, toBlock uint64, snapshotDir string, snapshotMode string) error {
kv := ethdb.NewLMDB().Path(dbPath).MustOpen()
var err error
snKV := ethdb.NewLMDB().WithBucketsConfig(func(defaultBuckets dbutils.BucketsCfg) dbutils.BucketsCfg {
return dbutils.BucketsCfg{
dbutils.BlockBodyPrefix: dbutils.BucketConfigItem{},
dbutils.BodiesSnapshotInfoBucket: dbutils.BucketConfigItem{},
}
}).Path(snapshotPath).MustOpen()
snDB := ethdb.NewObjectDatabase(snKV)
tx, err := kv.BeginRo(context.Background())
if err != nil {
return err
}
defer tx.Rollback()
t := time.Now()
chunkFile := 30000
tuples := make(ethdb.MultiPutTuples, 0, chunkFile*3+100)
var hash common.Hash
for i := uint64(1); i <= toBlock; i++ {
if common.IsCanceled(ctx) {
return common.ErrStopped
}
hash, err = rawdb.ReadCanonicalHash(tx, i)
if err != nil {
return fmt.Errorf("getting canonical hash for block %d: %v", i, err)
}
body := rawdb.ReadBodyRLP(tx, hash, i)
tuples = append(tuples, []byte(dbutils.BlockBodyPrefix), dbutils.BlockBodyKey(i, hash), body)
if len(tuples) >= chunkFile {
log.Info("Committed", "block", i)
if _, err = snDB.MultiPut(tuples...); err != nil {
log.Crit("Multiput error", "err", err)
return err
}
tuples = tuples[:0]
}
}
if len(tuples) > 0 {
if _, err = snDB.MultiPut(tuples...); err != nil {
log.Crit("Multiput error", "err", err)
return err
}
}
err = snDB.Put(dbutils.BodiesSnapshotInfoBucket, []byte(dbutils.SnapshotBodyHeadNumber), big.NewInt(0).SetUint64(toBlock).Bytes())
if err != nil {
log.Crit("SnapshotBodyHeadNumber error", "err", err)
return err
}
err = snDB.Put(dbutils.BodiesSnapshotInfoBucket, []byte(dbutils.SnapshotBodyHeadHash), hash.Bytes())
if err != nil {
log.Crit("SnapshotBodyHeadHash error", "err", err)
return err
}
snDB.Close()
err = os.Remove(snapshotPath + "/lock.mdb")
if err != nil {
log.Warn("Remove lock", "err", err)
return err
}
log.Info("Finished", "duration", time.Since(t))
return nil
}