Add admin RPC to dump discovered peers (#127)

* add admin rpc to dump peers

* allow to specify file name

* return listen addreses in rpc

* add trace for admin rpc parameters
This commit is contained in:
Bo QIU 2024-07-15 18:19:30 +08:00 committed by GitHub
parent 504a67fdd0
commit 0f9f1c7bf2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 26 additions and 3 deletions

View File

@ -12,6 +12,7 @@ futures = "0.3.21"
jsonrpsee = { version = "0.14.0", features = ["full"] }
network = { path = "../network" }
serde = { version = "1.0.137", features = ["derive"] }
serde_json = "1.0.82"
base64 = "0.13.0"
shared_types = { path = "../shared_types" }
sync = { path = "../sync" }
@ -24,6 +25,3 @@ storage-async = { path = "../storage-async" }
merkle_light = { path = "../../common/merkle_light" }
merkle_tree = { path = "../../common/merkle_tree"}
futures-channel = "^0.3"
[dev-dependencies]
serde_json = "1.0.82"

View File

@ -32,4 +32,7 @@ pub trait Rpc {
#[method(name = "getNetworkInfo")]
async fn get_network_info(&self) -> RpcResult<NetworkInfo>;
#[method(name = "dumpPeers")]
async fn dump_peers(&self, file: Option<String>) -> RpcResult<usize>;
}

View File

@ -4,7 +4,9 @@ use crate::{error, Context};
use futures::prelude::*;
use jsonrpsee::core::async_trait;
use jsonrpsee::core::RpcResult;
use network::PeerInfo;
use std::collections::HashMap;
use std::fs::File;
use sync::{FileSyncInfo, SyncRequest, SyncResponse};
use task_executor::ShutdownReason;
@ -138,6 +140,7 @@ impl RpcServer for RpcServerImpl {
Ok(NetworkInfo {
peer_id: self.ctx.network_globals.local_peer_id().to_base58(),
listen_addresses: self.ctx.network_globals.listen_multiaddrs(),
total_peers: db.peers().count(),
banned_peers: db.banned_peers().count(),
disconnected_peers: db.disconnected_peers().count(),
@ -146,4 +149,21 @@ impl RpcServer for RpcServerImpl {
connected_incoming_peers: connected_peers - connected_outgoing_peers,
})
}
#[tracing::instrument(skip(self), err)]
async fn dump_peers(&self, file: Option<String>) -> RpcResult<usize> {
info!("admin_dumpPeers()");
let db = self.ctx.network_globals.peers.read();
let peers: HashMap<String, PeerInfo> = db
.peers()
.map(|(peer_id, info)| (peer_id.to_base58(), info.clone()))
.collect();
let file = File::create(file.unwrap_or("peers.json".into()))?;
serde_json::to_writer_pretty(&file, &peers)?;
Ok(peers.len())
}
}

View File

@ -4,6 +4,7 @@ use jsonrpsee::core::RpcResult;
use merkle_light::hash::Algorithm;
use merkle_light::merkle::{log2_pow2, next_pow2, MerkleTree};
use merkle_tree::RawLeafSha3Algorithm;
use network::Multiaddr;
use serde::{Deserialize, Serialize};
use shared_types::{
compute_padded_chunk_size, compute_segment_size, DataRoot, FileProof, Transaction, CHUNK_SIZE,
@ -29,6 +30,7 @@ pub struct Status {
#[serde(rename_all = "camelCase")]
pub struct NetworkInfo {
pub peer_id: String,
pub listen_addresses: Vec<Multiaddr>,
pub total_peers: usize,
pub banned_peers: usize,
pub disconnected_peers: usize,