Read and write the block header version, which is fixed at 4

This commit is contained in:
Deirdre Connolly 2020-02-03 20:02:30 -05:00 committed by Deirdre Connolly
parent 7ebb5ccd41
commit 4fcb66ad3b
3 changed files with 12 additions and 3 deletions

View File

@ -119,6 +119,8 @@ pub struct BlockHeader {
impl ZcashSerialize for BlockHeader {
fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), SerializationError> {
// "The current and only defined block version number for Zcash is 4."
writer.write_u32::<LittleEndian>(4)?;
self.previous_block_hash.zcash_serialize(&mut writer)?;
writer.write_all(&self.merkle_root_hash.0[..])?;
writer.write_all(&self.final_sapling_root_hash.0[..])?;
@ -132,6 +134,13 @@ impl ZcashSerialize for BlockHeader {
impl ZcashDeserialize for BlockHeader {
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
// "The current and only defined block version number for Zcash is 4."
let version = reader.read_u32::<LittleEndian>()?;
if version != 4 {
return Err(SerializationError::Parse("bad block header"));
}
Ok(BlockHeader {
previous_block_hash: BlockHeaderHash::zcash_deserialize(&mut reader)?,
merkle_root_hash: MerkleTreeRootHash(reader.read_32_bytes()?),

View File

@ -1,5 +1,6 @@
// Copied from librustzcash
// From mainnet block 415000.
// https://explorer.zcha.in/blocks/415000
pub const HEADER_MAINNET_415000: [u8; 1487] = [
0x04, 0x00, 0x00, 0x00, 0x52, 0x74, 0xb4, 0x3b, 0x9e, 0x4a, 0xd8, 0xf4, 0x3e, 0x93, 0xf7, 0x84,
0x63, 0xd2, 0x4d, 0xcf, 0xe5, 0x31, 0xae, 0xb4, 0x71, 0x98, 0x19, 0xf4, 0xf9, 0x7f, 0x7e, 0x03,

View File

@ -81,7 +81,7 @@ fn blockheaderhash_from_blockheader() {
assert_eq!(
format!("{:?}", hash),
"BlockHeaderHash(\"738948d714d44f3040c2b6809ba7dbc8f4ba673dad39fd755ea4aeaa514cc386\")"
"BlockHeaderHash(\"e942461f17dd1daea4157df9ba7a2aa5c90a885ac205375a0161f682635da207\")"
);
let mut bytes = Cursor::new(Vec::new());
@ -99,9 +99,8 @@ fn blockheaderhash_from_blockheader() {
#[test]
fn deserialize_blockheader() {
// This deserializes without error but the result looks wrong.
// https://explorer.zcha.in/blocks/415000
let header = BlockHeader::zcash_deserialize(&test_vectors::HEADER_MAINNET_415000[..])
let _header = BlockHeader::zcash_deserialize(&test_vectors::HEADER_MAINNET_415000[..])
.expect("blockheader test vector should deserialize");
}