Add a test for Header size

Calculate the header size and test it is equal to `Header::SIZE`
This commit is contained in:
Jamil Lambert, PhD 2025-02-05 21:37:31 +00:00
parent 0df15d5cfd
commit ff3b3ddb67
No known key found for this signature in database
GPG Key ID: 54DC29234AB5D2C0
1 changed files with 24 additions and 0 deletions

View File

@ -380,4 +380,28 @@ mod tests {
let version = Version::from_consensus(1234567890);
assert_eq!(version.to_consensus(), 1234567890);
}
// Check that the size of the header consensus serialization matches the const SIZE value
#[test]
fn header_size() {
let header = Header {
version: Version::ONE,
prev_blockhash: BlockHash::from_byte_array([0x99; 32]),
merkle_root: TxMerkleNode::from_byte_array([0x77; 32]),
time: 2,
bits: CompactTarget::from_consensus(3),
nonce: 4,
};
// Calculate the size of the block header in bytes from the sum of the serialized lengths
// it's fields: version, prev_blockhash, merkle_root, time, bits, nonce.
let header_size = header.version.to_consensus().to_le_bytes().len()
+ header.prev_blockhash.as_byte_array().len()
+ header.merkle_root.as_byte_array().len()
+ header.time.to_le_bytes().len()
+ header.bits.to_consensus().to_le_bytes().len()
+ header.nonce.to_le_bytes().len();
assert_eq!(header_size, Header::SIZE);
}
}