erigon-pulse/turbo/snapshotsync/wrapdb.go
b00ris 8db5790838
State snapshot sync (#1417)
* move experiments to new branch&reorganise kv_snapshot

* walk&modify tests

* added delete from snapshot tests

* fmt

* state snapshot debug

* snapshot validation passed. copy state snapshot

* debug

* snapshot cursor.Prev test

* Prev works correct. Added Current check

* add err check

* added walk forward and backward test

* before refactoring

* refactoring

* execution with snapshot debug

* fix

* remove useless test

* before dupcursor implimentation

* tests with prev and delete works

* execution based on state snapshot passed

* remove useless tests

* blocks to 1140000 passed

* clean verifier

* cleanup state generation

* clean verify && seeder

* remove debug code

* tests passed

* fix lint

* save state

* test passed

* fix lint

* add state hash

* fix lint
2021-01-02 19:28:37 +00:00

99 lines
3.6 KiB
Go

package snapshotsync
import (
"github.com/ledgerwatch/lmdb-go/lmdb"
"github.com/ledgerwatch/turbo-geth/common/dbutils"
"github.com/ledgerwatch/turbo-geth/ethdb"
"github.com/ledgerwatch/turbo-geth/log"
)
var (
bucketConfigs = map[SnapshotType]dbutils.BucketsCfg{
SnapshotType_bodies: {
dbutils.BlockBodyPrefix: dbutils.BucketConfigItem{},
dbutils.BodiesSnapshotInfoBucket: dbutils.BucketConfigItem{},
},
SnapshotType_headers: {
dbutils.HeaderPrefix: dbutils.BucketConfigItem{},
dbutils.HeadersSnapshotInfoBucket: dbutils.BucketConfigItem{},
},
SnapshotType_state: {
dbutils.PlainStateBucket: dbutils.BucketConfigItem{
Flags: dbutils.DupSort,
AutoDupSortKeysConversion: true,
DupFromLen: 60,
DupToLen: 28,
},
dbutils.PlainContractCodeBucket: dbutils.BucketConfigItem{},
dbutils.CodeBucket: dbutils.BucketConfigItem{},
dbutils.StateSnapshotInfoBucket: dbutils.BucketConfigItem{},
},
}
)
func WrapBySnapshotsFromDir(kv ethdb.KV, snapshotDir string, mode SnapshotMode) (ethdb.KV, error) {
log.Info("Wrap db to snapshots", "dir", snapshotDir, "mode", mode.ToString())
snkv := ethdb.NewSnapshot2KV().DB(kv)
if mode.Bodies {
snapshotKV, err := ethdb.NewLMDB().Flags(func(flags uint) uint { return flags | lmdb.Readonly }).Path(snapshotDir + "/bodies").WithBucketsConfig(func(defaultBuckets dbutils.BucketsCfg) dbutils.BucketsCfg {
return bucketConfigs[SnapshotType_bodies]
}).Open()
if err != nil {
log.Error("Can't open body snapshot", "err", err)
return nil, err
} else { //nolint
snkv.SnapshotDB([]string{dbutils.BlockBodyPrefix, dbutils.BodiesSnapshotInfoBucket}, snapshotKV)
}
}
if mode.Headers {
snapshotKV, err := ethdb.NewLMDB().Flags(func(flags uint) uint { return flags | lmdb.Readonly }).Path(snapshotDir + "/headers").WithBucketsConfig(func(defaultBuckets dbutils.BucketsCfg) dbutils.BucketsCfg {
return bucketConfigs[SnapshotType_headers]
}).Open()
if err != nil {
log.Error("Can't open headers snapshot", "err", err)
return nil, err
} else { //nolint
snkv.SnapshotDB([]string{dbutils.HeaderPrefix, dbutils.HeadersSnapshotInfoBucket}, snapshotKV)
}
}
if mode.State {
snapshotKV, err := ethdb.NewLMDB().Flags(func(flags uint) uint { return flags | lmdb.Readonly }).Path(snapshotDir + "/headers").WithBucketsConfig(func(defaultBuckets dbutils.BucketsCfg) dbutils.BucketsCfg {
return bucketConfigs[SnapshotType_headers]
}).Open()
if err != nil {
log.Error("Can't open headers snapshot", "err", err)
return nil, err
} else { //nolint
snkv.SnapshotDB([]string{dbutils.StateSnapshotInfoBucket, dbutils.PlainStateBucket, dbutils.PlainContractCodeBucket, dbutils.CodeBucket}, snapshotKV)
}
}
return snkv.MustOpen(), nil
}
func WrapBySnapshotsFromDownloader(kv ethdb.KV, snapshots map[SnapshotType]*SnapshotsInfo) (ethdb.KV, error) {
snKV := ethdb.NewSnapshot2KV().DB(kv)
for k, v := range snapshots {
log.Info("Wrap db by", "snapshot", k.String(), "dir", v.Dbpath)
cfg := bucketConfigs[k]
snapshotKV, err := ethdb.NewLMDB().Flags(func(flags uint) uint { return flags | lmdb.Readonly }).Path(v.Dbpath).WithBucketsConfig(func(defaultBuckets dbutils.BucketsCfg) dbutils.BucketsCfg {
return cfg
}).Open()
if err != nil {
log.Error("Can't open snapshot", "err", err)
return nil, err
} else { //nolint
buckets := make([]string, 0, 1)
for bucket := range bucketConfigs[k] {
buckets = append(buckets, bucket)
}
snKV.SnapshotDB(buckets, snapshotKV)
}
}
return snKV.MustOpen(), nil
}