Skip to content

Conversation

@itamar-starkware
Copy link
Contributor

@itamar-starkware itamar-starkware commented Nov 4, 2025

TL;DR

Optimize transaction validation by prefetching mempool data asynchronously instead of blocking on async operations.

What changed?

  • Refactored ProcessTxBlockingTask to receive the mempool check result instead of performing it during execution
  • Modified the Gateway to prefetch the mempool result in an async context before creating the blocking task
  • Updated extract_state_nonce_and_run_validations to accept the prefetched mempool result instead of performing the check itself
  • Removed the runtime handle from the blocking task as it's no longer needed
  • Updated all related tests to accommodate the new approach

How to test?

  • Run existing tests to ensure they pass with the new implementation
  • Verify that transaction processing still works correctly with the prefetched mempool data
  • Check that the gateway correctly handles both positive and negative mempool check results

Why make this change?

This change eliminates a performance bottleneck where a blocking thread was calling block_on for an async operation. By prefetching the mempool data in an async context before creating the blocking task, we avoid blocking-on-async in a blocking thread, which improves overall performance and resource utilization. This is particularly important for the gateway component which handles transaction processing and needs to maintain high throughput.

@itamar-starkware itamar-starkware self-assigned this Nov 4, 2025
@itamar-starkware itamar-starkware marked this pull request as ready for review November 4, 2025 09:17
@itamar-starkware itamar-starkware force-pushed the 11-04-apollo_gateway_extract_async_mempool_query_from_blocking_task branch from 693a7c8 to 474b0e0 Compare November 4, 2025 10:02
@reviewable-StarkWare
Copy link

This change is Reviewable

@itamar-starkware itamar-starkware force-pushed the 11-04-apollo_gateway_extract_async_mempool_query_from_blocking_task branch 2 times, most recently from 8349379 to 7faf351 Compare November 4, 2025 13:25
@ArniStarkware
Copy link
Contributor

crates/apollo_gateway/src/gateway_test.rs line 333 at r2 (raw file):

        executable_tx: executable_invoke_tx(invoke_args()),
        is_account_tx_in_mempool,
    }

Can we simplify this test util?

Suggestion:

fn process_tx_task(
    stateful_transaction_validator_factory: MockStatefulTransactionValidatorFactoryTrait,
) -> ProcessTxBlockingTask {
    let is_account_tx_in_mempool = false;
    ProcessTxBlockingTask {
        stateful_tx_validator_factory: Arc::new(stateful_transaction_validator_factory),
        state_reader_factory: Arc::new(MockStateReaderFactory::new()),
        executable_tx: executable_invoke_tx(invoke_args()),
        is_account_tx_in_mempool,
    }

Copy link
Contributor

@ArniStarkware ArniStarkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 4 files reviewed, 3 unresolved discussions (waiting on @itamar-starkware and @TzahiTaub)


crates/apollo_gateway/src/gateway.rs line 162 at r2 (raw file):

            .mempool_client
            .account_tx_in_pool_or_recent_block(executable_tx.contract_address())
            .await;

We don't want to call the mempool for every transaction. That is very cost-ineffective:

Suggestion:

        // Please clean up this code - no need for this value to be mut.
        let mut is_account_tx_in_mempool = None;
        if tx.nonce() == Nonce(Felt::ONE) && account_nonce == Nonce(Felt::ZERO) {
            // Value is Some only if the question `is_account_tx_in_mempool` is in any way interesting.
            // Otherwise we don't care - it is irrelevant for skip validate.
        let is_account_tx_in_mempool: Optional<bool> = Some(self
            .mempool_client
            .account_tx_in_pool_or_recent_block(executable_tx.contract_address())
            .await.map_err(..).inspect(..)?);
        }

crates/apollo_gateway/src/stateful_transaction_validator.rs line 309 at r2 (raw file):

    }

    Ok(false)

Philosophically, I would extract this error handling outside of the blocking task and next to the query to the mempool.

Suggestion:

