Skip to content

Commit 72e97c6

Browse files
committed
Add a hash value to Inventory's Error variant
While the hash value of the Error variant is meaningless, the variant still conforms to all other Inventory messages and requires a 32 byte hash to be sent over the wire. This is how bitcoin core operates. This patch adds the 32 byte array to the Error variant in order to make its Encoding and Decoding paths symmetrical. This also allows a reader to discard the 32 bytes when decoding a message. The hash is still not exposed to the caller. This was never a problem before because the top level RawNetworkPackage pulls all the required bytes off a reader before decoding. But this is not as easy to do with the v2 p2p network messages.
1 parent ebe43b6 commit 72e97c6

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

bitcoin/src/p2p/message.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ mod test {
583583
)]),
584584
NetworkMessage::Inv(vec![Inventory::Block(hash([8u8; 32]).into())]),
585585
NetworkMessage::GetData(vec![Inventory::Transaction(hash([45u8; 32]).into())]),
586-
NetworkMessage::NotFound(vec![Inventory::Error]),
586+
NetworkMessage::NotFound(vec![Inventory::Error([0u8; 32])]),
587587
NetworkMessage::GetBlocks(GetBlocksMessage::new(
588588
vec![hash([1u8; 32]).into(), hash([4u8; 32]).into()],
589589
hash([5u8; 32]).into(),

bitcoin/src/p2p/message_blockdata.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ use crate::transaction::{Txid, Wtxid};
1616
/// An inventory item.
1717
#[derive(PartialEq, Eq, Clone, Debug, Copy, Hash, PartialOrd, Ord)]
1818
pub enum Inventory {
19-
/// Error --- these inventories can be ignored
20-
Error,
19+
/// Error --- these inventories can be ignored.
20+
/// While a 32 byte hash is expected over the wire, the value is meaningless.
21+
Error([u8; 32]),
2122
/// Transaction
2223
Transaction(Txid),
2324
/// Block
@@ -42,10 +43,10 @@ pub enum Inventory {
4243
impl Inventory {
4344
/// Return the item value represented as a SHA256-d hash.
4445
///
45-
/// Returns [None] only for [Inventory::Error].
46+
/// Returns [None] only for [Inventory::Error] who's hash value is meaningless.
4647
pub fn network_hash(&self) -> Option<[u8; 32]> {
4748
match self {
48-
Inventory::Error => None,
49+
Inventory::Error(_) => None,
4950
Inventory::Transaction(t) => Some(t.to_byte_array()),
5051
Inventory::Block(b) => Some(b.to_byte_array()),
5152
Inventory::CompactBlock(b) => Some(b.to_byte_array()),
@@ -66,7 +67,7 @@ impl Encodable for Inventory {
6667
};
6768
}
6869
Ok(match *self {
69-
Inventory::Error => encode_inv!(0, [0; 32]),
70+
Inventory::Error(_) => encode_inv!(0, [0; 32]),
7071
Inventory::Transaction(ref t) => encode_inv!(1, t),
7172
Inventory::Block(ref b) => encode_inv!(2, b),
7273
Inventory::CompactBlock(ref b) => encode_inv!(4, b),
@@ -83,7 +84,7 @@ impl Decodable for Inventory {
8384
fn consensus_decode<R: BufRead + ?Sized>(r: &mut R) -> Result<Self, encode::Error> {
8485
let inv_type: u32 = Decodable::consensus_decode(r)?;
8586
Ok(match inv_type {
86-
0 => Inventory::Error,
87+
0 => Inventory::Error(Decodable::consensus_decode(r)?),
8788
1 => Inventory::Transaction(Decodable::consensus_decode(r)?),
8889
2 => Inventory::Block(Decodable::consensus_decode(r)?),
8990
4 => Inventory::CompactBlock(Decodable::consensus_decode(r)?),

0 commit comments

Comments
 (0)