Add Inventory::network_hash() method

This commit is contained in:
Steven Roose 2020-11-09 20:01:18 +00:00
parent e83a2d3422
commit 3d524e06e4
No known key found for this signature in database
GPG Key ID: 2F2A88D7F8D68E87
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 { impl Encodable for Inventory {
#[inline] #[inline]
fn consensus_encode<W: io::Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> { fn consensus_encode<W: io::Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {