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 (#9783)
Browse files Browse the repository at this point in the history
* Support eth_chainId RPC method

 * ethereum/EIPs#695
 * Original PR is #6329

* rpc: remove parity_chainId
  • Loading branch information
hackmod authored and sorpaas committed Oct 20, 2018
1 parent 1f103ab commit b8da38f
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 29 deletions.
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
6 changes: 1 addition & 5 deletions rpc/src/v1/impls/light/parity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use v1::helpers::light_fetch::LightFetch;
use v1::metadata::Metadata;
use v1::traits::Parity;
use v1::types::{
Bytes, U256, U64, H64, H160, H256, H512, CallRequest,
Bytes, U256, H64, H160, H256, H512, CallRequest,
Peers, Transaction, RpcSettings, Histogram,
TransactionStats, LocalTransactionStatus,
BlockNumber, LightBlockNumber, ConsensusCapability, VersionInfo,
Expand Down Expand Up @@ -328,10 +328,6 @@ impl Parity for ParityClient {
Err(errors::light_unimplemented(None))
}

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

fn chain(&self) -> Result<String> {
Ok(self.settings.chain.clone())
}
Expand Down
6 changes: 1 addition & 5 deletions rpc/src/v1/impls/parity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use v1::helpers::{self, errors, fake_sign, ipfs, SigningQueue, SignerService, Ne
use v1::metadata::Metadata;
use v1::traits::Parity;
use v1::types::{
Bytes, U256, U64, H64, H160, H256, H512, CallRequest,
Bytes, U256, H64, H160, H256, H512, CallRequest,
Peers, Transaction, RpcSettings, Histogram,
TransactionStats, LocalTransactionStatus,
BlockNumber, ConsensusCapability, VersionInfo,
Expand Down Expand Up @@ -173,10 +173,6 @@ impl<C, M, U, S> Parity for ParityClient<C, M, U> where
Ok(self.settings.chain.clone())
}

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

fn chain(&self) -> Result<String> {
Ok(self.client.spec_name())
}
Expand Down
9 changes: 9 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,15 @@ fn rpc_eth_syncing() {
assert_eq!(tester.io.handle_request_sync(request), Some(false_res.to_owned()));
}

#[test]
fn rpc_eth_chain_id() {
let tester = EthTester::default();
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
11 changes: 0 additions & 11 deletions rpc/src/v1/tests/mocked/parity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,6 @@ fn rpc_parity_extra_data() {
assert_eq!(io.handle_request_sync(request), Some(response.to_owned()));
}

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

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

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

#[test]
fn rpc_parity_default_extra_data() {
use version::version_data;
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
8 changes: 1 addition & 7 deletions rpc/src/v1/traits/parity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use jsonrpc_core::{BoxFuture, Result};
use jsonrpc_macros::Trailing;

use v1::types::{
H64, H160, H256, H512, U256, U64, Bytes, CallRequest,
H64, H160, H256, H512, U256, Bytes, CallRequest,
Peers, Transaction, RpcSettings, Histogram,
TransactionStats, LocalTransactionStatus,
BlockNumber, ConsensusCapability, VersionInfo,
Expand Down Expand Up @@ -172,12 +172,6 @@ build_rpc_trait! {
#[rpc(name = "parity_mode")]
fn mode(&self) -> Result<String>;

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

/// Get the chain name. Returns one of the pre-configured chain names or a filename.
#[rpc(name = "parity_chain")]
fn chain(&self) -> Result<String>;
Expand Down

0 comments on commit b8da38f

Please sign in to comment.