Skip to content

Implement listbanned method and test #225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 16, 2025
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 @@ -79,6 +79,7 @@ crate::impl_client_v17__get_connection_count!();
crate::impl_client_v17__get_net_totals!();
crate::impl_client_v17__get_network_info!();
crate::impl_client_v17__get_peer_info!();
crate::impl_client_v17__list_banned!();
crate::impl_client_v17__ping!();
crate::impl_client_v17__set_ban!();
crate::impl_client_v17__set_network_active!();
Expand Down
10 changes: 10 additions & 0 deletions client/src/client_sync/v17/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ macro_rules! impl_client_v17__get_peer_info {
};
}

/// Implements Bitcoin Core JSON-RPC API method `listbanned`
#[macro_export]
macro_rules! impl_client_v17__list_banned {
() => {
impl Client {
pub fn list_banned(&self) -> Result<ListBanned> { self.call("listbanned", &[]) }
}
};
}

/// Implements Bitcoin Core JSON-RPC API method `ping`
#[macro_export]
macro_rules! impl_client_v17__ping {
Expand Down
1 change: 1 addition & 0 deletions client/src/client_sync/v18/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ crate::impl_client_v17__get_net_totals!();
crate::impl_client_v17__get_network_info!();
crate::impl_client_v18__get_node_addresses!();
crate::impl_client_v17__get_peer_info!();
crate::impl_client_v17__list_banned!();
crate::impl_client_v17__ping!();
crate::impl_client_v17__set_ban!();
crate::impl_client_v17__set_network_active!();
Expand Down
1 change: 1 addition & 0 deletions client/src/client_sync/v19/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ crate::impl_client_v17__get_net_totals!();
crate::impl_client_v17__get_network_info!();
crate::impl_client_v18__get_node_addresses!();
crate::impl_client_v17__get_peer_info!();
crate::impl_client_v17__list_banned!();
crate::impl_client_v17__ping!();
crate::impl_client_v17__set_ban!();
crate::impl_client_v17__set_network_active!();
Expand Down
1 change: 1 addition & 0 deletions client/src/client_sync/v20/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ crate::impl_client_v17__get_net_totals!();
crate::impl_client_v17__get_network_info!();
crate::impl_client_v18__get_node_addresses!();
crate::impl_client_v17__get_peer_info!();
crate::impl_client_v17__list_banned!();
crate::impl_client_v17__ping!();
crate::impl_client_v17__set_ban!();
crate::impl_client_v17__set_network_active!();
Expand Down
1 change: 1 addition & 0 deletions client/src/client_sync/v21/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ crate::impl_client_v17__get_net_totals!();
crate::impl_client_v17__get_network_info!();
crate::impl_client_v18__get_node_addresses!();
crate::impl_client_v17__get_peer_info!();
crate::impl_client_v17__list_banned!();
crate::impl_client_v17__ping!();
crate::impl_client_v17__set_ban!();
crate::impl_client_v17__set_network_active!();
Expand Down
1 change: 1 addition & 0 deletions client/src/client_sync/v22/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ crate::impl_client_v17__get_net_totals!();
crate::impl_client_v17__get_network_info!();
crate::impl_client_v18__get_node_addresses!();
crate::impl_client_v17__get_peer_info!();
crate::impl_client_v17__list_banned!();
crate::impl_client_v17__ping!();
crate::impl_client_v17__set_ban!();
crate::impl_client_v17__set_network_active!();
Expand Down
1 change: 1 addition & 0 deletions client/src/client_sync/v23/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ crate::impl_client_v17__get_net_totals!();
crate::impl_client_v17__get_network_info!();
crate::impl_client_v18__get_node_addresses!();
crate::impl_client_v17__get_peer_info!();
crate::impl_client_v17__list_banned!();
crate::impl_client_v17__ping!();
crate::impl_client_v17__set_ban!();
crate::impl_client_v17__set_network_active!();
Expand Down
1 change: 1 addition & 0 deletions client/src/client_sync/v24/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ crate::impl_client_v17__get_net_totals!();
crate::impl_client_v17__get_network_info!();
crate::impl_client_v18__get_node_addresses!();
crate::impl_client_v17__get_peer_info!();
crate::impl_client_v17__list_banned!();
crate::impl_client_v17__ping!();
crate::impl_client_v17__set_ban!();
crate::impl_client_v17__set_network_active!();
Expand Down
1 change: 1 addition & 0 deletions client/src/client_sync/v25/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ crate::impl_client_v17__get_net_totals!();
crate::impl_client_v17__get_network_info!();
crate::impl_client_v18__get_node_addresses!();
crate::impl_client_v17__get_peer_info!();
crate::impl_client_v17__list_banned!();
crate::impl_client_v17__ping!();
crate::impl_client_v17__set_ban!();
crate::impl_client_v17__set_network_active!();
Expand Down
1 change: 1 addition & 0 deletions client/src/client_sync/v26/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ crate::impl_client_v17__get_net_totals!();
crate::impl_client_v17__get_network_info!();
crate::impl_client_v18__get_node_addresses!();
crate::impl_client_v17__get_peer_info!();
crate::impl_client_v17__list_banned!();
crate::impl_client_v17__ping!();
crate::impl_client_v17__set_ban!();
crate::impl_client_v17__set_network_active!();
Expand Down
1 change: 1 addition & 0 deletions client/src/client_sync/v27/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ crate::impl_client_v17__get_net_totals!();
crate::impl_client_v17__get_network_info!();
crate::impl_client_v18__get_node_addresses!();
crate::impl_client_v17__get_peer_info!();
crate::impl_client_v17__list_banned!();
crate::impl_client_v17__ping!();
crate::impl_client_v17__set_ban!();
crate::impl_client_v17__set_network_active!();
Expand Down
1 change: 1 addition & 0 deletions client/src/client_sync/v28/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ crate::impl_client_v17__get_net_totals!();
crate::impl_client_v17__get_network_info!();
crate::impl_client_v18__get_node_addresses!();
crate::impl_client_v17__get_peer_info!();
crate::impl_client_v17__list_banned!();
crate::impl_client_v17__ping!();
crate::impl_client_v17__set_ban!();
crate::impl_client_v17__set_network_active!();
Expand Down
1 change: 1 addition & 0 deletions client/src/client_sync/v29/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ crate::impl_client_v17__get_net_totals!();
crate::impl_client_v17__get_network_info!();
crate::impl_client_v18__get_node_addresses!();
crate::impl_client_v17__get_peer_info!();
crate::impl_client_v17__list_banned!();
crate::impl_client_v17__ping!();
crate::impl_client_v17__set_ban!();
crate::impl_client_v17__set_network_active!();
Expand Down
14 changes: 14 additions & 0 deletions integration_test/tests/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ fn get_peer_info_three_node_network() {
assert!(node3.peers_connected() >= 1);
}

#[test]
fn network__list_banned() {
let node = Node::with_wallet(Wallet::None, &[]);
let dummy_subnet = "192.0.2.5/32";

node.client.set_ban(dummy_subnet, SetBanCommand::Add).expect("setban add");
let json: ListBanned = node.client.list_banned().expect("listbanned");
assert!(json.0.iter().any(|item| item.address == dummy_subnet));

node.client.set_ban(dummy_subnet, SetBanCommand::Remove).expect("setban remove");
let json: ListBanned = node.client.list_banned().expect("listbanned");
assert!(json.0.iter().all(|item| item.address != dummy_subnet));
}

#[test]
fn network__ping() {
let node = Node::with_wallet(Wallet::None, &[]);
Expand Down
2 changes: 1 addition & 1 deletion types/src/v17/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
//! | getnettotals | version | |
//! | getnetworkinfo | version + model | |
//! | getpeerinfo | version | |
//! | listbanned | returns string | |
//! | listbanned | version | |
//! | ping | returns nothing | |
//! | setban | returns nothing | |
//! | setnetworkactive | version | |
Expand Down
12 changes: 11 additions & 1 deletion types/src/v17/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,17 @@ pub struct ListBanned(pub Vec<Banned>);

/// An item from the list returned by the JSON-RPC method `listbanned`
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct Banned(String); // FIXME: The docs are empty so I don't know what shape this is.
pub struct Banned {
// NOTE: Shape taken from Core source code,as method is undocumented in the Bitcoin RPC CLI for version 17 to 20.
/// The IP/Subnet of the banned node.
pub address: String,
/// The UNIX epoch time the ban was expires.
pub banned_until: u32,
/// The UNIX epoch time the ban was created.
pub ban_created: u32,
/// The reason for the ban.
pub ban_reason: String,
}

/// Result of JSON-RPC method `setnetworkactive`.
///
Expand Down
2 changes: 1 addition & 1 deletion types/src/v18/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
//! | getnetworkinfo | version + model | |
//! | getnodeaddresses | version + model | |
//! | getpeerinfo | version | |
//! | listbanned | returns string | |
//! | listbanned | version | |
//! | ping | returns nothing | |
//! | setban | returns nothing | |
//! | setnetworkactive | version | |
Expand Down
2 changes: 1 addition & 1 deletion types/src/v19/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
//! | getnetworkinfo | version + model | |
//! | getnodeaddresses | version + model | |
//! | getpeerinfo | version | |
//! | listbanned | returns string | |
//! | listbanned | version | |
//! | ping | returns nothing | |
//! | setban | returns nothing | |
//! | setnetworkactive | version | |
Expand Down
42 changes: 23 additions & 19 deletions types/src/v20/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
//! | getnetworkinfo | version + model | |
//! | getnodeaddresses | version + model | |
//! | getpeerinfo | version | |
//! | listbanned | returns string | |
//! | listbanned | version | |
//! | ping | returns nothing | |
//! | setban | returns nothing | |
//! | setnetworkactive | version | |
Expand Down Expand Up @@ -227,18 +227,22 @@

// JSON-RPC types by API section.
mod control;
mod network;

#[doc(inline)]
pub use self::control::Logging;
pub use self::{
control::Logging,
network::{Banned, ListBanned},
};
#[doc(inline)]
pub use crate::{
v17::{
AbortRescan, AddMultisigAddress, AddMultisigAddressError, AddedNode, AddedNodeAddress,
AddressInformation, Banned, BumpFee, BumpFeeError, ChainTips, ChainTipsError,
ChainTipsStatus, CombinePsbt, CombineRawTransaction, ConvertToPsbt, CreateMultisig,
CreateMultisigError, CreatePsbt, CreateRawTransaction, CreateWallet, DecodePsbt,
DecodePsbtError, DecodeRawTransaction, DecodeScript, DecodeScriptError, DumpPrivKey,
DumpWallet, EstimateSmartFee, FinalizePsbt, FinalizePsbtError, FundRawTransaction,
AddressInformation, BumpFee, BumpFeeError, ChainTips, ChainTipsError, ChainTipsStatus,
CombinePsbt, CombineRawTransaction, ConvertToPsbt, CreateMultisig, CreateMultisigError,
CreatePsbt, CreateRawTransaction, CreateWallet, DecodePsbt, DecodePsbtError,
DecodeRawTransaction, DecodeScript, DecodeScriptError, DumpPrivKey, DumpWallet,
EstimateSmartFee, FinalizePsbt, FinalizePsbtError, FundRawTransaction,
FundRawTransactionError, Generate, GenerateToAddress, GetAddedNodeInfo, GetAddressInfo,
GetAddressInfoEmbedded, GetAddressInfoEmbeddedError, GetAddressInfoError,
GetAddressInfoLabel, GetAddressesByLabel, GetBalance, GetBestBlockHash, GetBlockCount,
Expand All @@ -253,18 +257,18 @@ pub use crate::{
GetReceivedByAddress, GetTransaction, GetTransactionDetail, GetTransactionDetailError,
GetTransactionError, GetTxOut, GetTxOutError, GetTxOutSetInfo, GetTxOutSetInfoError,
GetUnconfirmedBalance, GetWalletInfo, GetWalletInfoError, GetZmqNotifications,
ListAddressGroupings, ListAddressGroupingsError, ListAddressGroupingsItem, ListBanned,
ListLabels, ListLockUnspent, ListLockUnspentItem, ListLockUnspentItemError,
ListReceivedByAddress, ListReceivedByAddressError, ListReceivedByAddressItem,
ListSinceBlock, ListSinceBlockError, ListSinceBlockTransaction,
ListSinceBlockTransactionError, ListTransactions, ListTransactionsItem,
ListTransactionsItemError, ListUnspentItemError, ListWallets, LoadWallet, Locked, PeerInfo,
PruneBlockchain, RawTransactionError, RawTransactionInput, RawTransactionOutput,
RescanBlockchain, SendMany, SendRawTransaction, SendToAddress, SetNetworkActive,
SignMessage, SignMessageWithPrivKey, SignRawTransaction, SignRawTransactionError,
SoftforkReject, TestMempoolAccept, TransactionCategory, UploadTarget, ValidateAddress,
ValidateAddressError, VerifyChain, VerifyMessage, VerifyTxOutProof, WalletCreateFundedPsbt,
WalletCreateFundedPsbtError, WalletProcessPsbt, WitnessUtxo,
ListAddressGroupings, ListAddressGroupingsError, ListAddressGroupingsItem, ListLabels,
ListLockUnspent, ListLockUnspentItem, ListLockUnspentItemError, ListReceivedByAddress,
ListReceivedByAddressError, ListReceivedByAddressItem, ListSinceBlock, ListSinceBlockError,
ListSinceBlockTransaction, ListSinceBlockTransactionError, ListTransactions,
ListTransactionsItem, ListTransactionsItemError, ListUnspentItemError, ListWallets,
LoadWallet, Locked, PeerInfo, PruneBlockchain, RawTransactionError, RawTransactionInput,
RawTransactionOutput, RescanBlockchain, SendMany, SendRawTransaction, SendToAddress,
SetNetworkActive, SignMessage, SignMessageWithPrivKey, SignRawTransaction,
SignRawTransactionError, SoftforkReject, TestMempoolAccept, TransactionCategory,
UploadTarget, ValidateAddress, ValidateAddressError, VerifyChain, VerifyMessage,
VerifyTxOutProof, WalletCreateFundedPsbt, WalletCreateFundedPsbtError, WalletProcessPsbt,
WitnessUtxo,
},
v18::{
ActiveCommand, AnalyzePsbt, AnalyzePsbtError, AnalyzePsbtInput, AnalyzePsbtInputMissing,
Expand Down
26 changes: 26 additions & 0 deletions types/src/v20/network.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-License-Identifier: CC0-1.0

//! The JSON-RPC API for Bitcoin Core `v0.20.2` - network.
//!
//! Types for methods found under the `== Network ==` section of the API docs.

use serde::{Deserialize, Serialize};

/// Result of JSON-RPC method `listbanned`.
///
/// > listbanned
///
/// > List all banned IPs/Subnets.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct ListBanned(pub Vec<Banned>);

/// An item from the list returned by the JSON-RPC method `listbanned`
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct Banned {
/// The IP/Subnet of the banned node.
pub address: String,
/// The UNIX epoch time the ban was expires.
pub banned_until: u32,
/// The UNIX epoch time the ban was created.
pub ban_created: u32,
}
38 changes: 19 additions & 19 deletions types/src/v21/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
//! | getnetworkinfo | version + model | |
//! | getnodeaddresses | version + model | |
//! | getpeerinfo | version | |
//! | listbanned | returns string | |
//! | listbanned | version | |
//! | ping | returns nothing | |
//! | setban | returns nothing | |
//! | setnetworkactive | version | |
Expand Down Expand Up @@ -240,11 +240,11 @@ pub use self::wallet::UnloadWallet;
pub use crate::{
v17::{
AbortRescan, AddMultisigAddress, AddMultisigAddressError, AddedNode, AddedNodeAddress,
AddressInformation, Banned, BumpFee, BumpFeeError, ChainTips, ChainTipsError,
ChainTipsStatus, CombinePsbt, CombineRawTransaction, ConvertToPsbt, CreateMultisig,
CreateMultisigError, CreatePsbt, CreateRawTransaction, CreateWallet, DecodePsbt,
DecodePsbtError, DecodeRawTransaction, DecodeScript, DecodeScriptError, DumpPrivKey,
DumpWallet, EstimateSmartFee, FinalizePsbt, FinalizePsbtError, FundRawTransaction,
AddressInformation, BumpFee, BumpFeeError, ChainTips, ChainTipsError, ChainTipsStatus,
CombinePsbt, CombineRawTransaction, ConvertToPsbt, CreateMultisig, CreateMultisigError,
CreatePsbt, CreateRawTransaction, CreateWallet, DecodePsbt, DecodePsbtError,
DecodeRawTransaction, DecodeScript, DecodeScriptError, DumpPrivKey, DumpWallet,
EstimateSmartFee, FinalizePsbt, FinalizePsbtError, FundRawTransaction,
FundRawTransactionError, Generate, GenerateToAddress, GetAddedNodeInfo, GetAddressInfo,
GetAddressInfoEmbedded, GetAddressInfoEmbeddedError, GetAddressInfoError,
GetAddressInfoLabel, GetAddressesByLabel, GetBalance, GetBestBlockHash, GetBlockCount,
Expand All @@ -259,18 +259,18 @@ pub use crate::{
GetReceivedByAddress, GetTransaction, GetTransactionDetail, GetTransactionDetailError,
GetTransactionError, GetTxOut, GetTxOutError, GetTxOutSetInfo, GetTxOutSetInfoError,
GetUnconfirmedBalance, GetWalletInfo, GetWalletInfoError, GetZmqNotifications,
ListAddressGroupings, ListAddressGroupingsError, ListAddressGroupingsItem, ListBanned,
ListLabels, ListLockUnspent, ListLockUnspentItem, ListLockUnspentItemError,
ListReceivedByAddress, ListReceivedByAddressError, ListReceivedByAddressItem,
ListSinceBlock, ListSinceBlockError, ListSinceBlockTransaction,
ListSinceBlockTransactionError, ListTransactions, ListTransactionsItem,
ListTransactionsItemError, ListUnspentItemError, ListWallets, LoadWallet, Locked, PeerInfo,
PruneBlockchain, RawTransactionError, RawTransactionInput, RawTransactionOutput,
RescanBlockchain, SendMany, SendRawTransaction, SendToAddress, SetNetworkActive,
SignMessage, SignMessageWithPrivKey, SignRawTransaction, SignRawTransactionError,
SoftforkReject, TestMempoolAccept, TransactionCategory, UploadTarget, ValidateAddress,
ValidateAddressError, VerifyChain, VerifyMessage, VerifyTxOutProof, WalletCreateFundedPsbt,
WalletCreateFundedPsbtError, WalletProcessPsbt, WitnessUtxo,
ListAddressGroupings, ListAddressGroupingsError, ListAddressGroupingsItem, ListLabels,
ListLockUnspent, ListLockUnspentItem, ListLockUnspentItemError, ListReceivedByAddress,
ListReceivedByAddressError, ListReceivedByAddressItem, ListSinceBlock, ListSinceBlockError,
ListSinceBlockTransaction, ListSinceBlockTransactionError, ListTransactions,
ListTransactionsItem, ListTransactionsItemError, ListUnspentItemError, ListWallets,
LoadWallet, Locked, PeerInfo, PruneBlockchain, RawTransactionError, RawTransactionInput,
RawTransactionOutput, RescanBlockchain, SendMany, SendRawTransaction, SendToAddress,
SetNetworkActive, SignMessage, SignMessageWithPrivKey, SignRawTransaction,
SignRawTransactionError, SoftforkReject, TestMempoolAccept, TransactionCategory,
UploadTarget, ValidateAddress, ValidateAddressError, VerifyChain, VerifyMessage,
VerifyTxOutProof, WalletCreateFundedPsbt, WalletCreateFundedPsbtError, WalletProcessPsbt,
WitnessUtxo,
},
v18::{
ActiveCommand, AnalyzePsbt, AnalyzePsbtError, AnalyzePsbtInput, AnalyzePsbtInputMissing,
Expand All @@ -286,5 +286,5 @@ pub use crate::{
MempoolEntry, MempoolEntryError, MempoolEntryFees, MempoolEntryFeesError, Softfork,
SoftforkType,
},
v20::Logging,
v20::{Banned, ListBanned, Logging},
};
Loading