Skip to content

Conversation

@itamar-starkware
Copy link
Contributor

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

TL;DR

Make instantiate_validator async to avoid blocking the runtime.

What changed?

  • Changed instantiate_validator in StatefulTransactionValidatorFactoryTrait to be an async function
  • Updated the implementation to use await instead of block_on for asynchronous operations
  • Modified process_tx in ProcessTxBlockingTask to use runtime.block_on to call the now-async instantiate_validator
  • Updated tests to accommodate the async interface

How to test?

Run the existing test suite to ensure that the functionality works as expected with the new async interface.

Why make this change?

This change improves the code by avoiding blocking operations within async contexts. By making instantiate_validator async, we eliminate the need to call block_on within the method, which can lead to runtime issues when called from an async context. This creates a more consistent async/await pattern throughout the codebase and prevents potential deadlocks that can occur when blocking the runtime.

@reviewable-StarkWare
Copy link

This change is Reviewable

@itamar-starkware itamar-starkware self-assigned this Nov 12, 2025
@itamar-starkware itamar-starkware marked this pull request as ready for review November 12, 2025 20:41
@itamar-starkware itamar-starkware changed the base branch from 11-12-apollo_gateway_move_get_latest_block_info_to_be_async to graphite-base/10122 November 13, 2025 08:56
@itamar-starkware itamar-starkware force-pushed the 11-12-apollo_gateway_move_instantiate_validator_to_be_async branch from abdeac9 to 2460176 Compare November 13, 2025 08:57
@itamar-starkware itamar-starkware changed the base branch from graphite-base/10122 to 11-12-apollo_gateway_move_get_latest_block_info_to_be_async November 13, 2025 08:57
@itamar-starkware itamar-starkware changed the base branch from 11-12-apollo_gateway_move_get_latest_block_info_to_be_async to graphite-base/10122 November 24, 2025 13:26
@itamar-starkware itamar-starkware force-pushed the 11-12-apollo_gateway_move_instantiate_validator_to_be_async branch from 2460176 to dde47f9 Compare November 24, 2025 14:56
@itamar-starkware itamar-starkware changed the base branch from graphite-base/10122 to 11-12-apollo_gateway_move_get_latest_block_info_to_be_async November 24, 2025 14:56
@ArniStarkware
Copy link
Contributor

crates/apollo_gateway/src/stateful_transaction_validator_test.rs line 159 at r1 (raw file):

#[rstest]
#[tokio::test(flavor = "multi_thread")]

Do we still need this?

Code quote:

#[tokio::test(flavor = "multi_thread")]

@itamar-starkware
Copy link
Contributor Author

crates/apollo_gateway/src/stateful_transaction_validator_test.rs line 159 at r1 (raw file):

Previously, ArniStarkware (Arnon Hod) wrote…

Do we still need this?

No, fixed

@itamar-starkware itamar-starkware force-pushed the 11-12-apollo_gateway_move_get_latest_block_info_to_be_async branch from 86763ed to d56e2f7 Compare November 25, 2025 09:20
@itamar-starkware itamar-starkware force-pushed the 11-12-apollo_gateway_move_instantiate_validator_to_be_async branch from dde47f9 to 90355f7 Compare November 25, 2025 09:20
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.

@ArniStarkware reviewed 1 of 4 files at r1.
Reviewable status: 0 of 4 files reviewed, all discussions resolved (waiting on @TzahiTaub)

@itamar-starkware itamar-starkware force-pushed the 11-12-apollo_gateway_move_get_latest_block_info_to_be_async branch from d56e2f7 to cf45842 Compare November 25, 2025 10:03
@itamar-starkware itamar-starkware force-pushed the 11-12-apollo_gateway_move_instantiate_validator_to_be_async branch 2 times, most recently from 3bcd98e to e6c5529 Compare November 25, 2025 13:34
Copy link
Contributor

@TzahiTaub TzahiTaub left a comment

Choose a reason for hiding this comment

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

@TzahiTaub reviewed 1 of 1 files at r3, 1 of 1 files at r4, all commit messages.
Reviewable status: 2 of 4 files reviewed, 1 unresolved discussion (waiting on @ArniStarkware and @itamar-starkware)


