6
6
//!
7
7
/// These types do not implement `into_model` because apart from fee rate there is no additional
8
8
/// `rust-bitcoin` types needed.
9
+ use core:: fmt;
10
+ use std:: collections:: BTreeMap ;
11
+
12
+ use bitcoin:: amount:: ParseAmountError ;
13
+ use internals:: write_err;
9
14
use serde:: { Deserialize , Serialize } ;
10
15
16
+ use crate :: model;
17
+
11
18
/// Result of JSON-RPC method `getaddednodeinfo`.
12
19
///
13
20
/// > getaddednodeinfo ( "node" )
@@ -18,17 +25,17 @@ use serde::{Deserialize, Serialize};
18
25
/// > Arguments:
19
26
/// > 1. "node" (string, optional) If provided, return information about this specific node, otherwise all nodes are returned.
20
27
#[ derive( Clone , Debug , PartialEq , Deserialize , Serialize ) ]
21
- pub struct GetAddedNodeInfo ( Vec < AddedNode > ) ;
28
+ pub struct GetAddedNodeInfo ( pub Vec < AddedNode > ) ;
22
29
23
30
/// An item from the list returned by the JSON-RPC method `getaddednodeinfo`.
24
31
#[ derive( Clone , Debug , PartialEq , Deserialize , Serialize ) ]
25
32
pub struct AddedNode {
26
- /// The node IP address or name (as provided to addnode)
33
+ /// The node IP address or name (as provided to addnode).
27
34
#[ serde( rename = "addednode" ) ]
28
35
pub added_node : String ,
29
- /// If connected
36
+ /// If connected.
30
37
pub connected : bool ,
31
- /// Only when connected = true
38
+ /// Only when connected = true.
32
39
pub addresses : Vec < AddedNodeAddress > ,
33
40
}
34
41
@@ -37,7 +44,7 @@ pub struct AddedNode {
37
44
pub struct AddedNodeAddress {
38
45
/// The bitcoin server IP and port we're connected to.
39
46
pub address : String ,
40
- /// connection , inbound or outbound
47
+ /// Connection , inbound or outbound.
41
48
pub connected : String ,
42
49
}
43
50
@@ -105,8 +112,8 @@ pub struct GetNetworkInfo {
105
112
pub time_offset : isize ,
106
113
/// The total number of connections.
107
114
pub connections : usize ,
108
- #[ serde( rename = "networkactive" ) ]
109
115
/// Whether p2p networking is enabled.
116
+ #[ serde( rename = "networkactive" ) ]
110
117
pub network_active : bool ,
111
118
/// Information per network.
112
119
pub networks : Vec < GetNetworkInfoNetwork > ,
@@ -141,14 +148,92 @@ pub struct GetNetworkInfoNetwork {
141
148
/// Part of the result of the JSON-RPC method `getnetworkinfo` (local address info).
142
149
#[ derive( Clone , Debug , PartialEq , Eq , Deserialize , Serialize ) ]
143
150
pub struct GetNetworkInfoAddress {
144
- /// Network address
151
+ /// Network address.
145
152
pub address : String ,
146
- /// Network port
153
+ /// Network port.
147
154
pub port : u16 ,
148
- /// Relative score
155
+ /// Relative score.
149
156
pub score : u32 ,
150
157
}
151
158
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
+
152
237
/// Result of JSON-RPC method `getpeerinfo`.
153
238
///
154
239
/// > getpeerinfo
@@ -160,7 +245,7 @@ pub struct GetPeerInfo(pub Vec<PeerInfo>);
160
245
/// An item from the list returned by the JSON-RPC method `getpeerinfo`.
161
246
#[ derive( Clone , Debug , PartialEq , Deserialize , Serialize ) ]
162
247
pub struct PeerInfo {
163
- /// Peer index
248
+ /// Peer index.
164
249
pub id : u32 ,
165
250
/// The IP address and port of the peer ("host:port").
166
251
#[ serde( rename = "addr" ) ]
@@ -176,22 +261,22 @@ pub struct PeerInfo {
176
261
/// Whether peer has asked us to relay transactions to it.
177
262
#[ serde( rename = "relaytxes" ) ]
178
263
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.
180
265
#[ serde( rename = "lastsend" ) ]
181
266
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.
183
268
#[ serde( rename = "lastrecv" ) ]
184
269
pub last_received : u32 ,
185
- /// The total bytes sent
270
+ /// The total bytes sent.
186
271
#[ serde( rename = "bytessent" ) ]
187
272
pub bytes_sent : u64 ,
188
- /// The total bytes received
273
+ /// The total bytes received.
189
274
#[ serde( rename = "bytesrecv" ) ]
190
275
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).
192
277
#[ serde( rename = "conntime" ) ]
193
278
pub connection_time : u32 ,
194
- /// The time offset in seconds
279
+ /// The time offset in seconds.
195
280
#[ serde( rename = "timeoffset" ) ]
196
281
pub time_offset : u32 ,
197
282
/// Ping time (if available).
@@ -206,7 +291,8 @@ pub struct PeerInfo {
206
291
/// The peer version, such as 70001.
207
292
pub version : u32 ,
208
293
/// The string version (e.g. "/Satoshi:0.8.5/").
209
- pub subver : String ,
294
+ #[ serde( rename = "subver" ) ]
295
+ pub subversion : String ,
210
296
/// Inbound (true) or Outbound (false).
211
297
pub inbound : bool ,
212
298
/// Whether connection was due to addnode/-connect or if it was an automatic/inbound connection.
@@ -227,14 +313,21 @@ pub struct PeerInfo {
227
313
/// Whether the peer is whitelisted.
228
314
pub whitelisted : bool ,
229
315
/// 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 > ,
231
318
/// 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 > ,
233
321
}
234
322
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.
236
328
#[ 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