Skip to content

Commit 018689a

Browse files
authored
Add diagnostics query (#37)
* update freenet-stdlib version to 0.1.10 and add node diagnostics query support * Clean diagnostic structure
1 parent 8caf4ac commit 018689a

File tree

3 files changed

+157
-21
lines changed

3 files changed

+157
-21
lines changed

rust/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "freenet-stdlib"
3-
version = "0.1.9"
3+
version = "0.1.10"
44
edition = "2021"
55
rust-version = "1.71.1"
66
publish = true

rust/src/client_api/client_events.rs

Lines changed: 145 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -259,14 +259,13 @@ pub enum ClientRequest<'a> {
259259
}
260260

261261
#[derive(Serialize, Deserialize, Debug, Clone)]
262-
pub enum NodeQuery {
263-
ConnectedPeers,
264-
SubscriptionInfo,
265-
}
262+
pub struct ConnectedPeers {}
266263

267-
// For backward compatibility
268264
#[derive(Serialize, Deserialize, Debug, Clone)]
269-
pub struct ConnectedPeers {}
265+
pub struct NodeDiagnostics {
266+
/// Optional contract key to filter diagnostics for specific contract
267+
pub contract_key: Option<ContractKey>,
268+
}
270269

271270
impl ClientRequest<'_> {
272271
pub fn into_owned(self) -> ClientRequest<'static> {
@@ -724,22 +723,153 @@ pub enum HostResponse<T = WrappedState> {
724723
type Peer = String;
725724

726725
#[derive(Serialize, Deserialize, Debug)]
726+
pub enum QueryResponse {
727+
ConnectedPeers { peers: Vec<(Peer, SocketAddr)> },
728+
NetworkDebug(NetworkDebugInfo),
729+
NodeDiagnostics(NodeDiagnosticsResponse),
730+
}
731+
732+
#[derive(Serialize, Deserialize, Debug, Clone)]
733+
pub struct NetworkDebugInfo {
734+
pub subscriptions: Vec<SubscriptionInfo>,
735+
pub connected_peers: Vec<(String, SocketAddr)>,
736+
}
737+
738+
#[derive(Serialize, Deserialize, Debug, Clone)]
739+
pub struct NodeDiagnosticsResponse {
740+
/// Node information
741+
pub node_info: Option<NodeInfo>,
742+
743+
/// Network connectivity information
744+
pub network_info: Option<NetworkInfo>,
745+
746+
/// Contract subscription information
747+
pub subscriptions: Vec<SubscriptionInfo>,
748+
749+
/// Contract states for specific contracts
750+
pub contract_states: std::collections::HashMap<ContractKey, ContractState>,
751+
752+
/// System metrics
753+
pub system_metrics: Option<SystemMetrics>,
754+
755+
/// Information about connected peers with detailed data
756+
pub connected_peers_detailed: Vec<ConnectedPeerInfo>,
757+
}
758+
759+
#[derive(Serialize, Deserialize, Debug, Clone)]
760+
pub struct NodeInfo {
761+
pub peer_id: String,
762+
pub is_gateway: bool,
763+
pub location: String,
764+
pub uptime_seconds: u64,
765+
}
766+
767+
#[derive(Serialize, Deserialize, Debug, Clone)]
768+
pub struct NetworkInfo {
769+
pub connected_peers: Vec<(String, String)>, // (peer_id, address)
770+
pub active_connections: usize,
771+
}
772+
773+
#[derive(Serialize, Deserialize, Debug, Clone)]
774+
pub struct ContractState {
775+
/// Number of nodes subscribed to this contract
776+
pub subscribers: u32,
777+
/// Peer IDs of nodes that are subscribed to this contract
778+
pub subscriber_peer_ids: Vec<String>,
779+
}
780+
781+
#[derive(Serialize, Deserialize, Debug, Clone)]
782+
pub struct SystemMetrics {
783+
pub active_connections: u32,
784+
pub seeding_contracts: u32,
785+
}
786+
787+
#[derive(Serialize, Deserialize, Debug, Clone)]
727788
pub struct SubscriptionInfo {
728789
pub contract_key: ContractKey,
729790
pub client_id: usize,
730-
pub last_update: Option<std::time::SystemTime>,
731791
}
732792

733-
#[derive(Serialize, Deserialize, Debug)]
734-
pub struct NetworkDebugInfo {
735-
pub subscriptions: Vec<SubscriptionInfo>,
736-
pub connected_peers: Vec<(Peer, SocketAddr)>,
793+
/// Basic information about a connected peer
794+
#[derive(Serialize, Deserialize, Debug, Clone)]
795+
pub struct ConnectedPeerInfo {
796+
pub peer_id: String,
797+
pub address: String,
737798
}
738799

739-
#[derive(Serialize, Deserialize, Debug)]
740-
pub enum QueryResponse {
741-
ConnectedPeers { peers: Vec<(Peer, SocketAddr)> },
742-
NetworkDebug(NetworkDebugInfo),
800+
#[derive(Serialize, Deserialize, Debug, Clone)]
801+
pub enum NodeQuery {
802+
ConnectedPeers,
803+
SubscriptionInfo,
804+
NodeDiagnostics {
805+
/// Diagnostic configuration specifying what information to collect
806+
config: NodeDiagnosticsConfig,
807+
},
808+
}
809+
810+
#[derive(Serialize, Deserialize, Debug, Clone)]
811+
pub struct NodeDiagnosticsConfig {
812+
/// Include basic node information (ID, location, uptime, etc.)
813+
pub include_node_info: bool,
814+
815+
/// Include network connectivity information
816+
pub include_network_info: bool,
817+
818+
/// Include contract subscription information
819+
pub include_subscriptions: bool,
820+
821+
/// Include contract states for specific contracts (empty = all contracts)
822+
pub contract_keys: Vec<ContractKey>,
823+
824+
/// Include memory and performance metrics
825+
pub include_system_metrics: bool,
826+
827+
/// Include detailed information about connected peers (vs basic peer list)
828+
pub include_detailed_peer_info: bool,
829+
830+
/// Include peer IDs of subscribers in contract state information
831+
pub include_subscriber_peer_ids: bool,
832+
}
833+
834+
impl NodeDiagnosticsConfig {
835+
/// Create a comprehensive diagnostic config for debugging update propagation issues
836+
pub fn for_update_propagation_debugging(contract_key: ContractKey) -> Self {
837+
Self {
838+
include_node_info: true,
839+
include_network_info: true,
840+
include_subscriptions: true,
841+
contract_keys: vec![contract_key],
842+
include_system_metrics: true,
843+
include_detailed_peer_info: true,
844+
include_subscriber_peer_ids: true,
845+
}
846+
}
847+
848+
/// Create a lightweight diagnostic config for basic node status
849+
pub fn basic_status() -> Self {
850+
Self {
851+
include_node_info: true,
852+
include_network_info: true,
853+
include_subscriptions: false,
854+
contract_keys: vec![],
855+
include_system_metrics: false,
856+
include_detailed_peer_info: false,
857+
include_subscriber_peer_ids: false,
858+
}
859+
}
860+
861+
/// Create a full diagnostic config (all information)
862+
pub fn full() -> Self {
863+
Self {
864+
include_node_info: true,
865+
include_network_info: true,
866+
include_subscriptions: true,
867+
contract_keys: vec![], // empty = all contracts
868+
include_system_metrics: true,
869+
include_detailed_peer_info: true,
870+
include_subscriber_peer_ids: true,
871+
}
872+
}
743873
}
744874

745875
impl HostResponse {

rust/src/versioning.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,14 @@ impl DelegateCode<'static> {
124124
mut contract_data: Cursor<Vec<u8>>,
125125
) -> Result<(Self, APIVersion), std::io::Error> {
126126
// Get contract version
127-
let version = contract_data
128-
.read_u64::<BigEndian>()
129-
.map_err(|_| std::io::Error::new(std::io::ErrorKind::InvalidData, "Failed to read version"))?;
127+
let version = contract_data.read_u64::<BigEndian>().map_err(|_| {
128+
std::io::Error::new(std::io::ErrorKind::InvalidData, "Failed to read version")
129+
})?;
130130
let version = APIVersion::from_u64(version).map_err(|e| {
131-
std::io::Error::new(std::io::ErrorKind::InvalidData, format!("Version error: {}", e))
131+
std::io::Error::new(
132+
std::io::ErrorKind::InvalidData,
133+
format!("Version error: {}", e),
134+
)
132135
})?;
133136

134137
if version == APIVersion::Version0_0_1 {
@@ -307,7 +310,10 @@ impl ContractCode<'static> {
307310
std::io::Error::new(std::io::ErrorKind::InvalidData, "Failed to read version")
308311
})?;
309312
let version = APIVersion::from_u64(version).map_err(|e| {
310-
std::io::Error::new(std::io::ErrorKind::InvalidData, format!("Version error: {}", e))
313+
std::io::Error::new(
314+
std::io::ErrorKind::InvalidData,
315+
format!("Version error: {}", e),
316+
)
311317
})?;
312318

313319
if version == APIVersion::Version0_0_1 {

0 commit comments

Comments
 (0)