Skip to content

prune erigon state #4634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: erigonv3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions blockchain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type (
BlobStoreDBPath string `yaml:"blobStoreDBPath"`
BlobStoreRetentionDays uint32 `yaml:"blobStoreRetentionDays"`
HistoryIndexPath string `yaml:"historyIndexPath"`
HistoryBlockRetention uint64 `yaml:"historyBlockRetention"`
ID uint32 `yaml:"id"`
EVMNetworkID uint32 `yaml:"evmNetworkID"`
Address string `yaml:"address"`
Expand Down
11 changes: 6 additions & 5 deletions state/factory/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,7 @@ func (sdb *stateDB) newWorkingSetWithErigonOutput(ctx context.Context, height ui
if err != nil {
return nil, err
}
ws.store = newStateDBWorkingSetStoreWithErigonOutput(
ws.store.(*stateDBWorkingSetStore),
e,
)
ws.store = newStateDBWorkingSetStoreWithErigonOutput(ws.store, e)
return ws, nil
}

Expand Down Expand Up @@ -445,7 +442,11 @@ func (sdb *stateDB) PutBlock(ctx context.Context, blk *block.Block) error {
sdb.currentChainHeight, h,
)
}

if retention := sdb.cfg.Chain.HistoryBlockRetention; retention > 0 && h > retention {
if err := ws.PruneTo(h - retention); err != nil {
return err
}
}
if err := ws.Commit(ctx); err != nil {
return err
}
Expand Down
7 changes: 7 additions & 0 deletions state/factory/workingset.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,13 @@ func (ws *workingSet) freshAccountConversion(ctx context.Context, actCtx *protoc
return nil
}

func (ws *workingSet) PruneTo(height uint64) error {
if p, ok := ws.store.(interface{ Prune(uint64, uint64) error }); ok {
return p.Prune(height, ws.height)
}
return nil
}

// Commit persists all changes in RunActions() into the DB
func (ws *workingSet) Commit(ctx context.Context) error {
if err := protocolPreCommit(ctx, ws); err != nil {
Expand Down
47 changes: 45 additions & 2 deletions state/factory/workingsetstore_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ import (
"github.com/holiman/uint256"
"github.com/iotexproject/go-pkgs/hash"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/datadir"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/temporal/historyv2"
erigonstate "github.com/ledgerwatch/erigon/core/state"
"github.com/ledgerwatch/erigon/eth/ethconfig"
"github.com/ledgerwatch/erigon/eth/stagedsync"
"github.com/ledgerwatch/erigon/eth/stagedsync/stages"
"github.com/ledgerwatch/erigon/ethdb/prune"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
Expand Down Expand Up @@ -54,7 +60,7 @@ type writer interface {
// it's used for PutBlock, generating historical states in erigon
type stateDBWorkingSetStoreWithErigonOutput struct {
reader
store *stateDBWorkingSetStore
store workingSetStore
erigonStore *erigonStore
snMap map[int]int
}
Expand All @@ -70,7 +76,7 @@ func init() {
prometheus.MustRegister(perfMtc)
}

func newStateDBWorkingSetStoreWithErigonOutput(store *stateDBWorkingSetStore, erigonStore *erigonStore) *stateDBWorkingSetStoreWithErigonOutput {
func newStateDBWorkingSetStoreWithErigonOutput(store workingSetStore, erigonStore *erigonStore) *stateDBWorkingSetStoreWithErigonOutput {
return &stateDBWorkingSetStoreWithErigonOutput{
reader: store,
store: store,
Expand Down Expand Up @@ -119,6 +125,10 @@ func (store *stateDBWorkingSetStoreWithErigonOutput) Delete(ns string, key []byt
return store.store.Delete(ns, key)
}

func (store *stateDBWorkingSetStoreWithErigonOutput) Prune(from, to uint64) error {
return store.erigonStore.prune(from, to)
}

func (store *stateDBWorkingSetStoreWithErigonOutput) Commit(ctx context.Context) error {
t1 := time.Now()
if err := store.store.Commit(ctx); err != nil {
Expand Down Expand Up @@ -200,13 +210,46 @@ func (store *erigonStore) finalize(ctx context.Context, height uint64, ts uint64

func (store *erigonStore) commit(ctx context.Context) error {
defer store.tx.Rollback()

err := store.tx.Commit()
if err != nil {
return err
}
return nil
}

func (store *erigonStore) prune(from, to uint64) error {
if from >= to {
log.L().Panic("prune execution stage", zap.Uint64("from", from), zap.Uint64("to", to))
}
s := stagedsync.PruneState{ID: stages.Execution, ForwardProgress: to}
num := to - from
cfg := stagedsync.StageExecuteBlocksCfg(nil,
prune.Mode{
History: prune.Distance(num),
Receipts: prune.Distance(num),
CallTraces: prune.Distance(num),
},
0, nil, nil, nil, nil, nil, false, false, false, datadir.Dirs{}, nil, nil, nil, ethconfig.Sync{}, nil, nil)
err := stagedsync.PruneExecutionStage(&s, store.tx.(kv.RwTx), cfg, context.Background(), false)
if err != nil {
return errors.Wrapf(err, "failed to prune execution stage from %d to %d", from, to)
}
// debug
available, err := historyv2.AvailableFrom(store.tx)
if err != nil {
log.L().Error(" failed to get available from", zap.Error(err))
}
availableStorage, err := historyv2.AvailableStorageFrom(store.tx)
if err != nil {
log.L().Error(" failed to get available storage from", zap.Error(err))
}
if to%5000 == 0 {
log.L().Info("prune execution stage", zap.Uint64("from", from), zap.Uint64("to", to), zap.Uint64("available", available), zap.Uint64("availableStorage", availableStorage))
}
return nil
}

func (store *erigonStore) put(ns string, key []byte, value []byte) (err error) {
// only handling account, contract storage handled by evm adapter
// others are ignored
Expand Down