@@ -686,32 +686,16 @@ impl SenderAllocationState {
686686 & mut self ,
687687 receipts : & [ ReceiptWithState < Failed > ] ,
688688 ) -> Result < ( ) > {
689- let mut query_str = String :: from (
690- "INSERT INTO scalar_tap_receipts_invalid (
691- signer_address,
692- signature,
693- allocation_id,
694- timestamp_ns,
695- nonce,
696- value
697- )
698- VALUES " ) ;
699- for i in 0 ..receipts. len ( ) {
700- query_str = query_str +"("
701- +"$" +& ( i* 6 +1 ) . to_string ( ) +", "
702- +"$" +& ( i* 6 +2 ) . to_string ( ) +", "
703- +"$" +& ( i* 6 +3 ) . to_string ( ) +", "
704- +"$" +& ( i* 6 +4 ) . to_string ( ) +", "
705- +"$" +& ( i* 6 +5 ) . to_string ( ) +", "
706- +"$" +& ( i* 6 +6 ) . to_string ( )
707- +")" ;
708- if i!=receipts. len ( ) -1 {
709- query_str = query_str +" , "
710- }
711- }
712- query_str = query_str+";" ;
713- let mut query = sqlx:: query ( & query_str) ;
714- for received_receipt in receipts. iter ( ) {
689+ let reciepts_len = receipts. len ( ) ;
690+ let mut reciepts_signers = Vec :: with_capacity ( reciepts_len) ;
691+ let mut encoded_signatures = Vec :: with_capacity ( reciepts_len) ;
692+ let mut allocation_ids = Vec :: with_capacity ( reciepts_len) ;
693+ let mut timestamps = Vec :: with_capacity ( reciepts_len) ;
694+ let mut nounces = Vec :: with_capacity ( reciepts_len) ;
695+ let mut values = Vec :: with_capacity ( reciepts_len) ;
696+ let mut error_logs = Vec :: with_capacity ( reciepts_len) ;
697+
698+ for received_receipt in receipts. iter ( ) {
715699 let receipt = received_receipt. signed_receipt ( ) ;
716700 let allocation_id = receipt. message . allocation_id ;
717701 let encoded_signature = receipt. signature . as_bytes ( ) . to_vec ( ) ;
@@ -728,14 +712,47 @@ impl SenderAllocationState {
728712 receipt_signer. encode_hex( ) ,
729713 receipt_error
730714 ) ;
731- query = query. bind ( receipt_signer. encode_hex ( ) ) ;
732- query = query. bind ( encoded_signature) ;
733- query = query. bind ( allocation_id. encode_hex ( ) ) ;
734- query = query. bind ( BigDecimal :: from ( receipt. message . timestamp_ns ) ) ;
735- query = query. bind ( BigDecimal :: from ( receipt. message . nonce ) ) ;
736- query = query. bind ( BigDecimal :: from ( BigInt :: from ( receipt. message . value ) ) ) ;
737- }
738- query. execute ( & self . pgpool ) . await ?;
715+ reciepts_signers. push ( receipt_signer. encode_hex ( ) ) ;
716+ encoded_signatures. push ( encoded_signature) ;
717+ allocation_ids. push ( allocation_id. encode_hex ( ) ) ;
718+ timestamps. push ( BigDecimal :: from ( receipt. message . timestamp_ns ) ) ;
719+ nounces. push ( BigDecimal :: from ( receipt. message . nonce ) ) ;
720+ values. push ( BigDecimal :: from ( BigInt :: from ( receipt. message . value ) ) ) ;
721+ error_logs. push ( receipt_error) ;
722+ }
723+ sqlx:: query!(
724+ r#"INSERT INTO scalar_tap_receipts_invalid (
725+ signer_address,
726+ signature,
727+ allocation_id,
728+ timestamp_ns,
729+ nonce,
730+ value,
731+ error_log
732+ ) SELECT * FROM UNNEST(
733+ $1::CHAR(40)[],
734+ $2::BYTEA[],
735+ $3::CHAR(40)[],
736+ $4::NUMERIC(20)[],
737+ $5::NUMERIC(20)[],
738+ $6::NUMERIC(40)[],
739+ $7::TEXT[]
740+ )"# ,
741+ & reciepts_signers,
742+ & encoded_signatures,
743+ & allocation_ids,
744+ & timestamps,
745+ & nounces,
746+ & values,
747+ & error_logs
748+ )
749+ . execute ( & self . pgpool )
750+ . await
751+ . map_err ( |e| {
752+ error ! ( "Failed to store invalid receipt: {}" , e) ;
753+ anyhow ! ( e)
754+ } ) ?;
755+
739756 let fees = receipts
740757 . iter ( )
741758 . map ( |receipt| receipt. signed_receipt ( ) . message . value )
0 commit comments