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

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

Merged
merged 12 commits into from Sep 26, 2017
5 changes: 5 additions & 0 deletions js/src/api/rpc/parity/parity.js
Expand Up @@ -53,6 +53,11 @@ export default class Parity {
);
}

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

chainStatus () {
return this._transport
.execute('parity_chainStatus')
Expand Down
10 changes: 10 additions & 0 deletions js/src/jsonrpc/interfaces/parity.js
Expand Up @@ -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',
Expand Down
6 changes: 6 additions & 0 deletions rpc/src/v1/impls/light/parity.rs
Expand Up @@ -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>,
Expand Down Expand Up @@ -83,6 +84,7 @@ impl ParityClient {
dapps_address,
ws_address,
eip86_transition: client.eip86_transition(),
client: client,
}
}

Expand Down Expand Up @@ -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())
}
Expand Down
4 changes: 4 additions & 0 deletions rpc/src/v1/impls/parity.rs
Expand Up @@ -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> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  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)).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tomusdrw Where's the U64 struct defined?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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())
}
Expand Down
11 changes: 11 additions & 0 deletions rpc/src/v1/tests/mocked/parity.rs
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions rpc/src/v1/traits/parity.rs
Expand Up @@ -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>;
Expand Down