crates/apollo_gateway/src/stateful_transaction_validator_test.rs line 178 at r4 (raw file):

    let validator =
        stateful_validator_factory.instantiate_validator(Arc::new(mock_state_reader_factory)).await;

This can be removed, became irrelevant a long time ago apperantly

Suggestion:

    let validator =
        stateful_validator_factory.instantiate_validator(Arc::new(state_reader_factory)).await;

Copy link
Contributor Author

@itamar-starkware itamar-starkware 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: 2 of 4 files reviewed, 1 unresolved discussion (waiting on @ArniStarkware, @itamar-starkware, and @TzahiTaub)


crates/apollo_gateway/src/stateful_transaction_validator_test.rs line 178 at r4 (raw file):

Previously, TzahiTaub (Tzahi) wrote…

This can be removed, became irrelevant a long time ago apperantly

Done.

@itamar-starkware itamar-starkware force-pushed the 11-12-apollo_gateway_move_instantiate_validator_to_be_async branch from e6c5529 to 6f8b1a1 Compare November 25, 2025 15:52
Copy link
Contributor

@TzahiTaub TzahiTaub left a comment

Choose a reason for hiding this comment

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

@TzahiTaub reviewed 1 of 4 files at r1, 1 of 2 files at r2, 2 of 2 files at r5, all commit messages.
Reviewable status: all files reviewed, 4 unresolved discussions (waiting on @itamar-starkware)


crates/apollo_gateway/src/gateway_test.rs line 573 at r4 (raw file):

            ))
        })
    });

Revert (see comment in stateful_transaction_validator)

Code quote:

    mock_stateful_transaction_validator_factory.expect_instantiate_validator().return_once(|_| {
        Box::pin(async {
            Ok::<Box<dyn StatefulTransactionValidatorTrait>, _>(Box::new(
                mock_stateful_transaction_validator,
            ))
        })
    });

crates/apollo_gateway/src/gateway_test.rs line 616 at r4 (raw file):

    mock_stateful_transaction_validator_factory
        .expect_instantiate_validator()
        .return_once(|_| Box::pin(async { Err::<_, _>(expected_error) }));

Revert (see comment in stateful_transaction_validator)

Code quote:

      .return_once(|_| Box::pin(async { Err::<_, _>(expected_error) }));

crates/apollo_gateway/src/rpc_state_reader.rs line 116 at r5 (raw file):

        let block_header: BlockHeader = serde_json::from_value(get_block_with_tx_hashes_result)
            .map_err(serde_err_to_state_err)?;

Why was this changed? The former seems better.

Code quote:

        let block_header: BlockHeader = serde_json::from_value(get_block_with_tx_hashes_result)
            .map_err(serde_err_to_state_err)?;

crates/apollo_gateway/src/stateful_transaction_validator.rs line 53 at r4 (raw file):

#[async_trait]
#[cfg_attr(test, mockall::automock)]

See the first bullet here:

  • The #[automock] attribute must appear before the crate’s attribute.

This caused the Box::Pin requirement.

Please search for similar wrongly ordered async+automock annotations in the repo and replace them as well (in a separate PR).

Suggestion:

#[cfg_attr(test, mockall::automock)]
#[async_trait]

@itamar-starkware itamar-starkware force-pushed the 11-12-apollo_gateway_move_instantiate_validator_to_be_async branch from 6f8b1a1 to 9825d27 Compare November 26, 2025 09:29
@itamar-starkware itamar-starkware force-pushed the 11-12-apollo_gateway_move_get_latest_block_info_to_be_async branch from cf45842 to d0a3118 Compare November 26, 2025 09:29
@itamar-starkware
Copy link
Contributor Author

crates/apollo_gateway/src/rpc_state_reader.rs line 116 at r5 (raw file):

Previously, TzahiTaub (Tzahi) wrote…

Why was this changed? The former seems better.

Fixed.

@itamar-starkware
Copy link
Contributor Author

crates/apollo_gateway/src/stateful_transaction_validator.rs line 53 at r4 (raw file):

