Skip to content

Commit 5bcbcd8

Browse files
query macro
1 parent 9315908 commit 5bcbcd8

File tree

1 file changed

+44
-33
lines changed

1 file changed

+44
-33
lines changed

tap-agent/src/agent/sender_allocation.rs

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -685,31 +685,14 @@ impl SenderAllocationState {
685685
&mut self,
686686
receipts: &[ReceiptWithState<Failed>],
687687
) -> Result<()> {
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);
688+
let reciepts_len = receipts.len();
689+
let mut reciepts_signers = Vec::with_capacity(reciepts_len);
690+
let mut encoded_signatures = Vec::with_capacity(reciepts_len);
691+
let mut allocation_ids = Vec::with_capacity(reciepts_len);
692+
let mut timestamps = Vec::with_capacity(reciepts_len);
693+
let mut nounces = Vec::with_capacity(reciepts_len);
694+
let mut values = Vec::with_capacity(reciepts_len);
695+
713696
for received_receipt in receipts.iter(){
714697
let receipt = received_receipt.signed_receipt();
715698
let allocation_id = receipt.message.allocation_id;
@@ -727,14 +710,42 @@ impl SenderAllocationState {
727710
receipt_signer.encode_hex(),
728711
receipt_error
729712
);
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?;
713+
reciepts_signers.push(receipt_signer.encode_hex());
714+
encoded_signatures.push(encoded_signature);
715+
allocation_ids.push(allocation_id.encode_hex());
716+
timestamps.push(BigDecimal::from(receipt.message.timestamp_ns));
717+
nounces.push(BigDecimal::from(receipt.message.nonce));
718+
values.push(BigDecimal::from(BigInt::from(receipt.message.value)));
719+
}
720+
sqlx::query!(
721+
r#"INSERT INTO scalar_tap_receipts_invalid (
722+
signer_address,
723+
signature,
724+
allocation_id,
725+
timestamp_ns,
726+
nonce,
727+
value
728+
) SELECT * FROM UNNEST(
729+
$1::CHAR(40)[],
730+
$2::BYTEA[],
731+
$3::CHAR(40)[],
732+
$4::NUMERIC(20)[],
733+
$5::NUMERIC(20)[],
734+
$6::NUMERIC(40)[]
735+
)"#,
736+
&signers,
737+
&signatures,
738+
&allocation_ids,
739+
&timestamps,
740+
&nonces,
741+
&values,
742+
).execute(&self.pgpool)
743+
.await
744+
.map_err(|e| {
745+
error!("Failed to store receipt: {}", e);
746+
anyhow!(e)
747+
})?;
748+
738749
let fees = receipts
739750
.iter()
740751
.map(|receipt| receipt.signed_receipt().message.value)

0 commit comments

Comments
 (0)