Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Support eth_chainId RPC method
Browse files Browse the repository at this point in the history
 * ethereum/EIPs#695
 * Original PR is #6329
  • Loading branch information
hackmod committed Oct 19, 2018
1 parent 1f103ab commit 90e9cec
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions rpc/src/v1/impls/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ use v1::types::{
RichBlock, Block, BlockTransactions, BlockNumber, Bytes, SyncStatus, SyncInfo,
Transaction, CallRequest, Index, Filter, Log, Receipt, Work,
H64 as RpcH64, H256 as RpcH256, H160 as RpcH160, U256 as RpcU256, block_number_to_id,
U64 as RpcU64,
};
use v1::metadata::Metadata;

Expand Down Expand Up @@ -513,6 +514,10 @@ impl<C, SN: ?Sized, S: ?Sized, M, EM, T: StateInfo + 'static> Eth for EthClient<
Ok(self.miner.is_currently_sealing())
}

fn chain_id(&self) -> Result<Option<RpcU64>> {
Ok(self.client.signing_chain_id().map(RpcU64::from))
}

fn hashrate(&self) -> Result<RpcU256> {
Ok(RpcU256::from(self.external_miner.hashrate()))
}
Expand Down
5 changes: 5 additions & 0 deletions rpc/src/v1/impls/light/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ use v1::types::{
RichBlock, Block, BlockTransactions, BlockNumber, LightBlockNumber, Bytes, SyncStatus, SyncInfo,
Transaction, CallRequest, Index, Filter, Log, Receipt, Work,
H64 as RpcH64, H256 as RpcH256, H160 as RpcH160, U256 as RpcU256,
U64 as RpcU64,
};
use v1::metadata::Metadata;

Expand Down Expand Up @@ -246,6 +247,10 @@ impl<T: LightChainClient + 'static> Eth for EthClient<T> {
Ok(false)
}

fn chain_id(&self) -> Result<Option<RpcU64>> {
Ok(self.client.signing_chain_id().map(RpcU64::from))
}

fn hashrate(&self) -> Result<RpcU256> {
Ok(Default::default())
}
Expand Down
11 changes: 11 additions & 0 deletions rpc/src/v1/tests/mocked/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,17 @@ fn rpc_eth_syncing() {
assert_eq!(tester.io.handle_request_sync(request), Some(false_res.to_owned()));
}

#[test]
fn rpc_eth_chain_id() {
let deps = Dependencies::new();
let io = deps.default_client();

let request = r#"{"jsonrpc": "2.0", "method": "eth_chainId", "params": [], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":null,"id":1}"#;

assert_eq!(tester.io.handle_request_sync(request), Some(response.to_owned()));
}

#[test]
fn rpc_eth_hashrate() {
let tester = EthTester::default();
Expand Down
8 changes: 7 additions & 1 deletion rpc/src/v1/traits/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use jsonrpc_macros::Trailing;

use v1::types::{RichBlock, BlockNumber, Bytes, CallRequest, Filter, FilterChanges, Index};
use v1::types::{Log, Receipt, SyncStatus, Transaction, Work};
use v1::types::{H64, H160, H256, U256};
use v1::types::{H64, H160, H256, U256, U64};

build_rpc_trait! {
/// Eth rpc interface.
Expand All @@ -47,6 +47,12 @@ build_rpc_trait! {
#[rpc(name = "eth_mining")]
fn is_mining(&self) -> Result<bool>;

/// Returns the chain ID used for transaction signing at the
/// current best block. None is returned if not
/// available.
#[rpc(name = "eth_chainId")]
fn chain_id(&self) -> Result<Option<U64>>;

/// Returns current gas_price.
#[rpc(name = "eth_gasPrice")]
fn gas_price(&self) -> Result<U256>;
Expand Down

0 comments on commit 90e9cec

Please sign in to comment.