Add default_rpc_port method to NetworkExt trait
What changed, and why it matters
This commit adds a simple helper function that returns the default Bitcoin JSON-RPC port number for each supported network (Bitcoin mainnet, Signet, Testnet versions, and Regtest). It is purely additive: it exposes no new network service, changes no existing behavior, and contains no input parsing, cryptography, or memory-unsafe code. There is no security issue here.
No security action required. This is a benign feature addition with no defensive relevance.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch extends the NetworkExt trait in p2p/src/network_ext.rs with default_rpc_port(&self) -> u16, implemented via a match on Network variants returning well-known static port constants (8332, 38332, 18332, 48332, 18442). It also adds a unit test verifying those constants. The implementation mirrors the pre-existing default_p2p_port method. No unsafe code, no external I/O, no parsing, no state mutation, and no security-sensitive logic are introduced.
Changed components
p2p/src/network_ext.rsInspect captured patch +34 / −0
diff --git a/p2p/src/network_ext.rs b/p2p/src/network_ext.rs
index 93aee0ab..0a5051ce 100644
--- a/p2p/src/network_ext.rs
+++ b/p2p/src/network_ext.rs
@@ -15,6 +15,9 @@ pub trait NetworkExt {
/// The default network [`Magic`] for a given [`Network`].
fn default_network_magic(self) -> Magic;
+
+ /// Returns the default RPC port for the given network.
+ fn default_rpc_port(&self) -> u16;
}
impl NetworkExt for Network {
@@ -33,6 +36,20 @@ impl NetworkExt for Network {
}
}
+ /// The default RPC port for a given [`Network`].
+ ///
+ /// Note: All [`TestnetVersion`] variants >4 are treated as [`TestnetVersion::V4`].
+ /// This function will be updated as new test networks are defined.
+ fn default_rpc_port(&self) -> u16 {
+ match self {
+ Self::Bitcoin => 8332,
+ Self::Signet => 38332,
+ Self::Testnet(TestnetVersion::V3) => 18332,
+ Self::Testnet(TestnetVersion::V4 | _) => 48332,
+ Self::Regtest => 18442,
+ }
+ }
+
/// The default network [`Magic`] for a given [`Network`].
///
/// Note: All [`TestnetVersion`] variants >4 are treated as [`TestnetVersion::V4`].
@@ -55,6 +72,23 @@ mod tests {
use super::*;
+ #[test]
+ fn default_rpc_port() {
+ let networks = [
+ Network::Bitcoin,
+ Network::Signet,
+ Network::Testnet(TestnetVersion::V3),
+ Network::Testnet(TestnetVersion::V4),
+ Network::Regtest,
+ ];
+
+ let rpc_ports = vec![8332, 38332, 18332, 48332, 18442];
+
+ for (network, rpc_port) in networks.iter().zip(rpc_ports) {
+ assert_eq!(network.default_rpc_port(), rpc_port);
+ }
+ }
+
#[test]
fn default_p2p_port() {
let networks = [
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.