github.com/Finschia/finschia-sdk@v0.48.1/store/types/pruning.go (about)

     1  package types
     2  
     3  import "fmt"
     4  
     5  // Pruning option string constants
     6  const (
     7  	PruningOptionDefault    = "default"
     8  	PruningOptionEverything = "everything"
     9  	PruningOptionNothing    = "nothing"
    10  	PruningOptionCustom     = "custom"
    11  )
    12  
    13  var (
    14  	// PruneDefault defines a pruning strategy where the last 362880 heights are
    15  	// kept in addition to every 100th and where to-be pruned heights are pruned
    16  	// at every 10th height. The last 362880 heights are kept assuming the typical
    17  	// block time is 5s and typical unbonding period is 21 days. If these values
    18  	// do not match the applications' requirements, use the "custom" option.
    19  	PruneDefault = NewPruningOptions(362880, 100, 10)
    20  
    21  	// PruneEverything defines a pruning strategy where all committed heights are
    22  	// deleted, storing only the current height and where to-be pruned heights are
    23  	// pruned at every 10th height.
    24  	PruneEverything = NewPruningOptions(2, 0, 10)
    25  
    26  	// PruneNothing defines a pruning strategy where all heights are kept on disk.
    27  	PruneNothing = NewPruningOptions(0, 1, 0)
    28  )
    29  
    30  // PruningOptions defines the pruning strategy used when determining which
    31  // heights are removed from disk when committing state.
    32  type PruningOptions struct {
    33  	// KeepRecent defines how many recent heights to keep on disk.
    34  	KeepRecent uint64
    35  
    36  	// KeepEvery defines how many offset heights are kept on disk past KeepRecent.
    37  	KeepEvery uint64
    38  
    39  	// Interval defines when the pruned heights are removed from disk.
    40  	Interval uint64
    41  }
    42  
    43  func NewPruningOptions(keepRecent, keepEvery, interval uint64) PruningOptions {
    44  	return PruningOptions{
    45  		KeepRecent: keepRecent,
    46  		KeepEvery:  keepEvery,
    47  		Interval:   interval,
    48  	}
    49  }
    50  
    51  func (po PruningOptions) Validate() error {
    52  	if po.KeepEvery == 0 && po.Interval == 0 {
    53  		return fmt.Errorf("invalid 'Interval' when pruning everything: %d", po.Interval)
    54  	}
    55  	if po.KeepEvery == 1 && po.Interval != 0 { // prune nothing
    56  		return fmt.Errorf("invalid 'Interval' when pruning nothing: %d", po.Interval)
    57  	}
    58  	if po.KeepEvery > 1 && po.Interval == 0 {
    59  		return fmt.Errorf("invalid 'Interval' when pruning: %d", po.Interval)
    60  	}
    61  
    62  	return nil
    63  }
    64  
    65  func NewPruningOptionsFromString(strategy string) PruningOptions {
    66  	switch strategy {
    67  	case PruningOptionEverything:
    68  		return PruneEverything
    69  
    70  	case PruningOptionNothing:
    71  		return PruneNothing
    72  
    73  	case PruningOptionDefault:
    74  		return PruneDefault
    75  
    76  	default:
    77  		return PruneDefault
    78  	}
    79  }