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 8, 2023
1 parent 2722fd5 commit e389fae
Show file tree
Hide file tree
Showing 3 changed files with 60 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
46 changes: 46 additions & 0 deletions kai/state/cstate/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,52 @@ func TestPruneState(t *testing.T) {
assert.NotNil(t, valsInfo)
}

func TestNoPruneState(t *testing.T) {
db := memorydb.New()
stateStore := cstate.NewStore(db)
stateStore.SetPruning(false)
val, _ := types.RandValidator(true, 10)
val1, _ := types.RandValidator(true, 10)
val2, _ := types.RandValidator(true, 10)
lvals := types.NewValidatorSet([]*types.Validator{val})
vals := types.NewValidatorSet([]*types.Validator{val, val1})
nvals := types.NewValidatorSet([]*types.Validator{val, val1, val2})
cparams := configs.DefaultConsensusParams()

// Block height H
var checkpointH uint64 = cstate.PRUNE_LATEST_BLOCK_STATE_INTERVAL
// Block height 2H
var checkpoint2H uint64 = 2 * cstate.PRUNE_LATEST_BLOCK_STATE_INTERVAL

// Consensus state at block H - 1, which will be pruned if pruning is enable
// But in this case, pruning is disable so this state is still persisted.
state := cstate.LatestBlockState{
LastBlockHeight: checkpointH - 1,
LastValidators: lvals,
Validators: vals,
NextValidators: vals,
LastHeightValidatorsChanged: checkpointH - 1,
ConsensusParams: *cparams,
LastHeightConsensusParamsChanged: 0,
}
stateStore.Save(state)

// Consensus state at block 2H, the checkpoint that triggers the pruning
state = cstate.LatestBlockState{
LastBlockHeight: checkpoint2H,
LastValidators: nvals,
Validators: nvals,
NextValidators: nvals,
LastHeightValidatorsChanged: checkpoint2H,
ConsensusParams: *cparams,
LastHeightConsensusParamsChanged: 0,
}
stateStore.Save(state)

prunedState := rawdb.ReadConsensusStateHeight(db, checkpointH-1)
assert.NotNil(t, prunedState)
}

func TestLoadValidators(t *testing.T) {
db := memorydb.New()
stateStore := cstate.NewStore(db)
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 e389fae

Please sign in to comment.