Merge rust-bitcoin/rust-bitcoin#515: Add Inventory::network_hash() method

3d524e06e4 Add Inventory::network_hash() method (Steven Roose)

Pull request description:

  I'm not positive we won't ever had inv items that are not `sha256d::Hash`, though. I would expect them to stay like that, but we never know.

  Would accept that as a reasonable objection against this helper.

ACKs for top commit:
  apoelstra:
    ACK 3d524e06e4
  tcharding:
    ACK 3d524e06e4

Tree-SHA512: 97d64b08ff99dbec2d907bdb98aa41ede4a15b988551ad8dd87a10ac5c9750ae4d516bb558c1e4f2171612777355c2baf1ffbe671cb92320a1af64d73bd150dd
This commit is contained in:
Andrew Poelstra 2023-04-13 22:38:55 +00:00
commit 3795f60fa8
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 18 additions and 0 deletions

View File

@ -42,6 +42,24 @@ pub enum Inventory {
},
}
impl Inventory {
/// Return the item value represented as a SHA256-d hash.
///
/// Returns [None] only for [Inventory::Error].
pub fn network_hash(&self) -> Option<[u8; 32]> {
match self {
Inventory::Error => None,
Inventory::Transaction(t) => Some(t.to_byte_array()),
Inventory::Block(b) => Some(b.to_byte_array()),
Inventory::CompactBlock(b) => Some(b.to_byte_array()),
Inventory::WTx(t) => Some(t.to_byte_array()),
Inventory::WitnessTransaction(t) => Some(t.to_byte_array()),
Inventory::WitnessBlock(b) => Some(b.to_byte_array()),
Inventory::Unknown { hash, .. } => Some(*hash),
}
}
}
impl Encodable for Inventory {
#[inline]
fn consensus_encode<W: io::Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {