-
Notifications
You must be signed in to change notification settings - Fork 250
refactor(block): allow pruning when da disabled #3075
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cc58a5d
refactor(block): allow pruning when da disabled
julienrbrt fbd665f
cl
julienrbrt 5226977
cleanups
julienrbrt bec01ff
Merge branch 'main' into julien/pruning-2
julienrbrt 72f7c6c
test(pruner): use t.Context() in test functions (#3077)
Copilot d36055b
Merge branch 'main' into julien/pruning-2
julienrbrt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ type Pruner struct { | |
| execPruner coreexecutor.ExecPruner | ||
| cfg config.PruningConfig | ||
| blockTime time.Duration | ||
| daEnabled bool | ||
| logger zerolog.Logger | ||
|
|
||
| // Lifecycle | ||
|
|
@@ -38,12 +39,14 @@ func New( | |
| execPruner coreexecutor.ExecPruner, | ||
| cfg config.PruningConfig, | ||
| blockTime time.Duration, | ||
| daAddress string, | ||
| ) *Pruner { | ||
| return &Pruner{ | ||
| store: store, | ||
| execPruner: execPruner, | ||
| cfg: cfg, | ||
| blockTime: blockTime, | ||
| daEnabled: daAddress != "", // DA is enabled if address is provided | ||
| logger: logger.With().Str("component", "pruner").Logger(), | ||
| } | ||
| } | ||
|
|
@@ -105,22 +108,28 @@ func (p *Pruner) pruneLoop() { | |
|
|
||
| // pruneBlocks prunes blocks and their metadatas. | ||
| func (p *Pruner) pruneBlocks() error { | ||
| var currentDAIncluded uint64 | ||
| currentDAIncludedBz, err := p.store.GetMetadata(p.ctx, store.DAIncludedHeightKey) | ||
| if err == nil && len(currentDAIncludedBz) == 8 { | ||
| currentDAIncluded = binary.LittleEndian.Uint64(currentDAIncludedBz) | ||
| } else { | ||
| // if we cannot get the current DA height, we cannot safely prune, so we skip pruning until we can get it. | ||
| return nil | ||
| } | ||
|
|
||
| storeHeight, err := p.store.Height(p.ctx) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get store height for pruning: %w", err) | ||
| } | ||
|
|
||
| // Never prune blocks that are not DA included | ||
| upperBound := min(storeHeight, currentDAIncluded) | ||
| upperBound := storeHeight | ||
|
|
||
| // If DA is enabled, only prune blocks that are DA included | ||
| if p.daEnabled { | ||
| var currentDAIncluded uint64 | ||
| currentDAIncludedBz, err := p.store.GetMetadata(p.ctx, store.DAIncludedHeightKey) | ||
| if err == nil && len(currentDAIncludedBz) == 8 { | ||
| currentDAIncluded = binary.LittleEndian.Uint64(currentDAIncludedBz) | ||
| } else { | ||
| p.logger.Debug().Msg("skipping pruning: DA is enabled but DA included height is not available yet") | ||
| return nil | ||
| } | ||
|
|
||
| // Never prune blocks that are not DA included | ||
| upperBound = min(storeHeight, currentDAIncluded) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| } | ||
|
|
||
| if upperBound <= p.cfg.KeepRecent { | ||
| // Not enough fully included blocks to prune | ||
| return nil | ||
|
|
@@ -149,7 +158,7 @@ func (p *Pruner) pruneBlocks() error { | |
| } | ||
| } | ||
|
|
||
| p.logger.Debug().Uint64("pruned_up_to_height", batchEnd).Msg("pruned blocks up to height") | ||
| p.logger.Debug().Uint64("pruned_up_to_height", batchEnd).Bool("da_enabled", p.daEnabled).Msg("pruned blocks up to height") | ||
| return nil | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,7 +32,7 @@ func (e *execMetaAdapter) PruneExec(ctx context.Context, height uint64) error { | |
| func TestPrunerPruneMetadata(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| ctx := context.Background() | ||
| ctx := t.Context() | ||
| kv := dssync.MutexWrap(ds.NewMapDatastore()) | ||
| stateStore := store.New(kv) | ||
|
|
||
|
|
@@ -51,7 +51,7 @@ func TestPrunerPruneMetadata(t *testing.T) { | |
| KeepRecent: 1, | ||
| } | ||
|
|
||
| pruner := New(zerolog.New(zerolog.NewTestWriter(t)), stateStore, execAdapter, cfg, 100*time.Millisecond) | ||
| pruner := New(zerolog.New(zerolog.NewTestWriter(t)), stateStore, execAdapter, cfg, 100*time.Millisecond, "") // Empty DA address | ||
| require.NoError(t, pruner.pruneMetadata()) | ||
|
|
||
| _, err := stateStore.GetStateAtHeight(ctx, 1) | ||
|
|
@@ -63,3 +63,103 @@ func TestPrunerPruneMetadata(t *testing.T) { | |
| _, exists := execAdapter.existing[1] | ||
| require.False(t, exists) | ||
| } | ||
|
|
||
| func TestPrunerPruneBlocksWithoutDA(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| ctx := t.Context() | ||
| kv := dssync.MutexWrap(ds.NewMapDatastore()) | ||
| stateStore := store.New(kv) | ||
|
|
||
| // Create blocks without setting DAIncludedHeightKey (simulating node without DA) | ||
| for height := uint64(1); height <= 100; height++ { | ||
| header := &types.SignedHeader{Header: types.Header{BaseHeader: types.BaseHeader{Height: height}}} | ||
| data := &types.Data{} | ||
| sig := types.Signature([]byte{byte(height)}) | ||
|
|
||
| batch, err := stateStore.NewBatch(ctx) | ||
| require.NoError(t, err) | ||
| require.NoError(t, batch.SaveBlockData(header, data, &sig)) | ||
| require.NoError(t, batch.SetHeight(height)) | ||
| require.NoError(t, batch.UpdateState(types.State{LastBlockHeight: height})) | ||
| require.NoError(t, batch.Commit()) | ||
| } | ||
|
|
||
| execAdapter := &execMetaAdapter{existing: make(map[uint64]struct{})} | ||
| for h := uint64(1); h <= 100; h++ { | ||
| execAdapter.existing[h] = struct{}{} | ||
| } | ||
|
|
||
| // Test with empty DA address (DA disabled) - should prune successfully | ||
| cfg := config.PruningConfig{ | ||
| Mode: config.PruningModeAll, | ||
| Interval: config.DurationWrapper{Duration: 1 * time.Second}, | ||
| KeepRecent: 10, | ||
| } | ||
|
|
||
| pruner := New(zerolog.New(zerolog.NewTestWriter(t)), stateStore, execAdapter, cfg, 100*time.Millisecond, "") // Empty DA address = DA disabled | ||
| require.NoError(t, pruner.pruneBlocks()) | ||
|
|
||
| // Verify blocks were pruned (batch size is 40 blocks: 1s interval / 100ms block time * 4) | ||
| // So we expect to prune from height 1 up to min(0 + 40, 90) = 40 | ||
| height, err := stateStore.Height(ctx) | ||
| require.NoError(t, err) | ||
| require.Equal(t, uint64(100), height) | ||
|
|
||
| // Verify old blocks were pruned (up to height 40) | ||
| for h := uint64(1); h <= 40; h++ { | ||
| _, _, err := stateStore.GetBlockData(ctx, h) | ||
| require.Error(t, err, "expected block data at height %d to be pruned", h) | ||
| } | ||
|
|
||
| // Verify blocks after batch were kept | ||
| for h := uint64(41); h <= 100; h++ { | ||
| _, _, err := stateStore.GetBlockData(ctx, h) | ||
| require.NoError(t, err, "expected block data at height %d to be kept", h) | ||
| } | ||
|
|
||
| // Verify exec metadata was also pruned (strictly less than 40) | ||
| for h := uint64(1); h < 40; h++ { | ||
| _, exists := execAdapter.existing[h] | ||
| require.False(t, exists, "expected exec metadata at height %d to be pruned", h) | ||
| } | ||
| } | ||
|
|
||
| func TestPrunerPruneBlocksWithDAEnabled(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| ctx := t.Context() | ||
| kv := dssync.MutexWrap(ds.NewMapDatastore()) | ||
| stateStore := store.New(kv) | ||
|
|
||
| // Create blocks without setting DAIncludedHeightKey | ||
| for height := uint64(1); height <= 100; height++ { | ||
| header := &types.SignedHeader{Header: types.Header{BaseHeader: types.BaseHeader{Height: height}}} | ||
| data := &types.Data{} | ||
| sig := types.Signature([]byte{byte(height)}) | ||
|
|
||
| batch, err := stateStore.NewBatch(ctx) | ||
| require.NoError(t, err) | ||
| require.NoError(t, batch.SaveBlockData(header, data, &sig)) | ||
| require.NoError(t, batch.SetHeight(height)) | ||
| require.NoError(t, batch.UpdateState(types.State{LastBlockHeight: height})) | ||
| require.NoError(t, batch.Commit()) | ||
| } | ||
|
|
||
| // Test with DA address provided (DA enabled) - should skip pruning when DA height is not available | ||
| cfg := config.PruningConfig{ | ||
| Mode: config.PruningModeAll, | ||
| Interval: config.DurationWrapper{Duration: 1 * time.Second}, | ||
| KeepRecent: 10, | ||
| } | ||
|
|
||
| pruner := New(zerolog.New(zerolog.NewTestWriter(t)), stateStore, nil, cfg, 100*time.Millisecond, "localhost:1234") // DA enabled | ||
| // Should return nil (skip pruning) since DA height is not available | ||
| require.NoError(t, pruner.pruneBlocks()) | ||
|
|
||
| // Verify no blocks were pruned (all blocks should still be retrievable) | ||
| for h := uint64(1); h <= 100; h++ { | ||
| _, _, err := stateStore.GetBlockData(ctx, h) | ||
| require.NoError(t, err, "expected block data at height %d to still exist (no pruning should have happened)", h) | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it would be good to also test store height < da incl height for regression |
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This changelog entry appears to be from a different pull request. This PR's changes are about allowing pruning when the DA layer is disabled. Please update the changelog to reflect the correct changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nope, the changelog is unrelated to this pr (that do not require any)