Skip to content
This repository was archived by the owner on Nov 26, 2024. It is now read-only.

Commit 53f679a

Browse files
authored
v17: Finish network section (#42)
Finish all RPC methods under the `network` section of the docs. Do a bunch of clean ups on the way.
2 parents af6db57 + c3b9b6d commit 53f679a

File tree

12 files changed

+245
-39
lines changed

12 files changed

+245
-39
lines changed

client/src/client_sync/v17/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ crate::impl_client_v17__generatetoaddress!();
4646
crate::impl_client_v17__generate!();
4747

4848
// == Network ==
49+
crate::impl_client_v17__getaddednodeinfo!();
4950
crate::impl_client_v17__getnettotals!();
5051
crate::impl_client_v17__getnetworkinfo!();
5152
crate::impl_client_v17__getpeerinfo!();

client/src/client_sync/v17/network.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@
99
//!
1010
//! See, or use the `define_jsonrpc_minreq_client!` macro to define a `Client`.
1111
12+
/// Implements bitcoind JSON-RPC API method `getaddednodeinfo`
13+
#[macro_export]
14+
macro_rules! impl_client_v17__getaddednodeinfo {
15+
() => {
16+
impl Client {
17+
pub fn get_added_node_info(&self) -> Result<GetAddedNodeInfo> {
18+
self.call("getaddednodeinfo", &[])
19+
}
20+
}
21+
};
22+
}
23+
1224
/// Implements bitcoind JSON-RPC API method `getnettotals`
1325
#[macro_export]
1426
macro_rules! impl_client_v17__getnettotals {

integration_test/src/v17/network.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@
55
//! Specifically this is methods found under the `== Network ==` section of the
66
//! API docs of `bitcoind v0.17.1`.
77
8+
/// Requires `Client` to be in scope and to implement `get_network_info`.
9+
#[macro_export]
10+
macro_rules! impl_test_v17__getaddednodeinfo {
11+
() => {
12+
#[test]
13+
fn get_added_node_info() {
14+
let bitcoind = $crate::bitcoind_no_wallet();
15+
let _ = bitcoind.client.get_added_node_info().expect("getaddednodeinfo");
16+
}
17+
};
18+
}
19+
820
/// Requires `Client` to be in scope and to implement `get_network_info`.
921
#[macro_export]
1022
macro_rules! impl_test_v17__getnettotals {
@@ -17,15 +29,18 @@ macro_rules! impl_test_v17__getnettotals {
1729
};
1830
}
1931

20-
/// Requires `Client` to be in scope and to implement `get_network_info`.
32+
/// Requires `Client` to be in scope and to implement `get_network_info` and
33+
/// `check_expected_server_version`.
2134
#[macro_export]
2235
macro_rules! impl_test_v17__getnetworkinfo {
2336
() => {
2437
#[test]
2538
fn get_network_info() {
2639
let bitcoind = $crate::bitcoind_no_wallet();
27-
let _ = bitcoind.client.get_network_info().expect("getnetworkinfo");
40+
let json = bitcoind.client.get_network_info().expect("getnetworkinfo");
41+
assert!(json.into_model().is_ok());
2842

43+
// Server version is returned as part of the getnetworkinfo method.
2944
bitcoind.client.check_expected_server_version().expect("unexpected version");
3045
}
3146
};

integration_test/tests/v17_api.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ mod generating {
4343
mod network {
4444
use super::*;
4545

46+
impl_test_v17__getaddednodeinfo!();
4647
impl_test_v17__getnettotals!();
4748
impl_test_v17__getnetworkinfo!();
4849
impl_test_v17__getpeerinfo!();

json/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ pub mod model;
2525

2626
use std::fmt;
2727

28+
use bitcoin::amount::ParseAmountError;
29+
use bitcoin::{Amount, FeeRate};
30+
2831
/// Converts an `i64` numeric type to a `u32`.
2932
///
3033
/// The Bitcoin Core JSONRPC API has fields marked as 'numeric'. It is not obvious what Rust
@@ -68,3 +71,15 @@ impl fmt::Display for NumericError {
6871
}
6972

7073
impl std::error::Error for NumericError {}
74+
75+
/// Converts `fee_rate` in BTC/kB to `FeeRate`.
76+
fn btc_per_kb(btc_per_kb: f64) -> Result<Option<FeeRate>, ParseAmountError> {
77+
let btc_per_byte = btc_per_kb / 1000_f64;
78+
let sats_per_byte = Amount::from_btc(btc_per_byte)?;
79+
80+
// Virtual bytes equal bytes before segwit.
81+
let rate = FeeRate::from_sat_per_vb(sats_per_byte.to_sat());
82+
83+
Ok(rate)
84+
}
85+

json/src/model/control.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
//!
55
//! These structs model the types returned by the JSON-RPC API but have concrete types
66
//! and are not specific to a specific version of Bitcoin Core.
7+
8+
// Control types currently have no need for model equivalents.

json/src/model/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ mod blockchain;
1212
mod control;
1313
mod generating;
1414
mod mining;
15+
mod network;
1516
mod raw_transactions;
1617
mod util;
1718
mod wallet;
@@ -32,6 +33,7 @@ pub use self::{
3233
GetMempoolAncestorsVerbose, GetTxOut, Softfork, SoftforkType,
3334
},
3435
generating::{Generate, GenerateToAddress},
36+
network::{GetNetworkInfo, GetNetworkInfoAddress, GetNetworkInfoNetwork},
3537
raw_transactions::SendRawTransaction,
3638
wallet::{
3739
CreateWallet, GetBalance, GetBalances, GetBalancesMine, GetBalancesWatchOnly,

json/src/model/network.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// SPDX-License-Identifier: CC0-1.0
2+
3+
//! Types for methods found under the `== Network ==` section of the API docs.
4+
//!
5+
//! These structs model the types returned by the JSON-RPC API but have concrete types
6+
//! and are not specific to a specific version of Bitcoin Core.
7+
8+
use bitcoin::FeeRate;
9+
use serde::{Deserialize, Serialize};
10+
11+
/// Models the result of JSON-RPC method `getnetworkinfo`.
12+
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
13+
pub struct GetNetworkInfo {
14+
/// The server version.
15+
pub version: usize,
16+
/// The server subversion string.
17+
pub subversion: String,
18+
/// The protocol version.
19+
pub protocol_version: usize,
20+
/// The services we offer to the network (hex string).
21+
pub local_services: String,
22+
/// `true` if transaction relay is requested from peers.
23+
pub local_relay: bool,
24+
/// The time offset.
25+
pub time_offset: isize,
26+
/// The total number of connections.
27+
pub connections: usize,
28+
/// Whether p2p networking is enabled.
29+
pub network_active: bool,
30+
/// Information per network.
31+
pub networks: Vec<GetNetworkInfoNetwork>,
32+
/// Minimum relay fee rate for transactions.
33+
pub relay_fee: Option<FeeRate>, // `Some` if parsing succeeds.
34+
/// Minimum fee rate increment for mempool limiting or replacement.
35+
pub incremental_fee: Option<FeeRate>, // `Some` if parsing succeeds.
36+
/// List of local addresses.
37+
pub local_addresses: Vec<GetNetworkInfoAddress>,
38+
/// Any network and blockchain warnings.
39+
pub warnings: String,
40+
}
41+
42+
/// Part of the result of the JSON-RPC method `getnetworkinfo` (information per network).
43+
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
44+
pub struct GetNetworkInfoNetwork {
45+
/// Network (ipv4, ipv6, onion, i2p, cjdns).
46+
pub name: String,
47+
/// Is the network limited using -onlynet?.
48+
pub limited: bool,
49+
/// Is the network reachable?
50+
pub reachable: bool,
51+
/// ("host:port"): The proxy that is used for this network, or empty if none.
52+
pub proxy: String,
53+
/// Whether randomized credentials are used.
54+
pub proxy_randomize_credentials: bool,
55+
}
56+
57+
/// Part of the result of the JSON-RPC method `getnetworkinfo` (local address info).
58+
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
59+
pub struct GetNetworkInfoAddress {
60+
/// Network address.
61+
pub address: String,
62+
/// Network port.
63+
pub port: u16,
64+
/// Relative score.
65+
pub score: u32,
66+
}

json/src/v17/blockchain.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ impl GetBestBlockHash {
4242
/// Result of JSON-RPC method `getblock` with verbosity set to 0.
4343
///
4444
/// A string that is serialized, hex-encoded data for block 'hash'.
45-
///
46-
/// Method call: `getblock "blockhash" ( verbosity )`
4745
#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
4846
pub struct GetBlockVerbosityZero(pub String);
4947

json/src/v17/mod.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
//! in the `model` module are version non-specific and are strongly typed using `rust-bitcoin`.
99
//!
1010
//! Key:
11-
//! - `[ ]` means not yet done.
12-
//! - `[x]` marks means implemented _and_ tested.
13-
//! - `[-]` means it was considered and intentionally not done.
11+
//! - `[ ]` Not yet done.
12+
//! - `[x]` Implemented _and_ tested.
13+
//! - `[-]` Intentionally not done, typically because method does not return anything, returns
14+
//! a single integer, or is deprecated.
1415
//!
1516
//! **== Blockchain ==**
1617
//! - [x] `getbestblockhash`
@@ -60,7 +61,7 @@
6061
//! - [-] `addnode "node" "add|remove|onetry"`
6162
//! - [-] `clearbanned`
6263
//! - [-] `disconnectnode "[address]" [nodeid]`
63-
//! - [ ] `getaddednodeinfo ( "node" )`
64+
//! - [x] `getaddednodeinfo ( "node" )`
6465
//! - [-] `getconnectioncount`
6566
//! - [x] `getnettotals`
6667
//! - [x] `getnetworkinfo`
@@ -180,8 +181,9 @@ pub use self::{
180181
control::{GetMemoryInfoStats, Locked, Logging, Uptime},
181182
generating::{Generate, GenerateToAddress},
182183
network::{
183-
AddedNodeAddress, BytesPerMessage, GetAddedNodeInfo, GetNetTotals, GetNetworkInfo,
184-
GetNetworkInfoAddress, GetNetworkInfoNetwork, GetPeerInfo, PeerInfo, UploadTarget,
184+
AddedNode, AddedNodeAddress, Banned, GetAddedNodeInfo, GetNetTotals, GetNetworkInfo,
185+
GetNetworkInfoAddress, GetNetworkInfoNetwork, GetPeerInfo, ListBanned, PeerInfo,
186+
UploadTarget,
185187
},
186188
raw_transactions::SendRawTransaction,
187189
wallet::{

0 commit comments

Comments
 (0)