Skip to content

Commit

Permalink
v17: Finish network section
Browse files Browse the repository at this point in the history
Finish implementing all JSON types for stuff under the network section.
Includes various cleanups and testing.
  • Loading branch information
tcharding committed Oct 15, 2024
1 parent 8fcceff commit 7e668f8
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 29 deletions.
1 change: 1 addition & 0 deletions client/src/client_sync/v17/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ crate::impl_client_v17__generatetoaddress!();
crate::impl_client_v17__generate!();

// == Network ==
crate::impl_client_v17__getaddednodeinfo!();
crate::impl_client_v17__getnettotals!();
crate::impl_client_v17__getnetworkinfo!();
crate::impl_client_v17__getpeerinfo!();
Expand Down
12 changes: 12 additions & 0 deletions client/src/client_sync/v17/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@
//!
//! See, or use the `define_jsonrpc_minreq_client!` macro to define a `Client`.

/// Implements bitcoind JSON-RPC API method `getaddednodeinfo`
#[macro_export]
macro_rules! impl_client_v17__getaddednodeinfo {
() => {
impl Client {
pub fn get_added_node_info(&self) -> Result<GetAddedNodeInfo> {
self.call("getaddednodeinfo", &[])
}
}
};
}

/// Implements bitcoind JSON-RPC API method `getnettotals`
#[macro_export]
macro_rules! impl_client_v17__getnettotals {
Expand Down
19 changes: 17 additions & 2 deletions integration_test/src/v17/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
//! Specifically this is methods found under the `== Network ==` section of the
//! API docs of `bitcoind v0.17.1`.

/// Requires `Client` to be in scope and to implement `get_network_info`.
#[macro_export]
macro_rules! impl_test_v17__getaddednodeinfo {
() => {
#[test]
fn get_added_node_info() {
let bitcoind = $crate::bitcoind_no_wallet();
let _ = bitcoind.client.get_added_node_info().expect("getaddednodeinfo");
}
};
}

/// Requires `Client` to be in scope and to implement `get_network_info`.
#[macro_export]
macro_rules! impl_test_v17__getnettotals {
Expand All @@ -17,15 +29,18 @@ macro_rules! impl_test_v17__getnettotals {
};
}

/// Requires `Client` to be in scope and to implement `get_network_info`.
/// Requires `Client` to be in scope and to implement `get_network_info` and
/// `check_expected_server_version`.
#[macro_export]
macro_rules! impl_test_v17__getnetworkinfo {
() => {
#[test]
fn get_network_info() {
let bitcoind = $crate::bitcoind_no_wallet();
let _ = bitcoind.client.get_network_info().expect("getnetworkinfo");
let json = bitcoind.client.get_network_info().expect("getnetworkinfo");
assert!(json.into_model().is_ok());

// Server version is returned as part of the getnetworkinfo method.
bitcoind.client.check_expected_server_version().expect("unexpected version");
}
};
Expand Down
1 change: 1 addition & 0 deletions integration_test/tests/v17_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ mod generating {
mod network {
use super::*;

impl_test_v17__getaddednodeinfo!();
impl_test_v17__getnettotals!();
impl_test_v17__getnetworkinfo!();
impl_test_v17__getpeerinfo!();
Expand Down
15 changes: 15 additions & 0 deletions json/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ pub mod model;

use std::fmt;

use bitcoin::amount::ParseAmountError;
use bitcoin::{Amount, FeeRate};

/// Converts an `i64` numeric type to a `u32`.
///
/// The Bitcoin Core JSONRPC API has fields marked as 'numeric'. It is not obvious what Rust
Expand Down Expand Up @@ -68,3 +71,15 @@ impl fmt::Display for NumericError {
}

impl std::error::Error for NumericError {}

/// Converts `fee_rate` in BTC/kB to `FeeRate`.
fn btc_per_kb(btc_per_kb: f64) -> Result<Option<FeeRate>, ParseAmountError> {
let btc_per_byte = btc_per_kb / 1000 as f64;
let sats_per_byte = Amount::from_btc(btc_per_byte)?;

// Virtual bytes equal bytes before segwit.
let rate = FeeRate::from_sat_per_vb(sats_per_byte.to_sat());

Ok(rate)
}

2 changes: 2 additions & 0 deletions json/src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod blockchain;
mod control;
mod generating;
mod mining;
mod network;
mod raw_transactions;
mod util;
mod wallet;
Expand All @@ -32,6 +33,7 @@ pub use self::{
GetMempoolAncestorsVerbose, GetTxOut, Softfork, SoftforkType,
},
generating::{Generate, GenerateToAddress},
network::{GetNetworkInfo, GetNetworkInfoAddress, GetNetworkInfoNetwork},
raw_transactions::SendRawTransaction,
wallet::{
CreateWallet, GetBalance, GetBalances, GetBalancesMine, GetBalancesWatchOnly,
Expand Down
66 changes: 66 additions & 0 deletions json/src/model/network.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// SPDX-License-Identifier: CC0-1.0

//! Types for methods found under the `== Network ==` section of the API docs.
//!
//! These structs model the types returned by the JSON-RPC API but have concrete types
//! and are not specific to a specific version of Bitcoin Core.

use bitcoin::FeeRate;
use serde::{Deserialize, Serialize};

/// Models the result of JSON-RPC method `getnetworkinfo`.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct GetNetworkInfo {
/// The server version.
pub version: usize,
/// The server subversion string.
pub subversion: String,
/// The protocol version.
pub protocol_version: usize,
/// The services we offer to the network (hex string).
pub local_services: String,
/// `true` if transaction relay is requested from peers.
pub local_relay: bool,
/// The time offset.
pub time_offset: isize,
/// The total number of connections.
pub connections: usize,
/// Whether p2p networking is enabled.
pub network_active: bool,
/// Information per network.
pub networks: Vec<GetNetworkInfoNetwork>,
/// Minimum relay fee rate for transactions.
pub relay_fee: Option<FeeRate>, // `Some` if parsing succeeds.
/// Minimum fee rate increment for mempool limiting or replacement.
pub incremental_fee: Option<FeeRate>, // `Some` if parsing succeeds.
/// List of local addresses.
pub local_addresses: Vec<GetNetworkInfoAddress>,
/// Any network and blockchain warnings.
pub warnings: String,
}

/// Part of the result of the JSON-RPC method `getnetworkinfo` (information per network).
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct GetNetworkInfoNetwork {
/// Network (ipv4, ipv6, onion, i2p, cjdns).
pub name: String,
/// Is the network limited using -onlynet?.
pub limited: bool,
/// Is the network reachable?
pub reachable: bool,
/// ("host:port"): The proxy that is used for this network, or empty if none.
pub proxy: String,
/// Whether randomized credentials are used.
pub proxy_randomize_credentials: bool,
}

/// Part of the result of the JSON-RPC method `getnetworkinfo` (local address info).
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct GetNetworkInfoAddress {
/// Network address.
pub address: String,
/// Network port.
pub port: u16,
/// Relative score.
pub score: u32,
}
7 changes: 4 additions & 3 deletions json/src/v17/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
//! - [-] `addnode "node" "add|remove|onetry"`
//! - [-] `clearbanned`
//! - [-] `disconnectnode "[address]" [nodeid]`
//! - [ ] `getaddednodeinfo ( "node" )`
//! - [x] `getaddednodeinfo ( "node" )`
//! - [-] `getconnectioncount`
//! - [x] `getnettotals`
//! - [x] `getnetworkinfo`
Expand Down Expand Up @@ -181,8 +181,9 @@ pub use self::{
control::{GetMemoryInfoStats, Locked, Logging, Uptime},
generating::{Generate, GenerateToAddress},
network::{
AddedNodeAddress, BytesPerMessage, GetAddedNodeInfo, GetNetTotals, GetNetworkInfo,
GetNetworkInfoAddress, GetNetworkInfoNetwork, GetPeerInfo, PeerInfo, UploadTarget,
AddedNode, AddedNodeAddress, Banned, GetAddedNodeInfo, GetNetTotals, GetNetworkInfo,
GetNetworkInfoAddress, GetNetworkInfoNetwork, GetPeerInfo, ListBanned, PeerInfo,
UploadTarget,
},
raw_transactions::SendRawTransaction,
wallet::{
Expand Down
Loading

0 comments on commit 7e668f8

Please sign in to comment.