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

test: improve tx injector and common test suite #61

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 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
6 changes: 2 additions & 4 deletions proxy/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ func (c *Client) InitChain(ctx context.Context, genesisTime time.Time, initialHe
return types.Hash{}, 0, err
}

var stateRoot types.Hash
copy(stateRoot[:], resp.StateRoot)
stateRoot := copyHash(resp.StateRoot)

return stateRoot, resp.MaxBytes, nil
}
Expand Down Expand Up @@ -99,8 +98,7 @@ func (c *Client) ExecuteTxs(ctx context.Context, txs []types.Tx, blockHeight uin
return types.Hash{}, 0, err
}

var updatedStateRoot types.Hash
copy(updatedStateRoot[:], resp.UpdatedStateRoot)
updatedStateRoot := copyHash(resp.UpdatedStateRoot)

return updatedStateRoot, resp.MaxBytes, nil
}
Expand Down
12 changes: 6 additions & 6 deletions proxy/grpc/client_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ func TestClientServer(t *testing.T) {
chainID := "test-chain"

// initialize a new Hash with a fixed size
expectedStateRoot := make([]byte, 32)
expectedStateRoot := types.Hash(make([]byte, 32))
copy(expectedStateRoot, []byte{1, 2, 3})
var stateRootHash types.Hash
copy(stateRootHash[:], expectedStateRoot)

expectedMaxBytes := uint64(1000000)

Expand All @@ -68,12 +66,14 @@ func TestClientServer(t *testing.T) {
expectedTime := time.Unix(unixTime, 0).UTC()

mockExec.On("InitChain", mock.Anything, expectedTime, initialHeight, chainID).
Return(stateRootHash, expectedMaxBytes, nil).Once()
Return(expectedStateRoot, expectedMaxBytes, nil).Once()

stateRoot, maxBytes, err := client.InitChain(context.TODO(), genesisTime, initialHeight, chainID)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
stateRoot, maxBytes, err := client.InitChain(ctx, genesisTime, initialHeight, chainID)

require.NoError(t, err)
assert.Equal(t, stateRootHash, stateRoot)
assert.Equal(t, expectedStateRoot, stateRoot)
assert.Equal(t, expectedMaxBytes, maxBytes)
mockExec.AssertExpectations(t)
})
Expand Down
3 changes: 1 addition & 2 deletions proxy/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ func (s *Server) ExecuteTxs(ctx context.Context, req *pb.ExecuteTxsRequest) (*pb
txs[i] = tx
}

var prevStateRoot types.Hash
copy(prevStateRoot[:], req.PrevStateRoot)
prevStateRoot := copyHash(req.PrevStateRoot)

updatedStateRoot, maxBytes, err := s.exec.ExecuteTxs(
ctx,
Expand Down
9 changes: 9 additions & 0 deletions proxy/grpc/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package grpc

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

func copyHash(src []byte) types.Hash {
dst := make([]byte, len(src))
copy(dst, src)
return dst
}
17 changes: 15 additions & 2 deletions test/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package test

import (
"bytes"
"crypto/rand"

"context"
"crypto/sha512"
"fmt"
Expand Down Expand Up @@ -52,12 +54,23 @@ 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 {
e.mu.Lock()
defer e.mu.Unlock()

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
72 changes: 46 additions & 26 deletions test/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
TxInjector TxInjector
}

const maxTestDuration = 3 * time.Second

// 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 @@ -28,7 +30,10 @@
initialHeight := uint64(1)
chainID := "test-chain"

stateRoot, maxBytes, err := s.Exec.InitChain(context.TODO(), genesisTime, initialHeight, chainID)
ctx, cancel := context.WithTimeout(context.Background(), maxTestDuration)
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))
Expand All @@ -38,12 +43,12 @@
func (s *ExecutorSuite) TestGetTxs() {
s.skipIfInjectorNotSet()

tx1 := types.Tx("tx1")
tx2 := types.Tx("tx2")
ctx, cancel := context.WithTimeout(context.Background(), maxTestDuration)
defer cancel()

s.TxInjector.InjectTx(tx1)
s.TxInjector.InjectTx(tx2)
txs, err := s.Exec.GetTxs(context.TODO())
tx1 := s.TxInjector.InjectRandomTx()
tx2 := s.TxInjector.InjectRandomTx()
txs, err := s.Exec.GetTxs(ctx)
s.Require().NoError(err)
s.Require().Len(txs, 2)
s.Require().Contains(txs, tx1)
Expand All @@ -58,41 +63,46 @@

// 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()}
initialHeight := uint64(1)

stateRoot, maxBytes, err := s.Exec.ExecuteTxs(context.TODO(), txs, blockHeight, timestamp, prevStateRoot)
ctx, cancel := context.WithTimeout(context.Background(), maxTestDuration)
defer cancel()

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

stateRoot, maxBytes, err := s.Exec.ExecuteTxs(ctx, txs, initialHeight, genesisTime.Add(time.Second), genesisStateRoot)
s.Require().NoError(err)
s.NotEqual(types.Hash{}, stateRoot)
s.Greater(maxBytes, uint64(0))
s.Require().NotEmpty(stateRoot)
s.Require().NotEqualValues(genesisStateRoot, stateRoot)
s.Require().Greater(maxBytes, uint64(0))
}

// TestSetFinal tests SetFinal method.
func (s *ExecutorSuite) TestSetFinal() {
ctx, cancel := context.WithTimeout(context.Background(), maxTestDuration)
defer cancel()

// finalizing invalid height must return error
err := s.Exec.SetFinal(context.TODO(), 1)
err := s.Exec.SetFinal(ctx, 7)
s.Require().Error(err)

_, _, err = s.Exec.ExecuteTxs(context.TODO(), nil, 2, time.Now(), types.Hash("test state"))
initialHeight := uint64(1)
_, stateRoot, _ := s.initChain(ctx, initialHeight)
_, _, err = s.Exec.ExecuteTxs(ctx, nil, initialHeight, time.Now(), stateRoot)
s.Require().NoError(err)
err = s.Exec.SetFinal(context.TODO(), 2)
err = s.Exec.SetFinal(ctx, initialHeight)
s.Require().NoError(err)
}

// 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)
ctx, cancel := context.WithTimeout(context.Background(), maxTestDuration)
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))
initialHeight := uint64(1)
genesisTime, stateRoot, maxBytes := s.initChain(ctx, initialHeight)

Check failure on line 105 in test/suite.go

View workflow job for this annotation

GitHub Actions / lint / golangci-lint

ineffectual assignment to maxBytes (ineffassign)

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

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

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