Skip to content

Commit f65d95c

Browse files
fix(tap-agent): bulk insert of failed receipts (#329)
* Bulk insert of failed receipts * query macro * sqlx bulk insert after running compiling codes * sqlx bulk insert after running compiling codes * added error logs * minor changes
1 parent 5edf6cf commit f65d95c

File tree

3 files changed

+74
-43
lines changed

3 files changed

+74
-43
lines changed

.sqlx/query-1e672d98779cf3082906a5aaee744861fecdad20b4a52d0cec851712f8cba862.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.sqlx/query-6c365bc1b0728ec8d9f1239d7bffe7861ff17c6b8de4365a39f28099f5711613.json

Lines changed: 0 additions & 20 deletions
This file was deleted.

tap-agent/src/agent/sender_allocation.rs

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use tap_core::{
2727
signed_message::EIP712SignedMessage,
2828
};
2929
use thegraph_core::Address;
30-
use tracing::{error, warn};
30+
use tracing::{debug, error, warn};
3131

3232
use crate::{agent::sender_account::ReceiptFees, lazy_static};
3333

@@ -685,6 +685,15 @@ impl SenderAllocationState {
685685
&mut self,
686686
receipts: &[ReceiptWithState<Failed>],
687687
) -> Result<()> {
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+
let mut error_logs = Vec::with_capacity(reciepts_len);
696+
688697
for received_receipt in receipts.iter() {
689698
let receipt = received_receipt.signed_receipt();
690699
let allocation_id = receipt.message.allocation_id;
@@ -696,31 +705,53 @@ impl SenderAllocationState {
696705
error!("Failed to recover receipt signer: {}", e);
697706
anyhow!(e)
698707
})?;
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,
708+
debug!(
709+
"Receipt for allocation {} and signer {} failed reason: {}",
714710
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)),
711+
receipt_signer.encode_hex(),
718712
receipt_error
719-
)
720-
.execute(&self.pgpool)
721-
.await
722-
.map_err(|e| anyhow!("Failed to store invalid receipt: {:?}", e))?;
713+
);
714+
reciepts_signers.push(receipt_signer.encode_hex());
715+
encoded_signatures.push(encoded_signature);
716+
allocation_ids.push(allocation_id.encode_hex());
717+
timestamps.push(BigDecimal::from(receipt.message.timestamp_ns));
718+
nounces.push(BigDecimal::from(receipt.message.nonce));
719+
values.push(BigDecimal::from(BigInt::from(receipt.message.value)));
720+
error_logs.push(receipt_error);
723721
}
722+
sqlx::query!(
723+
r#"INSERT INTO scalar_tap_receipts_invalid (
724+
signer_address,
725+
signature,
726+
allocation_id,
727+
timestamp_ns,
728+
nonce,
729+
value,
730+
error_log
731+
) SELECT * FROM UNNEST(
732+
$1::CHAR(40)[],
733+
$2::BYTEA[],
734+
$3::CHAR(40)[],
735+
$4::NUMERIC(20)[],
736+
$5::NUMERIC(20)[],
737+
$6::NUMERIC(40)[],
738+
$7::TEXT[]
739+
)"#,
740+
&reciepts_signers,
741+
&encoded_signatures,
742+
&allocation_ids,
743+
&timestamps,
744+
&nounces,
745+
&values,
746+
&error_logs
747+
)
748+
.execute(&self.pgpool)
749+
.await
750+
.map_err(|e| {
751+
error!("Failed to store invalid receipt: {}", e);
752+
anyhow!(e)
753+
})?;
754+
724755
let fees = receipts
725756
.iter()
726757
.map(|receipt| receipt.signed_receipt().message.value)

0 commit comments

Comments
 (0)