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

Commit

Permalink
net_version caches network_id to avoid redundant aquire of sync read …
Browse files Browse the repository at this point in the history
…lock (#9544)

* net_version caches network_id to avoid redundant aquire of sync read lock, #8746

* use lower_hex display formatting for net_peerCount rpc method
  • Loading branch information
debris authored and 5chdn committed Sep 17, 2018
1 parent 87dbfc4 commit 2a61467
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions rpc/src/v1/impls/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,31 @@ use v1::traits::Net;

/// Net rpc implementation.
pub struct NetClient<S: ?Sized> {
sync: Arc<S>
sync: Arc<S>,
/// Cached `network_id`.
///
/// We cache it to avoid redundant aquire of sync read lock.
/// https://github.com/paritytech/parity-ethereum/issues/8746
network_id: u64,
}

impl<S: ?Sized> NetClient<S> where S: SyncProvider {
/// Creates new NetClient.
pub fn new(sync: &Arc<S>) -> Self {
NetClient {
sync: sync.clone(),
network_id: sync.status().network_id,
}
}
}

impl<S: ?Sized> Net for NetClient<S> where S: SyncProvider + 'static {
fn version(&self) -> Result<String> {
Ok(format!("{}", self.sync.status().network_id).to_owned())
Ok(format!("{}", self.network_id))
}

fn peer_count(&self) -> Result<String> {
Ok(format!("0x{:x}", self.sync.status().num_peers as u64).to_owned())
Ok(format!("{:#x}", self.sync.status().num_peers as u64))
}

fn is_listening(&self) -> Result<bool> {
Expand Down

0 comments on commit 2a61467

Please sign in to comment.