What changed, and why it matters
This commit is a routine feature refactor: it makes the p2p crate compatible with no_std environments by adding an explicit std feature flag (still enabled by default). It does not fix a bug, change behavior for normal users, or introduce any obvious security issue.
No security action required; review as normal feature work if desired.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch adds a default-on std feature to the p2p crate, switches imports from std to alloc/core where possible, and gates std-only modules and error trait impls behind #[cfg(feature = “std”)]. Because the feature is default, existing users see no change. The change is architectural, not security-relevant.
Changed components
p2p/Cargo.tomlp2p/src/lib.rsp2p/src/address.rsp2p/src/consensus.rsp2p/src/message.rsp2p/src/message_blockdata.rsp2p/src/message_bloom.rsp2p/src/message_filter.rsp2p/src/message_network.rsp2p/src/network_ext.rsInspect captured patch +60 / −11
diff --git a/p2p/Cargo.toml b/p2p/Cargo.toml
index 892eee90..a7b89af0 100644
--- a/p2p/Cargo.toml
+++ b/p2p/Cargo.toml
@@ -12,19 +12,24 @@ edition = "2021"
rust-version = "1.63.0"
exclude = ["tests", "contrib"]
+[features]
+default = ["std"]
+std = ["hashes/std", "hex/std", "internals/std", "io/std", "units/std", "bitcoin/std"]
+
[dependencies]
-bitcoin = { path = "../bitcoin/" }
-hashes = { package = "bitcoin_hashes", path = "../hashes", default-features = false, features = ["std"] }
-hex = { package = "hex-conservative", version = "0.3.0", default-features = false, features = ["std"] }
-internals = { package = "bitcoin-internals", path = "../internals", features = ["std"] }
-io = { package = "bitcoin-io", path = "../io", default-features = false, features = ["std"] }
-units = { package = "bitcoin-units", path = "../units", default-features = false, features = ["std"] }
+bitcoin = { path = "../bitcoin/", default-features = false }
+hashes = { package = "bitcoin_hashes", path = "../hashes", default-features = false }
+hex = { package = "hex-conservative", version = "0.3.0", default-features = false }
+internals = { package = "bitcoin-internals", path = "../internals", default-features = false }
+io = { package = "bitcoin-io", path = "../io", default-features = false }
+units = { package = "bitcoin-units", path = "../units", default-features = false }
[dev-dependencies]
hex_lit = "0.1.1"
[[example]]
name = "handshake"
+required-features = ["std"]
[package.metadata.docs.rs]
all-features = true
diff --git a/p2p/src/address.rs b/p2p/src/address.rs
index 44252cbc..82a71571 100644
--- a/p2p/src/address.rs
+++ b/p2p/src/address.rs
@@ -5,6 +5,8 @@
//! This module defines the structures and functions needed to encode
//! network addresses in Bitcoin messages.
+use alloc::vec;
+use alloc::vec::Vec;
use core::{fmt, iter};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs};
@@ -442,6 +444,7 @@ impl std::error::Error for AddrV2ToIpv6AddrError {}
#[cfg(test)]
mod test {
+ use alloc::{format, vec};
use std::net::IpAddr;
use bitcoin::consensus::encode::{deserialize, serialize};
diff --git a/p2p/src/consensus.rs b/p2p/src/consensus.rs
index 57a4ae69..95aae1bd 100644
--- a/p2p/src/consensus.rs
+++ b/p2p/src/consensus.rs
@@ -1,6 +1,9 @@
+#[cfg(feature = "std")]
use bitcoin::consensus::encode::WriteExt;
+#[cfg(feature = "std")]
use io::Write;
+#[cfg(feature = "std")]
pub(crate) fn consensus_encode_with_size<W: Write + ?Sized>(
data: &[u8],
w: &mut W,
@@ -53,6 +56,7 @@ macro_rules! impl_consensus_encoding {
}
pub(crate) use impl_consensus_encoding;
+#[cfg(feature = "std")]
macro_rules! impl_vec_wrapper {
($wrapper: ident, $type: ty) => {
impl bitcoin::consensus::encode::Encodable for $wrapper {
@@ -94,4 +98,5 @@ macro_rules! impl_vec_wrapper {
};
}
+#[cfg(feature = "std")]
pub(crate) use impl_vec_wrapper;
diff --git a/p2p/src/lib.rs b/p2p/src/lib.rs
index 27405349..8c4a24c9 100644
--- a/p2p/src/lib.rs
+++ b/p2p/src/lib.rs
@@ -2,6 +2,7 @@
//! Rust Bitcoin Peer to Peer Message Types
+#![no_std]
// Experimental features we need.
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
// Coding conventions.
@@ -15,21 +16,28 @@
#![allow(clippy::manual_range_contains)] // More readable than clippy's format.
#![allow(clippy::uninlined_format_args)] // Allow `format!("{}", x)`instead of enforcing `format!("{x}")`
+#[cfg(feature = "std")]
pub mod address;
mod consensus;
+#[cfg(feature = "std")]
pub mod message;
pub mod message_blockdata;
pub mod message_bloom;
pub mod message_compact_blocks;
pub mod message_filter;
+#[cfg(feature = "std")]
pub mod message_network;
mod network_ext;
extern crate alloc;
+#[cfg(feature = "std")]
+extern crate std;
+use alloc::borrow::ToOwned;
+use alloc::string::String;
+use core::borrow::{Borrow, BorrowMut};
use core::str::FromStr;
use core::{fmt, ops};
-use std::borrow::{Borrow, BorrowMut, ToOwned};
use bitcoin::consensus::encode::{self, Decodable, Encodable};
use bitcoin::network::{Network, Params, TestnetVersion};
@@ -39,7 +47,12 @@ use io::{BufRead, Write};
#[rustfmt::skip]
#[doc(inline)]
-pub use self::{address::Address, network_ext::NetworkExt};
+pub use self::network_ext::NetworkExt;
+
+#[cfg(feature = "std")]
+#[rustfmt::skip]
+#[doc(inline)]
+pub use self::address::Address;
/// Version of the protocol as appearing in network version handshakes and some message headers.
///
@@ -416,6 +429,7 @@ impl fmt::Display for ParseMagicError {
}
}
+#[cfg(feature = "std")]
impl std::error::Error for ParseMagicError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.error) }
}
@@ -431,6 +445,7 @@ impl fmt::Display for UnknownMagicError {
}
}
+#[cfg(feature = "std")]
impl std::error::Error for UnknownMagicError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
}
@@ -446,12 +461,15 @@ impl fmt::Display for UnknownNetworkError {
}
}
+#[cfg(feature = "std")]
impl std::error::Error for UnknownNetworkError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
}
#[cfg(test)]
mod tests {
+ use alloc::string::ToString;
+
use bitcoin::consensus::encode::{deserialize, serialize};
use super::*;
diff --git a/p2p/src/message.rs b/p2p/src/message.rs
index 9065ef16..403f2a57 100644
--- a/p2p/src/message.rs
+++ b/p2p/src/message.rs
@@ -5,9 +5,11 @@
//! This module defines the `NetworkMessage` and `RawNetworkMessage` types that
//! are used for (de)serializing Bitcoin objects for transmission on the network.
+use alloc::borrow::{Cow, ToOwned};
+use alloc::boxed::Box;
+use alloc::string::String;
+use alloc::vec::Vec;
use core::fmt;
-use std::borrow::{Cow, ToOwned};
-use std::boxed::Box;
use bitcoin::consensus::encode::{self, CheckedData, Decodable, Encodable, ReadExt, WriteExt};
use bitcoin::merkle_tree::MerkleBlock;
@@ -744,6 +746,8 @@ impl Decodable for V2NetworkMessage {
#[cfg(test)]
mod test {
+ use alloc::string::ToString;
+ use alloc::vec;
use std::net::Ipv4Addr;
use bitcoin::bip152::BlockTransactionsRequest;
diff --git a/p2p/src/message_blockdata.rs b/p2p/src/message_blockdata.rs
index a52d92cd..5e4ca85a 100644
--- a/p2p/src/message_blockdata.rs
+++ b/p2p/src/message_blockdata.rs
@@ -5,6 +5,8 @@
//! This module describes network messages which are used for passing
//! Bitcoin data (blocks and transactions) around.
+use alloc::vec::Vec;
+
use bitcoin::block::BlockHash;
use bitcoin::consensus::encode::{self, Decodable, Encodable};
use bitcoin::transaction::{Txid, Wtxid};
diff --git a/p2p/src/message_bloom.rs b/p2p/src/message_bloom.rs
index 762c05e3..0b1ed8c8 100644
--- a/p2p/src/message_bloom.rs
+++ b/p2p/src/message_bloom.rs
@@ -4,6 +4,8 @@
//!
//! This module describes BIP37 Connection Bloom filtering network messages.
+use alloc::vec::Vec;
+
use bitcoin::consensus::{encode, Decodable, Encodable, ReadExt};
use io::{BufRead, Write};
diff --git a/p2p/src/message_filter.rs b/p2p/src/message_filter.rs
index 83fb461b..104c8dc6 100644
--- a/p2p/src/message_filter.rs
+++ b/p2p/src/message_filter.rs
@@ -4,6 +4,8 @@
//!
//! This module describes BIP157 Client Side Block Filtering network messages.
+use alloc::vec::Vec;
+
use bitcoin::bip158::{FilterHash, FilterHeader};
use bitcoin::block::BlockHash;
use units::BlockHeight;
diff --git a/p2p/src/message_network.rs b/p2p/src/message_network.rs
index d23aa8f0..6b609641 100644
--- a/p2p/src/message_network.rs
+++ b/p2p/src/message_network.rs
@@ -4,7 +4,11 @@
//!
//! This module defines network messages which describe peers and their
//! capabilities.
-use std::borrow::Cow;
+
+use alloc::borrow::Cow;
+use alloc::format;
+use alloc::string::{String, ToString};
+use alloc::vec::Vec;
use bitcoin::consensus::{encode, Decodable, Encodable, ReadExt, WriteExt};
use hashes::sha256d;
@@ -335,6 +339,8 @@ impl_vec_wrapper!(Alert, Vec<u8>);
#[cfg(test)]
mod tests {
+ use alloc::string::ToString;
+
use bitcoin::consensus::encode::{deserialize, serialize};
use hex_lit::hex;
diff --git a/p2p/src/network_ext.rs b/p2p/src/network_ext.rs
index b677f99d..713306cd 100644
--- a/p2p/src/network_ext.rs
+++ b/p2p/src/network_ext.rs
@@ -51,6 +51,8 @@ impl NetworkExt for Network {
#[cfg(test)]
mod tests {
+ use alloc::vec;
+
use super::*;
#[test]
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.