Stats RPC
This commit is contained in:
@@ -34,7 +34,7 @@ use ethcore::account_provider::AccountProvider;
|
||||
|
||||
use jsonrpc_core::Error;
|
||||
use v1::traits::Parity;
|
||||
use v1::types::{Bytes, U256, H160, H256, H512, Peers, Transaction, RpcSettings, Histogram};
|
||||
use v1::types::{Bytes, U256, H160, H256, H512, Peers, Transaction, RpcSettings, Histogram, TransactionStats};
|
||||
use v1::helpers::{errors, SigningQueue, SignerService, NetworkSettings};
|
||||
use v1::helpers::dispatch::DEFAULT_MAC;
|
||||
|
||||
@@ -259,6 +259,16 @@ impl<C, M, S: ?Sized> Parity for ParityClient<C, M, S> where
|
||||
Ok(take_weak!(self.miner).all_transactions().into_iter().map(Into::into).collect::<Vec<_>>())
|
||||
}
|
||||
|
||||
fn pending_transactions_stats(&self) -> Result<BTreeMap<H256, TransactionStats>, Error> {
|
||||
try!(self.active());
|
||||
|
||||
let stats = take_weak!(self.sync).transactions_stats();
|
||||
Ok(stats.into_iter()
|
||||
.map(|(hash, stats)| (hash.into(), stats.into()))
|
||||
.collect()
|
||||
)
|
||||
}
|
||||
|
||||
fn signer_port(&self) -> Result<u16, Error> {
|
||||
try!(self.active());
|
||||
|
||||
|
||||
@@ -16,8 +16,9 @@
|
||||
|
||||
//! Test implementation of SyncProvider.
|
||||
|
||||
use util::{RwLock};
|
||||
use ethsync::{SyncProvider, SyncStatus, SyncState, PeerInfo};
|
||||
use std::collections::BTreeMap;
|
||||
use util::{H256, RwLock};
|
||||
use ethsync::{SyncProvider, SyncStatus, SyncState, PeerInfo, TransactionStats};
|
||||
|
||||
/// TestSyncProvider config.
|
||||
pub struct Config {
|
||||
@@ -74,7 +75,7 @@ impl SyncProvider for TestSyncProvider {
|
||||
PeerInfo {
|
||||
id: Some("node1".to_owned()),
|
||||
client_version: "Parity/1".to_owned(),
|
||||
capabilities: vec!["eth/62".to_owned(), "eth/63".to_owned()],
|
||||
capabilities: vec!["eth/62".to_owned(), "eth/63".to_owned()],
|
||||
remote_address: "127.0.0.1:7777".to_owned(),
|
||||
local_address: "127.0.0.1:8888".to_owned(),
|
||||
eth_version: 62,
|
||||
@@ -84,7 +85,7 @@ impl SyncProvider for TestSyncProvider {
|
||||
PeerInfo {
|
||||
id: None,
|
||||
client_version: "Parity/2".to_owned(),
|
||||
capabilities: vec!["eth/63".to_owned(), "eth/64".to_owned()],
|
||||
capabilities: vec!["eth/63".to_owned(), "eth/64".to_owned()],
|
||||
remote_address: "Handshake".to_owned(),
|
||||
local_address: "127.0.0.1:3333".to_owned(),
|
||||
eth_version: 64,
|
||||
@@ -97,5 +98,22 @@ impl SyncProvider for TestSyncProvider {
|
||||
fn enode(&self) -> Option<String> {
|
||||
None
|
||||
}
|
||||
|
||||
fn transactions_stats(&self) -> BTreeMap<H256, TransactionStats> {
|
||||
map![
|
||||
1.into() => TransactionStats {
|
||||
first_seen: 10,
|
||||
propagated_to: map![
|
||||
128.into() => 16
|
||||
]
|
||||
},
|
||||
5.into() => TransactionStats {
|
||||
first_seen: 16,
|
||||
propagated_to: map![
|
||||
16.into() => 1
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -355,3 +355,15 @@ fn rpc_parity_next_nonce() {
|
||||
assert_eq!(io1.handle_request_sync(&request), Some(response1.to_owned()));
|
||||
assert_eq!(io2.handle_request_sync(&request), Some(response2.to_owned()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rpc_parity_transactions_stats() {
|
||||
let deps = Dependencies::new();
|
||||
let io = deps.default_client();
|
||||
|
||||
let request = r#"{"jsonrpc": "2.0", "method": "parity_pendingTransactionsStats", "params":[], "id": 1}"#;
|
||||
let response = r#"{"jsonrpc":"2.0","result":{"0x0000000000000000000000000000000000000000000000000000000000000001":{"firstSeen":10,"propagatedTo":{"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080":16}},"0x0000000000000000000000000000000000000000000000000000000000000005":{"firstSeen":16,"propagatedTo":{"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010":1}}},"id":1}"#;
|
||||
|
||||
assert_eq!(io.handle_request_sync(request), Some(response.to_owned()));
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ use jsonrpc_core::Error;
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use v1::helpers::auto_args::Wrap;
|
||||
use v1::types::{H160, H256, H512, U256, Bytes, Peers, Transaction, RpcSettings, Histogram};
|
||||
use v1::types::{H160, H256, H512, U256, Bytes, Peers, Transaction, RpcSettings, Histogram, TransactionStats};
|
||||
|
||||
build_rpc_trait! {
|
||||
/// Parity-specific rpc interface.
|
||||
@@ -115,6 +115,10 @@ build_rpc_trait! {
|
||||
#[rpc(name = "parity_pendingTransactions")]
|
||||
fn pending_transactions(&self) -> Result<Vec<Transaction>, Error>;
|
||||
|
||||
/// Returns propagation statistics on transactions pending in the queue.
|
||||
#[rpc(name = "parity_pendingTransactionsStats")]
|
||||
fn pending_transactions_stats(&self) -> Result<BTreeMap<H256, TransactionStats>, Error>;
|
||||
|
||||
/// Returns current Trusted Signer port or an error if signer is disabled.
|
||||
#[rpc(name = "parity_signerPort")]
|
||||
fn signer_port(&self) -> Result<u16, Error>;
|
||||
|
||||
@@ -43,7 +43,7 @@ pub use self::filter::{Filter, FilterChanges};
|
||||
pub use self::hash::{H64, H160, H256, H512, H520, H2048};
|
||||
pub use self::index::Index;
|
||||
pub use self::log::Log;
|
||||
pub use self::sync::{SyncStatus, SyncInfo, Peers, PeerInfo, PeerNetworkInfo, PeerProtocolsInfo, PeerEthereumProtocolInfo};
|
||||
pub use self::sync::{SyncStatus, SyncInfo, Peers, PeerInfo, PeerNetworkInfo, PeerProtocolsInfo, PeerEthereumProtocolInfo, TransactionStats};
|
||||
pub use self::transaction::Transaction;
|
||||
pub use self::transaction_request::TransactionRequest;
|
||||
pub use self::receipt::Receipt;
|
||||
|
||||
@@ -14,9 +14,10 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use ethsync::PeerInfo as SyncPeerInfo;
|
||||
use std::collections::BTreeMap;
|
||||
use ethsync::{PeerInfo as SyncPeerInfo, TransactionStats as SyncTransactionStats};
|
||||
use serde::{Serialize, Serializer};
|
||||
use v1::types::U256;
|
||||
use v1::types::{U256, H512};
|
||||
|
||||
/// Sync info
|
||||
#[derive(Default, Debug, Serialize, PartialEq)]
|
||||
@@ -117,8 +118,19 @@ impl Serialize for SyncStatus {
|
||||
}
|
||||
}
|
||||
|
||||
/// Propagation statistics for pending transaction.
|
||||
#[derive(Default, Debug, Serialize)]
|
||||
pub struct TransactionStats {
|
||||
/// Block no this transaction was first seen.
|
||||
#[serde(rename="firstSeen")]
|
||||
pub first_seen: u64,
|
||||
/// Peers this transaction was propagated to with count.
|
||||
#[serde(rename="propagatedTo")]
|
||||
pub propagated_to: BTreeMap<H512, usize>,
|
||||
}
|
||||
|
||||
impl From<SyncPeerInfo> for PeerInfo {
|
||||
fn from(p: SyncPeerInfo) -> PeerInfo {
|
||||
fn from(p: SyncPeerInfo) -> Self {
|
||||
PeerInfo {
|
||||
id: p.id,
|
||||
name: p.client_version,
|
||||
@@ -138,10 +150,23 @@ impl From<SyncPeerInfo> for PeerInfo {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<SyncTransactionStats> for TransactionStats {
|
||||
fn from(s: SyncTransactionStats) -> Self {
|
||||
TransactionStats {
|
||||
first_seen: s.first_seen,
|
||||
propagated_to: s.propagated_to
|
||||
.into_iter()
|
||||
.map(|(id, count)| (id.into(), count))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json;
|
||||
use super::{SyncInfo, SyncStatus, Peers};
|
||||
use std::collections::BTreeMap;
|
||||
use super::{SyncInfo, SyncStatus, Peers, TransactionStats};
|
||||
|
||||
#[test]
|
||||
fn test_serialize_sync_info() {
|
||||
@@ -176,4 +201,17 @@ mod tests {
|
||||
let serialized = serde_json::to_string(&t).unwrap();
|
||||
assert_eq!(serialized, r#"{"startingBlock":"0x0","currentBlock":"0x0","highestBlock":"0x0","warpChunksAmount":null,"warpChunksProcessed":null,"blockGap":["0x1","0x5"]}"#)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_serialize_transaction_stats() {
|
||||
let stats = TransactionStats {
|
||||
first_seen: 100,
|
||||
propagated_to: map![
|
||||
10.into() => 50
|
||||
]
|
||||
};
|
||||
|
||||
let serialized = serde_json::to_string(&stats).unwrap();
|
||||
assert_eq!(serialized, r#"{"firstSeen":100,"propagatedTo":{"0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a":50}}"#)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user