Manually implement consensus traits for Block

In preparation for hacking the `Block` type implement the consensus
traits manually instead of using the macro.

Refactor only, no logic changes.
This commit is contained in:
Tobin C. Harding 2024-11-06 12:08:44 +11:00
parent 72760bbe96
commit 5016a73207
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
1 changed files with 31 additions and 2 deletions

View File

@ -110,8 +110,6 @@ pub struct Block {
pub txdata: Vec<Transaction>, pub txdata: Vec<Transaction>,
} }
impl_consensus_encoding!(Block, header, txdata);
impl Block { impl Block {
/// Returns the block hash. /// Returns the block hash.
pub fn block_hash(&self) -> BlockHash { self.header.block_hash() } pub fn block_hash(&self) -> BlockHash { self.header.block_hash() }
@ -272,6 +270,37 @@ impl From<&Block> for BlockHash {
fn from(block: &Block) -> BlockHash { block.block_hash() } fn from(block: &Block) -> BlockHash { block.block_hash() }
} }
impl Encodable for Block {
#[inline]
fn consensus_encode<R: io::Write + ?Sized>(&self, r: &mut R) -> Result<usize, io::Error> {
let mut len = 0;
len += self.header.consensus_encode(r)?;
len += self.txdata.consensus_encode(r)?;
Ok(len)
}
}
impl Decodable for Block {
#[inline]
fn consensus_decode_from_finite_reader<R: io::BufRead + ?Sized>(
r: &mut R,
) -> Result<Block, encode::Error> {
Ok(Block {
header: Decodable::consensus_decode_from_finite_reader(r)?,
txdata: Decodable::consensus_decode_from_finite_reader(r)?,
})
}
#[inline]
fn consensus_decode<R: io::BufRead + ?Sized>(r: &mut R) -> Result<Block, encode::Error> {
let mut r = r.take(internals::ToU64::to_u64(encode::MAX_VEC_SIZE));
Ok(Block {
header: Decodable::consensus_decode(&mut r)?,
txdata: Decodable::consensus_decode(&mut r)?,
})
}
}
/// An error when looking up a BIP34 block height. /// An error when looking up a BIP34 block height.
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive] #[non_exhaustive]