Skip to content

Commit 9315908

Browse files
Bulk insert of failed receipts
1 parent 5edf6cf commit 9315908

File tree

1 file changed

+38
-24
lines changed

1 file changed

+38
-24
lines changed

tap-agent/src/agent/sender_allocation.rs

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,32 @@ impl SenderAllocationState {
685685
&mut self,
686686
receipts: &[ReceiptWithState<Failed>],
687687
) -> Result<()> {
688-
for received_receipt in receipts.iter() {
688+
let mut query_str = String::from(
689+
"INSERT INTO scalar_tap_receipts_invalid (
690+
signer_address,
691+
signature,
692+
allocation_id,
693+
timestamp_ns,
694+
nonce,
695+
value
696+
)
697+
VALUES ");
698+
for i in 0..receipts.len() {
699+
query_str = query_str +"("
700+
+"$"+&(i*6+1).to_string()+", "
701+
+"$"+&(i*6+2).to_string()+", "
702+
+"$"+&(i*6+3).to_string()+", "
703+
+"$"+&(i*6+4).to_string()+", "
704+
+"$"+&(i*6+5).to_string()+", "
705+
+"$"+&(i*6+6).to_string()
706+
+")";
707+
if i!=receipts.len()-1 {
708+
query_str = query_str +" , "
709+
}
710+
}
711+
query_str = query_str+";";
712+
let mut query = sqlx::query(&query_str);
713+
for received_receipt in receipts.iter(){
689714
let receipt = received_receipt.signed_receipt();
690715
let allocation_id = receipt.message.allocation_id;
691716
let encoded_signature = receipt.signature.as_bytes().to_vec();
@@ -696,31 +721,20 @@ impl SenderAllocationState {
696721
error!("Failed to recover receipt signer: {}", e);
697722
anyhow!(e)
698723
})?;
699-
sqlx::query!(
700-
r#"
701-
INSERT INTO scalar_tap_receipts_invalid (
702-
signer_address,
703-
signature,
704-
allocation_id,
705-
timestamp_ns,
706-
nonce,
707-
value,
708-
error_log
709-
)
710-
VALUES ($1, $2, $3, $4, $5, $6, $7)
711-
"#,
712-
receipt_signer.encode_hex(),
713-
encoded_signature,
724+
debug!(
725+
"Receipt for allocation {} and signer {} failed reason: {}",
714726
allocation_id.encode_hex(),
715-
BigDecimal::from(receipt.message.timestamp_ns),
716-
BigDecimal::from(receipt.message.nonce),
717-
BigDecimal::from(BigInt::from(receipt.message.value)),
727+
receipt_signer.encode_hex(),
718728
receipt_error
719-
)
720-
.execute(&self.pgpool)
721-
.await
722-
.map_err(|e| anyhow!("Failed to store invalid receipt: {:?}", e))?;
723-
}
729+
);
730+
query = query.bind(receipt_signer.encode_hex());
731+
query = query.bind(encoded_signature);
732+
query = query.bind(allocation_id.encode_hex());
733+
query = query.bind(BigDecimal::from(receipt.message.timestamp_ns));
734+
query = query.bind(BigDecimal::from(receipt.message.nonce));
735+
query = query.bind(BigDecimal::from(BigInt::from(receipt.message.value)));
736+
}
737+
query.execute(&self.pgpool).await?;
724738
let fees = receipts
725739
.iter()
726740
.map(|receipt| receipt.signed_receipt().message.value)

0 commit comments

Comments
 (0)