fn skip_stateful_validations(
    tx: &ExecutableTransaction,
    account_nonce: Nonce,
    is_account_tx_in_mempool: bool,
) -> StatefulTransactionValidatorResult<bool> {
    if let ExecutableTransaction::Invoke(ExecutableInvokeTransaction { tx, .. }) = tx {
        // check if the transaction nonce is 1, meaning it is post deploy_account, and the
        // account nonce is zero, meaning the account was not deployed yet.
        if tx.nonce() == Nonce(Felt::ONE) && account_nonce == Nonce(Felt::ZERO) {
            let account_address = tx.sender_address();
            debug!("Checking if deploy_account transaction exists for account {account_address}.");
            // We verify that a deploy_account transaction exists for this account. It is sufficient
            // to check if the account exists in the mempool since it means that either it has a
            // deploy_account transaction or transactions with future nonces that passed
            // validations.
            return Ok(is_account_tx_in_mempool);
        }
    }

    Ok(false)

Copy link
Contributor

@ArniStarkware ArniStarkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 4 files reviewed, 3 unresolved discussions (waiting on @itamar-starkware and @TzahiTaub)


crates/apollo_gateway/src/gateway.rs line 162 at r2 (raw file):

Previously, ArniStarkware (Arnon Hod) wrote…

We don't want to call the mempool for every transaction. That is very cost-ineffective:

another thing to make sure before we even start the call to the mempool - that the transaction in question is an invoke transaction.

I.E. move all the logic related to which transaction we query on.

@itamar-starkware itamar-starkware changed the base branch from main-v0.14.1 to graphite-base/9941 November 6, 2025 15:44
@itamar-starkware itamar-starkware force-pushed the 11-04-apollo_gateway_extract_async_mempool_query_from_blocking_task branch from 7faf351 to f8cf519 Compare November 6, 2025 15:44
@itamar-starkware itamar-starkware changed the base branch from graphite-base/9941 to 11-06-apollo_gateway_use_get_nonce_outside_extract_state_nonce_and_run_validations November 6, 2025 15:44
@itamar-starkware itamar-starkware force-pushed the 11-04-apollo_gateway_extract_async_mempool_query_from_blocking_task branch from f8cf519 to c51f57e Compare November 9, 2025 09:56
@itamar-starkware itamar-starkware force-pushed the 11-06-apollo_gateway_use_get_nonce_outside_extract_state_nonce_and_run_validations branch from c58285c to fb39f9f Compare November 9, 2025 09:56
@itamar-starkware itamar-starkware force-pushed the 11-04-apollo_gateway_extract_async_mempool_query_from_blocking_task branch from c51f57e to d2d5f77 Compare November 10, 2025 12:08
@itamar-starkware itamar-starkware force-pushed the 11-04-apollo_gateway_extract_async_mempool_query_from_blocking_task branch from d2d5f77 to e827b03 Compare November 11, 2025 11:07
@itamar-starkware itamar-starkware force-pushed the 11-06-apollo_gateway_use_get_nonce_outside_extract_state_nonce_and_run_validations branch from fb3c348 to 05a86d7 Compare November 11, 2025 11:07
@itamar-starkware itamar-starkware force-pushed the 11-04-apollo_gateway_extract_async_mempool_query_from_blocking_task branch from e827b03 to 14142cc Compare November 12, 2025 20:39
@itamar-starkware itamar-starkware force-pushed the 11-06-apollo_gateway_use_get_nonce_outside_extract_state_nonce_and_run_validations branch from 05a86d7 to 677edc2 Compare November 12, 2025 20:39
@itamar-starkware itamar-starkware changed the base branch from 11-06-apollo_gateway_use_get_nonce_outside_extract_state_nonce_and_run_validations to graphite-base/9941 November 13, 2025 08:59
@itamar-starkware itamar-starkware force-pushed the 11-04-apollo_gateway_extract_async_mempool_query_from_blocking_task branch from 14142cc to 90e1beb Compare November 13, 2025 09:03
@itamar-starkware itamar-starkware changed the base branch from graphite-base/9941 to 11-06-apollo_gateway_use_get_nonce_outside_extract_state_nonce_and_run_validations November 13, 2025 09:03
@itamar-starkware itamar-starkware force-pushed the 11-04-apollo_gateway_extract_async_mempool_query_from_blocking_task branch from 90e1beb to ba27508 Compare November 13, 2025 09:24
@itamar-starkware itamar-starkware force-pushed the 11-06-apollo_gateway_use_get_nonce_outside_extract_state_nonce_and_run_validations branch from 29e2f3f to a14ecc8 Compare November 25, 2025 12:28
@itamar-starkware itamar-starkware force-pushed the 11-04-apollo_gateway_extract_async_mempool_query_from_blocking_task branch from ba27508 to a9b8430 Compare November 25, 2025 12:28
@itamar-starkware itamar-starkware force-pushed the 11-06-apollo_gateway_use_get_nonce_outside_extract_state_nonce_and_run_validations branch from a14ecc8 to 8c1e6ea Compare November 25, 2025 13:34
@itamar-starkware itamar-starkware force-pushed the 11-04-apollo_gateway_extract_async_mempool_query_from_blocking_task branch 2 times, most recently from a1ef90a to fc642eb Compare November 25, 2025 15:52
@itamar-starkware itamar-starkware force-pushed the 11-06-apollo_gateway_use_get_nonce_outside_extract_state_nonce_and_run_validations branch from 8c1e6ea to 6af545f Compare November 25, 2025 15:52
@itamar-starkware itamar-starkware force-pushed the 11-04-apollo_gateway_extract_async_mempool_query_from_blocking_task branch from fc642eb to b4115d2 Compare November 26, 2025 12:36
@itamar-starkware itamar-starkware force-pushed the 11-06-apollo_gateway_use_get_nonce_outside_extract_state_nonce_and_run_validations branch from 6af545f to 081adc6 Compare November 26, 2025 12:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants