Skip to content

Commit

Permalink
go/consensus/cometbft/light: Keep more light blocks in store
Browse files Browse the repository at this point in the history
  • Loading branch information
kostko committed Nov 24, 2023
1 parent 7e35cb2 commit 9c7013d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
4 changes: 4 additions & 0 deletions .changelog/5466.cfg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Add `num_light_blocks_kept` configuration option

Located under `consensus.prune`, it allows configuring the number of light
blocks that are kept in the local trusted store (defaulting to 10000).
15 changes: 5 additions & 10 deletions go/consensus/cometbft/abci/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,11 @@ import (

"github.com/oasisprotocol/oasis-core/go/common/logging"
"github.com/oasisprotocol/oasis-core/go/consensus/cometbft/api"
"github.com/oasisprotocol/oasis-core/go/consensus/cometbft/config"
nodedb "github.com/oasisprotocol/oasis-core/go/storage/mkvs/db/api"
)

const (
// PruneDefault is the default PruneStrategy.
PruneDefault = pruneNone

pruneNone = "none"
pruneKeepN = "keep_n"

// LogEventABCIPruneDelete is a log event value that signals an ABCI pruning
// delete event.
LogEventABCIPruneDelete = "cometbft/abci/prune"
Expand All @@ -38,19 +33,19 @@ const (
func (s PruneStrategy) String() string {
switch s {
case PruneNone:
return pruneNone
return config.PruneStrategyNone
case PruneKeepN:
return pruneKeepN
return config.PruneStrategyKeepN
default:
return "[unknown]"
}
}

func (s *PruneStrategy) FromString(str string) error {
switch strings.ToLower(str) {
case pruneNone:
case config.PruneStrategyNone:
*s = PruneNone
case pruneKeepN:
case config.PruneStrategyKeepN:
*s = PruneKeepN
default:
return fmt.Errorf("abci/pruner: unknown pruning strategy: '%v'", str)
Expand Down
16 changes: 13 additions & 3 deletions go/consensus/cometbft/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ type SubmissionConfig struct {
MaxFee uint64 `yaml:"max_fee"`
}

const (
// PruneStrategyNone is the identifier of the strategy that disables pruning.
PruneStrategyNone = "none"
// PruneStrategyKeepN is the identifier of the strategy that keeps the last N versions.
PruneStrategyKeepN = "keep_n"
)

// PruneConfig is the CometBFT ABCI state pruning configuration structure.
type PruneConfig struct {
// ABCI state pruning strategy.
Expand All @@ -95,6 +102,8 @@ type PruneConfig struct {
NumKept uint64 `yaml:"num_kept"`
// ABCI state pruning interval.
Interval time.Duration `yaml:"interval"`
// Light blocks kept in trusted store.
NumLightBlocksKept uint16 `yaml:"num_light_blocks_kept"`
}

// CheckpointerConfig is the CometBFT ABCI state pruning configuration structure.
Expand Down Expand Up @@ -200,9 +209,10 @@ func DefaultConfig() Config {
HaltHeight: 0,
UpgradeStopDelay: 60 * time.Second,
Prune: PruneConfig{
Strategy: "none",
NumKept: 3600,
Interval: 2 * time.Minute,
Strategy: PruneStrategyNone,
NumKept: 3600,
Interval: 2 * time.Minute,
NumLightBlocksKept: 10000,
},
Checkpointer: CheckpointerConfig{
Disabled: false,
Expand Down
21 changes: 16 additions & 5 deletions go/consensus/cometbft/light/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"github.com/oasisprotocol/oasis-core/go/config"
consensus "github.com/oasisprotocol/oasis-core/go/consensus/api"
cmtAPI "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/api"
tmapi "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/api"
"github.com/oasisprotocol/oasis-core/go/consensus/cometbft/common"
cmtConfig "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/config"
"github.com/oasisprotocol/oasis-core/go/consensus/cometbft/db"
"github.com/oasisprotocol/oasis-core/go/consensus/cometbft/light/api"
p2pLight "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/light/p2p"
Expand Down Expand Up @@ -151,7 +151,7 @@ func (c *client) worker() {
c.logger.Error("failed to obtain chain context", "err", err)
return
}
tmChainID := tmapi.CometBFTChainID(chainCtx)
tmChainID := cmtAPI.CometBFTChainID(chainCtx)

// Loads the local block at the provided height and adds it to the trust store.
trustLocalBlock := func(ctx context.Context, height int64) error {
Expand Down Expand Up @@ -208,15 +208,26 @@ func (c *client) worker() {
providers = append(providers, p)
c.providers = append(c.providers, p)
}

opts := []cmtlight.Option{
cmtlight.MaxRetryAttempts(lcMaxRetryAttempts),
cmtlight.Logger(common.NewLogAdapter(!config.GlobalConfig.Consensus.LogDebug)),
cmtlight.DisableProviderRemoval(),
}
switch config.GlobalConfig.Consensus.Prune.Strategy {
case cmtConfig.PruneStrategyNone:
opts = append(opts, cmtlight.PruningSize(0)) // Disable pruning the light store.
default:
opts = append(opts, cmtlight.PruningSize(config.GlobalConfig.Consensus.Prune.NumLightBlocksKept))
}

tmc, err := cmtlight.NewClientFromTrustedStore(
tmChainID,
config.GlobalConfig.Consensus.StateSync.TrustPeriod,
providers[0], // Primary provider.
providers[1:], // Witnesses.
c.store,
cmtlight.MaxRetryAttempts(lcMaxRetryAttempts),
cmtlight.Logger(common.NewLogAdapter(!config.GlobalConfig.Consensus.LogDebug)),
cmtlight.DisableProviderRemoval(),
opts...,
)
if err != nil {
c.logger.Error("failed to initialize cometbft light client", "err", err)
Expand Down

0 comments on commit 9c7013d

Please sign in to comment.