@@ -57,6 +57,10 @@ pub struct Metadata(pub Vec<u8>);
5757#[ derive( Debug , Clone , PartialEq ) ]
5858pub struct BlockHeaderSize ( usize ) ;
5959
60+ /// Encoded block header as cbor
61+ #[ derive( Debug , Clone , PartialEq ) ]
62+ pub struct EncodedBlockHeader ( pub Vec < u8 > ) ;
63+
6064/// Decoded block data
6165#[ derive( Debug , Clone , PartialEq ) ]
6266pub struct DecodedBlockData ( Vec < u8 > ) ;
@@ -127,12 +131,18 @@ pub enum HashFunction {
127131///
128132/// Returns an error if block encoding fails
129133pub fn encode_block (
130- block_hdr_cbor : Vec < u8 > , block_data : & EncodedBlockData , validator_keys : & ValidatorKeys ,
131- hasher : & HashFunction ,
134+ block_hdr_cbor : EncodedBlockHeader , block_data : & EncodedBlockData ,
135+ validator_keys : & ValidatorKeys , hasher : & HashFunction ,
132136) -> anyhow:: Result < EncodedBlock > {
137+ // Enforce block data to be cbor encoded in the form of CBOR byte strings
138+ // which are just (ordered) series of bytes without further interpretation
139+ let binding = block_data. 0 . clone ( ) ;
140+ let mut block_data_cbor_encoding_check = minicbor:: Decoder :: new ( & binding) ;
141+ let _ = block_data_cbor_encoding_check. bytes ( ) ?;
142+
133143 let hashed_block_header = match hasher {
134- HashFunction :: Blake3 => blake3 ( & block_hdr_cbor) ?. to_vec ( ) ,
135- HashFunction :: Blake2b => blake2b_512 ( & block_hdr_cbor) ?. to_vec ( ) ,
144+ HashFunction :: Blake3 => blake3 ( & block_hdr_cbor. 0 ) ?. to_vec ( ) ,
145+ HashFunction :: Blake2b => blake2b_512 ( & block_hdr_cbor. 0 ) ?. to_vec ( ) ,
136146 } ;
137147
138148 // validator_signature MUST be a signature of the hashed block_header bytes
@@ -163,7 +173,7 @@ pub fn encode_block(
163173
164174 let block_data_with_sigs = encoder. writer ( ) . clone ( ) ;
165175 // block hdr + block data + sigs
166- let encoded_block = [ block_hdr_cbor, block_data_with_sigs] . concat ( ) ;
176+ let encoded_block = [ block_hdr_cbor. 0 , block_data_with_sigs] . concat ( ) ;
167177
168178 Ok ( encoded_block)
169179}
@@ -216,7 +226,7 @@ pub(crate) fn blake2b_512(value: &[u8]) -> anyhow::Result<[u8; 64]> {
216226 . map_err ( |_| anyhow:: anyhow!( "Invalid length of blake2b_512, expected 64 got {}" , b. len( ) ) )
217227}
218228
219- /// Encode block header
229+ /// Encode block header as cbor
220230/// ## Errors
221231///
222232/// Returns an error if block header encoding fails.
@@ -497,8 +507,8 @@ mod tests {
497507 use super :: { decode_genesis_block, encode_genesis} ;
498508 use crate :: serialize:: {
499509 blake2b_512, decode_block, decode_block_header, encode_block, encode_block_header,
500- BlockTimeStamp , ChainId , EncodedBlockData , HashFunction :: Blake2b , Height , Kid , LedgerType ,
501- Metadata , PreviousBlockHash , PurposeId , Validator , ValidatorKeys ,
510+ BlockTimeStamp , ChainId , EncodedBlockData , EncodedBlockHeader , HashFunction :: Blake2b ,
511+ Height , Kid , LedgerType , Metadata , PreviousBlockHash , PurposeId , Validator , ValidatorKeys ,
502512 } ;
503513 #[ test]
504514 fn block_header_encode_decode ( ) {
@@ -596,10 +606,11 @@ mod tests {
596606 ] ;
597607
598608 block_data. bytes ( block_data_bytes) . unwrap ( ) ;
609+ let encoded_block_data = block_data. writer ( ) . to_vec ( ) ;
599610
600611 let encoded_block = encode_block (
601- encoded_block_hdr. clone ( ) ,
602- & EncodedBlockData ( block_data_bytes . to_vec ( ) ) ,
612+ EncodedBlockHeader ( encoded_block_hdr. clone ( ) ) ,
613+ & EncodedBlockData ( encoded_block_data . clone ( ) ) ,
603614 & ValidatorKeys ( vec ! [ validator_secret_key_bytes, validator_secret_key_bytes] ) ,
604615 & Blake2b ,
605616 )
@@ -615,11 +626,11 @@ mod tests {
615626 assert_eq ! ( decoded. 0 . 6 , validators) ;
616627 assert_eq ! ( decoded. 0 . 7 , metadata) ;
617628
618- assert_eq ! ( decoded. 1 . 0 , block_data_bytes . to_vec ( ) ) ;
629+ assert_eq ! ( decoded. 1 . 0 , encoded_block_data ) ;
619630
620631 let data_to_sign = [
621632 blake2b_512 ( & encoded_block_hdr) . unwrap ( ) . to_vec ( ) ,
622- block_data_bytes . to_vec ( ) ,
633+ encoded_block_data . to_vec ( ) ,
623634 ]
624635 . concat ( ) ;
625636
0 commit comments