Skip to content

Commit a9d9168

Browse files
authored
Merge pull request #6183 from oasisprotocol/peternose/trivial/skip-prune-heights
go/consensus/cometbft/abci/prune: Lazy read earliest version
2 parents 5edb8b2 + 9d7ae87 commit a9d9168

File tree

3 files changed

+13
-29
lines changed

3 files changed

+13
-29
lines changed

.changelog/6182.trivial.md

Whitespace-only changes.

go/consensus/cometbft/abci/prune.go

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ type StatePruner interface {
8383
GetLastRetainedVersion() uint64
8484
}
8585

86-
type statePrunerInitializer interface {
87-
Initialize() error
88-
}
89-
9086
type nonePruner struct{}
9187

9288
func (p *nonePruner) Prune(uint64) error {
@@ -114,15 +110,6 @@ type genericPruner struct {
114110
handlers []consensus.StatePruneHandler
115111
}
116112

117-
func (p *genericPruner) Initialize() error {
118-
// Figure out the eldest version currently present in the tree.
119-
p.earliestVersion = p.ndb.GetEarliestVersion()
120-
// Initially, the earliest version is the last retained version.
121-
p.lastRetainedVersion = p.earliestVersion
122-
123-
return nil
124-
}
125-
126113
func (p *genericPruner) GetLastRetainedVersion() uint64 {
127114
p.Lock()
128115
defer p.Unlock()
@@ -133,6 +120,15 @@ func (p *genericPruner) Prune(latestVersion uint64) error {
133120
if latestVersion < p.keepN {
134121
return nil
135122
}
123+
if p.earliestVersion == 0 {
124+
// Figure out the eldest version currently present in the tree.
125+
p.earliestVersion = p.ndb.GetEarliestVersion()
126+
// Initially, the earliest version is the last retained version.
127+
p.lastRetainedVersion = p.earliestVersion
128+
}
129+
if p.earliestVersion == 0 {
130+
return nil
131+
}
136132

137133
p.logger.Debug("Prune: Start",
138134
"latest_version", latestVersion,
@@ -231,13 +227,7 @@ func newStatePruner(cfg *PruneConfig, ndb nodedb.NodeDB) (StatePruner, error) {
231227
return nil, fmt.Errorf("abci/pruner: unsupported pruning strategy: %v", cfg.Strategy)
232228
}
233229

234-
if initializer, ok := statePruner.(statePrunerInitializer); ok {
235-
if err := initializer.Initialize(); err != nil {
236-
return nil, err
237-
}
238-
}
239-
240-
logger.Debug("ABCI state pruner initialized",
230+
logger.Debug("ABCI state pruner created",
241231
"strategy", cfg.Strategy,
242232
"num_kept", cfg.NumKept,
243233
)

go/consensus/cometbft/abci/prune_test.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,20 @@ func TestPruneKeepN(t *testing.T) {
4545
require.NoError(err, "Finalize")
4646
}
4747

48-
earliestVersion := ndb.GetEarliestVersion()
49-
require.EqualValues(1, earliestVersion, "earliest version should be correct")
50-
latestVersion, exists := ndb.GetLatestVersion()
51-
require.EqualValues(11, latestVersion, "latest version should be correct")
52-
require.True(exists, "latest version should exist")
53-
5448
pruner, err := newStatePruner(&PruneConfig{
5549
Strategy: PruneKeepN,
5650
NumKept: 2,
5751
}, ndb)
5852
require.NoError(err, "newStatePruner failed")
5953

60-
earliestVersion = ndb.GetEarliestVersion()
54+
earliestVersion := ndb.GetEarliestVersion()
6155
require.EqualValues(1, earliestVersion, "earliest version should be correct")
62-
latestVersion, exists = ndb.GetLatestVersion()
56+
latestVersion, exists := ndb.GetLatestVersion()
6357
require.EqualValues(11, latestVersion, "latest version should be correct")
6458
require.True(exists, "latest version should exist")
6559

6660
lastRetainedVersion := pruner.GetLastRetainedVersion()
67-
require.EqualValues(1, lastRetainedVersion, "last retained version should be correct")
61+
require.Zero(lastRetainedVersion, "last retained version should not be set")
6862

6963
err = pruner.Prune(11)
7064
require.NoError(err, "Prune")

0 commit comments

Comments
 (0)