Skip to content

Commit

Permalink
fix eth_getCode and add test cases (#303)
Browse files Browse the repository at this point in the history
* test: add eth_call cases

* test: add eth_getCode cases

* fix: change eth_getCode not found response to 0x
  • Loading branch information
mayconamaroCW authored Mar 4, 2024
1 parent 0e53ee6 commit c7c1040
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
5 changes: 5 additions & 0 deletions e2e/test/e2e-json-rpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
32 changes: 32 additions & 0 deletions e2e/test/e2e-tx-serial-contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
6 changes: 5 additions & 1 deletion src/eth/rpc/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ async fn eth_get_code(params: Params<'_>, ctx: Arc<RpcContext>) -> 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
Expand Down Expand Up @@ -403,3 +403,7 @@ fn hex_num_zero_padded(value: impl Into<U256>) -> String {
fn hex_zero() -> String {
"0x0".to_owned()
}

fn hex_null() -> String {
"0x".to_owned()
}

0 comments on commit c7c1040

Please sign in to comment.