Skip to content

Commit

Permalink
[MOON-1489] return meaningful error message when block missing during…
Browse files Browse the repository at this point in the history
… eth_call (#625)

* fix merge conflicts

* merge imports in client/rpc/src/eth.rs

Co-authored-by: Qinxuan Chen <koushiro.cqx@gmail.com>

* use alternate syntax to return promise

Co-authored-by: Qinxuan Chen <koushiro.cqx@gmail.com>
  • Loading branch information
nbaztec and koushiro authored May 10, 2022
1 parent 90dc0eb commit 4f30755
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 6 deletions.
13 changes: 12 additions & 1 deletion client/rpc/src/eth/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use sc_network::ExHashT;
use sc_transaction_pool::ChainApi;
use sp_api::{ApiExt, ProvideRuntimeApi};
use sp_block_builder::BlockBuilder as BlockBuilderApi;
use sp_blockchain::HeaderBackend;
use sp_blockchain::{BlockStatus, HeaderBackend};
use sp_runtime::{
generic::BlockId,
traits::{BlakeTwo256, Block as BlockT},
Expand All @@ -41,6 +41,9 @@ use crate::{
frontier_backend_client, internal_err,
};

/// Default JSONRPC error code return by geth
pub const JSON_RPC_ERROR_DEFAULT: i64 = -32000;

impl<B, C, P, CT, BE, H: ExHashT, A: ChainApi> Eth<B, C, P, CT, BE, H, A>
where
B: BlockT<Hash = H256> + Send + Sync + 'static,
Expand Down Expand Up @@ -89,6 +92,14 @@ where
}
};

if let Ok(BlockStatus::Unknown) = self.client.status(id) {
return Err(Error {
code: JSON_RPC_ERROR_DEFAULT.into(),
message: String::from("header not found"),
data: None,
});
}

let api_version =
if let Ok(Some(api_version)) = api.api_version::<dyn EthereumRuntimeRPCApi<B>>(&id) {
api_version
Expand Down
42 changes: 42 additions & 0 deletions ts-tests/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions ts-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
"@types/chai": "^4.2.11",
"@types/mocha": "^8.0.0",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"ethers": "^5.4.6",
"mocha": "^8.0.1",
"mocha-steps": "^1.3.0",
"rimraf": "^3.0.2",
"truffle": "^5.1.62",
"ts-node": "^8.10.2",
"typescript": "^3.9.6",
"web3": "^1.3.4",
"ethers": "^5.4.6"
"web3": "^1.3.4"
},
"devDependencies": {
"@types/chai-as-promised": "^7.1.5"
}
}
15 changes: 12 additions & 3 deletions ts-tests/tests/test-contract.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { expect } from "chai";
import { expect, use as chaiUse } from "chai";
import chaiAsPromised from "chai-as-promised";

import Test from "../build/contracts/Test.json"
import { createAndFinalizeBlock, customRequest, describeWithFrontier } from "./util";

chaiUse(chaiAsPromised);

describeWithFrontier("Frontier RPC (Contract)", (context) => {
const GENESIS_ACCOUNT = "0x6be02d1d3665660d22ff9624b7be0551ee1ac91b";
const GENESIS_ACCOUNT_PRIVATE_KEY = "0x99B3C12287537E38C90A9219D4CB074A89A16E9CDB20BF85728EBD97C343E342";
Expand Down Expand Up @@ -50,8 +53,7 @@ describeWithFrontier("Frontier RPC (Contract)", (context) => {
expect(await customRequest(context.web3, "eth_getCode", [FIRST_CONTRACT_ADDRESS])).to.deep.equal({
id: 1,
jsonrpc: "2.0",
result:
TEST_CONTRACT_DEPLOYED_BYTECODE,
result: TEST_CONTRACT_DEPLOYED_BYTECODE,
});
});

Expand All @@ -60,4 +62,11 @@ describeWithFrontier("Frontier RPC (Contract)", (context) => {
data: TEST_CONTRACT_BYTECODE
})).to.be.eq(TEST_CONTRACT_DEPLOYED_BYTECODE);
});

it("eth_call at missing block returns error", async function () {
const nonExistingBlockNumber = "999999";
return expect(context.web3.eth.call({
data: TEST_CONTRACT_BYTECODE,
}, nonExistingBlockNumber)).to.eventually.rejectedWith('header not found');
});
});

0 comments on commit 4f30755

Please sign in to comment.