Skip to content

Commit

Permalink
test: refactor transaction injection interface and update tests
Browse files Browse the repository at this point in the history
Refactored `TxInjector` to replace `InjectRandomTx` with `GetRandomTxs` and `InjectTxs` for better flexibility and clarity. Updated related test cases to reflect the new interface, ensuring consistency and enhanced test coverage.
  • Loading branch information
tzdybal committed Feb 27, 2025
1 parent d24bbf4 commit a0fa37e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 32 deletions.
19 changes: 13 additions & 6 deletions test/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,23 @@ func (e *DummyExecutor) GetTxs(context.Context) ([]types.Tx, error) {
return txs, nil
}

// InjectRandomTx adds a transaction to the internal list of injected transactions in the DummyExecutor instance.
func (e *DummyExecutor) InjectRandomTx() types.Tx {
// GetRandomTxs generates a slice of n random transactions (types.Tx), each containing 100 random bytes.
func (e *DummyExecutor) GetRandomTxs(n int) []types.Tx {
txs := make([]types.Tx, n)
for i := 0; i < n; i++ {
txs[i] = mustGetRandomBytes(100)
}
return txs
}

// InjectTxs adds a slice of transactions to the internal list of injected transactions in a thread-safe manner.
func (e *DummyExecutor) InjectTxs(txs []types.Tx) error {
e.mu.Lock()
defer e.mu.Unlock()

tx := types.Tx(mustGetRandomBytes(100))
e.injectedTxs = append(e.injectedTxs, tx)
return tx
e.injectedTxs = append(e.injectedTxs, txs...)
return nil
}

func mustGetRandomBytes(n int) []byte {
b := make([]byte, n)
_, err := rand.Read(b)
Expand Down
18 changes: 12 additions & 6 deletions test/dummy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,22 @@ func TestDummySuite(t *testing.T) {
func TestTxRemoval(t *testing.T) {
exec := NewDummyExecutor()

tx1 := exec.InjectRandomTx()
tx2 := exec.InjectRandomTx()
// Generate random transactions using GetRandomTxs
randomTxs := exec.GetRandomTxs(2)

// first execution of GetTxs - nothing special
// Inject the random transactions into the executor
err := exec.InjectTxs(randomTxs)
require.NoError(t, err)

tx1 := randomTxs[0]
tx2 := randomTxs[1]

// Retrieve transactions and verify
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)

require.Contains(t, txs, randomTxs[0])
require.Contains(t, txs, randomTxs[1])
// ExecuteTxs was not called, so 2 txs should still be returned
txs, err = exec.GetTxs(context.Background())
require.NoError(t, err)
Expand Down
52 changes: 32 additions & 20 deletions test/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ type ExecutorSuite struct {

// TxInjector provides an interface for injecting transactions into a test suite.
type TxInjector interface {
InjectRandomTx() types.Tx
GetRandomTxs(n int) []types.Tx
InjectTxs(tx []types.Tx) error
}

// TestInitChain tests InitChain method.
Expand Down Expand Up @@ -52,13 +53,16 @@ func (s *ExecutorSuite) TestGetTxs() {
s.Require().Empty(txs)

// inject two txs and retrieve them
tx1 := s.TxInjector.InjectRandomTx()
tx2 := s.TxInjector.InjectRandomTx()
txs, err = s.Exec.GetTxs(ctx)
// inject two txs and retrieve them using GetRandomTxs and InjectTxs
randomTxs := s.TxInjector.GetRandomTxs(2) // Retrieve 2 random transactions
err = s.TxInjector.InjectTxs(randomTxs) // Inject the transactions into the state
s.Require().NoError(err)

txs, err = s.Exec.GetTxs(ctx) // Retrieve transactions from the executor
s.Require().NoError(err)
s.Require().Len(txs, 2)
s.Require().Contains(txs, tx1)
s.Require().Contains(txs, tx2)
s.Require().Contains(txs, randomTxs[0])
s.Require().Contains(txs, randomTxs[1])
}

func (s *ExecutorSuite) skipIfInjectorNotSet() {
Expand All @@ -77,34 +81,41 @@ func (s *ExecutorSuite) TestExecuteTxs() {
stateRootChanged bool
}{
{
name: "nil txs",
txs: nil,
name: "empty txs",
txs: []types.Tx{},
stateRootChanged: false,
},
{
name: "empty txs",
txs: []types.Tx{},
name: "nil txs",
txs: nil,
stateRootChanged: false,
},
{
name: "two txs",
txs: []types.Tx{s.TxInjector.InjectRandomTx(), s.TxInjector.InjectRandomTx()},
txs: s.TxInjector.GetRandomTxs(2),
stateRootChanged: true,
},
}

for i, c := range cases {
s.Run(c.name, func() {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

genesisTime, genesisStateRoot, _ := s.initChain(ctx, uint64(i+1))
genesisTime, lastStateRoot, _ := s.initChain(ctx, uint64(1))

stateRoot, maxBytes, err := s.Exec.ExecuteTxs(ctx, c.txs, uint64(1), genesisTime.Add(time.Second), genesisStateRoot)
for i, c := range cases {
s.Run(c.name, func() {
err := s.TxInjector.InjectTxs(c.txs)
s.Require().NoError(err)
txs, _ := s.Exec.GetTxs(ctx)
fmt.Println(len(txs))
stateRoot, maxBytes, err := s.Exec.ExecuteTxs(ctx, c.txs, uint64(i+1), genesisTime.Add(time.Duration(i)*time.Second), lastStateRoot)
s.Require().NoError(err)
s.Require().NotEmpty(stateRoot)
s.Require().NotEqual(c.stateRootChanged, bytes.Equal(genesisStateRoot, stateRoot))
s.Assert().Equal(c.stateRootChanged, !bytes.Equal(lastStateRoot, stateRoot))
s.Require().Greater(maxBytes, uint64(0))
lastStateRoot = stateRoot
s.Exec.SetFinal(ctx, uint64(i+1))
time.Sleep(3 * time.Second)
})
}
}
Expand Down Expand Up @@ -134,15 +145,16 @@ func (s *ExecutorSuite) TestMultipleBlocks() {
genesisTime, prevStateRoot, _ := s.initChain(ctx, initialHeight)

for i := initialHeight; i <= 10; i++ {
s.TxInjector.InjectRandomTx()
err := s.TxInjector.InjectTxs(s.TxInjector.GetRandomTxs(2))
s.Require().NoError(err)
txs, err := s.Exec.GetTxs(ctx)
s.Require().NoError(err)

blockTime := genesisTime.Add(time.Duration(i+1) * time.Second) //nolint:gosec
stateRoot, maxBytes, err := s.Exec.ExecuteTxs(ctx, txs, i, blockTime, prevStateRoot)
s.Require().NoError(err)
s.Require().NotZero(maxBytes)
s.Require().NotEqual(prevStateRoot, stateRoot)
s.Assert().NotEqual(prevStateRoot, stateRoot)

prevStateRoot = stateRoot

Expand Down

0 comments on commit a0fa37e

Please sign in to comment.