Skip to content

Commit

Permalink
Merge pull request #239 from onflow/gregor/batched-transactions-testing
Browse files Browse the repository at this point in the history
Improve tests for batched transactions
  • Loading branch information
m-Peter authored May 30, 2024
2 parents 0d5c333 + 1909727 commit 86dc85e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 45 deletions.
67 changes: 40 additions & 27 deletions tests/e2e_web3js_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package tests

import (
"encoding/hex"
"math/big"
"testing"

"github.com/onflow/cadence"
"github.com/onflow/flow-emulator/emulator"
"github.com/onflow/go-ethereum/common"
"github.com/onflow/go-ethereum/crypto"
"github.com/stretchr/testify/require"
)

func TestWeb3_E2E(t *testing.T) {
Expand Down Expand Up @@ -33,49 +38,57 @@ func TestWeb3_E2E(t *testing.T) {
runWeb3Test(t, "eth_get_filter_logs_test")
})

t.Run("streaming of entities and subscription", func(t *testing.T) {
runWeb3Test(t, "eth_streaming_test")
})
t.Run("batch run transactions", func(t *testing.T) {
// create multiple value transfers and batch run them before the test
runWeb3TestWithSetup(t, "eth_batch_retrieval_test", func(emu emulator.Emulator) {
// crate test accounts
senderKey, err := crypto.HexToECDSA("6a0eb450085e825dd41cc3dd85e4166d4afbb0162488a3d811a0637fa7656abf")
require.NoError(t, err)
receiver := common.HexToAddress("0xd0bA5bc19775c36faD888a9C856baffD6d575482")

t.Run("streaming of entities and subscription with filters", func(t *testing.T) {
runWeb3Test(t, "eth_streaming_filters_test")
})
// create a value transfer transactions
const batchSize = 10
encodedTxs := make([]cadence.Value, batchSize)
for i := range encodedTxs {
transferPayload, _, err := evmSign(big.NewInt(0), 50000, senderKey, uint64(i), &receiver, nil)
require.NoError(t, err)

t.Run("batch run transactions", func(t *testing.T) {
runWeb3TestWithSetup(t, "eth_batch_retrieval_test", func(emu emulator.Emulator) error {
tx1, err := cadence.NewString("f9015880808301e8488080b901086060604052341561000f57600080fd5b60eb8061001d6000396000f300606060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063c6888fa1146044575b600080fd5b3415604e57600080fd5b606260048080359060200190919050506078565b6040518082815260200191505060405180910390f35b60007f24abdb5865df5079dcc5ac590ff6f01d5c16edbc5fab4e195d9febd1114503da600783026040518082815260200191505060405180910390a16007820290509190505600a165627a7a7230582040383f19d9f65246752244189b02f56e8d0980ed44e7a56c0b200458caad20bb002982052fa09c05a7389284dc02b356ec7dee8a023c5efd3a9d844fa3c481882684b0640866a057e96d0a71a857ed509bb2b7333e78b2408574b8cc7f51238f25c58812662653")
if err != nil {
return err
}
tx2, err := cadence.NewString("f885018082c3509499466ed2e37b892a2ee3e9cd55a98b68f5735db280a4c6888fa10000000000000000000000000000000000000000000000000000000000000006820530a03547bcd56e6c6103e78c8c3b34f480108f66ad37282d887033b8c5951f0c70a0a00f5136f6033244a265e1ebaf48cf83d4fdf13f53b468d8fd924c9deb1537dd8d")
if err != nil {
return err
tx, err := cadence.NewString(hex.EncodeToString(transferPayload))
require.NoError(t, err)

encodedTxs[i] = tx
}

res, err := flowSendTransaction(
emu,
`transaction(tx1: String, tx2: String) {
`transaction(encodedTxs: [String]) {
prepare(signer: auth(Storage) &Account) {
let txs: [[UInt8]] = [tx1.decodeHex(), tx2.decodeHex()]
var txs: [[UInt8]] = []
for enc in encodedTxs {
txs.append(enc.decodeHex())
}
let txResults = EVM.batchRun(
txs: txs,
coinbase: EVM.EVMAddress(bytes: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
)
for txResult in txResults {
assert(txResult.status == EVM.Status.successful, message: "failed to execute tx")
assert(txResult.status == EVM.Status.successful, message: "failed to execute tx, error: ".concat(txResult.errorCode.toString()))
}
}
}`,
tx1,
tx2,
cadence.NewArray(encodedTxs),
)
if err != nil {
return err
}
if res.Error != nil {
return res.Error
}
return nil
require.NoError(t, err)
require.NoError(t, res.Error)
})
})

t.Run("streaming of entities and subscription", func(t *testing.T) {
runWeb3Test(t, "eth_streaming_test")
})

t.Run("streaming of entities and subscription with filters", func(t *testing.T) {
runWeb3Test(t, "eth_streaming_filters_test")
})
}
5 changes: 2 additions & 3 deletions tests/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,10 @@ func runWeb3Test(t *testing.T, name string) {
func runWeb3TestWithSetup(
t *testing.T,
name string,
setupFunc func(emu emulator.Emulator) error,
setupFunc func(emu emulator.Emulator),
) {
emu, stop := servicesSetup(t)
err := setupFunc(emu)
require.NoError(t, err)
setupFunc(emu)
executeTest(t, name)
stop()
}
Expand Down
28 changes: 14 additions & 14 deletions tests/web3js/eth_batch_retrieval_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ const { assert } = require('chai')
const conf = require('./config')
const web3 = conf.web3

it('retrieve batch transactions', async () => {
// this test relies on the setup of batched transactions found in ../e2e_web3js_test.go

it('retrieve batch transactions', async() => {
let latestHeight = await web3.eth.getBlockNumber()
let block = await web3.eth.getBlock(latestHeight)
assert.lengthOf(block.transactions, 2)
let latestHeight = await web3.eth.getBlockNumber()
let block = await web3.eth.getBlock(latestHeight)

let deployTx = await web3.eth.getTransactionFromBlock(latestHeight, 0)
assert.equal(block.number, deployTx.blockNumber)
assert.equal(block.hash, deployTx.blockHash)
assert.equal(0, deployTx.type)
assert.equal(0, deployTx.transactionIndex)
let batchSize = 10
assert.lengthOf(block.transactions, batchSize)

let callTx = await web3.eth.getTransactionFromBlock(latestHeight, 1)
assert.equal(block.number, callTx.blockNumber)
assert.equal(block.hash, callTx.blockHash)
assert.equal(0, callTx.type)
assert.equal(1, callTx.transactionIndex)
for (let i = 0; i < block.transactions.length; i++) {
let tx = await web3.eth.getTransactionFromBlock(latestHeight, i)
assert.equal(tx.blockNumber, block.number, "wrong block number")
assert.equal(tx.blockHash, block.hash, "wrong block hash")
assert.equal(tx.type, 0, "wrong type")
assert.equal(tx.transactionIndex, i, "wrong index")
assert.isBelow(i, batchSize, "wrong batch size")
}
})
2 changes: 1 addition & 1 deletion tests/web3js/eth_streaming_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,4 @@ it('streaming of logs using filters', async() => {
await ws.eth.clearSubscriptions()

process.exit(0)
}).timeout(timeout*1000)
}).timeout(timeout*1500)

0 comments on commit 86dc85e

Please sign in to comment.