Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RPC eth_chainId for querying the current blockchain chain ID #6329

Merged
merged 12 commits into from Sep 26, 2017
@@ -53,6 +53,11 @@ export default class Parity {
);
}

chainId () {
return this._transport
.execute('parity_chainId');
}

chainStatus () {
return this._transport
.execute('parity_chainStatus')
@@ -56,6 +56,16 @@ export default {
}
},

chainId: {
desc: 'Returns the current chain ID used for tranaction signing.',
params: [],
returns: {
type: Quantity,
desc: 'The current blockchain chain ID',
example: '0x1'
}
},

chainStatus: {
section: SECTION_NET,
desc: 'Returns the information on warp sync blocks',
@@ -49,6 +49,7 @@ use v1::types::{

/// Parity implementation for light client.
pub struct ParityClient {
client: Arc<LightChainClient>,
light_dispatch: Arc<LightDispatcher>,
accounts: Arc<AccountProvider>,
logger: Arc<RotatingLogger>,
@@ -83,6 +84,7 @@ impl ParityClient {
dapps_address,
ws_address,
eip86_transition: client.eip86_transition(),
client: client,
}
}

@@ -320,6 +322,10 @@ impl Parity for ParityClient {
Err(errors::light_unimplemented(None))
}

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

fn chain(&self) -> Result<String, Error> {
Ok(self.settings.chain.clone())
}
@@ -200,6 +200,10 @@ impl<C, M, U> Parity for ParityClient<C, M, U> where
Ok(self.settings.chain.clone())
}

fn chain_id(&self) -> Result<Option<U256>, Error> {

This comment has been minimized.

Copy link
@tomusdrw

tomusdrw Sep 18, 2017

Contributor
  1. minor: U64 would be enough
  2. As mentioned earlier chain_id is already returned as decimal (u64) in transactions and I'd prefer consistency (either all U64 (hex) or all u64 (decimal)).

This comment has been minimized.

Copy link
@sorpaas

sorpaas Sep 18, 2017

Author Member

@tomusdrw Where's the U64 struct defined?

This comment has been minimized.

Copy link
@tomusdrw

tomusdrw Sep 18, 2017

Contributor

Thought we already have it, but seems we only have U128 and U256. You can create U64 in types/uint.rs.

Ok(self.client.signing_chain_id().map(U256::from))
}

fn chain(&self) -> Result<String, Error> {
Ok(self.client.spec_name())
}
@@ -220,6 +220,17 @@ 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 util::misc;
@@ -172,6 +172,12 @@ build_rpc_trait! {
#[rpc(name = "parity_mode")]
fn mode(&self) -> Result<String, Error>;

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

/// Get the chain name. Returns one of: "foundation", "kovan", &c. of a filename.
#[rpc(name = "parity_chain")]
fn chain(&self) -> Result<String, Error>;
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.