Skip to content

Commit

Permalink
Fix sorting in mempool (#2344)
Browse files Browse the repository at this point in the history
Signed-off-by: Danil <[email protected]>
  • Loading branch information
Deniallugo authored Mar 22, 2023
1 parent eb9a6ef commit 93f3c06
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion core/lib/balancer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl<R> Balancer<R> {

pub async fn run(mut self) {
// It's an obvious way of balancing. Send an equal number of requests to each ticker
let mut channel_indexes = (0..self.channels.len()).into_iter().cycle();
let mut channel_indexes = (0..self.channels.len()).cycle();
// It's the easiest way how to cycle over channels, because cycle required clone trait.
while let Some(request) = self.requests.next().await {
let channel_index = channel_indexes
Expand Down
22 changes: 11 additions & 11 deletions core/lib/mempool/src/mempool_transactions_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,26 @@ impl MempoolTransactionsQueue {
fn prepare_new_ready_l2_transactions(&mut self, block_timestamp: u64) {
// Move some pending transactions to the ready_txs queue
let mut ready_pending_l2_operations = {
let mut ready_pending_l2_operations = Vec::new();
let mut ready_pending_l2_operations = VecDeque::new();

while let Some(pending_tx) = self.pending_l2_transactions.peek() {
if pending_tx.valid_from <= block_timestamp {
ready_pending_l2_operations.push(pending_tx.tx.clone());
ready_pending_l2_operations.push_back(pending_tx.tx.clone());
self.pending_l2_transactions.pop();
} else {
break;
}
}
ready_pending_l2_operations
};

// Now transactions should be sorted by the nonce (transaction natural order)
// According to our convention in batch `fee transaction` would be the last one, so we would use nonce from it as a key for sort
ready_pending_l2_operations.sort_by_key(|tx| match tx {
// Now transactions should be sorted by the nonce (transaction natural order)
// According to our convention in batch `fee transaction` would be the last one, so we would use nonce from it as a key for sort
self.ready_l2_transactions
.append(&mut ready_pending_l2_operations);
self.ready_l2_transactions
.make_contiguous()
.sort_by_key(|tx| match tx {
SignedTxVariant::Tx(tx) => tx.tx.nonce(),
SignedTxVariant::Batch(batch) => batch
.txs
Expand All @@ -117,12 +123,6 @@ impl MempoolTransactionsQueue {
.tx
.nonce(),
});

VecDeque::<SignedTxVariant>::from(ready_pending_l2_operations)
};

self.ready_l2_transactions
.append(&mut ready_pending_l2_operations);
}

/// Collect txs depending on desired chunks and execution time
Expand Down
2 changes: 1 addition & 1 deletion core/lib/storage/src/chain/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ impl<'a, 'c> BlockSchema<'a, 'c> {
.await?;

transaction.commit().await?;
metrics::histogram!("sql.chain.block.load_pending_block", start.elapsed());
metrics::histogram!("sql.chain.block.save_pending_block", start.elapsed());

Ok(())
}
Expand Down
1 change: 0 additions & 1 deletion core/lib/storage/src/tests/chain/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1314,7 +1314,6 @@ async fn test_get_block_transactions_hashes(mut storage: StorageProcessor<'_>) -
.await?;
let len = setup.blocks[0].block_transactions.len();
let expected: Vec<Vec<u8>> = (0..len)
.into_iter()
.map(|index| setup.get_tx_hash(0, index).as_ref().to_vec())
.collect();
assert_eq!(after_commit, expected);
Expand Down

0 comments on commit 93f3c06

Please sign in to comment.