@@ -9,17 +9,18 @@ use super::sql::{
99 sql_count_user_ops_for_frame, sql_insert_open_batch, sql_insert_open_batch_with_index,
1010 sql_insert_open_frame, sql_insert_safe_inputs_batch,
1111 sql_insert_sequenced_direct_inputs_for_frame, sql_insert_user_ops_and_sequenced_batch,
12- sql_select_frames_for_batch, sql_select_latest_batch_index,
12+ sql_select_batch_policy , sql_select_frames_for_batch, sql_select_latest_batch_index,
1313 sql_select_latest_batch_with_user_op_count, sql_select_latest_frame_in_batch_for_batch,
1414 sql_select_max_safe_input_index, sql_select_ordered_l2_tx_count,
1515 sql_select_ordered_l2_txs_for_batch, sql_select_ordered_l2_txs_from_offset,
16- sql_select_ordered_l2_txs_page_from_offset, sql_select_recommended_fee , sql_select_safe_block,
16+ sql_select_ordered_l2_txs_page_from_offset, sql_select_safe_block,
1717 sql_select_safe_input_payloads_for_sender, sql_select_safe_inputs_range,
1818 sql_select_total_drained_direct_inputs, sql_select_user_ops_for_frame,
19- sql_update_recommended_fee , sql_update_safe_block,
19+ sql_update_batch_policy_alpha , sql_update_batch_policy_gas_price , sql_update_safe_block,
2020} ;
2121use super :: {
22- FrameHeader , SafeFrontier , SafeInputRange , StorageOpenError , StoredSafeInput , WriteHead ,
22+ BatchPolicy , FrameHeader , SafeFrontier , SafeInputRange , StorageOpenError , StoredSafeInput ,
23+ WriteHead ,
2324} ;
2425use crate :: inclusion_lane:: PendingUserOp ;
2526use alloy_primitives:: Address ;
@@ -247,30 +248,44 @@ impl Storage {
247248 ) ;
248249
249250 let now_ms = now_unix_ms ( ) ;
250- let frame_fee = query_recommended_fee ( & tx) ?;
251+ let policy = query_batch_policy ( & tx) ?;
251252 insert_open_batch_with_index ( & tx, 0 , now_ms) ?;
252- insert_open_frame ( & tx, 0 , 0 , now_ms, frame_fee , safe_block) ?;
253+ insert_open_frame ( & tx, 0 , 0 , now_ms, policy . recommended_fee , safe_block) ?;
253254 persist_frame_direct_sequence ( & tx, 0 , 0 , leading_direct_range) ?;
254255 tx. commit ( ) ?;
255256
256257 Ok ( WriteHead {
257258 batch_index : 0 ,
258259 batch_created_at : from_unix_ms ( now_ms) ,
259- frame_fee,
260+ frame_fee : policy . recommended_fee ,
260261 safe_block,
261262 batch_user_op_count : 0 ,
262263 open_frame_user_op_count : 0 ,
263264 frame_in_batch : 0 ,
265+ max_batch_user_op_bytes : policy. batch_size_target ,
264266 } )
265267 }
266268
267- pub fn recommended_fee ( & mut self ) -> Result < u64 > {
268- let value = sql_select_recommended_fee ( & self . conn ) ?;
269- Ok ( i64_to_u64 ( value) )
269+ pub fn batch_policy ( & mut self ) -> Result < BatchPolicy > {
270+ let ( fee, target) = sql_select_batch_policy ( & self . conn ) ?;
271+ Ok ( BatchPolicy {
272+ recommended_fee : i64_to_u64 ( fee) ,
273+ batch_size_target : i64_to_u64 ( target) ,
274+ } )
275+ }
276+
277+ pub fn set_gas_price ( & mut self , gas_price : u64 ) -> Result < ( ) > {
278+ let changed_rows =
279+ sql_update_batch_policy_gas_price ( & self . conn , u64_to_i64 ( gas_price) ) ?;
280+ if changed_rows != 1 {
281+ return Err ( rusqlite:: Error :: StatementChangedRows ( changed_rows) ) ;
282+ }
283+ Ok ( ( ) )
270284 }
271285
272- pub fn set_recommended_fee ( & mut self , fee : u64 ) -> Result < ( ) > {
273- let changed_rows = sql_update_recommended_fee ( & self . conn , u64_to_i64 ( fee) ) ?;
286+ pub fn set_alpha ( & mut self , num : u64 , denom : u64 ) -> Result < ( ) > {
287+ let changed_rows =
288+ sql_update_batch_policy_alpha ( & self . conn , u64_to_i64 ( num) , u64_to_i64 ( denom) ) ?;
274289 if changed_rows != 1 {
275290 return Err ( rusqlite:: Error :: StatementChangedRows ( changed_rows) ) ;
276291 }
@@ -317,14 +332,14 @@ impl Storage {
317332 . transaction_with_behavior ( TransactionBehavior :: Immediate ) ?;
318333 assert_write_head_matches_open_state ( & tx, head) ?;
319334 let now_ms = now_unix_ms ( ) ;
320- let next_frame_fee = query_recommended_fee ( & tx) ?;
335+ let policy = query_batch_policy ( & tx) ?;
321336 let next_frame_in_batch = head. frame_in_batch . saturating_add ( 1 ) ;
322337 insert_open_frame (
323338 & tx,
324339 head. batch_index ,
325340 next_frame_in_batch,
326341 now_ms,
327- next_frame_fee ,
342+ policy . recommended_fee ,
328343 next_safe_block,
329344 ) ?;
330345 persist_frame_direct_sequence (
@@ -334,7 +349,7 @@ impl Storage {
334349 leading_direct_range,
335350 ) ?;
336351 tx. commit ( ) ?;
337- head. advance_frame ( next_frame_fee , next_safe_block) ;
352+ head. advance_frame ( policy , next_safe_block) ;
338353 Ok ( ( ) )
339354 }
340355
@@ -348,23 +363,23 @@ impl Storage {
348363 . transaction_with_behavior ( TransactionBehavior :: Immediate ) ?;
349364 assert_write_head_matches_open_state ( & tx, head) ?;
350365 let now_ms = now_unix_ms ( ) ;
351- // Frame fee is committed here: we sample the current recommendation once and
352- // assign it to the newly opened frame .
353- let next_frame_fee = query_recommended_fee ( & tx) ?;
366+ // Batch policy is sampled here: the derived fee is committed to the newly
367+ // opened frame, and the batch size target is stored on the write head .
368+ let policy = query_batch_policy ( & tx) ?;
354369 let next_batch_index = insert_open_batch ( & tx, now_ms) ?;
355370 insert_open_frame (
356371 & tx,
357372 next_batch_index,
358373 0 ,
359374 now_ms,
360- next_frame_fee ,
375+ policy . recommended_fee ,
361376 next_safe_block,
362377 ) ?;
363378 tx. commit ( ) ?;
364379 head. move_to_next_batch (
365380 next_batch_index,
366381 from_unix_ms ( now_ms) ,
367- next_frame_fee ,
382+ policy ,
368383 next_safe_block,
369384 ) ;
370385 Ok ( ( ) )
@@ -516,6 +531,7 @@ fn load_current_write_head(tx: &Transaction<'_>) -> Result<Option<WriteHead>> {
516531 } ;
517532 let ( frame_in_batch, frame_fee, safe_block) = query_latest_frame_in_batch ( tx, batch_index) ?;
518533 let open_frame_user_op_count = query_frame_user_op_count ( tx, batch_index, frame_in_batch) ?;
534+ let policy = query_batch_policy ( tx) ?;
519535 Ok ( Some ( WriteHead {
520536 batch_index,
521537 batch_created_at,
@@ -524,6 +540,7 @@ fn load_current_write_head(tx: &Transaction<'_>) -> Result<Option<WriteHead>> {
524540 batch_user_op_count,
525541 open_frame_user_op_count,
526542 frame_in_batch,
543+ max_batch_user_op_bytes : policy. batch_size_target ,
527544 } ) )
528545}
529546
@@ -606,9 +623,12 @@ fn query_current_safe_block(tx: &Connection) -> Result<u64> {
606623 Ok ( i64_to_u64 ( value) )
607624}
608625
609- fn query_recommended_fee ( tx : & Transaction < ' _ > ) -> Result < u64 > {
610- let value = sql_select_recommended_fee ( tx) ?;
611- Ok ( i64_to_u64 ( value) )
626+ fn query_batch_policy ( tx : & Transaction < ' _ > ) -> Result < BatchPolicy > {
627+ let ( fee, target) = sql_select_batch_policy ( tx) ?;
628+ Ok ( BatchPolicy {
629+ recommended_fee : i64_to_u64 ( fee) ,
630+ batch_size_target : i64_to_u64 ( target) ,
631+ } )
612632}
613633
614634fn persist_frame_direct_sequence (
@@ -761,12 +781,13 @@ mod tests {
761781 }
762782
763783 #[ test]
764- fn next_frame_fee_comes_from_recommended_fee_singleton ( ) {
765- let db = temp_db ( "recommended -fee" ) ;
784+ fn next_frame_fee_comes_from_batch_policy ( ) {
785+ let db = temp_db ( "batch-policy -fee" ) ;
766786 let mut storage = Storage :: open ( db. path . as_str ( ) , "NORMAL" ) . expect ( "open storage" ) ;
767- assert_eq ! ( storage. recommended_fee( ) . expect( "default recommended" ) , 0 ) ;
787+ let policy = storage. batch_policy ( ) . expect ( "default policy" ) ;
788+ assert_eq ! ( policy. recommended_fee, 0 ) ;
768789
769- storage. set_recommended_fee ( 7 ) . expect ( "set recommended fee " ) ;
790+ storage. set_gas_price ( 100 ) . expect ( "set gas price " ) ;
770791
771792 let mut head = storage
772793 . initialize_open_state ( 0 , SafeInputRange :: empty_at ( 0 ) )
@@ -776,8 +797,10 @@ mod tests {
776797 . close_frame_and_batch ( & mut head, next_safe_block)
777798 . expect ( "rotate batch" ) ;
778799
779- assert_eq ! ( head. frame_fee, 7 ) ;
780- assert_eq ! ( storage. recommended_fee( ) . expect( "read recommended" ) , 7 ) ;
800+ let policy = storage. batch_policy ( ) . expect ( "read policy" ) ;
801+ assert ! ( head. frame_fee > 0 , "frame fee should be derived from gas_price" ) ;
802+ assert_eq ! ( head. frame_fee, policy. recommended_fee) ;
803+ assert ! ( head. max_batch_user_op_bytes > 0 , "batch size target should be set" ) ;
781804 }
782805
783806 #[ test]
0 commit comments