erigon-pulse/cmd/state/generate/generate_body_snapshot.go
b00ris 4ebade3583
Add sync by bittorrent snapshots (#1160)
* save state

* torrent experiments

* torrent experiment passed

* fixes after merge

* snapshot headers processing passed

* save state

* save state

* download headers works after snapshot processing

* save state

* save state

* save state

* save state

* add lazy load tx to snapshots, increase number of trackers

* save state

* speedup getting info

* change logging

* move to turbo package

* save state

* save state

* save state

* cleanup

* save state

* add test test

* save state

* lmdb debugging

* fix readonly mode

* save state

* fix build

* sync works

* save state

* save state

* save state

* allow cmd stages stageSenders use snapshots

* debugging failed hashing

* remove experimental tests

* remove torrent experimental tests

* fix lint

* extract snapshot wrapper

* metainfo checker

* add remote seeder

* add logs

* update gomod

* remove useless code

* fix lint&remove useless code

* extract verify snapshot to separated command

* skip debug test

* fix test

* change type of seedSnapshot flag

* add eth logger to torrent lib

* skip debug test

* add Close method

* review fixes

* fix lint

Co-authored-by: alex.sharov <AskAlexSharov@gmail.com>
Co-authored-by: Alexey Akhunov <akhounov@gmail.com>
2020-10-06 20:24:48 +01:00

72 lines
2.0 KiB
Go

package generate
import (
"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"
"math/big"
"os"
"time"
)
func BodySnapshot(dbPath, snapshotPath string, toBlock uint64) error {
kv := ethdb.NewLMDB().Path(dbPath).MustOpen()
snkv := ethdb.NewLMDB().WithBucketsConfig(func(defaultBuckets dbutils.BucketsCfg) dbutils.BucketsCfg {
return dbutils.BucketsCfg{
dbutils.BlockBodyPrefix: dbutils.BucketConfigItem{},
dbutils.SnapshotInfoBucket: dbutils.BucketConfigItem{},
}
}).Path(snapshotPath).MustOpen()
db := ethdb.NewObjectDatabase(kv)
sndb := ethdb.NewObjectDatabase(snkv)
t := time.Now()
chunkFile := 30000
tuples := make(ethdb.MultiPutTuples, 0, chunkFile*3+100)
var hash common.Hash
for i := uint64(1); i <= toBlock; i++ {
hash = rawdb.ReadCanonicalHash(db, i)
body := rawdb.ReadBodyRLP(db, hash, i)
tuples = append(tuples, []byte(dbutils.BlockBodyPrefix), dbutils.BlockBodyKey(i, hash), body)
if len(tuples) >= chunkFile {
log.Info("Committed", "block", i)
_, err := sndb.MultiPut(tuples...)
if err != nil {
log.Crit("Multiput error", "err", err)
return err
}
tuples = tuples[:0]
}
}
if len(tuples) > 0 {
_, err := sndb.MultiPut(tuples...)
if err != nil {
log.Crit("Multiput error", "err", err)
return err
}
}
err := sndb.Put(dbutils.SnapshotInfoBucket, []byte(dbutils.SnapshotBodyHeadNumber), big.NewInt(0).SetUint64(toBlock).Bytes())
if err != nil {
log.Crit("SnapshotBodyHeadNumber error", "err", err)
return err
}
err = sndb.Put(dbutils.SnapshotInfoBucket, []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
}