Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: block production panic #314

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Next release

- fix: rejected transaction block production panic
- fix(sync): pending block retrying mechanism
- fix:(tests): Add testing feature to mc-db dev dependency (#294)
- feat: new crate gateway client & server
Expand Down
31 changes: 22 additions & 9 deletions crates/client/mempool/src/block_production.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// TODO: Move this into its own crate.

use crate::close_block::close_block;
use crate::header::make_pending_header;
use crate::{clone_account_tx, L1DataProvider, MempoolProvider, MempoolTransaction};
use blockifier::blockifier::transaction_executor::{TransactionExecutor, VisitedSegmentsMapping};
use blockifier::bouncer::{Bouncer, BouncerWeights, BuiltinCount};
use blockifier::state::cached_state::CommitmentStateDiff;
Expand All @@ -21,15 +24,12 @@ use mp_state_update::{
use mp_transactions::TransactionWithHash;
use mp_utils::graceful_shutdown;
use starknet_types_core::felt::Felt;
use std::borrow::Cow;
use std::collections::VecDeque;
use std::mem;
use std::sync::Arc;
use std::time::Instant;

use crate::close_block::close_block;
use crate::header::make_pending_header;
use crate::{clone_account_tx, L1DataProvider, MempoolProvider, MempoolTransaction};

#[derive(Default, Clone)]
struct ContinueBlockStats {
/// Number of batches executed before reaching the bouncer capacity.
Expand All @@ -52,6 +52,8 @@ pub enum Error {
ExecutionContext(#[from] mc_exec::Error),
#[error("Import error: {0:#}")]
Import(#[from] mc_block_import::BlockImportError),
#[error("Unexpected error: {0:#}")]
Unexpected(Cow<'static, str>),
}

fn csd_to_state_diff(
Expand Down Expand Up @@ -235,8 +237,16 @@ impl<Mempool: MempoolProvider> BlockProductionTask<Mempool> {
loop {
// Take transactions from mempool.
let to_take = batch_size.saturating_sub(txs_to_process.len());
let cur_len = txs_to_process.len();
if to_take > 0 {
self.mempool.take_txs_chunk(/* extend */ &mut txs_to_process, batch_size);

txs_to_process_blockifier.extend(
txs_to_process
.iter()
.skip(cur_len)
.map(|tx| Transaction::AccountTransaction(clone_account_tx(&tx.tx))),
);
}

if txs_to_process.is_empty() {
Expand All @@ -246,16 +256,16 @@ impl<Mempool: MempoolProvider> BlockProductionTask<Mempool> {

stats.n_batches += 1;

txs_to_process_blockifier
.extend(txs_to_process.iter().map(|tx| Transaction::AccountTransaction(clone_account_tx(&tx.tx))));

// Execute the transactions.
let all_results = self.executor.execute_txs(&txs_to_process_blockifier);
// When the bouncer cap is reached, blockifier will return fewer results than what we asked for.
let block_now_full = all_results.len() < txs_to_process_blockifier.len();

txs_to_process_blockifier.drain(..all_results.len()); // remove the used txs

for exec_result in all_results {
let mut mempool_tx = txs_to_process.pop_front().expect("Vector length mismatch");
let mut mempool_tx =
txs_to_process.pop_front().ok_or_else(|| Error::Unexpected("Vector length mismatch".into()))?;
match exec_result {
Ok(execution_info) => {
// Reverted transactions appear here as Ok too.
Expand Down Expand Up @@ -283,7 +293,10 @@ impl<Mempool: MempoolProvider> BlockProductionTask<Mempool> {
// errors during the execution of Declare and DeployAccount also appear here as they cannot be reverted.
// We reject them.
// Note that this is a big DoS vector.
log::error!("Rejected transaction {} for unexpected error: {err:#}", mempool_tx.tx_hash());
log::error!(
"Rejected transaction {:#x} for unexpected error: {err:#}",
mempool_tx.tx_hash().to_felt()
);
stats.n_rejected += 1;
}
}
Expand Down
Loading