Skip to content
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

fix: remove txs after execution in DummyExecutor #48

Merged
merged 2 commits into from
Jan 15, 2025
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
11 changes: 10 additions & 1 deletion test/dummy.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package test

import (
"bytes"

"context"
"crypto/sha512"
"fmt"
"slices"
"time"

"github.com/rollkit/go-execution/types"
Expand Down Expand Up @@ -38,7 +41,6 @@ func (e *DummyExecutor) InitChain(ctx context.Context, genesisTime time.Time, in
// GetTxs returns the list of transactions (types.Tx) within the DummyExecutor instance and an error if any.
func (e *DummyExecutor) GetTxs(context.Context) ([]types.Tx, error) {
txs := e.injectedTxs
e.injectedTxs = nil
return txs, nil
}

Expand All @@ -56,6 +58,7 @@ func (e *DummyExecutor) ExecuteTxs(ctx context.Context, txs []types.Tx, blockHei
}
pending := hash.Sum(nil)
e.pendingRoots[blockHeight] = pending
e.removeExecutedTxs(txs)
return pending, e.maxBytes, nil
}

Expand All @@ -68,3 +71,9 @@ func (e *DummyExecutor) SetFinal(ctx context.Context, blockHeight uint64) error
}
return fmt.Errorf("cannot set finalized block at height %d", blockHeight)
}

func (e *DummyExecutor) removeExecutedTxs(txs []types.Tx) {
e.injectedTxs = slices.DeleteFunc(e.injectedTxs, func(tx types.Tx) bool {
return slices.ContainsFunc(txs, func(t types.Tx) bool { return bytes.Equal(tx, t) })
})
}
39 changes: 39 additions & 0 deletions test/dummy_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package test

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

"github.com/rollkit/go-execution/types"
)

type DummyTestSuite struct {
Expand All @@ -17,3 +22,37 @@ func (s *DummyTestSuite) SetupTest() {
func TestDummySuite(t *testing.T) {
suite.Run(t, new(DummyTestSuite))
}

func TestTxRemoval(t *testing.T) {
exec := NewDummyExecutor()
tx1 := types.Tx([]byte{1, 2, 3})
tx2 := types.Tx([]byte{3, 2, 1})

exec.InjectTx(tx1)
exec.InjectTx(tx2)

// first execution of GetTxs - nothing special
txs, err := exec.GetTxs(context.Background())
require.NoError(t, err)
require.Len(t, txs, 2)
require.Contains(t, txs, tx1)
require.Contains(t, txs, tx2)

// ExecuteTxs was not called, so 2 txs should still be returned
txs, err = exec.GetTxs(context.Background())
require.NoError(t, err)
require.Len(t, txs, 2)
require.Contains(t, txs, tx1)
require.Contains(t, txs, tx2)

state, _, err := exec.ExecuteTxs(context.Background(), []types.Tx{tx1}, 1, time.Now(), nil)
require.NoError(t, err)
require.NotEmpty(t, state)

// ExecuteTxs was called, 1 tx remaining in mempool
txs, err = exec.GetTxs(context.Background())
require.NoError(t, err)
require.Len(t, txs, 1)
require.NotContains(t, txs, tx1)
require.Contains(t, txs, tx2)
}
Loading