@@ -713,31 +713,37 @@ impl ProtocolSim for UniswapV4State {
713713 }
714714
715715 // Update block information if provided in the delta attributes
716- if let Some ( block_number_bytes) = delta
716+ let block_number_bytes = delta
717717 . updated_attributes
718718 . get ( "block_number" )
719- {
720- self . block . number = u64:: from_be_bytes (
721- block_number_bytes[ block_number_bytes. len ( ) - 8 ..]
722- . try_into ( )
723- . map_err ( |_| {
724- TransitionError :: DecodeError ( "Invalid block_number bytes" . to_string ( ) )
725- } ) ?,
726- ) ;
727- }
719+ . ok_or_else ( || {
720+ SimulationError :: FatalError ( "block_number not found in updated attributes" . into ( ) )
721+ } ) ?;
728722
729- if let Some ( block_timestamp_bytes) = delta
723+ self . block . number = u64:: from_be_bytes (
724+ block_number_bytes[ block_number_bytes. len ( ) - 8 ..]
725+ . try_into ( )
726+ . map_err ( |_| {
727+ TransitionError :: DecodeError ( "Invalid block_number bytes" . to_string ( ) )
728+ } ) ?,
729+ ) ;
730+
731+ let block_timestamp_bytes = delta
730732 . updated_attributes
731733 . get ( "block_timestamp" )
732- {
733- self . block . timestamp = u64:: from_be_bytes (
734- block_timestamp_bytes[ block_timestamp_bytes. len ( ) - 8 ..]
735- . try_into ( )
736- . map_err ( |_| {
737- TransitionError :: DecodeError ( "Invalid block_timestamp bytes" . to_string ( ) )
738- } ) ?,
739- ) ;
740- }
734+ . ok_or_else ( || {
735+ SimulationError :: FatalError (
736+ "block_timestamp not found in updated attributes" . into ( ) ,
737+ )
738+ } ) ?;
739+
740+ self . block . timestamp = u64:: from_be_bytes (
741+ block_timestamp_bytes[ block_timestamp_bytes. len ( ) - 8 ..]
742+ . try_into ( )
743+ . map_err ( |_| {
744+ TransitionError :: DecodeError ( "Invalid block_timestamp bytes" . to_string ( ) )
745+ } ) ?,
746+ ) ;
741747
742748 // Apply attribute changes
743749 if let Some ( liquidity) = delta
@@ -890,7 +896,7 @@ mod tests {
890896 }
891897
892898 #[ test]
893- fn test_delta_transition_block_update ( ) {
899+ fn test_delta_transition_missing_block_update ( ) {
894900 let block = BlockHeader {
895901 number : 1000 ,
896902 hash : Bytes :: from_str (
@@ -914,25 +920,19 @@ mod tests {
914920 assert_eq ! ( pool. block. number, 1000 ) ;
915921 assert_eq ! ( pool. block. timestamp, 1758201863 ) ;
916922
917- let attributes: HashMap < String , Bytes > = [
918- ( "block_number" . to_string ( ) , Bytes :: from ( 2000_u64 . to_be_bytes ( ) . to_vec ( ) ) ) ,
919- ( "block_timestamp" . to_string ( ) , Bytes :: from ( 1758201935_u64 . to_be_bytes ( ) . to_vec ( ) ) ) ,
920- ]
921- . into_iter ( )
922- . collect ( ) ;
923+ let attributes: HashMap < String , Bytes > =
924+ [ ( "block_number" . to_string ( ) , Bytes :: from ( 2000_u64 . to_be_bytes ( ) . to_vec ( ) ) ) ]
925+ . into_iter ( )
926+ . collect ( ) ;
923927
924928 let delta = ProtocolStateDelta {
925929 component_id : "State1" . to_owned ( ) ,
926930 updated_attributes : attributes,
927931 deleted_attributes : HashSet :: new ( ) ,
928932 } ;
929933
930- pool. delta_transition ( delta, & HashMap :: new ( ) , & Balances :: default ( ) )
931- . unwrap ( ) ;
932-
933- // Verify block was updated
934- assert_eq ! ( pool. block. number, 2000 ) ;
935- assert_eq ! ( pool. block. timestamp, 1758201935 ) ;
934+ let result = pool. delta_transition ( delta, & HashMap :: new ( ) , & Balances :: default ( ) ) ;
935+ assert ! ( result. is_err( ) )
936936 }
937937
938938 #[ test]
@@ -966,6 +966,8 @@ mod tests {
966966 ( "fee" . to_string ( ) , Bytes :: from ( 100_u32 . to_be_bytes ( ) . to_vec ( ) ) ) ,
967967 ( "ticks/-120/net_liquidity" . to_string ( ) , Bytes :: from ( 10200_u64 . to_be_bytes ( ) . to_vec ( ) ) ) ,
968968 ( "ticks/120/net_liquidity" . to_string ( ) , Bytes :: from ( 9800_u64 . to_be_bytes ( ) . to_vec ( ) ) ) ,
969+ ( "block_number" . to_string ( ) , Bytes :: from ( 2000_u64 . to_be_bytes ( ) . to_vec ( ) ) ) ,
970+ ( "block_timestamp" . to_string ( ) , Bytes :: from ( 1758201935_u64 . to_be_bytes ( ) . to_vec ( ) ) ) ,
969971 ]
970972 . into_iter ( )
971973 . collect ( ) ;
@@ -999,6 +1001,9 @@ mod tests {
9991001 . net_liquidity,
10001002 9800
10011003 ) ;
1004+ // Verify block was updated
1005+ assert_eq ! ( pool. block. number, 2000 ) ;
1006+ assert_eq ! ( pool. block. timestamp, 1758201935 ) ;
10021007 }
10031008
10041009 #[ tokio:: test]
0 commit comments