erigon-pulse/migrations/prune.go

90 lines
2.2 KiB
Go
Raw Normal View History

package migrations
import (
"context"
2021-07-29 11:53:13 +00:00
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/common/math"
"github.com/ledgerwatch/erigon/ethdb/prune"
"github.com/ledgerwatch/erigon/params"
)
var storageMode = Migration{
Name: "storage_mode",
Up: func(db kv.RwDB, tmpdir string, progress []byte, BeforeCommit Callback) (err error) {
tx, err := db.BeginRw(context.Background())
if err != nil {
return err
}
defer tx.Rollback()
var ( // old db keys
//StorageModeHistory - does node save history.
StorageModeHistory = []byte("smHistory")
//StorageModeReceipts - does node save receipts.
StorageModeReceipts = []byte("smReceipts")
//StorageModeTxIndex - does node save transactions index.
StorageModeTxIndex = []byte("smTxIndex")
//StorageModeCallTraces - does not build index of call traces
StorageModeCallTraces = []byte("smCallTraces")
)
pm := prune.Mode{Initialised: true}
castToPruneDistance := func(v []byte) prune.Distance {
if len(v) == 1 && v[0] == 2 {
return params.FullImmutabilityThreshold // means, prune enabled
}
return math.MaxUint64 // means, prune disabled
}
{
v, err := tx.GetOne(kv.DatabaseInfo, StorageModeHistory)
if err != nil {
return err
}
2021-11-04 10:23:45 +00:00
if v == nil { // if no records in db - means Erigon just started first time and nothing to migrate. Noop.
if err := BeforeCommit(tx, nil, true); err != nil {
return err
}
return tx.Commit()
}
pm.History = castToPruneDistance(v)
}
{
v, err := tx.GetOne(kv.DatabaseInfo, StorageModeReceipts)
if err != nil {
return err
}
pm.Receipts = castToPruneDistance(v)
}
{
v, err := tx.GetOne(kv.DatabaseInfo, StorageModeTxIndex)
if err != nil {
return err
}
pm.TxIndex = castToPruneDistance(v)
}
{
v, err := tx.GetOne(kv.DatabaseInfo, StorageModeCallTraces)
if err != nil {
return err
}
pm.CallTraces = castToPruneDistance(v)
}
{
v, err := tx.GetOne(kv.DatabaseInfo, kv.StorageModeTEVM)
if err != nil {
return err
}
pm.Experiments.TEVM = len(v) == 1 && v[0] == 1
}
err = prune.SetIfNotExist(tx, pm)
if err != nil {
return err
}
if err := BeforeCommit(tx, nil, true); err != nil {
return err
}
return tx.Commit()
},
}