Skip to content

Commit

Permalink
Revert commit with mempool (#2358)
Browse files Browse the repository at this point in the history
Signed-off-by: Danil <[email protected]>
  • Loading branch information
Deniallugo authored Mar 24, 2023
1 parent 646dba9 commit 6a62cd2
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 87 deletions.
75 changes: 2 additions & 73 deletions core/bin/zksync_core/src/state_keeper/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::{HashMap, VecDeque};
use std::collections::VecDeque;
use std::time::{Duration, Instant};

// External uses
Expand Down Expand Up @@ -71,14 +71,6 @@ pub struct ZkSyncStateKeeper {
/// Queue of reverted blocks
/// They will be executed before the start of the StateKeeper
reverted_blocks: VecDeque<IncompleteBlock>,

/// If transaction has a nonce that is too big, we don't want to reject it right away.
/// We will try to process it later, when the nonce gap is filled.
/// However, we don't want to keep it forever, so we will drop it after some time.
postponed_tx_attempts: HashMap<TxHash, usize>,

/// Maximum attempts to execute a transaction before dropping it.
max_attempts_to_execute_tx: usize,
}

impl ZkSyncStateKeeper {
Expand Down Expand Up @@ -156,13 +148,6 @@ impl ZkSyncStateKeeper {
)
};

// We have a default value, but want to keep it configurable just in case.
// If set to 0, all transactions would be executed (no attempts to postpone them).
let max_attempts_to_execute_tx = std::env::var("SK_MAX_ATTEMPTS_TO_EXECUTE_TX")
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(100);

let mut keeper = ZkSyncStateKeeper {
state: sk_state,
pending_block,
Expand All @@ -174,9 +159,6 @@ impl ZkSyncStateKeeper {

root_hash_queue,
reverted_blocks: initial_state.reverted_blocks.clone(),

postponed_tx_attempts: Default::default(),
max_attempts_to_execute_tx,
};
keeper.initialize(initial_state.pending_block);

Expand Down Expand Up @@ -444,60 +426,7 @@ impl ZkSyncStateKeeper {
}
}

// Only try to execute transactions with appropriate nonce.
let proposed_txs_len = proposed_block.txs.len();
let mut tx_queue = proposed_block
.txs
.into_iter()
.filter(|tx| match tx {
SignedTxVariant::Tx(tx) => {
let mut can_execute = self.state.can_execute_tx(tx);
if !can_execute {
// Check if we tried to execute this tx too many times.
let tx_hash = tx.hash();
let attempts = self.postponed_tx_attempts.entry(tx_hash).or_insert(0);
*attempts += 1;
if *attempts > self.max_attempts_to_execute_tx {
vlog::warn!("Transaction with hash {:?} was postponed for too long, it will be executed anyway", tx_hash);
self.postponed_tx_attempts.remove(&tx_hash);
can_execute = true;
}
}
can_execute
}
SignedTxVariant::Batch(batch) => {
// If there are multiple transactions from a single account in a batch, next ones would have
// higher nonces and obviously can't be executed.
// So instead we check if there is at least one tx that can be executed.
let mut can_execute = batch.txs.iter().any(|tx| self.state.can_execute_tx(tx));
if !can_execute {
// Check if we tried to execute this batch too many times.

// Identify the batch by its first tx.
let first_tx_hash = batch.txs.first().unwrap().hash();
let attempts = self.postponed_tx_attempts.entry(first_tx_hash).or_insert(0);
*attempts += 1;
if *attempts > self.max_attempts_to_execute_tx {
vlog::warn!("Batch with the first tx hash {:?} was postponed for too long, it will be executed anyway", first_tx_hash);
self.postponed_tx_attempts.remove(&first_tx_hash);
can_execute = true;
}
}

can_execute
}
})
.collect::<VecDeque<_>>();
if proposed_txs_len != 0 && tx_queue.is_empty() {
vlog::warn!("All the transactions were filtered out");
}
vlog::info!(
"Txs to process: {}, postponed txs: {}, total {}",
tx_queue.len(),
proposed_txs_len - tx_queue.len(),
proposed_txs_len
);

let mut tx_queue = proposed_block.txs.into_iter().collect::<VecDeque<_>>();
while let Some(variant) = tx_queue.pop_front() {
match &variant {
SignedTxVariant::Tx(tx) => {
Expand Down
15 changes: 1 addition & 14 deletions core/lib/state/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use zksync_crypto::{merkle_tree::TreeMemoryUsage, params, params::NFT_STORAGE_AC
use zksync_types::{
helpers::reverse_updates,
operations::{TransferOp, TransferToNewOp, ZkSyncOp},
Account, AccountId, AccountMap, AccountTree, AccountUpdate, AccountUpdates, Address, Nonce,
Account, AccountId, AccountMap, AccountTree, AccountUpdate, AccountUpdates, Address,
SignedZkSyncTx, TokenId, ZkSyncPriorityOp, ZkSyncTx, NFT,
};

Expand Down Expand Up @@ -74,19 +74,6 @@ impl ZkSyncState {
}
}

pub fn can_execute_tx(&self, tx: &ZkSyncTx) -> bool {
let account_nonce = self
.get_account_by_address(&tx.account())
.map(|(_, account)| account.nonce)
.unwrap_or(Nonce(0));
let tx_nonce = tx.nonce();

// We can execute transaction if its nonce is less or equal to the account nonce.
// Transactions with lesser nonce would be rejected.
// Transactions with greater nonce cannot be executed yet.
tx_nonce <= account_nonce
}

pub fn from_acc_map(accounts: AccountMap) -> Self {
let mut empty = Self::empty();

Expand Down

0 comments on commit 6a62cd2

Please sign in to comment.