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

Improve tests for batched transactions #239

Merged
merged 4 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
53 changes: 33 additions & 20 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 @@ -42,40 +47,48 @@ func TestWeb3_E2E(t *testing.T) {
})

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")
m-Peter marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
tx2, err := cadence.NewString("f885018082c3509499466ed2e37b892a2ee3e9cd55a98b68f5735db280a4c6888fa10000000000000000000000000000000000000000000000000000000000000006820530a03547bcd56e6c6103e78c8c3b34f480108f66ad37282d887033b8c5951f0c70a0a00f5136f6033244a265e1ebaf48cf83d4fdf13f53b468d8fd924c9deb1537dd8d")
if err != nil {
return err
// 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")

// 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)

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)
})
})
}
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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using const for latestHeight as it is assigned only once.

- let latestHeight = await web3.eth.getBlockNumber()
+ const latestHeight = await web3.eth.getBlockNumber()

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
let latestHeight = await web3.eth.getBlockNumber()
const latestHeight = await web3.eth.getBlockNumber()

let block = await web3.eth.getBlock(latestHeight)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using const for block as it is assigned only once.

- let block = await web3.eth.getBlock(latestHeight)
+ const block = await web3.eth.getBlock(latestHeight)

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
let block = await web3.eth.getBlock(latestHeight)
const 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using const for batchSize as it is assigned only once.

- let batchSize = 10
+ const batchSize = 10

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
let batchSize = 10
const 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using const for tx as it is assigned only once.

- let tx = await web3.eth.getTransactionFromBlock(latestHeight, i)
+ const tx = await web3.eth.getTransactionFromBlock(latestHeight, i)

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
let tx = await web3.eth.getTransactionFromBlock(latestHeight, i)
const 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")
}
})
Loading