Skip to content

Commit e242dd6

Browse files
committed
Move bip152 to p2p
Replaces rust-bitcoin#5333 and rust-bitcoin#5365 by manually implementing `hex` traits for the `ShortId` type. `FromStr` is omitted as I can't think of a logical use of that method in this context. To make any sense of a string short ID one needs the nonce and siphash keys. This module is only relevant between two peers relaying blocks over the peer to peer network.
1 parent 33a01c0 commit e242dd6

File tree

6 files changed

+44
-21
lines changed

6 files changed

+44
-21
lines changed

bitcoin/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ pub mod ext {
116116
}
117117
#[macro_use]
118118
pub mod address;
119-
pub mod bip152;
120119
pub mod bip158;
121120
pub mod bip32;
122121
pub mod blockdata;

fuzz/fuzz_targets/bitcoin/deserialize_prefilled_transaction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use honggfuzz::fuzz;
22

33
fn do_test(data: &[u8]) {
44
// We already fuzz Transactions in `./deserialize_transaction.rs`.
5-
let tx_result: Result<bitcoin::bip152::PrefilledTransaction, _> =
5+
let tx_result: Result<p2p::bip152::PrefilledTransaction, _> =
66
bitcoin::consensus::encode::deserialize(data);
77

88
match tx_result {
Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//!
55
//! Implementation of compact blocks data structure and algorithms.
66
7+
use alloc::vec::Vec;
78
use core::convert::Infallible;
89
use core::{convert, fmt, mem};
910
#[cfg(feature = "std")]
@@ -16,11 +17,9 @@ use internals::array::ArrayExt as _;
1617
use internals::ToU64 as _;
1718
use io::{BufRead, Write};
1819

19-
use crate::consensus::encode::{self, Decodable, Encodable, ReadExt, WriteExt};
20-
use crate::internal_macros::{self, impl_array_newtype_stringify};
21-
use crate::prelude::Vec;
22-
use crate::transaction::TxIdentifier;
23-
use crate::{block, consensus, Block, BlockChecked, BlockHash, Transaction};
20+
use bitcoin::consensus::encode::{self, Decodable, Encodable, ReadExt, WriteExt};
21+
use bitcoin::transaction::TxIdentifier;
22+
use bitcoin::{block, Block, BlockChecked, BlockHash, Transaction};
2423

2524
/// A BIP-0152 error
2625
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -89,7 +88,7 @@ impl Decodable for PrefilledTransaction {
8988
fn consensus_decode<R: BufRead + ?Sized>(r: &mut R) -> Result<Self, encode::Error> {
9089
let idx = r.read_compact_size()?;
9190
let idx = u16::try_from(idx).map_err(|_| {
92-
consensus::parse_failed_error("BIP-0152 prefilled tx index out of bounds")
91+
crate::consensus::parse_failed_error("BIP-0152 prefilled tx index out of bounds")
9392
})?;
9493
let tx = Transaction::consensus_decode(r)?;
9594
Ok(Self { idx, tx })
@@ -100,7 +99,6 @@ impl Decodable for PrefilledTransaction {
10099
#[derive(PartialEq, Eq, Clone, Copy, Hash, Default, PartialOrd, Ord)]
101100
pub struct ShortId([u8; 6]);
102101
internals::impl_array_newtype!(ShortId, u8, 6);
103-
impl_array_newtype_stringify!(ShortId, 6);
104102

105103
impl ShortId {
106104
/// Calculates the SipHash24 keys used to calculate short IDs.
@@ -134,6 +132,30 @@ impl ShortId {
134132
}
135133
}
136134

135+
impl core::fmt::LowerHex for ShortId {
136+
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
137+
hex::display::fmt_hex_exact!(f, 6, &self.0, hex::Case::Lower)
138+
}
139+
}
140+
141+
impl core::fmt::UpperHex for ShortId {
142+
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
143+
hex::display::fmt_hex_exact!(f, 6, &self.0, hex::Case::Upper)
144+
}
145+
}
146+
147+
impl core::fmt::Display for ShortId {
148+
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
149+
core::fmt::LowerHex::fmt(self, f)
150+
}
151+
}
152+
153+
impl core::fmt::Debug for ShortId {
154+
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
155+
core::fmt::LowerHex::fmt(self, f)
156+
}
157+
}
158+
137159
impl Encodable for ShortId {
138160
#[inline]
139161
fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
@@ -177,7 +199,7 @@ impl Decodable for HeaderAndShortIds {
177199
};
178200
match header_short_ids.short_ids.len().checked_add(header_short_ids.prefilled_txs.len()) {
179201
Some(x) if x <= u16::MAX.into() => Ok(header_short_ids),
180-
_ => Err(consensus::parse_failed_error("indexes overflowed 16 bits")),
202+
_ => Err(crate::consensus::parse_failed_error("indexes overflowed 16 bits")),
181203
}
182204
}
183205
}
@@ -313,7 +335,7 @@ impl Decodable for BlockTransactionsRequest {
313335
// transactions that would be allowed in a vector.
314336
let byte_size = nb_indexes
315337
.checked_mul(mem::size_of::<Transaction>())
316-
.ok_or(consensus::parse_failed_error("invalid length"))?;
338+
.ok_or(crate::consensus::parse_failed_error("invalid length"))?;
317339
if byte_size > encode::MAX_VEC_SIZE {
318340
return Err(encode::ParseError::OversizedVectorAllocation {
319341
requested: byte_size,
@@ -328,12 +350,12 @@ impl Decodable for BlockTransactionsRequest {
328350
let differential = r.read_compact_size()?;
329351
last_index = match last_index.checked_add(differential) {
330352
Some(i) => i,
331-
None => return Err(consensus::parse_failed_error("block index overflow")),
353+
None => return Err(crate::consensus::parse_failed_error("block index overflow")),
332354
};
333355
indexes.push(last_index);
334356
last_index = match last_index.checked_add(1) {
335357
Some(i) => i,
336-
None => return Err(consensus::parse_failed_error("block index overflow")),
358+
None => return Err(crate::consensus::parse_failed_error("block index overflow")),
337359
};
338360
}
339361
indexes
@@ -373,7 +395,7 @@ pub struct BlockTransactions {
373395
/// The transactions provided.
374396
pub transactions: Vec<Transaction>,
375397
}
376-
internal_macros::impl_consensus_encoding!(BlockTransactions, block_hash, transactions);
398+
crate::consensus::impl_consensus_encoding!(BlockTransactions, block_hash, transactions);
377399

378400
impl BlockTransactions {
379401
/// Constructs a new [`BlockTransactions`] from a [`BlockTransactionsRequest`] and
@@ -446,14 +468,15 @@ impl<'a> Arbitrary<'a> for BlockTransactionsRequest {
446468

447469
#[cfg(test)]
448470
mod test {
471+
use alloc::vec;
449472
use hex::FromHex;
450473

451474
use super::*;
452-
use crate::consensus::encode::{deserialize, serialize};
453-
use crate::locktime::absolute;
454-
use crate::merkle_tree::TxMerkleNode;
455-
use crate::transaction::OutPointExt;
456-
use crate::{
475+
use bitcoin::consensus::encode::{deserialize, serialize};
476+
use bitcoin::locktime::absolute;
477+
use bitcoin::merkle_tree::TxMerkleNode;
478+
use bitcoin::transaction::OutPointExt;
479+
use bitcoin::{
457480
transaction, Amount, BlockChecked, BlockTime, CompactTarget, OutPoint, ScriptPubKeyBuf,
458481
ScriptSigBuf, Sequence, TxIn, TxOut, Txid, Witness,
459482
};

p2p/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ mod network_ext;
1919

2020
#[cfg(feature = "std")]
2121
pub mod address;
22+
pub mod bip152;
2223
#[cfg(feature = "std")]
2324
pub mod message;
2425
pub mod message_blockdata;

p2p/src/message.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,6 @@ mod test {
956956
use alloc::vec;
957957
use std::net::Ipv4Addr;
958958

959-
use bitcoin::bip152::BlockTransactionsRequest;
960959
use bitcoin::bip158::{FilterHash, FilterHeader};
961960
use bitcoin::block::{Block, BlockHash};
962961
use bitcoin::consensus::encode::{deserialize, deserialize_partial, serialize};
@@ -966,6 +965,7 @@ mod test {
966965

967966
use super::*;
968967
use crate::address::AddrV2;
968+
use crate::bip152::BlockTransactionsRequest;
969969
use crate::message_blockdata::{GetBlocksMessage, GetHeadersMessage, Inventory};
970970
use crate::message_bloom::{BloomFlags, FilterAdd, FilterLoad};
971971
use crate::message_compact_blocks::{GetBlockTxn, SendCmpct};

p2p/src/message_compact_blocks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
66
#[cfg(feature = "arbitrary")]
77
use arbitrary::{Arbitrary, Unstructured};
8-
use bitcoin::bip152;
98

9+
use crate::bip152;
1010
use crate::consensus::impl_consensus_encoding;
1111

1212
/// sendcmpct message

0 commit comments

Comments
 (0)