Skip to content

Commit

Permalink
add pruning option to consensus state store
Browse files Browse the repository at this point in the history
  • Loading branch information
rameight committed Jul 7, 2023
1 parent 2722fd5 commit 2d69c69
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
15 changes: 12 additions & 3 deletions kai/state/cstate/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const (
)

type Store interface {
SetPruning(pruning bool)
LoadStateFromDBOrGenesisDoc(genesisDoc *genesis.Genesis) (LatestBlockState, error)
Load() LatestBlockState
Save(LatestBlockState)
Expand All @@ -54,11 +55,15 @@ type Store interface {
}

type dbStore struct {
db kaidb.Database
db kaidb.Database
pruning bool
}

func NewStore(db kaidb.Database) Store {
return &dbStore{db: db}
return &dbStore{
db: db,
pruning: true,
}
}

// LoadStateFromDBOrGenesisDoc loads the most recent state from the database,
Expand All @@ -78,6 +83,10 @@ func (s *dbStore) LoadStateFromDBOrGenesisDoc(genesisDoc *genesis.Genesis) (Late
return state, nil
}

func (s *dbStore) SetPruning(pruning bool) {
s.pruning = pruning
}

// SaveState persists the State, the ValidatorsInfo, and the ConsensusParamsInfo to the database.
// This flushes the writes (e.g. calls SetSync).
func (s *dbStore) Save(state LatestBlockState) {
Expand All @@ -86,7 +95,7 @@ func (s *dbStore) Save(state LatestBlockState) {
// Starting from the 2nd interval, we try to prune
// from beginning of previous interval
// to the end of previous interval
if state.LastBlockHeight%PRUNE_LATEST_BLOCK_STATE_INTERVAL == 0 && state.LastBlockHeight/PRUNE_LATEST_BLOCK_STATE_INTERVAL > 1 {
if s.pruning && state.LastBlockHeight%PRUNE_LATEST_BLOCK_STATE_INTERVAL == 0 && state.LastBlockHeight/PRUNE_LATEST_BLOCK_STATE_INTERVAL > 1 {
to := state.LastBlockHeight - PRUNE_LATEST_BLOCK_STATE_INTERVAL
from := to - PRUNE_LATEST_BLOCK_STATE_INTERVAL
if from == 0 { // dont prune state at block #0
Expand Down
2 changes: 2 additions & 0 deletions mainchain/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ func New(stack *node.Node, config *Config) (*Kardiachain, error) {
kai.APIBackend = &KaiAPIBackend{kai, nil}

stateDB := cstate.NewStore(chainDb)
stateDB.SetPruning(!config.NoPruning)

evPool, err := evidence.NewPool(stateDB, chainDb, kai.blockchain)
if err != nil {
return nil, err
Expand Down

0 comments on commit 2d69c69

Please sign in to comment.