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 (#56)
  • Loading branch information
nbaztec authored Apr 4, 2022
1 parent 04af2a3 commit 8ccf41f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 6 deletions.
14 changes: 13 additions & 1 deletion client/rpc/src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use ethereum::{BlockV2 as EthereumBlock, TransactionV2 as EthereumTransaction};
use ethereum_types::{H160, H256, H512, H64, U256, U64};
use evm::{ExitError, ExitReason};
use futures::{future::TryFutureExt, StreamExt};
use jsonrpc_core::{futures::future, BoxFuture, Result};
use jsonrpc_core::{futures::future, BoxFuture, Error, Result};
use crate::cache::LRUCacheByteLimited;
use tokio::sync::{mpsc, oneshot};

Expand All @@ -42,6 +42,7 @@ use sc_transaction_pool::{ChainApi, Pool};
use sc_transaction_pool_api::{InPoolTransaction, TransactionPool};
use sp_api::{ApiExt, BlockId, Core, HeaderT, ProvideRuntimeApi};
use sp_block_builder::BlockBuilder;
use sp_blockchain::BlockStatus;
use sp_blockchain::HeaderBackend;
use sp_core::hashing::keccak_256;
use sp_runtime::{
Expand All @@ -66,6 +67,9 @@ use crate::{
public_key, EthSigner, StorageOverride,
};

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

type WaitList<Hash, T> = HashMap<Hash, Vec<oneshot::Sender<Option<T>>>>;

pub struct EthApi<B: BlockT, C, P, CT, BE, H: ExHashT, A: ChainApi, F: Formatter> {
Expand Down Expand Up @@ -1214,6 +1218,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";
await expect(context.web3.eth.call({
data: TEST_CONTRACT_BYTECODE,
}, nonExistingBlockNumber)).to.eventually.rejectedWith('header not found');
});
});

0 comments on commit 8ccf41f

Please sign in to comment.