66//!
77/// These types do not implement `into_model` because apart from fee rate there is no additional
88/// `rust-bitcoin` types needed.
9+ use core:: fmt;
10+ use std:: collections:: BTreeMap ;
11+
12+ use bitcoin:: amount:: ParseAmountError ;
13+ use internals:: write_err;
914use serde:: { Deserialize , Serialize } ;
1015
16+ use crate :: model;
17+
1118/// Result of JSON-RPC method `getaddednodeinfo`.
1219///
1320/// > getaddednodeinfo ( "node" )
@@ -18,17 +25,17 @@ use serde::{Deserialize, Serialize};
1825/// > Arguments:
1926/// > 1. "node" (string, optional) If provided, return information about this specific node, otherwise all nodes are returned.
2027#[ derive( Clone , Debug , PartialEq , Deserialize , Serialize ) ]
21- pub struct GetAddedNodeInfo ( Vec < AddedNode > ) ;
28+ pub struct GetAddedNodeInfo ( pub Vec < AddedNode > ) ;
2229
2330/// An item from the list returned by the JSON-RPC method `getaddednodeinfo`.
2431#[ derive( Clone , Debug , PartialEq , Deserialize , Serialize ) ]
2532pub struct AddedNode {
26- /// The node IP address or name (as provided to addnode)
33+ /// The node IP address or name (as provided to addnode).
2734 #[ serde( rename = "addednode" ) ]
2835 pub added_node : String ,
29- /// If connected
36+ /// If connected.
3037 pub connected : bool ,
31- /// Only when connected = true
38+ /// Only when connected = true.
3239 pub addresses : Vec < AddedNodeAddress > ,
3340}
3441
@@ -37,7 +44,7 @@ pub struct AddedNode {
3744pub struct AddedNodeAddress {
3845 /// The bitcoin server IP and port we're connected to.
3946 pub address : String ,
40- /// connection , inbound or outbound
47+ /// Connection , inbound or outbound.
4148 pub connected : String ,
4249}
4350
@@ -105,8 +112,8 @@ pub struct GetNetworkInfo {
105112 pub time_offset : isize ,
106113 /// The total number of connections.
107114 pub connections : usize ,
108- #[ serde( rename = "networkactive" ) ]
109115 /// Whether p2p networking is enabled.
116+ #[ serde( rename = "networkactive" ) ]
110117 pub network_active : bool ,
111118 /// Information per network.
112119 pub networks : Vec < GetNetworkInfoNetwork > ,
@@ -141,14 +148,92 @@ pub struct GetNetworkInfoNetwork {
141148/// Part of the result of the JSON-RPC method `getnetworkinfo` (local address info).
142149#[ derive( Clone , Debug , PartialEq , Eq , Deserialize , Serialize ) ]
143150pub struct GetNetworkInfoAddress {
144- /// Network address
151+ /// Network address.
145152 pub address : String ,
146- /// Network port
153+ /// Network port.
147154 pub port : u16 ,
148- /// Relative score
155+ /// Relative score.
149156 pub score : u32 ,
150157}
151158
159+ impl GetNetworkInfo {
160+ /// Converts version specific type to a version in-specific, more strongly typed type.
161+ pub fn into_model ( self ) -> Result < model:: GetNetworkInfo , GetNetworkInfoError > {
162+ use GetNetworkInfoError as E ;
163+
164+ let relay_fee = crate :: btc_per_kb ( self . relay_fee ) . map_err ( E :: RelayFee ) ?;
165+ let incremental_fee = crate :: btc_per_kb ( self . incremental_fee ) . map_err ( E :: IncrementalFee ) ?;
166+
167+ Ok ( model:: GetNetworkInfo {
168+ version : self . version ,
169+ subversion : self . subversion ,
170+ protocol_version : self . protocol_version ,
171+ local_services : self . local_services ,
172+ local_relay : self . local_relay ,
173+ time_offset : self . time_offset ,
174+ connections : self . connections ,
175+ network_active : self . network_active ,
176+ networks : self . networks . into_iter ( ) . map ( |n| n. into_model ( ) ) . collect ( ) ,
177+ relay_fee,
178+ incremental_fee,
179+ local_addresses : self . local_addresses . into_iter ( ) . map ( |a| a. into_model ( ) ) . collect ( ) ,
180+ warnings : self . warnings ,
181+ } )
182+ }
183+ }
184+
185+ /// Error when converting a `GetTransaction` type into the model type.
186+ #[ derive( Debug ) ]
187+ pub enum GetNetworkInfoError {
188+ /// Conversion of the `relay_fee` field failed.
189+ RelayFee ( ParseAmountError ) ,
190+ /// Conversion of the `incremental_fee` field failed.
191+ IncrementalFee ( ParseAmountError ) ,
192+ }
193+
194+ impl fmt:: Display for GetNetworkInfoError {
195+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
196+ use GetNetworkInfoError as E ;
197+
198+ match * self {
199+ E :: RelayFee ( ref e) => write_err ! ( f, "conversion of the `relay_fee` field failed" ; e) ,
200+ E :: IncrementalFee ( ref e) =>
201+ write_err ! ( f, "conversion of the `incremental_fee` field failed" ; e) ,
202+ }
203+ }
204+ }
205+
206+ impl std:: error:: Error for GetNetworkInfoError {
207+ fn source ( & self ) -> Option < & ( dyn std:: error:: Error + ' static ) > {
208+ use GetNetworkInfoError as E ;
209+
210+ match * self {
211+ E :: RelayFee ( ref e) => Some ( e) ,
212+ E :: IncrementalFee ( ref e) => Some ( e) ,
213+ }
214+ }
215+ }
216+
217+ impl GetNetworkInfoNetwork {
218+ /// Converts version specific type to a version in-specific, more strongly typed type.
219+ pub fn into_model ( self ) -> model:: GetNetworkInfoNetwork {
220+ model:: GetNetworkInfoNetwork {
221+ name : self . name ,
222+ limited : self . limited ,
223+ reachable : self . reachable ,
224+ proxy : self . proxy ,
225+ proxy_randomize_credentials : self . proxy_randomize_credentials ,
226+ }
227+ }
228+ }
229+
230+ impl GetNetworkInfoAddress {
231+ /// Converts version specific type to a version in-specific, more strongly typed type.
232+ pub fn into_model ( self ) -> model:: GetNetworkInfoAddress {
233+ model:: GetNetworkInfoAddress { address : self . address , port : self . port , score : self . score }
234+ }
235+ }
236+
152237/// Result of JSON-RPC method `getpeerinfo`.
153238///
154239/// > getpeerinfo
@@ -160,7 +245,7 @@ pub struct GetPeerInfo(pub Vec<PeerInfo>);
160245/// An item from the list returned by the JSON-RPC method `getpeerinfo`.
161246#[ derive( Clone , Debug , PartialEq , Deserialize , Serialize ) ]
162247pub struct PeerInfo {
163- /// Peer index
248+ /// Peer index.
164249 pub id : u32 ,
165250 /// The IP address and port of the peer ("host:port").
166251 #[ serde( rename = "addr" ) ]
@@ -176,22 +261,22 @@ pub struct PeerInfo {
176261 /// Whether peer has asked us to relay transactions to it.
177262 #[ serde( rename = "relaytxes" ) ]
178263 pub relay_transactions : bool ,
179- /// The time in seconds since epoch (Jan 1 1970 GMT) of the last send
264+ /// The time in seconds since epoch (Jan 1 1970 GMT) of the last send.
180265 #[ serde( rename = "lastsend" ) ]
181266 pub last_send : u32 ,
182- /// The time in seconds since epoch (Jan 1 1970 GMT) of the last receive
267+ /// The time in seconds since epoch (Jan 1 1970 GMT) of the last receive.
183268 #[ serde( rename = "lastrecv" ) ]
184269 pub last_received : u32 ,
185- /// The total bytes sent
270+ /// The total bytes sent.
186271 #[ serde( rename = "bytessent" ) ]
187272 pub bytes_sent : u64 ,
188- /// The total bytes received
273+ /// The total bytes received.
189274 #[ serde( rename = "bytesrecv" ) ]
190275 pub bytes_received : u64 ,
191- /// The connection time in seconds since epoch (Jan 1 1970 GMT)
276+ /// The connection time in seconds since epoch (Jan 1 1970 GMT).
192277 #[ serde( rename = "conntime" ) ]
193278 pub connection_time : u32 ,
194- /// The time offset in seconds
279+ /// The time offset in seconds.
195280 #[ serde( rename = "timeoffset" ) ]
196281 pub time_offset : u32 ,
197282 /// Ping time (if available).
@@ -206,7 +291,8 @@ pub struct PeerInfo {
206291 /// The peer version, such as 70001.
207292 pub version : u32 ,
208293 /// The string version (e.g. "/Satoshi:0.8.5/").
209- pub subver : String ,
294+ #[ serde( rename = "subver" ) ]
295+ pub subversion : String ,
210296 /// Inbound (true) or Outbound (false).
211297 pub inbound : bool ,
212298 /// Whether connection was due to addnode/-connect or if it was an automatic/inbound connection.
@@ -227,14 +313,21 @@ pub struct PeerInfo {
227313 /// Whether the peer is whitelisted.
228314 pub whitelisted : bool ,
229315 /// The total bytes sent aggregated by message type.
230- pub bytes_sent_per_message : Vec < BytesPerMessage > ,
316+ #[ serde( rename = "bytessent_per_msg" ) ]
317+ pub bytes_sent_per_message : BTreeMap < String , u64 > ,
231318 /// The total bytes received aggregated by message type.
232- pub bytes_received_per_message : Vec < BytesPerMessage > ,
319+ #[ serde( rename = "bytesrecv_per_msg" ) ]
320+ pub bytes_received_per_message : BTreeMap < String , u64 > ,
233321}
234322
235- /// An item from the list returned by the JSON-RPC method `getpeerinfo`.
323+ /// Result of JSON-RPC method `listbanned`.
324+ ///
325+ /// > listbanned
326+ ///
327+ /// > List all banned IPs/Subnets.
236328#[ derive( Clone , Debug , PartialEq , Deserialize , Serialize ) ]
237- pub struct BytesPerMessage {
238- #[ serde( rename = "addr" ) ]
239- pub address : u32 , // FIXME: This looks wrong.
240- }
329+ pub struct ListBanned ( pub Vec < Banned > ) ;
330+
331+ /// An item from the list returned by the JSON-RPC method `listbanned`
332+ #[ derive( Clone , Debug , PartialEq , Deserialize , Serialize ) ]
333+ pub struct Banned ( String ) ; // FIXME: The docs are empty so I don't know what shape this is.
0 commit comments