@@ -4,16 +4,11 @@ use alloy_primitives::{Bytes as AlloyBytes, U256};
44use alloy_rpc_types_engine:: ExecutionPayloadV3 ;
55use anyhow:: { Result , anyhow} ;
66use bytes:: { Buf , BufMut } ;
7- use commonware_codec:: { EncodeSize , Error , Read , ReadExt as _, Write } ;
7+ use commonware_codec:: { EncodeSize , Error , Read , Write } ;
8+ use commonware_consensus:: Viewable ;
89use commonware_consensus:: types:: { Height , View } ;
910use commonware_consensus:: { Block as ConsensusBlock , Heightable } ;
10- use commonware_consensus:: {
11- Viewable ,
12- aggregation:: scheme:: bls12381_multisig:: Scheme ,
13- simplex:: types:: { Finalization , Notarization } ,
14- } ;
15- use commonware_cryptography:: bls12381:: primitives:: variant:: { MinPk , Variant } ;
16- use commonware_cryptography:: { Committable , Digestible , Hasher , Sha256 , Signer , sha256:: Digest } ;
11+ use commonware_cryptography:: { Committable , Digestible , Hasher , Sha256 , sha256:: Digest } ;
1712use ssz:: Encode as _;
1813
1914#[ derive( Clone , Debug , PartialEq , Eq ) ]
@@ -291,178 +286,12 @@ impl Committable for Block {
291286 }
292287}
293288
294- #[ derive( Clone , Debug , PartialEq , Eq ) ]
295- pub struct Notarized < C : Signer , V : Variant > {
296- pub proof : Notarization < Scheme < C :: PublicKey , V > , Digest > ,
297- pub block : Block ,
298- }
299-
300- impl < C : Signer , V : Variant > Notarized < C , V > {
301- pub fn new ( proof : Notarization < Scheme < C :: PublicKey , V > , Digest > , block : Block ) -> Self {
302- Self { proof, block }
303- }
304- }
305-
306- impl < C : Signer , V : Variant > Write for Notarized < C , V > {
307- fn write ( & self , buf : & mut impl BufMut ) {
308- self . proof . write ( buf) ;
309- self . block . write ( buf) ;
310- }
311- }
312-
313- impl < C : Signer , V : Variant > Read for Notarized < C , V > {
314- type Cfg = ( ) ;
315-
316- fn read_cfg ( buf : & mut impl Buf , _: & Self :: Cfg ) -> Result < Self , Error > {
317- let proof =
318- Notarization :: < Scheme < C :: PublicKey , V > , Digest > :: read_cfg ( buf, & buf. remaining ( ) ) ?; // todo: get a test on this to make sure buf.remaining is safe
319- let block = Block :: read ( buf) ?;
320-
321- // Ensure the proof is for the block
322- if proof. proposal . payload != block. digest ( ) {
323- return Err ( Error :: Invalid (
324- "types::Notarized" ,
325- "Proof payload does not match block digest" ,
326- ) ) ;
327- }
328- Ok ( Self { proof, block } )
329- }
330- }
331-
332- impl < C : Signer , V : Variant > EncodeSize for Notarized < C , V > {
333- fn encode_size ( & self ) -> usize {
334- self . proof . encode_size ( ) + self . block . encode_size ( )
335- }
336- }
337-
338- #[ derive( Clone , Debug , PartialEq , Eq ) ]
339- pub struct Finalized < C : Signer , V : Variant > {
340- pub proof : Finalization < Scheme < C :: PublicKey , V > , Digest > ,
341- pub block : Block ,
342- }
343-
344- impl < C : Signer , V : Variant > Finalized < C , V > {
345- pub fn new ( proof : Finalization < Scheme < C :: PublicKey , V > , Digest > , block : Block ) -> Self {
346- Self { proof, block }
347- }
348- }
349-
350- impl < C : Signer , V : Variant > Write for Finalized < C , V > {
351- fn write ( & self , buf : & mut impl BufMut ) {
352- self . proof . write ( buf) ;
353- self . block . write ( buf) ;
354- }
355- }
356-
357- impl < C : Signer , V : Variant > Read for Finalized < C , V > {
358- type Cfg = ( ) ;
359-
360- fn read_cfg ( buf : & mut impl Buf , _: & Self :: Cfg ) -> Result < Self , Error > {
361- let proof =
362- Finalization :: < Scheme < C :: PublicKey , V > , Digest > :: read_cfg ( buf, & buf. remaining ( ) ) ?;
363- let block = Block :: read ( buf) ?;
364-
365- // Ensure the proof is for the block
366- if proof. proposal . payload != block. digest ( ) {
367- return Err ( Error :: Invalid (
368- "types::Finalized" ,
369- "Proof payload does not match block digest" ,
370- ) ) ;
371- }
372- Ok ( Self { proof, block } )
373- }
374- }
375-
376- impl < C : Signer , V : Variant > EncodeSize for Finalized < C , V > {
377- fn encode_size ( & self ) -> usize {
378- self . proof . encode_size ( ) + self . block . encode_size ( )
379- }
380- }
381-
382- #[ derive( Clone , Debug , PartialEq , Eq ) ]
383- pub struct BlockWithFinalization <
384- C : Signer = commonware_cryptography:: bls12381:: PrivateKey ,
385- V : Variant = MinPk ,
386- > {
387- pub block : Block ,
388- pub finalized : Option < Finalization < Scheme < C :: PublicKey , V > , Digest > > ,
389- }
390-
391- impl < C : Signer , V : Variant > Digestible for BlockWithFinalization < C , V > {
392- type Digest = Digest ;
393-
394- fn digest ( & self ) -> Digest {
395- self . block . header . digest
396- }
397- }
398-
399- impl < C : Signer , V : Variant > Committable for BlockWithFinalization < C , V > {
400- type Commitment = Digest ;
401-
402- fn commitment ( & self ) -> Digest {
403- self . block . header . digest
404- }
405- }
406-
407- impl < C : Signer , V : Variant > Heightable for BlockWithFinalization < C , V > {
408- fn height ( & self ) -> Height {
409- Height :: new ( self . block . header . height )
410- }
411- }
412-
413- impl < C : Signer , V : Variant > ConsensusBlock for BlockWithFinalization < C , V > {
414- fn parent ( & self ) -> Self :: Commitment {
415- self . block . header . parent
416- }
417- }
418-
419- impl < C : Signer , V : Variant > Read for BlockWithFinalization < C , V > {
420- type Cfg = ( ) ;
421-
422- fn read_cfg ( buf : & mut impl Buf , _: & Self :: Cfg ) -> Result < Self , Error > {
423- let block = Block :: read ( buf) ?;
424- let has_finalized = buf. get_u8 ( ) ;
425- let finalized = if has_finalized == 1 {
426- Some ( Finalization :: < Scheme < C :: PublicKey , V > , Digest > :: read_cfg (
427- buf,
428- & buf. remaining ( ) ,
429- ) ?)
430- } else {
431- None
432- } ;
433- Ok ( Self { block, finalized } )
434- }
435- }
436-
437- impl < C : Signer , V : Variant > Write for BlockWithFinalization < C , V > {
438- fn write ( & self , buf : & mut impl BufMut ) {
439- self . block . write ( buf) ;
440- if let Some ( ref finalized) = self . finalized {
441- buf. put_u8 ( 1u8 ) ;
442- finalized. write ( buf) ;
443- } else {
444- buf. put_u8 ( 0u8 ) ;
445- }
446- }
447- }
448-
449- impl < C : Signer , V : Variant > EncodeSize for BlockWithFinalization < C , V > {
450- fn encode_size ( & self ) -> usize {
451- let mut size = self . block . encode_size ( ) + 1 ; // +1 for the has_finalized flag
452- if let Some ( ref finalized) = self . finalized {
453- size += finalized. encode_size ( ) ;
454- }
455- size
456- }
457- }
458-
459289#[ cfg( test) ]
460290mod test {
461291 use super :: * ;
462292 use alloy_primitives:: { Bytes as AlloyBytes , U256 , hex} ;
463293 use alloy_rpc_types_engine:: { ExecutionPayloadV1 , ExecutionPayloadV2 } ;
464294 use commonware_codec:: { DecodeExt as _, Encode as _} ;
465- use commonware_cryptography:: bls12381:: primitives:: variant:: MinPk ;
466295 use commonware_cryptography:: { Signer , bls12381} ;
467296
468297 fn create_test_public_key ( seed : u8 ) -> PublicKey {
@@ -629,82 +458,4 @@ mod test {
629458 assert_eq ! ( actual_encoded. len( ) , pure_ssz. len( ) + 4 ) ;
630459 assert_eq ! ( actual_encoded. len( ) , encode_len) ;
631460 }
632-
633- #[ test]
634- fn test_block_envelope_without_finalization ( ) {
635- let block = Block :: genesis ( [ 0 ; 32 ] ) ;
636- let envelope =
637- BlockWithFinalization :: < commonware_cryptography:: bls12381:: PrivateKey , MinPk > {
638- block : block. clone ( ) ,
639- finalized : None ,
640- } ;
641-
642- // Test encoding and decoding
643- let encoded = envelope. encode ( ) ;
644- let decoded =
645- BlockWithFinalization :: < commonware_cryptography:: bls12381:: PrivateKey , MinPk > :: decode (
646- encoded. clone ( ) ,
647- )
648- . unwrap ( ) ;
649-
650- // Verify round-trip: encode the decoded value and compare bytes
651- let re_encoded = decoded. encode ( ) ;
652- assert_eq ! (
653- encoded, re_encoded,
654- "Round-trip encoding should be identical"
655- ) ;
656-
657- // Verify structure
658- assert ! ( decoded. finalized. is_none( ) ) ;
659- assert_eq ! ( envelope. block. header. height, decoded. block. header. height) ;
660- assert_eq ! (
661- envelope. block. header. timestamp,
662- decoded. block. header. timestamp
663- ) ;
664- }
665-
666- #[ test]
667- fn test_block_envelope_encode_size_without_finalization ( ) {
668- let block = Block :: genesis ( [ 0 ; 32 ] ) ;
669- let envelope =
670- BlockWithFinalization :: < commonware_cryptography:: bls12381:: PrivateKey , MinPk > {
671- block : block. clone ( ) ,
672- finalized : None ,
673- } ;
674-
675- let encode_size = envelope. encode_size ( ) ;
676- let actual_encoded = envelope. encode ( ) ;
677-
678- // Size should be block size + 1 byte for the flag
679- assert_eq ! ( actual_encoded. len( ) , encode_size) ;
680- assert_eq ! ( encode_size, block. encode_size( ) + 1 ) ;
681- }
682-
683- #[ test]
684- fn test_block_envelope_digestible ( ) {
685- let block = Block :: genesis ( [ 0 ; 32 ] ) ;
686- let envelope =
687- BlockWithFinalization :: < commonware_cryptography:: bls12381:: PrivateKey , MinPk > {
688- block : block. clone ( ) ,
689- finalized : None ,
690- } ;
691-
692- // BlockEnvelope digest should match the underlying block digest
693- assert_eq ! ( envelope. digest( ) , block. digest( ) ) ;
694- assert_eq ! ( envelope. commitment( ) , block. commitment( ) ) ;
695- }
696-
697- #[ test]
698- fn test_block_envelope_consensus_block ( ) {
699- let block = Block :: genesis ( [ 0 ; 32 ] ) ;
700- let envelope =
701- BlockWithFinalization :: < commonware_cryptography:: bls12381:: PrivateKey , MinPk > {
702- block : block. clone ( ) ,
703- finalized : None ,
704- } ;
705-
706- // BlockEnvelope should expose the same ConsensusBlock properties as the underlying block
707- assert_eq ! ( envelope. height( ) . get( ) , block. height( ) ) ;
708- assert_eq ! ( envelope. parent( ) , block. parent( ) ) ;
709- }
710461}
0 commit comments