Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

chore(UT): pruning UT #996

Merged
merged 3 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (m *Manager) applyBlock(block *types.Block, commit *types.Commit, blockMeta

// Prune old heights, if requested by ABCI app.
if 0 < retainHeight {
err = m.pruneBlocks(uint64(retainHeight))
err = m.PruneBlocks(uint64(retainHeight))
if err != nil {
m.logger.Error("prune blocks", "retain_height", retainHeight, "err", err)
}
Expand Down
2 changes: 1 addition & 1 deletion block/pruning.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/dymensionxyz/gerr-cosmos/gerrc"
)

func (m *Manager) pruneBlocks(retainHeight uint64) error {
func (m *Manager) PruneBlocks(retainHeight uint64) error {
if m.IsSequencer() && m.NextHeightToSubmit() < retainHeight { // do not delete anything that we might submit in future
return fmt.Errorf("cannot prune blocks before they have been submitted: retain height %d: next height to submit: %d: %w",
retainHeight,
Expand Down
57 changes: 57 additions & 0 deletions block/pruning_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package block_test

import (
"context"
"testing"

"github.com/dymensionxyz/dymint/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/proxy"
)

func TestPruningRetainHeight(t *testing.T) {
require := require.New(t)
app := testutil.GetAppMock()
ctx := context.Background()
// Create proxy app
clientCreator := proxy.NewLocalClientCreator(app)
proxyApp := proxy.NewAppConns(clientCreator)
err := proxyApp.Start()
require.NoError(err)

manager, err := testutil.GetManager(testutil.GetManagerConfig(), nil, nil, 1, 1, 0, proxyApp, nil)
require.NoError(err)

// Check initial assertions
require.Zero(manager.State.Height())
require.Zero(manager.LastSubmittedHeight.Load())

batchSize := 10

// Produce blocks
for i := 0; i < batchSize; i++ {
_, _, err = manager.ProduceAndGossipBlock(ctx, true)
require.NoError(err)
}
// submit and validate sync target
manager.CreateAndSubmitBatch(100000000)
lastSubmitted := manager.LastSubmittedHeight.Load()
assert.EqualValues(t, manager.State.Height(), lastSubmitted)
assert.Equal(t, lastSubmitted, uint64(batchSize))

// Produce new blocks
for i := 0; i < batchSize; i++ {
_, _, err = manager.ProduceAndGossipBlock(ctx, true)
require.NoError(err)
}

validRetainHeight := lastSubmitted + 1 // the max possible valid retain height
for i := validRetainHeight + 1; i < manager.State.Height(); i++ {
err = manager.PruneBlocks(i)
require.Error(err) // cannot prune blocks before they have been submitted
}

err = manager.PruneBlocks(validRetainHeight)
require.NoError(err)
}
1 change: 1 addition & 0 deletions testutil/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ func GenerateState(initialHeight int64, lastBlockHeight int64) *types.State {
s := &types.State{
ChainID: "test-chain",
InitialHeight: uint64(initialHeight),
BaseHeight: uint64(initialHeight),
AppHash: [32]byte{},
LastResultsHash: GetEmptyLastResultsHash(),
Version: tmstate.Version{
Expand Down
Loading