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
Merged
5 changes: 5 additions & 0 deletions js/src/api/rpc/eth/eth.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export default class Eth {
this._transport = transport;
}

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

accounts () {
return this._transport
.execute('eth_accounts')
Expand Down
10 changes: 10 additions & 0 deletions js/src/jsonrpc/interfaces/eth.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ The following options are possible for the \`defaultBlock\` parameter:
}
},

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

call: {
desc: 'Executes a new message call immediately without creating a transaction on the block chain.',
params: [
Expand Down
8 changes: 8 additions & 0 deletions js/src/views/RpcCalls/data/rpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@
"inputFormatters": [],
"outputFormatter": null
},
{
"name": "eth_chainId",
"desc": "Returns the current chain ID used for tranaction signing.",
"params": [],
"returns": "`String` - The current blockchain chain ID",
"inputFormatters": [],
"outputFormatter": null
},
{
"name": "eth_syncing",
"desc": "Returns an object with data about the sync status or `false`.",
Expand Down
5 changes: 5 additions & 0 deletions rpc/src/v1/impls/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,11 @@ impl<C, SN: ?Sized, S: ?Sized, M, EM> Eth for EthClient<C, SN, S, M, EM> where
Ok(format!("{}", version))
}

fn chain_id(&self) -> Result<String, Error> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Chain id in the transactions is serialized as Option<u64>, would be best to keep it constistent.
IMHO using a generic String response is a bit misleading. If we want to return a hex-encoded value then Option<U64> would be a right choice.

Note that using Option indicates that in case the chain id is missing null will be returned.

let client = take_weak!(self.client);
Ok(client.signing_network_id().map(|v| format!("0x{:x}", v)).unwrap_or("".to_string()))
}

fn syncing(&self) -> Result<SyncStatus, Error> {
use ethcore::snapshot::RestorationStatus;

Expand Down
4 changes: 4 additions & 0 deletions rpc/src/v1/impls/light/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ impl Eth for EthClient {
Ok(format!("{}", ::light::net::MAX_PROTOCOL_VERSION))
}

fn chain_id(&self) -> Result<String, Error> {
Ok(self.client.signing_network_id().map(|v| format!("0x{:x}", v)).unwrap_or("".to_string()))
}

fn syncing(&self) -> Result<SyncStatus, Error> {
if self.sync.is_major_importing() {
let chain_info = self.client.chain_info();
Expand Down
8 changes: 8 additions & 0 deletions rpc/src/v1/tests/mocked/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ fn rpc_eth_protocol_version() {
assert_eq!(EthTester::default().io.handle_request_sync(request), Some(response.to_owned()));
}

#[test]
fn rpc_eth_chain_id() {
let request = r#"{"jsonrpc": "2.0", "method": "eth_chainId", "params": [], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":"0x1","id":1}"#;

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

#[test]
fn rpc_eth_syncing() {
use ethcore::snapshot::RestorationStatus;
Expand Down
6 changes: 6 additions & 0 deletions rpc/src/v1/traits/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ build_rpc_trait! {
#[rpc(name = "eth_protocolVersion")]
fn protocol_version(&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 = "eth_chainId")]
fn chain_id(&self) -> Result<String, Error>;

/// Returns an object with data about the sync status or false. (wtf?)
#[rpc(name = "eth_syncing")]
fn syncing(&self) -> Result<SyncStatus, Error>;
Expand Down