|
| 1 | +// SPDX-License-Identifier: CC0-1.0 |
| 2 | + |
| 3 | +//! The JSON-RPC API for Bitcoin Core v28.0 - network. |
| 4 | +//! |
| 5 | +//! Types for methods found under the `== Network ==` section of the API docs. |
| 6 | +use serde::{Deserialize, Serialize}; |
| 7 | + |
| 8 | +use super::{GetNetworkInfoAddress, GetNetworkInfoError, GetNetworkInfoNetwork}; |
| 9 | +use crate::model; |
| 10 | + |
| 11 | +/// Result of the JSON-RPC method `getnetworkinfo`. |
| 12 | +/// |
| 13 | +/// > getnetworkinfo |
| 14 | +/// |
| 15 | +/// > Returns an object containing various state info regarding P2P networking. |
| 16 | +#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] |
| 17 | +pub struct GetNetworkInfo { |
| 18 | + /// The server version. |
| 19 | + pub version: usize, |
| 20 | + /// The server subversion string. |
| 21 | + pub subversion: String, |
| 22 | + /// The protocol version. |
| 23 | + #[serde(rename = "protocolversion")] |
| 24 | + pub protocol_version: usize, |
| 25 | + /// The services we offer to the network (hex string). |
| 26 | + #[serde(rename = "localservices")] |
| 27 | + pub local_services: String, |
| 28 | + /// `true` if transaction relay is requested from peers. |
| 29 | + #[serde(rename = "localrelay")] |
| 30 | + pub local_relay: bool, |
| 31 | + /// The time offset. |
| 32 | + #[serde(rename = "timeoffset")] |
| 33 | + pub time_offset: isize, |
| 34 | + /// The total number of connections. |
| 35 | + pub connections: usize, |
| 36 | + /// Whether p2p networking is enabled. |
| 37 | + #[serde(rename = "networkactive")] |
| 38 | + pub network_active: bool, |
| 39 | + /// Information per network. |
| 40 | + pub networks: Vec<GetNetworkInfoNetwork>, |
| 41 | + /// Minimum relay fee rate for transactions in BTC/kB. |
| 42 | + #[serde(rename = "relayfee")] |
| 43 | + pub relay_fee: f64, |
| 44 | + /// Minimum fee rate increment for mempool limiting or replacement in BTC/kB. |
| 45 | + #[serde(rename = "incrementalfee")] |
| 46 | + pub incremental_fee: f64, |
| 47 | + /// List of local addresses. |
| 48 | + #[serde(rename = "localaddresses")] |
| 49 | + pub local_addresses: Vec<GetNetworkInfoAddress>, |
| 50 | + /// Any network and blockchain warnings. |
| 51 | + pub warnings: Vec<String>, |
| 52 | +} |
| 53 | + |
| 54 | +impl GetNetworkInfo { |
| 55 | + /// Converts version specific type to a version in-specific, more strongly typed type. |
| 56 | + pub fn into_model(self) -> Result<model::GetNetworkInfo, GetNetworkInfoError> { |
| 57 | + use GetNetworkInfoError as E; |
| 58 | + |
| 59 | + let relay_fee = crate::btc_per_kb(self.relay_fee).map_err(E::RelayFee)?; |
| 60 | + let incremental_fee = crate::btc_per_kb(self.incremental_fee).map_err(E::IncrementalFee)?; |
| 61 | + |
| 62 | + Ok(model::GetNetworkInfo { |
| 63 | + version: self.version, |
| 64 | + subversion: self.subversion, |
| 65 | + protocol_version: self.protocol_version, |
| 66 | + local_services: self.local_services, |
| 67 | + local_relay: self.local_relay, |
| 68 | + time_offset: self.time_offset, |
| 69 | + connections: self.connections, |
| 70 | + network_active: self.network_active, |
| 71 | + networks: self.networks.into_iter().map(|n| n.into_model()).collect(), |
| 72 | + relay_fee, |
| 73 | + incremental_fee, |
| 74 | + local_addresses: self.local_addresses.into_iter().map(|a| a.into_model()).collect(), |
| 75 | + warnings: self.warnings, |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
0 commit comments