Skip to content

Commit

Permalink
feat(tx-sender): Limit concurrent tx submissions (#1473)
Browse files Browse the repository at this point in the history
## What ❔

Limits tx submissions in a way that at most one tx per (address, nonce)
can be inserted into DB at any given moment.

## Why ❔

Database poorly handles concurrent tx insertions

## 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`.
  • Loading branch information
perekopskiy committed Mar 21, 2024
1 parent e5c8127 commit 4bdf3ca
Show file tree
Hide file tree
Showing 20 changed files with 364 additions and 295 deletions.

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

This file was deleted.

This file was deleted.

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

3 changes: 2 additions & 1 deletion core/lib/dal/src/blocks_web3_dal.rs
Expand Up @@ -863,7 +863,8 @@ mod tests {
for (i, tx) in transactions.into_iter().enumerate() {
conn.transactions_dal()
.insert_transaction_l2(tx.clone(), TransactionExecutionMetrics::default())
.await;
.await
.unwrap();
let mut tx_result = mock_execution_result(tx);
tx_result.call_traces.push(Call {
from: Address::from_low_u64_be(i as u64),
Expand Down
3 changes: 2 additions & 1 deletion core/lib/dal/src/sync_dal.rs
Expand Up @@ -165,7 +165,8 @@ mod tests {
let tx = mock_l2_transaction();
conn.transactions_dal()
.insert_transaction_l2(tx.clone(), TransactionExecutionMetrics::default())
.await;
.await
.unwrap();
conn.blocks_dal()
.insert_miniblock(&miniblock_header)
.await
Expand Down
21 changes: 14 additions & 7 deletions core/lib/dal/src/tests/mod.rs
Expand Up @@ -172,13 +172,15 @@ async fn workflow_with_submit_tx_equal_hashes() {
let tx = mock_l2_transaction();
let result = transactions_dal
.insert_transaction_l2(tx.clone(), mock_tx_execution_metrics())
.await;
.await
.unwrap();

assert_eq!(result, L2TxSubmissionResult::Added);

let result = transactions_dal
.insert_transaction_l2(tx, mock_tx_execution_metrics())
.await;
.await
.unwrap();

assert_eq!(result, L2TxSubmissionResult::Duplicate);
}
Expand All @@ -196,7 +198,8 @@ async fn workflow_with_submit_tx_diff_hashes() {

let result = transactions_dal
.insert_transaction_l2(tx, mock_tx_execution_metrics())
.await;
.await
.unwrap();

assert_eq!(result, L2TxSubmissionResult::Added);

Expand All @@ -205,7 +208,8 @@ async fn workflow_with_submit_tx_diff_hashes() {
tx.common_data.initiator_address = initiator_address;
let result = transactions_dal
.insert_transaction_l2(tx, mock_tx_execution_metrics())
.await;
.await
.unwrap();

assert_eq!(result, L2TxSubmissionResult::Replaced);
}
Expand All @@ -227,12 +231,14 @@ async fn remove_stuck_txs() {
tx.received_timestamp_ms = unix_timestamp_ms() - Duration::new(1000, 0).as_millis() as u64;
transactions_dal
.insert_transaction_l2(tx, mock_tx_execution_metrics())
.await;
.await
.unwrap();
// Tx in mempool
let tx = mock_l2_transaction();
transactions_dal
.insert_transaction_l2(tx, mock_tx_execution_metrics())
.await;
.await
.unwrap();

// Stuck L1 tx. We should never ever remove L1 tx
let mut tx = mock_l1_execute();
Expand All @@ -247,7 +253,8 @@ async fn remove_stuck_txs() {
unix_timestamp_ms() - Duration::new(1000, 0).as_millis() as u64;
transactions_dal
.insert_transaction_l2(executed_tx.clone(), mock_tx_execution_metrics())
.await;
.await
.unwrap();

// Get all txs
transactions_dal.reset_mempool().await.unwrap();
Expand Down

0 comments on commit 4bdf3ca

Please sign in to comment.