Previously, TzahiTaub (Tzahi) wrote…

See the first bullet here:

  • The #[automock] attribute must appear before the crate’s attribute.

This caused the Box::Pin requirement.

Please search for similar wrongly ordered async+automock annotations in the repo and replace them as well (in a separate PR).

Done.
This is the only place in sequencer repo with this combination.

@itamar-starkware
Copy link
Contributor Author

crates/apollo_gateway/src/gateway_test.rs line 616 at r4 (raw file):

Previously, TzahiTaub (Tzahi) wrote…

Revert (see comment in stateful_transaction_validator)

Done.

@itamar-starkware
Copy link
Contributor Author

crates/apollo_gateway/src/gateway_test.rs line 573 at r4 (raw file):

Previously, TzahiTaub (Tzahi) wrote…

Revert (see comment in stateful_transaction_validator)

Done.

@itamar-starkware itamar-starkware force-pushed the 11-12-apollo_gateway_move_instantiate_validator_to_be_async branch from 9825d27 to e1ada60 Compare November 26, 2025 10:11
Copy link
Contributor

@TzahiTaub TzahiTaub left a comment

Choose a reason for hiding this comment

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

@TzahiTaub reviewed 3 of 3 files at r6, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @itamar-starkware)


crates/apollo_gateway/src/stateful_transaction_validator.rs line 53 at r4 (raw file):

Previously, itamar-starkware wrote…

Done.
This is the only place in sequencer repo with this combination.

We have two more (pub trait PreconfirmedBlockWriterTrait and pub trait SignatureManagerClient)

@itamar-starkware
Copy link
Contributor Author

crates/apollo_gateway/src/stateful_transaction_validator.rs line 53 at r4 (raw file):

Previously, TzahiTaub (Tzahi) wrote…

We have two more (pub trait PreconfirmedBlockWriterTrait and pub trait SignatureManagerClient)

Sorry for that. I should have verified it better.
PR is open here:
#10438

@ArniStarkware
Copy link
Contributor

crates/apollo_gateway/src/stateful_transaction_validator.rs line 53 at r6 (raw file):

#[cfg_attr(test, mockall::automock)]
#[async_trait]

The original change is unnecessary.

Suggestion:

#[async_trait]
#[cfg_attr(test, mockall::automock)]

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.

@ArniStarkware reviewed 1 of 3 files at r6.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @itamar-starkware)

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.

@ArniStarkware reviewed 1 of 1 files at r4, 1 of 2 files at r5.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @itamar-starkware)


crates/apollo_gateway/src/stateful_transaction_validator.rs line 71 at r6 (raw file):

    async fn instantiate_validator(
        &self,
        state_reader_factory: Arc<dyn StateReaderFactory>,

Why the arc? Is it explained in the upstream?

Code quote:

state_reader_factory: Arc<dyn StateReaderFactory>,

@itamar-starkware
Copy link
Contributor Author

crates/apollo_gateway/src/stateful_transaction_validator.rs line 71 at r6 (raw file):

Previously, ArniStarkware (Arnon Hod) wrote…

Why the arc? Is it explained in the upstream?

We shared pointer across tasks.
The gateway and processing_tx_block both hold the state_reader.

The exact error I get:

E0195: mockall::automock can’t generate mocks for an async trait method that takes a borrowed parameter. The non-'static reference in an async fn’s signature introduces lifetimes that mockall’s automock attribute doesn’t support.

The reason is: since automock is done before async_trait (first annotation done first)
then automock tries to mock async function directly with borrowed argument (state_reader_factory).

@itamar-starkware
Copy link
Contributor Author

crates/apollo_gateway/src/stateful_transaction_validator.rs line 53 at r6 (raw file):

Previously, ArniStarkware (Arnon Hod) wrote…

The original change is unnecessary.

This documentation says otherwise:
https://docs.rs/mockall/latest/mockall/#async-traits
Am I missing something?

@graphite-app graphite-app bot changed the base branch from 11-12-apollo_gateway_move_get_latest_block_info_to_be_async to graphite-base/10122 November 27, 2025 14:45
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.

5 participants