Support eth_chainId RPC method (#9783)

* Support eth_chainId RPC method

 * https://github.com/ethereum/EIPs/pull/695
 * Original PR is #6329

* rpc: remove parity_chainId
This commit is contained in:
HackyMiner 2018-10-21 04:13:23 +09:00 committed by Wei Tang
parent 1f103ab7f1
commit b8da38f4e4
8 changed files with 29 additions and 29 deletions

View File

@ -48,6 +48,7 @@ use v1::types::{
RichBlock, Block, BlockTransactions, BlockNumber, Bytes, SyncStatus, SyncInfo, RichBlock, Block, BlockTransactions, BlockNumber, Bytes, SyncStatus, SyncInfo,
Transaction, CallRequest, Index, Filter, Log, Receipt, Work, Transaction, CallRequest, Index, Filter, Log, Receipt, Work,
H64 as RpcH64, H256 as RpcH256, H160 as RpcH160, U256 as RpcU256, block_number_to_id, H64 as RpcH64, H256 as RpcH256, H160 as RpcH160, U256 as RpcU256, block_number_to_id,
U64 as RpcU64,
}; };
use v1::metadata::Metadata; use v1::metadata::Metadata;
@ -513,6 +514,10 @@ impl<C, SN: ?Sized, S: ?Sized, M, EM, T: StateInfo + 'static> Eth for EthClient<
Ok(self.miner.is_currently_sealing()) 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> { fn hashrate(&self) -> Result<RpcU256> {
Ok(RpcU256::from(self.external_miner.hashrate())) Ok(RpcU256::from(self.external_miner.hashrate()))
} }

View File

@ -49,6 +49,7 @@ use v1::types::{
RichBlock, Block, BlockTransactions, BlockNumber, LightBlockNumber, Bytes, SyncStatus, SyncInfo, RichBlock, Block, BlockTransactions, BlockNumber, LightBlockNumber, Bytes, SyncStatus, SyncInfo,
Transaction, CallRequest, Index, Filter, Log, Receipt, Work, Transaction, CallRequest, Index, Filter, Log, Receipt, Work,
H64 as RpcH64, H256 as RpcH256, H160 as RpcH160, U256 as RpcU256, H64 as RpcH64, H256 as RpcH256, H160 as RpcH160, U256 as RpcU256,
U64 as RpcU64,
}; };
use v1::metadata::Metadata; use v1::metadata::Metadata;
@ -246,6 +247,10 @@ impl<T: LightChainClient + 'static> Eth for EthClient<T> {
Ok(false) Ok(false)
} }
fn chain_id(&self) -> Result<Option<RpcU64>> {
Ok(self.client.signing_chain_id().map(RpcU64::from))
}
fn hashrate(&self) -> Result<RpcU256> { fn hashrate(&self) -> Result<RpcU256> {
Ok(Default::default()) Ok(Default::default())
} }

View File

@ -38,7 +38,7 @@ use v1::helpers::light_fetch::LightFetch;
use v1::metadata::Metadata; use v1::metadata::Metadata;
use v1::traits::Parity; use v1::traits::Parity;
use v1::types::{ use v1::types::{
Bytes, U256, U64, H64, H160, H256, H512, CallRequest, Bytes, U256, H64, H160, H256, H512, CallRequest,
Peers, Transaction, RpcSettings, Histogram, Peers, Transaction, RpcSettings, Histogram,
TransactionStats, LocalTransactionStatus, TransactionStats, LocalTransactionStatus,
BlockNumber, LightBlockNumber, ConsensusCapability, VersionInfo, BlockNumber, LightBlockNumber, ConsensusCapability, VersionInfo,
@ -328,10 +328,6 @@ impl Parity for ParityClient {
Err(errors::light_unimplemented(None)) 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> { fn chain(&self) -> Result<String> {
Ok(self.settings.chain.clone()) Ok(self.settings.chain.clone())
} }

View File

@ -40,7 +40,7 @@ use v1::helpers::{self, errors, fake_sign, ipfs, SigningQueue, SignerService, Ne
use v1::metadata::Metadata; use v1::metadata::Metadata;
use v1::traits::Parity; use v1::traits::Parity;
use v1::types::{ use v1::types::{
Bytes, U256, U64, H64, H160, H256, H512, CallRequest, Bytes, U256, H64, H160, H256, H512, CallRequest,
Peers, Transaction, RpcSettings, Histogram, Peers, Transaction, RpcSettings, Histogram,
TransactionStats, LocalTransactionStatus, TransactionStats, LocalTransactionStatus,
BlockNumber, ConsensusCapability, VersionInfo, BlockNumber, ConsensusCapability, VersionInfo,
@ -173,10 +173,6 @@ impl<C, M, U, S> Parity for ParityClient<C, M, U> where
Ok(self.settings.chain.clone()) 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> { fn chain(&self) -> Result<String> {
Ok(self.client.spec_name()) Ok(self.client.spec_name())
} }

View File

@ -178,6 +178,15 @@ fn rpc_eth_syncing() {
assert_eq!(tester.io.handle_request_sync(request), Some(false_res.to_owned())); 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] #[test]
fn rpc_eth_hashrate() { fn rpc_eth_hashrate() {
let tester = EthTester::default(); let tester = EthTester::default();

View File

@ -193,17 +193,6 @@ fn rpc_parity_extra_data() {
assert_eq!(io.handle_request_sync(request), Some(response.to_owned())); 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] #[test]
fn rpc_parity_default_extra_data() { fn rpc_parity_default_extra_data() {
use version::version_data; use version::version_data;

View File

@ -20,7 +20,7 @@ use jsonrpc_macros::Trailing;
use v1::types::{RichBlock, BlockNumber, Bytes, CallRequest, Filter, FilterChanges, Index}; use v1::types::{RichBlock, BlockNumber, Bytes, CallRequest, Filter, FilterChanges, Index};
use v1::types::{Log, Receipt, SyncStatus, Transaction, Work}; 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! { build_rpc_trait! {
/// Eth rpc interface. /// Eth rpc interface.
@ -47,6 +47,12 @@ build_rpc_trait! {
#[rpc(name = "eth_mining")] #[rpc(name = "eth_mining")]
fn is_mining(&self) -> Result<bool>; 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. /// Returns current gas_price.
#[rpc(name = "eth_gasPrice")] #[rpc(name = "eth_gasPrice")]
fn gas_price(&self) -> Result<U256>; fn gas_price(&self) -> Result<U256>;

View File

@ -22,7 +22,7 @@ use jsonrpc_core::{BoxFuture, Result};
use jsonrpc_macros::Trailing; use jsonrpc_macros::Trailing;
use v1::types::{ use v1::types::{
H64, H160, H256, H512, U256, U64, Bytes, CallRequest, H64, H160, H256, H512, U256, Bytes, CallRequest,
Peers, Transaction, RpcSettings, Histogram, Peers, Transaction, RpcSettings, Histogram,
TransactionStats, LocalTransactionStatus, TransactionStats, LocalTransactionStatus,
BlockNumber, ConsensusCapability, VersionInfo, BlockNumber, ConsensusCapability, VersionInfo,
@ -172,12 +172,6 @@ build_rpc_trait! {
#[rpc(name = "parity_mode")] #[rpc(name = "parity_mode")]
fn mode(&self) -> Result<String>; 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. /// Get the chain name. Returns one of the pre-configured chain names or a filename.
#[rpc(name = "parity_chain")] #[rpc(name = "parity_chain")]
fn chain(&self) -> Result<String>; fn chain(&self) -> Result<String>;