mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2025-01-06 11:02:18 +00:00
565a4250d6
* 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>
118 lines
3.1 KiB
Go
118 lines
3.1 KiB
Go
package commands
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"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(generateHeadersSnapshotCmd)
|
|
withSnapshotFile(generateHeadersSnapshotCmd)
|
|
withBlock(generateHeadersSnapshotCmd)
|
|
|
|
rootCmd.AddCommand(generateHeadersSnapshotCmd)
|
|
}
|
|
|
|
var generateHeadersSnapshotCmd = &cobra.Command{
|
|
Use: "headers",
|
|
Short: "Generate headers snapshot",
|
|
Example: "go run cmd/snapshots/generator/main.go headers --block 11000000 --datadir /media/b00ris/nvme/snapshotsync/ --snapshotDir /media/b00ris/nvme/snapshotsync/tg/snapshots/ --snapshotMode \"hb\" --snapshot /media/b00ris/nvme/snapshots/headers_test",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return HeaderSnapshot(cmd.Context(), chaindata, snapshotFile, block, snapshotDir, snapshotMode)
|
|
},
|
|
}
|
|
|
|
func HeaderSnapshot(ctx context.Context, dbPath, snapshotPath string, toBlock uint64, snapshotDir string, snapshotMode string) error {
|
|
if snapshotPath == "" {
|
|
return errors.New("empty snapshot path")
|
|
}
|
|
err := os.RemoveAll(snapshotPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
kv := ethdb.NewLMDB().Path(dbPath).MustOpen()
|
|
|
|
snKV := ethdb.NewLMDB().WithBucketsConfig(func(defaultBuckets dbutils.BucketsCfg) dbutils.BucketsCfg {
|
|
return dbutils.BucketsCfg{
|
|
dbutils.HeadersBucket: dbutils.BucketConfigItem{},
|
|
}
|
|
}).Path(snapshotPath).MustOpen()
|
|
|
|
tx, err := kv.BeginRo(context.Background())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback()
|
|
snTx, err := snKV.BeginRw(context.Background())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer snTx.Rollback()
|
|
|
|
t := time.Now()
|
|
var hash common.Hash
|
|
var header []byte
|
|
c, err := snTx.RwCursor(dbutils.HeadersBucket)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer c.Close()
|
|
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)
|
|
}
|
|
header = rawdb.ReadHeaderRLP(tx, hash, i)
|
|
if len(header) == 0 {
|
|
return fmt.Errorf("empty header: %v", i)
|
|
}
|
|
if err = c.Append(dbutils.HeaderKey(i, hash), header); err != nil {
|
|
return err
|
|
}
|
|
if i%1000 == 0 {
|
|
log.Info("Committed", "block", i)
|
|
}
|
|
}
|
|
|
|
err = snTx.Put(dbutils.HeadersSnapshotInfoBucket, []byte(dbutils.SnapshotHeadersHeadNumber), big.NewInt(0).SetUint64(toBlock).Bytes())
|
|
if err != nil {
|
|
log.Crit("SnapshotHeadersHeadNumber error", "err", err)
|
|
return err
|
|
}
|
|
err = snTx.Put(dbutils.HeadersSnapshotInfoBucket, []byte(dbutils.SnapshotHeadersHeadHash), hash.Bytes())
|
|
if err != nil {
|
|
log.Crit("SnapshotHeadersHeadHash error", "err", err)
|
|
return err
|
|
}
|
|
if err = snTx.Commit(); err != nil {
|
|
return err
|
|
}
|
|
|
|
snKV.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
|
|
}
|