@@ -16,7 +16,8 @@ use diesel::{
1616} ;
1717
1818use energy_tracker_lib:: {
19- Payload , ProofStruct , PublicValuesStruct , calc_slot_key, decode_slice, destructure_payload, extract_nonce, to_b256
19+ Payload , ProofStruct , PublicValuesStruct , calc_slot_key, decode_slice, destructure_payload,
20+ extract_nonce, to_b256,
2021} ;
2122use energy_tracker_verifier:: {
2223 check_gas_balance, check_program_vkey, commit_state, get_block_rpl_bytes, get_previous_values,
@@ -139,7 +140,7 @@ async fn main() {
139140 }
140141 } ;
141142 println ! ( "Committed state with tx hash: {}" , hash) ;
142- update_payload ( & mut conn, proof_fixture. new_nonces . to_vec ( ) ) . await ;
143+ let _ = update_payload ( & mut conn, proof_fixture. new_nonces . to_vec ( ) ) . await ;
143144 println ! ( "Updated payloads as verified" ) ;
144145 }
145146 Err ( e) => {
@@ -259,7 +260,7 @@ async fn run_prover_handler(
259260 }
260261 } ;
261262 println ! ( "Committed state with tx hash: {}" , hash) ;
262- update_payload ( & mut conn, proof_fixture. new_nonces . to_vec ( ) ) . await ;
263+ let _ = update_payload ( & mut conn, proof_fixture. new_nonces . to_vec ( ) ) . await ;
263264 println ! ( "Updated payloads as verified" ) ;
264265 } ) ;
265266
@@ -304,11 +305,7 @@ async fn batch_payload_handler(
304305 . into_iter ( )
305306 . map ( PayloadItem :: from)
306307 . filter ( |item| {
307- is_unique_nonce (
308- & mut connection,
309- item. m3ter_id ,
310- extract_nonce ( & item. message ) ,
311- )
308+ is_unique_nonce ( & mut connection, item. m3ter_id , extract_nonce ( & item. message ) )
312309 } )
313310 . map ( |payload| {
314311 let m3ter_id = payload. m3ter_id ;
@@ -483,7 +480,9 @@ async fn build_proving_payload(
483480async fn update_payload (
484481 connection : & mut PooledConnection < ConnectionManager < PgConnection > > ,
485482 nonces : Vec < u8 > ,
486- ) {
483+ ) -> Result < ( ) > {
484+ use self :: m3ter_payloads:: dsl:: * ;
485+ use diesel:: prelude:: * ;
487486 enum DataStrategy {
488487 Persist ,
489488 Delete ,
@@ -493,38 +492,30 @@ async fn update_payload(
493492 "delete" => DataStrategy :: Delete ,
494493 _ => DataStrategy :: Persist ,
495494 } ;
496- nonces
497- . chunks_exact ( 6 )
498- . enumerate ( )
499- . filter_map ( |( i, nonce) | {
500- let nonce = decode_slice ( nonce. try_into ( ) . ok ( ) ?) ;
501- if nonce != 0 {
502- Some ( ( i, nonce as i64 ) )
503- } else {
504- None
505- }
506- } )
507- . for_each ( |( i, nonce_filter) | {
508- use self :: m3ter_payloads:: dsl:: * ;
509- use diesel:: prelude:: * ;
510- match strategy {
511- DataStrategy :: Persist => {
512- let _ = diesel:: update (
513- m3ter_payloads. filter ( m3ter_id. eq ( i as i64 ) . and ( nonce. le ( nonce_filter) ) ) ,
514- )
515- . set ( is_verified. eq ( true ) )
516- . execute ( connection)
517- . expect ( "Failed to update payloads" ) ;
518- }
519- DataStrategy :: Delete => {
520- let _ = diesel:: delete (
521- m3ter_payloads. filter ( m3ter_id. eq ( i as i64 ) . and ( nonce. eq ( nonce_filter) ) ) ,
522- )
523- . execute ( connection)
524- . expect ( "Failed to delete payloads" ) ;
525- }
526- }
527- } ) ;
495+ let nonces_in_db = m3ter_payloads
496+ . select ( m3ter_id)
497+ . distinct ( )
498+ . load :: < i64 > ( connection) ?;
499+ nonces_in_db. iter ( ) . for_each ( |m3ter| {
500+ let n = * m3ter as usize ;
501+ let start = n * 6 ;
502+ let end = n * 6 + 6 ;
503+ let nonce_value = decode_slice ( & nonces[ start..end] . try_into ( ) . unwrap ( ) ) as i64 ;
504+ let rows = match strategy {
505+ DataStrategy :: Persist => diesel:: update ( m3ter_payloads)
506+ . filter ( m3ter_id. eq ( m3ter) )
507+ . filter ( nonce. le ( nonce_value) )
508+ . set ( is_verified. eq ( true ) )
509+ . execute ( connection) ,
510+ DataStrategy :: Delete => diesel:: delete ( m3ter_payloads)
511+ . filter ( m3ter_id. eq ( m3ter) )
512+ . filter ( nonce. le ( nonce_value) )
513+ . execute ( connection) ,
514+ } ;
515+
516+ println ! ( "rows updated {}" , rows. unwrap_or_default( ) ) ;
517+ } ) ;
518+ Ok ( ( ) )
528519}
529520
530521fn is_unique_nonce (
0 commit comments