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

v17: Finish network section #42

Merged
merged 5 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_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/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
//!
//! 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.

// Control types currently have no need for model equivalents.
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,
}
2 changes: 0 additions & 2 deletions json/src/v17/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ impl GetBestBlockHash {
/// Result of JSON-RPC method `getblock` with verbosity set to 0.
///
/// A string that is serialized, hex-encoded data for block 'hash'.
///
/// Method call: `getblock "blockhash" ( verbosity )`
#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
pub struct GetBlockVerbosityZero(pub String);

Expand Down
14 changes: 8 additions & 6 deletions json/src/v17/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
//! in the `model` module are version non-specific and are strongly typed using `rust-bitcoin`.
//!
//! Key:
//! - `[ ]` means not yet done.
//! - `[x]` marks means implemented _and_ tested.
//! - `[-]` means it was considered and intentionally not done.
//! - `[ ]` Not yet done.
//! - `[x]` Implemented _and_ tested.
//! - `[-]` Intentionally not done, typically because method does not return anything, returns
//! a single integer, or is deprecated.
//!
//! **== Blockchain ==**
//! - [x] `getbestblockhash`
Expand Down Expand Up @@ -60,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 @@ -180,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
Loading