8000 feat(p2p): create `NetworkExt` trait for default P2P port and `Magic` values by luisschwab · Pull Request #4639 · rust-bitcoin/rust-bitcoin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(p2p): create NetworkExt trait for default P2P port and Magic values #4639

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

Open
wants to merge 1 commit into
base: master
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
5 changes: 5 additions & 0 deletions p2p/src/lib.rs
< 8000 tr data-hunk="17f3ee7425981faf87b3d3ac894fcc085bb50540c02a5e61049673239c8e8eb3" class="show-top-border">
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod message_bloom;
pub mod message_compact_blocks;
pub mod message_filter;
pub mod message_network;
pub mod network_ext;

extern crate alloc;

Expand All @@ -40,6 +41,10 @@ use io::{BufRead, Write};
#[doc(inline)]
pub use self::address::Address;

#[rustfmt::skip]
#[doc(inline)]
pub use network_ext::NetworkExt;

/// Version of the protocol as appearing in network message headers.
///
/// This constant is used to signal to other peers which features you support. Increasing it implies
Expand Down
80 changes: 80 additions & 0 deletions p2p/src/network_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//! SPDX-License-Identifier: CC0-1.0
//!
//! This module implements an extension trait for [`Network`]
//! with getter methods for the default P2P port and network [`Magic`] bytes.

use crate::Magic;

use bitcoin::{Network, TestnetVersion};

/// Trait that extends [`Network`] by adding getter methods for the default P2P ports and [`Magic`] bytes.
pub trait NetworkExt {
/// The default P2P port for a given [`Network`].
fn p2p_port(&self) -> u16;
/// The default [`Magic`] for a given [`Network`].
fn network_magic(&self) -> Magic;
}

impl NetworkExt for Network {
fn p2p_port(&self) -> u16 {
match &self {
Network::Bitcoin => 8333,
Network::Signet => 38333,
Network::Testnet(TestnetVersion::V3) => 18333,
Network::Testnet(TestnetVersion::V4) => 48333,
Network::Regtest => 18444,
_ => unimplemented!("Unsupported network"),
}
}

fn network_magic(&self) -> Magic {
match &self {
Network::Bitcoin => Magic::BITCOIN,
Network::Signet => Magic::SIGNET,
Network::Testnet(TestnetVersion::V3) => Magic::TESTNET3,
Network::Testnet(TestnetVersion::V4) => Magic::TESTNET4,
Network::Regtest => Magic::REGTEST,
_ => unimplemented!("Unsupported network"),
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn p2p_port() {
let networks = [
Network::Bitcoin,
Network::Signet,
Network::Testnet(TestnetVersion::V3),
Network::Testnet(TestnetVersion::V4),
Network::Regtest,
];

let p2p_ports = vec![8333, 38333, 18333, 48333, 18444];

for (network, p2p_port) in networks.iter().zip(p2p_ports) {
assert_eq!(network.p2p_port(), p2p_port);
}
}

#[test]
fn network_magic() {
let networks = [
Network::Bitcoin,
Network::Signet,
Network::Testnet(TestnetVersion::V3),
Network::Testnet(TestnetVersion::V4),
Network::Regtest,
];

let network_magics =
vec![Magic::BITCOIN, Magic::SIGNET, Magic::TESTNET3, Magic::TESTNET4, Magic::REGTEST];

for (network, network_magic) in networks.iter().zip(network_magics) {
assert_eq!(network.network_magic(), network_magic);
}
}
}
0