From c7c1040b56d37f2f45fa5721d3d8947f13170e65 Mon Sep 17 00:00:00 2001 From: Maycon Amaro <131882788+mayconamaroCW@users.noreply.github.com> Date: Mon, 4 Mar 2024 14:22:05 -0300 Subject: [PATCH] fix eth_getCode and add test cases (#303) * test: add eth_call cases * test: add eth_getCode cases * fix: change eth_getCode not found response to 0x --- e2e/test/e2e-json-rpc.test.ts | 5 ++++ e2e/test/e2e-tx-serial-contract.test.ts | 32 +++++++++++++++++++++++++ src/eth/rpc/rpc_server.rs | 6 ++++- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/e2e/test/e2e-json-rpc.test.ts b/e2e/test/e2e-json-rpc.test.ts index 6ff05097a..11e82e87f 100644 --- a/e2e/test/e2e-json-rpc.test.ts +++ b/e2e/test/e2e-json-rpc.test.ts @@ -14,6 +14,11 @@ describe("JSON-RPC", () => { (await sendExpect("hardhat_reset", [])).eq(true); } }); + + it("Code for non-existent contract is 0x", async () => { + const addressWithNothingDeployed = ALICE.address; + (await sendExpect("eth_getCode", [addressWithNothingDeployed, "latest"])).eq("0x"); + }); }); describe("Metadata", () => { diff --git a/e2e/test/e2e-tx-serial-contract.test.ts b/e2e/test/e2e-tx-serial-contract.test.ts index b468d4678..e788b4469 100644 --- a/e2e/test/e2e-tx-serial-contract.test.ts +++ b/e2e/test/e2e-tx-serial-contract.test.ts @@ -25,10 +25,42 @@ describe("Transaction: serial TestContractBalances", () => { it("Resets blockchain", async () => { await sendReset(); }); + it("Contract is deployed", async () => { _contract = await deployTestContractBalances(); }); + it("Eth_getCode is not null for deployed contract", async () => { + const deployedCode = await send("eth_getCode", [_contract.target, "latest"]); + + expect(deployedCode).not.eq("0x"); + }); + + it("Eth_call works on read function", async () => { + // prepare transaction object + const data = _contract.interface.encodeFunctionData("get", [CHARLIE.address]) + const to = _contract.target; + const from = CHARLIE.address; + const transaction = {from: from, to: to, data: data}; + + const currentCharlieBalance = await send("eth_call", [transaction, "latest"]); + const expectedCharlieBalance = toPaddedHex(0, 32); + + expect(currentCharlieBalance).eq(expectedCharlieBalance); + }); + + it("Eth_call works on read function without from field", async () => { + // prepare transaction object + const data = _contract.interface.encodeFunctionData("get", [CHARLIE.address]) + const to = _contract.target; + const transaction = {to: to, data: data}; + + const currentCharlieBalance = await send("eth_call", [transaction, "latest"]); + const expectedCharlieBalance = toPaddedHex(0, 32); + + expect(currentCharlieBalance).eq(expectedCharlieBalance); + }); + it("Performs add and sub", async () => { _block = await sendGetBlockNumber(); diff --git a/src/eth/rpc/rpc_server.rs b/src/eth/rpc/rpc_server.rs index abc2c5034..5e9762b3d 100644 --- a/src/eth/rpc/rpc_server.rs +++ b/src/eth/rpc/rpc_server.rs @@ -339,7 +339,7 @@ async fn eth_get_code(params: Params<'_>, ctx: Arc) -> anyhow::Resul let point_in_time = ctx.storage.translate_to_point_in_time(&block_selection).await?; let account = ctx.storage.read_account(&address, &point_in_time).await?; - Ok(account.bytecode.map(hex_data).unwrap_or_else(hex_zero)) + Ok(account.bytecode.map(hex_data).unwrap_or_else(hex_null)) } // Subscription @@ -403,3 +403,7 @@ fn hex_num_zero_padded(value: impl Into) -> String { fn hex_zero() -> String { "0x0".to_owned() } + +fn hex_null() -> String { + "0x".to_owned() +}