Skip to content

Commit

Permalink
done stateDB writes batching
Browse files Browse the repository at this point in the history
  • Loading branch information
werty144 committed Jul 17, 2023
1 parent b3bc721 commit 78d8d38
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
11 changes: 10 additions & 1 deletion state/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,14 @@ func ValidateValidatorUpdates(abciUpdates []abci.ValidatorUpdate, params types.V
// store.go, exported exclusively and explicitly for testing.
func SaveValidatorsInfo(db dbm.DB, height, lastHeightChanged int64, valSet *types.ValidatorSet) error {
stateStore := dbStore{db, StoreOptions{DiscardABCIResponses: false}}
return stateStore.saveValidatorsInfo(height, lastHeightChanged, valSet)
batch := stateStore.db.NewBatch()
err := stateStore.saveValidatorsInfo(height, lastHeightChanged, valSet, batch)
if err != nil {
return err
}
err = batch.WriteSync()
if err != nil {
return err
}
return nil
}
58 changes: 41 additions & 17 deletions state/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package state
import (
"errors"
"fmt"

"github.com/cosmos/gogoproto/proto"

dbm "github.com/cometbft/cometbft-db"
Expand Down Expand Up @@ -173,61 +172,84 @@ func (store dbStore) Save(state State) error {
}

func (store dbStore) save(state State, key []byte) error {
batch := store.db.NewBatch()
defer func(batch dbm.Batch) {
err := batch.Close()
if err != nil {
panic(err)
}
}(batch)
nextHeight := state.LastBlockHeight + 1
// If first block, save validators for the block.
if nextHeight == 1 {
nextHeight = state.InitialHeight
// This extra logic due to validator set changes being delayed 1 block.
// It may get overwritten due to InitChain validator updates.
if err := store.saveValidatorsInfo(nextHeight, nextHeight, state.Validators); err != nil {
if err := store.saveValidatorsInfo(nextHeight, nextHeight, state.Validators, batch); err != nil {
return err
}
}
// Save next validators.
if err := store.saveValidatorsInfo(nextHeight+1, state.LastHeightValidatorsChanged, state.NextValidators); err != nil {
if err := store.saveValidatorsInfo(nextHeight+1, state.LastHeightValidatorsChanged, state.NextValidators, batch); err != nil {
return err
}

// Save next consensus params.
if err := store.saveConsensusParamsInfo(nextHeight,
state.LastHeightConsensusParamsChanged, state.ConsensusParams); err != nil {
state.LastHeightConsensusParamsChanged, state.ConsensusParams, batch); err != nil {
return err
}
err := store.db.SetSync(key, state.Bytes())
if err != nil {
if err := batch.Set(key, state.Bytes()); err != nil {
return err
}

if err := batch.WriteSync(); err != nil {
panic(err)
}
return nil
}

// BootstrapState saves a new state, used e.g. by state sync when starting from non-zero height.
func (store dbStore) Bootstrap(state State) error {
fmt.Println("Bootstrapping")
batch := store.db.NewBatch()
defer func(batch dbm.Batch) {
err := batch.Close()
if err != nil {
panic(err)
}
}(batch)
height := state.LastBlockHeight + 1
if height == 1 {
height = state.InitialHeight
}

if height > 1 && !state.LastValidators.IsNilOrEmpty() {
if err := store.saveValidatorsInfo(height-1, height-1, state.LastValidators); err != nil {
if err := store.saveValidatorsInfo(height-1, height-1, state.LastValidators, batch); err != nil {
return err
}
}

if err := store.saveValidatorsInfo(height, height, state.Validators); err != nil {
if err := store.saveValidatorsInfo(height, height, state.Validators, batch); err != nil {
return err
}

if err := store.saveValidatorsInfo(height+1, height+1, state.NextValidators); err != nil {
if err := store.saveValidatorsInfo(height+1, height+1, state.NextValidators, batch); err != nil {
return err
}

if err := store.saveConsensusParamsInfo(height,
state.LastHeightConsensusParamsChanged, state.ConsensusParams); err != nil {
state.LastHeightConsensusParamsChanged, state.ConsensusParams, batch); err != nil {
return err
}

if err := batch.Set(stateKey, state.Bytes()); err != nil {
return err
}

return store.db.SetSync(stateKey, state.Bytes())
if err := batch.WriteSync(); err != nil {
panic(err)
}

return batch.Close()
}

// PruneStates deletes states between the given heights (including from, excluding to). It is not
Expand Down Expand Up @@ -576,7 +598,7 @@ func loadValidatorsInfo(db dbm.DB, height int64) (*cmtstate.ValidatorsInfo, erro
// `height` is the effective height for which the validator is responsible for
// signing. It should be called from s.Save(), right before the state itself is
// persisted.
func (store dbStore) saveValidatorsInfo(height, lastHeightChanged int64, valSet *types.ValidatorSet) error {
func (store dbStore) saveValidatorsInfo(height, lastHeightChanged int64, valSet *types.ValidatorSet, batch dbm.Batch) error {
if lastHeightChanged > height {
return errors.New("lastHeightChanged cannot be greater than ValidatorsInfo height")
}
Expand All @@ -598,7 +620,8 @@ func (store dbStore) saveValidatorsInfo(height, lastHeightChanged int64, valSet
return err
}

err = store.db.Set(calcValidatorsKey(height), bz)
//err = store.db.Set(calcValidatorsKey(height), bz)
err = batch.Set(calcValidatorsKey(height), bz)
if err != nil {
return err
}
Expand Down Expand Up @@ -662,7 +685,7 @@ func (store dbStore) loadConsensusParamsInfo(height int64) (*cmtstate.ConsensusP
// It should be called from s.Save(), right before the state itself is persisted.
// If the consensus params did not change after processing the latest block,
// only the last height for which they changed is persisted.
func (store dbStore) saveConsensusParamsInfo(nextHeight, changeHeight int64, params types.ConsensusParams) error {
func (store dbStore) saveConsensusParamsInfo(nextHeight, changeHeight int64, params types.ConsensusParams, batch dbm.Batch) error {
paramsInfo := &cmtstate.ConsensusParamsInfo{
LastHeightChanged: changeHeight,
}
Expand All @@ -675,7 +698,8 @@ func (store dbStore) saveConsensusParamsInfo(nextHeight, changeHeight int64, par
return err
}

err = store.db.Set(calcConsensusParamsKey(nextHeight), bz)
//err = store.db.Set(calcConsensusParamsKey(nextHeight), bz)
err = batch.Set(calcConsensusParamsKey(nextHeight), bz)
if err != nil {
return err
}
Expand Down

0 comments on commit 78d8d38

Please sign in to comment.