Skip to content

Commit

Permalink
test: redesign tx injector
Browse files Browse the repository at this point in the history
  • Loading branch information
tzdybal committed Feb 18, 2025
1 parent d48c9dc commit bd2b4ce
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 25 deletions.
16 changes: 14 additions & 2 deletions test/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package test

import (
"bytes"
"crypto/rand"

"context"
"crypto/sha512"
Expand Down Expand Up @@ -44,9 +45,20 @@ func (e *DummyExecutor) GetTxs(context.Context) ([]types.Tx, error) {
return txs, nil
}

// InjectTx adds a transaction to the internal list of injected transactions in the DummyExecutor instance.
func (e *DummyExecutor) InjectTx(tx types.Tx) {
// InjectRandomTx adds a transaction to the internal list of injected transactions in the DummyExecutor instance.
func (e *DummyExecutor) InjectRandomTx() types.Tx {
tx := types.Tx(mustGetRandomBytes(100))
e.injectedTxs = append(e.injectedTxs, tx)
return tx
}

func mustGetRandomBytes(n int) []byte {
b := make([]byte, n)
_, err := rand.Read(b)
if err != nil {
panic(fmt.Errorf("failed to generate random bytes: %w", err))
}
return b
}

// ExecuteTxs simulate execution of transactions.
Expand Down
6 changes: 2 additions & 4 deletions test/dummy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ func TestDummySuite(t *testing.T) {

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)
tx1 := exec.InjectRandomTx()
tx2 := exec.InjectRandomTx()

// first execution of GetTxs - nothing special
txs, err := exec.GetTxs(context.Background())
Expand Down
40 changes: 21 additions & 19 deletions test/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type ExecutorSuite struct {

// TxInjector provides an interface for injecting transactions into a test suite.
type TxInjector interface {
InjectTx(tx types.Tx)
InjectRandomTx() types.Tx
}

// TestInitChain tests InitChain method.
Expand All @@ -38,11 +38,8 @@ func (s *ExecutorSuite) TestInitChain() {
func (s *ExecutorSuite) TestGetTxs() {
s.skipIfInjectorNotSet()

tx1 := types.Tx("tx1")
tx2 := types.Tx("tx2")

s.TxInjector.InjectTx(tx1)
s.TxInjector.InjectTx(tx2)
tx1 := s.TxInjector.InjectRandomTx()
tx2 := s.TxInjector.InjectRandomTx()
txs, err := s.Exec.GetTxs(context.TODO())
s.Require().NoError(err)
s.Require().Len(txs, 2)
Expand All @@ -58,14 +55,15 @@ func (s *ExecutorSuite) skipIfInjectorNotSet() {

// TestExecuteTxs tests ExecuteTxs method.
func (s *ExecutorSuite) TestExecuteTxs() {
txs := []types.Tx{[]byte("tx1"), []byte("tx2")}
blockHeight := uint64(1)
timestamp := time.Now().UTC()
prevStateRoot := types.Hash{1, 2, 3}
s.skipIfInjectorNotSet()
txs := []types.Tx{s.TxInjector.InjectRandomTx(), s.TxInjector.InjectRandomTx()}

genesisTime, initialHeight, genesisStateRoot, _ := s.initChain(context.TODO())

stateRoot, maxBytes, err := s.Exec.ExecuteTxs(context.TODO(), txs, blockHeight, timestamp, prevStateRoot)
stateRoot, maxBytes, err := s.Exec.ExecuteTxs(context.TODO(), txs, initialHeight+1, genesisTime.Add(time.Second), genesisStateRoot)
s.Require().NoError(err)
s.NotEqual(types.Hash{}, stateRoot)
s.NotEqual(genesisStateRoot, stateRoot)
s.Greater(maxBytes, uint64(0))
}

Expand All @@ -75,6 +73,7 @@ func (s *ExecutorSuite) TestSetFinal() {
err := s.Exec.SetFinal(context.TODO(), 1)
s.Require().Error(err)

_, _, _, _ = s.initChain(context.TODO())
_, _, err = s.Exec.ExecuteTxs(context.TODO(), nil, 2, time.Now(), types.Hash("test state"))
s.Require().NoError(err)
err = s.Exec.SetFinal(context.TODO(), 2)
Expand All @@ -83,16 +82,9 @@ func (s *ExecutorSuite) TestSetFinal() {

// TestMultipleBlocks is a basic test ensuring that all API methods used together can be used to produce multiple blocks.
func (s *ExecutorSuite) TestMultipleBlocks() {
genesisTime := time.Now().UTC()
initialHeight := uint64(1)
chainID := "test-chain"
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

stateRoot, maxBytes, err := s.Exec.InitChain(ctx, genesisTime, initialHeight, chainID)
s.Require().NoError(err)
s.NotEqual(types.Hash{}, stateRoot)
s.Greater(maxBytes, uint64(0))
genesisTime, initialHeight, stateRoot, maxBytes := s.initChain(ctx)

for i := initialHeight; i <= 10; i++ {
txs, err := s.Exec.GetTxs(ctx)
Expand All @@ -107,3 +99,13 @@ func (s *ExecutorSuite) TestMultipleBlocks() {
s.Require().NoError(err)
}
}

func (s *ExecutorSuite) initChain(ctx context.Context) (time.Time, uint64, types.Hash, uint64) {
genesisTime := time.Now().UTC()
initialHeight := uint64(1)
chainID := "test-chain"

stateRoot, maxBytes, err := s.Exec.InitChain(ctx, genesisTime, initialHeight, chainID)
s.Require().NoError(err)
return genesisTime, initialHeight, stateRoot, maxBytes
}

0 comments on commit bd2b4ce

Please sign in to comment.