@@ -388,7 +388,7 @@ impl<T: WriteConnectionProvider + ?Sized + Sync> DatabaseWriteOperations for T {
388388 let Some ( ( block_info, batch_info) ) =
389389 self . get_latest_safe_l2_info ( ) . await ?. filter ( |( block_info, _) | block_info. number > 0 )
390390 else {
391- return Ok ( ( None , None ) )
391+ return Ok ( ( None , None ) ) ;
392392 } ;
393393 let batch = self . get_batch_by_index ( batch_info. index ) . await ?. expect ( "batch must exist" ) ;
394394 Ok ( ( Some ( block_info) , Some ( batch. block_number . saturating_add ( 1 ) ) ) )
@@ -864,7 +864,7 @@ impl<T: ReadConnectionProvider + Sync + ?Sized> DatabaseReadOperations for T {
864864 // Provides a stream over all L1 messages with increasing queue index starting that have
865865 // not been included in an L2 block and have a block number less than or equal to the
866866 // finalized L1 block number (they have been finalized on L1).
867- Some ( L1MessageKey :: NotIncluded ( NotIncludedStart :: FinalizedWithBlockDepth ( depth) ) ) => {
867+ Some ( L1MessageKey :: NotIncluded ( NotIncludedKey :: FinalizedWithBlockDepth ( depth) ) ) => {
868868 // Lookup the finalized L1 block number.
869869 let finalized_block_number = self . get_finalized_l1_block_number ( ) . await ?;
870870
@@ -898,7 +898,7 @@ impl<T: ReadConnectionProvider + Sync + ?Sized> DatabaseReadOperations for T {
898898 // included in an L2 block and have a block number less than or equal to the
899899 // latest L1 block number minus the provided depth (they have been sufficiently deep
900900 // on L1 to be considered safe to include - reorg risk is low).
901- Some ( L1MessageKey :: NotIncluded ( NotIncludedStart :: BlockDepth ( depth) ) ) => {
901+ Some ( L1MessageKey :: NotIncluded ( NotIncludedKey :: BlockDepth ( depth) ) ) => {
902902 // Lookup the latest L1 block number.
903903 let latest_block_number = self . get_latest_l1_block_number ( ) . await ?;
904904
@@ -1055,7 +1055,8 @@ impl<T: ReadConnectionProvider + Sync + ?Sized> DatabaseReadOperations for T {
10551055/// A key for an L1 message stored in the database.
10561056///
10571057/// It can either be the queue index, queue hash or the transaction hash.
1058- #[ derive( Debug , Clone , PartialEq , Eq ) ]
1058+ #[ derive( Debug , Clone , PartialEq , Eq , serde:: Serialize , serde:: Deserialize ) ]
1059+ #[ serde( rename_all = "camelCase" ) ]
10591060pub enum L1MessageKey {
10601061 /// The queue index of the message.
10611062 QueueIndex ( u64 ) ,
@@ -1066,7 +1067,7 @@ pub enum L1MessageKey {
10661067 /// Start from the first message for the provided block number.
10671068 BlockNumber ( u64 ) ,
10681069 /// Start from messages that have not been included in a block yet.
1069- NotIncluded ( NotIncludedStart ) ,
1070+ NotIncluded ( NotIncludedKey ) ,
10701071}
10711072
10721073impl L1MessageKey {
@@ -1093,13 +1094,15 @@ impl L1MessageKey {
10931094
10941095/// This type defines where to start when fetching L1 messages that have not been included in a
10951096/// block yet.
1096- #[ derive( Debug , Clone , PartialEq , Eq ) ]
1097- pub enum NotIncludedStart {
1097+ #[ derive( Debug , Clone , PartialEq , Eq , serde :: Serialize , serde :: Deserialize ) ]
1098+ pub enum NotIncludedKey {
10981099 /// Start from finalized messages that have not been included in a block yet and have a L1
10991100 /// block number that is a specified number of blocks below the current finalized L1 block
11001101 /// number.
1102+ #[ serde( rename = "notIncludedFinalizedWithBlockDepth" ) ]
11011103 FinalizedWithBlockDepth ( u64 ) ,
11021104 /// Start from unfinalized messages that are included in L1 blocks at a specific depth.
1105+ #[ serde( rename = "notIncludedBlockDepth" ) ]
11031106 BlockDepth ( u64 ) ,
11041107}
11051108
@@ -1115,10 +1118,10 @@ impl fmt::Display for L1MessageKey {
11151118 Self :: TransactionHash ( hash) => write ! ( f, "TransactionHash({hash:#x})" ) ,
11161119 Self :: BlockNumber ( number) => write ! ( f, "BlockNumber({number})" ) ,
11171120 Self :: NotIncluded ( start) => match start {
1118- NotIncludedStart :: FinalizedWithBlockDepth ( depth) => {
1121+ NotIncludedKey :: FinalizedWithBlockDepth ( depth) => {
11191122 write ! ( f, "NotIncluded(Finalized:{depth})" )
11201123 }
1121- NotIncludedStart :: BlockDepth ( depth) => {
1124+ NotIncludedKey :: BlockDepth ( depth) => {
11221125 write ! ( f, "NotIncluded(BlockDepth({depth}))" )
11231126 }
11241127 } ,
@@ -1139,3 +1142,71 @@ pub struct UnwindResult {
11391142 /// The L2 safe block info after the unwind. This is only populated if the L2 safe has reorged.
11401143 pub l2_safe_block_info : Option < BlockInfo > ,
11411144}
1145+
1146+ mod tests {
1147+
1148+ #[ test]
1149+ fn test_l1_message_key_serialization ( ) {
1150+ use crate :: { L1MessageKey , NotIncludedKey } ;
1151+ use alloy_primitives:: B256 ;
1152+ use std:: str:: FromStr ;
1153+
1154+ // Test for `L1MessageKey::QueueIndex`
1155+ let key = L1MessageKey :: QueueIndex ( 42 ) ;
1156+ let json = serde_json:: to_string ( & key) . unwrap ( ) ;
1157+ let decoded: L1MessageKey = serde_json:: from_str ( & json) . unwrap ( ) ;
1158+ assert_eq ! ( key, decoded) ;
1159+
1160+ // Test for `L1MessageKey::TransactionHash`
1161+ let key = L1MessageKey :: TransactionHash (
1162+ B256 :: from_str ( "0xa46f0b1dbe17b3d0d86fa70cef4a23dca5efcd35858998cc8c53140d01429746" )
1163+ . unwrap ( ) ,
1164+ ) ;
1165+ let json = serde_json:: to_string ( & key) . unwrap ( ) ;
1166+ let decoded: L1MessageKey = serde_json:: from_str ( & json) . unwrap ( ) ;
1167+ assert_eq ! ( key, decoded) ;
1168+
1169+ // Test for `L1MessageKey::NotIncluded`
1170+ let key = L1MessageKey :: NotIncluded ( NotIncludedKey :: FinalizedWithBlockDepth ( 100 ) ) ;
1171+ let json = serde_json:: to_string ( & key) . unwrap ( ) ;
1172+ let decoded: L1MessageKey = serde_json:: from_str ( & json) . unwrap ( ) ;
1173+ assert_eq ! ( key, decoded) ;
1174+ }
1175+
1176+ #[ test]
1177+ fn test_l1_message_key_manual_serialization ( ) {
1178+ use crate :: { L1MessageKey , NotIncludedKey } ;
1179+ use alloy_primitives:: B256 ;
1180+ use std:: str:: FromStr ;
1181+
1182+ // Test for `L1MessageKey::QueueIndex`
1183+ let json_string_queue_index = r#"{"queueIndex":42}"# ;
1184+ let decoded_queue_index: L1MessageKey =
1185+ serde_json:: from_str ( json_string_queue_index) . unwrap ( ) ;
1186+ assert_eq ! ( decoded_queue_index, L1MessageKey :: QueueIndex ( 42 ) ) ;
1187+
1188+ // Test for `L1MessageKey::TransactionHash`
1189+ let json_string_transaction_hash = r#"{"transactionHash":"0xa46f0b1dbe17b3d0d86fa70cef4a23dca5efcd35858998cc8c53140d01429746"}"# ;
1190+ let decoded_transaction_hash: L1MessageKey =
1191+ serde_json:: from_str ( json_string_transaction_hash) . unwrap ( ) ;
1192+ assert_eq ! (
1193+ decoded_transaction_hash,
1194+ L1MessageKey :: TransactionHash (
1195+ B256 :: from_str(
1196+ "0xa46f0b1dbe17b3d0d86fa70cef4a23dca5efcd35858998cc8c53140d01429746"
1197+ )
1198+ . unwrap( )
1199+ )
1200+ ) ;
1201+
1202+ // Test for `L1MessageKey::NotIncluded`
1203+ let json_string_not_included_key =
1204+ r#"{"notIncluded":{"notIncludedFinalizedWithBlockDepth":100}}"# ;
1205+ let decoded_not_included_key: L1MessageKey =
1206+ serde_json:: from_str ( json_string_not_included_key) . unwrap ( ) ;
1207+ assert_eq ! (
1208+ decoded_not_included_key,
1209+ L1MessageKey :: NotIncluded ( NotIncludedKey :: FinalizedWithBlockDepth ( 100 ) )
1210+ ) ;
1211+ }
1212+ }
0 commit comments