From 7408548bd328611e17581ae0df2aaa41d12456ed Mon Sep 17 00:00:00 2001 From: sideninja <75445744+sideninja@users.noreply.github.com> Date: Thu, 2 May 2024 20:16:33 +0200 Subject: [PATCH 1/4] update tests to value transfers --- tests/e2e_web3js_test.go | 53 +++++++++++++++--------- tests/helpers.go | 5 +-- tests/web3js/eth_batch_retrieval_test.js | 31 +++++++------- 3 files changed, 52 insertions(+), 37 deletions(-) diff --git a/tests/e2e_web3js_test.go b/tests/e2e_web3js_test.go index 0efa1f3f..add0183d 100644 --- a/tests/e2e_web3js_test.go +++ b/tests/e2e_web3js_test.go @@ -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) { @@ -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") - 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) }) }) } diff --git a/tests/helpers.go b/tests/helpers.go index 980a289e..dbb25bb9 100644 --- a/tests/helpers.go +++ b/tests/helpers.go @@ -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() } diff --git a/tests/web3js/eth_batch_retrieval_test.js b/tests/web3js/eth_batch_retrieval_test.js index 1235fa06..6151e6c3 100644 --- a/tests/web3js/eth_batch_retrieval_test.js +++ b/tests/web3js/eth_batch_retrieval_test.js @@ -3,20 +3,23 @@ const conf = require('./config') const web3 = conf.web3 -it('retrieve batch transactions', async() => { - let latestHeight = await web3.eth.getBlockNumber() - let block = await web3.eth.getBlock(latestHeight) - assert.lengthOf(block.transactions, 2) +it('retrieve batch transactions', async () => { + // this test relies on the setup of batched transactions found in ../e2e_web3js_test.go - 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 latestHeight = await web3.eth.getBlockNumber() + let block = await web3.eth.getBlock(latestHeight) - 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) + let batchSize = 10 + assert.lengthOf(block.transactions, batchSize) + + for (let i = 0; i < block.transactions.length; i++) { + let tx = await web3.eth.getTransactionFromBlock(latestHeight, block.number) + console.log(tx) + console.log("-----") + 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") + } }) From 608059ef76d35914065565351f98ea2e98751bb2 Mon Sep 17 00:00:00 2001 From: Ardit Marku Date: Thu, 30 May 2024 14:33:12 +0300 Subject: [PATCH 2/4] Update tests for batch transactions --- tests/web3js/eth_batch_retrieval_test.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tests/web3js/eth_batch_retrieval_test.js b/tests/web3js/eth_batch_retrieval_test.js index 6151e6c3..84c9ad5d 100644 --- a/tests/web3js/eth_batch_retrieval_test.js +++ b/tests/web3js/eth_batch_retrieval_test.js @@ -2,7 +2,6 @@ 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 @@ -13,13 +12,11 @@ it('retrieve batch transactions', async () => { assert.lengthOf(block.transactions, batchSize) for (let i = 0; i < block.transactions.length; i++) { - let tx = await web3.eth.getTransactionFromBlock(latestHeight, block.number) - console.log(tx) - console.log("-----") + 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.equal(tx.transactionIndex, i, "wrong index") assert.isBelow(i, batchSize, "wrong batch size") } }) From 219de4dc0f01ded276fe6a88524d1377c1778cdf Mon Sep 17 00:00:00 2001 From: Ardit Marku Date: Thu, 30 May 2024 14:50:34 +0300 Subject: [PATCH 3/4] Increase timeout for streaming E2E test --- tests/web3js/eth_streaming_test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/web3js/eth_streaming_test.js b/tests/web3js/eth_streaming_test.js index f0595ea0..7f47b70e 100644 --- a/tests/web3js/eth_streaming_test.js +++ b/tests/web3js/eth_streaming_test.js @@ -103,4 +103,4 @@ it('streaming of logs using filters', async() => { await ws.eth.clearSubscriptions() process.exit(0) -}).timeout(timeout*1000) \ No newline at end of file +}).timeout(timeout*1500) From 190972799f6adf2c564fbeb15e72f1defe47b2d8 Mon Sep 17 00:00:00 2001 From: Ardit Marku Date: Thu, 30 May 2024 14:59:16 +0300 Subject: [PATCH 4/4] Move streaming E2E tests at the end of the test file --- tests/e2e_web3js_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/e2e_web3js_test.go b/tests/e2e_web3js_test.go index add0183d..bab577ea 100644 --- a/tests/e2e_web3js_test.go +++ b/tests/e2e_web3js_test.go @@ -38,14 +38,6 @@ 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("streaming of entities and subscription with filters", func(t *testing.T) { - runWeb3Test(t, "eth_streaming_filters_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) { @@ -91,4 +83,12 @@ func TestWeb3_E2E(t *testing.T) { 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") + }) }