net_version caches network_id to avoid redundant aquire of sync read 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
This commit is contained in:
Marek Kotewicz 2018-09-13 10:16:24 +02:00 committed by Afri Schoedon
parent 6e62d77e4d
commit 6b391312ab

View File

@ -22,7 +22,12 @@ use v1::traits::Net;
/// Net rpc implementation. /// Net rpc implementation.
pub struct NetClient<S: ?Sized> { 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 { impl<S: ?Sized> NetClient<S> where S: SyncProvider {
@ -30,17 +35,18 @@ impl<S: ?Sized> NetClient<S> where S: SyncProvider {
pub fn new(sync: &Arc<S>) -> Self { pub fn new(sync: &Arc<S>) -> Self {
NetClient { NetClient {
sync: sync.clone(), sync: sync.clone(),
network_id: sync.status().network_id,
} }
} }
} }
impl<S: ?Sized> Net for NetClient<S> where S: SyncProvider + 'static { impl<S: ?Sized> Net for NetClient<S> where S: SyncProvider + 'static {
fn version(&self) -> Result<String> { 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> { 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> { fn is_listening(&self) -> Result<bool> {