Skip to content

Commit

Permalink
fix(api): Return on duplicate earlier (#1059)
Browse files Browse the repository at this point in the history
## What ❔

Return on tx duplicate earlier, putting less load on DB

## Why ❔

Put less load on DB

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [ ] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [ ] Tests for the changes have been added / updated.
- [ ] Documentation comments have been added / updated.
- [ ] Code has been formatted via `zk fmt` and `zk lint`.
- [ ] Spellcheck has been run via `zk spellcheck`.
- [ ] Linkcheck has been run via `zk linkcheck`.

---------

Co-authored-by: Roman Brodetski <rb@matterlabs.dev>
  • Loading branch information
perekopskiy and RomanBrodetski committed Feb 14, 2024
1 parent 1701a4b commit cfa5701
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/lib/dal/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ async fn workflow_with_submit_tx_equal_hashes() {
.insert_transaction_l2(tx, mock_tx_execution_metrics())
.await;

assert_eq!(result, L2TxSubmissionResult::Replaced);
assert_eq!(result, L2TxSubmissionResult::Duplicate);
}

#[tokio::test]
Expand Down
22 changes: 22 additions & 0 deletions core/lib/dal/src/transactions_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,27 @@ impl TransactionsDal<'_, '_> {
) -> L2TxSubmissionResult {
{
let tx_hash = tx.hash();
let is_duplicate = sqlx::query!(
r#"
SELECT
TRUE
FROM
transactions
WHERE
hash = $1
"#,
tx_hash.as_bytes(),
)
.fetch_optional(self.storage.conn())
.await
.unwrap()
.is_some();

if is_duplicate {
tracing::debug!("Prevented inserting duplicate L2 transaction {tx_hash:?} to DB");
return L2TxSubmissionResult::Duplicate;
}

let initiator_address = tx.initiator_account();
let contract_address = tx.execute.contract_address.as_bytes();
let json_data = serde_json::to_value(&tx.execute)
Expand Down Expand Up @@ -411,6 +432,7 @@ impl TransactionsDal<'_, '_> {
if let error::Error::Database(ref error) = err {
if let Some(constraint) = error.constraint() {
if constraint == "transactions_pkey" {
tracing::debug!("Attempted to insert duplicate L2 transaction {tx_hash:?} to DB");
return L2TxSubmissionResult::Duplicate;
}
}
Expand Down

0 comments on commit cfa5701

Please sign in to comment.