From 23b7f51a804b3087a1527f375c32cf8588408b88 Mon Sep 17 00:00:00 2001 From: hackyminer Date: Sat, 20 Oct 2018 04:49:20 +0900 Subject: [PATCH] Support eth_chainId RPC method * https://github.com/ethereum/EIPs/pull/695 * Original PR is #6329 --- rpc/src/v1/impls/eth.rs | 5 +++++ rpc/src/v1/impls/light/eth.rs | 5 +++++ rpc/src/v1/tests/mocked/eth.rs | 9 +++++++++ rpc/src/v1/traits/eth.rs | 8 +++++++- 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index d912c13dd6d..548271f8929 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -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; @@ -513,6 +514,10 @@ impl Eth for EthClient< Ok(self.miner.is_currently_sealing()) } + fn chain_id(&self) -> Result> { + Ok(self.client.signing_chain_id().map(RpcU64::from)) + } + fn hashrate(&self) -> Result { Ok(RpcU256::from(self.external_miner.hashrate())) } diff --git a/rpc/src/v1/impls/light/eth.rs b/rpc/src/v1/impls/light/eth.rs index 34b89ac9f6b..2a2563016ae 100644 --- a/rpc/src/v1/impls/light/eth.rs +++ b/rpc/src/v1/impls/light/eth.rs @@ -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; @@ -246,6 +247,10 @@ impl Eth for EthClient { Ok(false) } + fn chain_id(&self) -> Result> { + Ok(self.client.signing_chain_id().map(RpcU64::from)) + } + fn hashrate(&self) -> Result { Ok(Default::default()) } diff --git a/rpc/src/v1/tests/mocked/eth.rs b/rpc/src/v1/tests/mocked/eth.rs index ff364cf7f0c..040bf4be69e 100644 --- a/rpc/src/v1/tests/mocked/eth.rs +++ b/rpc/src/v1/tests/mocked/eth.rs @@ -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(); diff --git a/rpc/src/v1/traits/eth.rs b/rpc/src/v1/traits/eth.rs index a11b8680635..0c0041bb7b9 100644 --- a/rpc/src/v1/traits/eth.rs +++ b/rpc/src/v1/traits/eth.rs @@ -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. @@ -47,6 +47,12 @@ build_rpc_trait! { #[rpc(name = "eth_mining")] fn is_mining(&self) -> Result; + /// 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>; + /// Returns current gas_price. #[rpc(name = "eth_gasPrice")] fn gas_price(&self) -> Result;