Skip to content
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

rpc(getpeerinfo): Add inbound peers to method response #9214

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion zebra-rpc/src/methods/get_block_template_rpcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1101,10 +1101,12 @@ where

async fn get_peer_info(&self) -> Result<Vec<PeerInfo>> {
let address_book = self.address_book.clone();

Ok(address_book
.recently_live_peers(chrono::Utc::now())
.into_iter()
.map(PeerInfo::from)
.filter(|meta_addr| meta_addr.has_connection_recently_responded(chrono::Utc::now()))
.map(|meta_addr| PeerInfo::new(meta_addr, meta_addr.is_inbound()))
Copy link
Contributor

@arya2 arya2 Feb 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional/Nitpick:

Suggested change
.map(|meta_addr| PeerInfo::new(meta_addr, meta_addr.is_inbound()))
.map(PeerInfo::new)

.collect())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,26 @@ use zebra_network::{types::MetaAddr, PeerSocketAddr};
pub struct PeerInfo {
/// The IP address and port of the peer
pub addr: PeerSocketAddr,

/// Inbound (true) or Outbound (false)
pub inbound: bool,
}

impl PeerInfo {
/// Create a new `PeerInfo` from a `MetaAddr`
pub fn new(meta_addr: MetaAddr, inbound: bool) -> Self {
Self {
addr: meta_addr.addr(),
inbound,
}
}
Comment on lines +17 to +22
Copy link
Contributor

@arya2 arya2 Feb 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional/Nitpick:

Suggested change
pub fn new(meta_addr: MetaAddr, inbound: bool) -> Self {
Self {
addr: meta_addr.addr(),
inbound,
}
}
pub fn new(meta_addr: MetaAddr) -> Self {
Self::from(meta_addr)
}

}

impl From<MetaAddr> for PeerInfo {
fn from(meta_addr: MetaAddr) -> Self {
Self {
addr: meta_addr.addr(),
inbound: meta_addr.is_inbound(),
}
}
}
Expand All @@ -21,6 +35,7 @@ impl Default for PeerInfo {
fn default() -> Self {
Self {
addr: PeerSocketAddr::unspecified(),
inbound: false,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ use zebra_chain::{
transparent,
work::difficulty::{CompactDifficulty, ParameterDifficulty as _},
};
use zebra_network::{address_book_peers::MockAddressBookPeers, types::MetaAddr};
use zebra_network::{
address_book_peers::MockAddressBookPeers,
types::{MetaAddr, PeerServices},
};
use zebra_node_services::mempool;

use zebra_state::{GetBlockTemplateChainInfo, ReadRequest, ReadResponse};
Expand Down Expand Up @@ -132,12 +135,14 @@ pub async fn test_responses<State, ReadState>(
mock_chain_tip_sender.send_best_tip_hash(fake_tip_hash);
mock_chain_tip_sender.send_estimated_distance_to_network_chain_tip(Some(0));

let mock_address_book = MockAddressBookPeers::new(vec![MetaAddr::new_initial_peer(
let mock_address_book = MockAddressBookPeers::new(vec![MetaAddr::new_connected(
SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
network.default_port(),
)
.into(),
&PeerServices::NODE_NETWORK,
false,
)
.into_new_meta_addr(Instant::now(), DateTime32::now())]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ expression: get_peer_info
---
[
{
"addr": "127.0.0.1:8233"
"addr": "127.0.0.1:8233",
"inbound": false
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ expression: get_peer_info
---
[
{
"addr": "127.0.0.1:18233"
"addr": "127.0.0.1:18233",
"inbound": false
}
]
59 changes: 53 additions & 6 deletions zebra-rpc/src/methods/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1343,7 +1343,7 @@ async fn rpc_getblockcount_empty_state() {
#[tokio::test(flavor = "multi_thread")]
async fn rpc_getpeerinfo() {
use zebra_chain::chain_sync_status::MockSyncStatus;
use zebra_network::address_book_peers::MockAddressBookPeers;
use zebra_network::{address_book_peers::MockAddressBookPeers, types::PeerServices};

let _init_guard = zebra_test::init();
let network = Mainnet;
Expand All @@ -1366,19 +1366,54 @@ async fn rpc_getpeerinfo() {
)
.await;

let mock_peer_address = zebra_network::types::MetaAddr::new_initial_peer(
// Add a connected outbound peer
let outbound_mock_peer_address = zebra_network::types::MetaAddr::new_connected(
std::net::SocketAddr::new(
std::net::IpAddr::V4(std::net::Ipv4Addr::new(127, 0, 0, 1)),
network.default_port(),
)
.into(),
&PeerServices::NODE_NETWORK,
false,
)
.into_new_meta_addr(
std::time::Instant::now(),
zebra_chain::serialization::DateTime32::now(),
);

let mock_address_book = MockAddressBookPeers::new(vec![mock_peer_address]);
// Add a connected inbound peer
let inbound_mock_peer_address = zebra_network::types::MetaAddr::new_connected(
std::net::SocketAddr::new(
std::net::IpAddr::V4(std::net::Ipv4Addr::new(127, 0, 0, 1)),
44444,
)
.into(),
&PeerServices::NODE_NETWORK,
true,
)
.into_new_meta_addr(
std::time::Instant::now(),
zebra_chain::serialization::DateTime32::now(),
);

// Add a peer that is not connected and will not be displayed in the RPC output
let not_connected_mock_peer_adderess = zebra_network::types::MetaAddr::new_initial_peer(
std::net::SocketAddr::new(
std::net::IpAddr::V4(std::net::Ipv4Addr::new(127, 0, 0, 1)),
55555,
)
.into(),
)
.into_new_meta_addr(
std::time::Instant::now(),
zebra_chain::serialization::DateTime32::now(),
);

let mock_address_book = MockAddressBookPeers::new(vec![
outbound_mock_peer_address,
inbound_mock_peer_address,
not_connected_mock_peer_adderess,
]);

// Init RPC
let get_block_template_rpc = get_block_template_rpcs::GetBlockTemplateRpcImpl::new(
Expand All @@ -1399,12 +1434,24 @@ async fn rpc_getpeerinfo() {
.await
.expect("We should have an array of addresses");

// Response of length should be 2. We have 2 connected peers and 1 unconnected peer in the address book.
assert_eq!(get_peer_info.len(), 2);

let mut res_iter = get_peer_info.into_iter();
// Check for the outbound peer
assert_eq!(
res_iter
.next()
.expect("there should be a mock peer address"),
outbound_mock_peer_address.into()
);

// Check for the inbound peer
assert_eq!(
get_peer_info
.into_iter()
res_iter
.next()
.expect("there should be a mock peer address"),
mock_peer_address.into()
inbound_mock_peer_address.into()
);

mempool.expect_no_requests().await;
Expand Down
Loading