2019-02-01 17:03:09 -08:00
|
|
|
package types
|
|
|
|
|
2020-06-22 13:31:33 -07:00
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
// Pruning option string constants
|
|
|
|
const (
|
|
|
|
PruningOptionDefault = "default"
|
|
|
|
PruningOptionEverything = "everything"
|
|
|
|
PruningOptionNothing = "nothing"
|
|
|
|
PruningOptionCustom = "custom"
|
|
|
|
)
|
|
|
|
|
2020-02-06 12:58:32 -08:00
|
|
|
var (
|
2021-08-06 07:23:13 -07:00
|
|
|
// PruneDefault defines a pruning strategy where the last 362880 heights are
|
|
|
|
// kept in addition to every 100th and where to-be pruned heights are pruned
|
|
|
|
// at every 10th height. The last 362880 heights are kept assuming the typical
|
|
|
|
// block time is 5s and typical unbonding period is 21 days. If these values
|
|
|
|
// do not match the applications' requirements, use the "custom" option.
|
|
|
|
PruneDefault = NewPruningOptions(362880, 100, 10)
|
2020-02-06 12:58:32 -08:00
|
|
|
|
2020-06-22 13:31:33 -07:00
|
|
|
// PruneEverything defines a pruning strategy where all committed heights are
|
|
|
|
// deleted, storing only the current height and where to-be pruned heights are
|
|
|
|
// pruned at every 10th height.
|
|
|
|
PruneEverything = NewPruningOptions(0, 0, 10)
|
2020-02-06 12:58:32 -08:00
|
|
|
|
2020-06-22 13:31:33 -07:00
|
|
|
// PruneNothing defines a pruning strategy where all heights are kept on disk.
|
|
|
|
PruneNothing = NewPruningOptions(0, 1, 0)
|
2020-02-06 12:58:32 -08:00
|
|
|
)
|
|
|
|
|
2020-06-22 13:31:33 -07:00
|
|
|
// PruningOptions defines the pruning strategy used when determining which
|
|
|
|
// heights are removed from disk when committing state.
|
2019-02-01 17:03:09 -08:00
|
|
|
type PruningOptions struct {
|
2020-06-22 13:31:33 -07:00
|
|
|
// KeepRecent defines how many recent heights to keep on disk.
|
|
|
|
KeepRecent uint64
|
|
|
|
|
|
|
|
// KeepEvery defines how many offset heights are kept on disk past KeepRecent.
|
|
|
|
KeepEvery uint64
|
|
|
|
|
|
|
|
// Interval defines when the pruned heights are removed from disk.
|
|
|
|
Interval uint64
|
2019-02-01 17:03:09 -08:00
|
|
|
}
|
|
|
|
|
2020-06-22 13:31:33 -07:00
|
|
|
func NewPruningOptions(keepRecent, keepEvery, interval uint64) PruningOptions {
|
|
|
|
return PruningOptions{
|
|
|
|
KeepRecent: keepRecent,
|
|
|
|
KeepEvery: keepEvery,
|
|
|
|
Interval: interval,
|
2020-02-06 12:58:32 -08:00
|
|
|
}
|
2020-06-22 13:31:33 -07:00
|
|
|
}
|
2020-02-06 12:58:32 -08:00
|
|
|
|
2020-06-22 13:31:33 -07:00
|
|
|
func (po PruningOptions) Validate() error {
|
2020-06-25 09:41:44 -07:00
|
|
|
if po.KeepEvery == 0 && po.Interval == 0 {
|
2020-06-22 13:31:33 -07:00
|
|
|
return fmt.Errorf("invalid 'Interval' when pruning everything: %d", po.Interval)
|
|
|
|
}
|
2020-06-25 09:41:44 -07:00
|
|
|
if po.KeepEvery == 1 && po.Interval != 0 { // prune nothing
|
2020-06-22 13:31:33 -07:00
|
|
|
return fmt.Errorf("invalid 'Interval' when pruning nothing: %d", po.Interval)
|
2019-02-01 17:03:09 -08:00
|
|
|
}
|
2020-06-25 09:41:44 -07:00
|
|
|
if po.KeepEvery > 1 && po.Interval == 0 {
|
|
|
|
return fmt.Errorf("invalid 'Interval' when pruning: %d", po.Interval)
|
|
|
|
}
|
2019-02-01 17:03:09 -08:00
|
|
|
|
2020-06-22 13:31:33 -07:00
|
|
|
return nil
|
2019-02-01 17:03:09 -08:00
|
|
|
}
|
|
|
|
|
2020-06-22 13:31:33 -07:00
|
|
|
func NewPruningOptionsFromString(strategy string) PruningOptions {
|
|
|
|
switch strategy {
|
|
|
|
case PruningOptionEverything:
|
|
|
|
return PruneEverything
|
2019-02-01 17:03:09 -08:00
|
|
|
|
2020-06-22 13:31:33 -07:00
|
|
|
case PruningOptionNothing:
|
|
|
|
return PruneNothing
|
|
|
|
|
|
|
|
case PruningOptionDefault:
|
|
|
|
return PruneDefault
|
|
|
|
|
|
|
|
default:
|
|
|
|
return PruneDefault
|
|
|
|
}
|
2020-02-06 12:58:32 -08:00
|
|
|
}
|