From 51cef371c7f656f5283e380f93f5277e949eb460 Mon Sep 17 00:00:00 2001 From: Arni Hod Date: Mon, 19 Aug 2024 15:26:03 +0300 Subject: [PATCH] refactor: share the chain info as a config for the gateway --- config/mempool/default_config.json | 26 +++++++++---------- crates/gateway/src/config.rs | 24 +++-------------- crates/gateway/src/gateway.rs | 14 +++++----- crates/gateway/src/gateway_test.rs | 3 ++- .../src/stateful_transaction_validator.rs | 5 ++-- .../stateful_transaction_validator_test.rs | 11 ++++---- .../src/integration_test_utils.rs | 18 ++++++++----- 7 files changed, 47 insertions(+), 54 deletions(-) diff --git a/config/mempool/default_config.json b/config/mempool/default_config.json index dfa3b6f36d0..38f0616845e 100644 --- a/config/mempool/default_config.json +++ b/config/mempool/default_config.json @@ -194,31 +194,31 @@ "privacy": "Public", "value": 1 }, - "gateway_config.network_config.ip": { - "description": "The gateway server ip.", - "privacy": "Public", - "value": "0.0.0.0" - }, - "gateway_config.network_config.port": { - "description": "The gateway server port.", - "privacy": "Public", - "value": 8080 - }, - "gateway_config.stateful_tx_validator_config.chain_info.chain_id": { + "gateway_config.chain_info.chain_id": { "description": "The chain ID of the StarkNet chain.", "privacy": "Public", "value": "0x0" }, - "gateway_config.stateful_tx_validator_config.chain_info.fee_token_addresses.eth_fee_token_address": { + "gateway_config.chain_info.fee_token_addresses.eth_fee_token_address": { "description": "Address of the ETH fee token.", "privacy": "Public", "value": "0x0" }, - "gateway_config.stateful_tx_validator_config.chain_info.fee_token_addresses.strk_fee_token_address": { + "gateway_config.chain_info.fee_token_addresses.strk_fee_token_address": { "description": "Address of the STRK fee token.", "privacy": "Public", "value": "0x0" }, + "gateway_config.network_config.ip": { + "description": "The gateway server ip.", + "privacy": "Public", + "value": "0.0.0.0" + }, + "gateway_config.network_config.port": { + "description": "The gateway server port.", + "privacy": "Public", + "value": 8080 + }, "gateway_config.stateful_tx_validator_config.max_nonce_for_validation_skip": { "description": "Maximum nonce for which the validation is skipped.", "privacy": "Public", diff --git a/crates/gateway/src/config.rs b/crates/gateway/src/config.rs index a6e2f1920c7..3710b45a173 100644 --- a/crates/gateway/src/config.rs +++ b/crates/gateway/src/config.rs @@ -16,6 +16,7 @@ pub struct GatewayConfig { pub network_config: GatewayNetworkConfig, pub stateless_tx_validator_config: StatelessTransactionValidatorConfig, pub stateful_tx_validator_config: StatefulTransactionValidatorConfig, + pub chain_info: ChainInfo, } impl SerializeConfig for GatewayConfig { @@ -30,6 +31,7 @@ impl SerializeConfig for GatewayConfig { self.stateful_tx_validator_config.dump(), "stateful_tx_validator_config", ), + append_sub_config_name(self.chain_info.dump(), "chain_info"), ] .into_iter() .flatten() @@ -169,9 +171,6 @@ pub struct StatefulTransactionValidatorConfig { pub max_nonce_for_validation_skip: Nonce, pub validate_max_n_steps: u32, pub max_recursion_depth: usize, - // TODO(Arni): Move this member out of the stateful transaction validator config. Move it into - // the gateway config. This is used during the transalation from external_tx to executable_tx. - pub chain_info: ChainInfo, } impl Default for StatefulTransactionValidatorConfig { @@ -180,14 +179,13 @@ impl Default for StatefulTransactionValidatorConfig { max_nonce_for_validation_skip: Nonce(Felt::ONE), validate_max_n_steps: 1_000_000, max_recursion_depth: 50, - chain_info: ChainInfo::default(), } } } impl SerializeConfig for StatefulTransactionValidatorConfig { fn dump(&self) -> BTreeMap { - let members = BTreeMap::from_iter([ + BTreeMap::from_iter([ ser_param( "max_nonce_for_validation_skip", &self.max_nonce_for_validation_skip, @@ -206,20 +204,6 @@ impl SerializeConfig for StatefulTransactionValidatorConfig { "Maximum recursion depth for nested calls during blockifier validation.", ParamPrivacyInput::Public, ), - ]); - let sub_configs = append_sub_config_name(self.chain_info.dump(), "chain_info"); - vec![members, sub_configs].into_iter().flatten().collect() - } -} - -impl StatefulTransactionValidatorConfig { - #[cfg(any(test, feature = "testing"))] - pub fn create_for_testing() -> Self { - StatefulTransactionValidatorConfig { - max_nonce_for_validation_skip: Default::default(), - validate_max_n_steps: 1000000, - max_recursion_depth: 50, - chain_info: ChainInfo::create_for_testing(), - } + ]) } } diff --git a/crates/gateway/src/gateway.rs b/crates/gateway/src/gateway.rs index ffd752bfb60..f203a77425b 100644 --- a/crates/gateway/src/gateway.rs +++ b/crates/gateway/src/gateway.rs @@ -6,6 +6,7 @@ use async_trait::async_trait; use axum::extract::State; use axum::routing::{get, post}; use axum::{Json, Router}; +use blockifier::context::ChainInfo; use starknet_api::rpc_transaction::RpcTransaction; use starknet_api::transaction::TransactionHash; use starknet_mempool_infra::component_runner::{ComponentStartError, ComponentStarter}; @@ -38,6 +39,7 @@ pub struct AppState { pub state_reader_factory: Arc, pub gateway_compiler: GatewayCompiler, pub mempool_client: SharedMempoolClient, + pub chain_info: ChainInfo, } impl Gateway { @@ -57,6 +59,7 @@ impl Gateway { state_reader_factory, gateway_compiler, mempool_client, + chain_info: config.chain_info.clone(), }; Gateway { config, app_state } } @@ -97,6 +100,7 @@ async fn add_tx( app_state.stateful_tx_validator.as_ref(), app_state.state_reader_factory.as_ref(), app_state.gateway_compiler, + &app_state.chain_info, tx, ) }) @@ -121,6 +125,7 @@ fn process_tx( stateful_tx_validator: &StatefulTransactionValidator, state_reader_factory: &dyn StateReaderFactory, gateway_compiler: GatewayCompiler, + chain_info: &ChainInfo, tx: RpcTransaction, ) -> GatewayResult { // TODO(Arni, 1/5/2024): Perform congestion control. @@ -128,13 +133,10 @@ fn process_tx( // Perform stateless validations. stateless_tx_validator.validate(&tx)?; - let executable_tx = external_tx_to_executable_tx( - &tx, - &gateway_compiler, - &stateful_tx_validator.config.chain_info.chain_id, - )?; + let executable_tx = external_tx_to_executable_tx(&tx, &gateway_compiler, &chain_info.chain_id)?; - let validator = stateful_tx_validator.instantiate_validator(state_reader_factory)?; + let validator = + stateful_tx_validator.instantiate_validator(state_reader_factory, chain_info)?; // TODO(Yael 31/7/24): refactor after IntrnalTransaction is ready, delete validate_info and // compute all the info outside of run_validate. let validate_info = stateful_tx_validator.run_validate(&executable_tx, validator)?; diff --git a/crates/gateway/src/gateway_test.rs b/crates/gateway/src/gateway_test.rs index cd3816b05a7..b6d270e19e5 100644 --- a/crates/gateway/src/gateway_test.rs +++ b/crates/gateway/src/gateway_test.rs @@ -32,13 +32,14 @@ pub fn app_state( config: StatelessTransactionValidatorConfig::default(), }, stateful_tx_validator: Arc::new(StatefulTransactionValidator { - config: StatefulTransactionValidatorConfig::create_for_testing(), + config: StatefulTransactionValidatorConfig::default(), }), gateway_compiler: GatewayCompiler::new_cairo_lang_compiler( SierraToCasmCompilationConfig::default(), ), state_reader_factory: Arc::new(state_reader_factory), mempool_client, + chain_info: ChainInfo::create_for_testing(), } } diff --git a/crates/gateway/src/stateful_transaction_validator.rs b/crates/gateway/src/stateful_transaction_validator.rs index 72e69fdc12a..45a1cde6822 100644 --- a/crates/gateway/src/stateful_transaction_validator.rs +++ b/crates/gateway/src/stateful_transaction_validator.rs @@ -4,7 +4,7 @@ use blockifier::blockifier::stateful_validator::{ StatefulValidatorResult as BlockifierStatefulValidatorResult, }; use blockifier::bouncer::BouncerConfig; -use blockifier::context::BlockContext; +use blockifier::context::{BlockContext, ChainInfo}; use blockifier::state::cached_state::CachedState; use blockifier::transaction::account_transaction::AccountTransaction; use blockifier::versioned_constants::VersionedConstants; @@ -89,6 +89,7 @@ impl StatefulTransactionValidator { pub fn instantiate_validator( &self, state_reader_factory: &dyn StateReaderFactory, + chain_info: &ChainInfo, ) -> StatefulTransactionValidatorResult { // TODO(yael 6/5/2024): consider storing the block_info as part of the // StatefulTransactionValidator and update it only once a new block is created. @@ -105,7 +106,7 @@ impl StatefulTransactionValidator { // able to read the block_hash of 10 blocks ago from papyrus. let block_context = BlockContext::new( block_info, - self.config.chain_info.clone(), + chain_info.clone(), versioned_constants, BouncerConfig::max(), ); diff --git a/crates/gateway/src/stateful_transaction_validator_test.rs b/crates/gateway/src/stateful_transaction_validator_test.rs index 07918ed1d6c..8c5fff2e62f 100644 --- a/crates/gateway/src/stateful_transaction_validator_test.rs +++ b/crates/gateway/src/stateful_transaction_validator_test.rs @@ -2,7 +2,7 @@ use blockifier::blockifier::stateful_validator::{ StatefulValidatorError as BlockifierStatefulValidatorError, StatefulValidatorResult as BlockifierStatefulValidatorResult, }; -use blockifier::context::BlockContext; +use blockifier::context::{BlockContext, ChainInfo}; use blockifier::test_utils::CairoVersion; use blockifier::transaction::errors::{TransactionFeeError, TransactionPreValidationError}; use mempool_test_utils::invoke_tx_args; @@ -60,7 +60,6 @@ fn stateful_validator(block_context: BlockContext) -> StatefulTransactionValidat max_nonce_for_validation_skip: Default::default(), validate_max_n_steps: block_context.versioned_constants().validate_max_n_steps, max_recursion_depth: block_context.versioned_constants().max_recursion_depth, - chain_info: block_context.chain_info().clone(), }, } } @@ -87,7 +86,7 @@ fn test_stateful_tx_validator( let executable_tx = external_tx_to_executable_tx( &external_tx, &gateway_compiler, - &stateful_validator.config.chain_info.chain_id, + &ChainInfo::create_for_testing().chain_id, ) .unwrap(); @@ -131,10 +130,10 @@ fn test_instantiate_validator() { max_nonce_for_validation_skip: Default::default(), validate_max_n_steps: block_context.versioned_constants().validate_max_n_steps, max_recursion_depth: block_context.versioned_constants().max_recursion_depth, - chain_info: block_context.chain_info().clone(), }, }; - let blockifier_validator = stateful_validator.instantiate_validator(&mock_state_reader_factory); + let blockifier_validator = stateful_validator + .instantiate_validator(&mock_state_reader_factory, block_context.chain_info()); assert!(blockifier_validator.is_ok()); } @@ -164,7 +163,7 @@ fn test_skip_stateful_validation( let executable_tx = external_tx_to_executable_tx( &external_tx, &GatewayCompiler::new_cairo_lang_compiler(Default::default()), - &stateful_validator.config.chain_info.chain_id, + &ChainInfo::create_for_testing().chain_id, ) .unwrap(); diff --git a/crates/tests-integration/src/integration_test_utils.rs b/crates/tests-integration/src/integration_test_utils.rs index 28b4b982520..2b7ad63ca69 100644 --- a/crates/tests-integration/src/integration_test_utils.rs +++ b/crates/tests-integration/src/integration_test_utils.rs @@ -1,6 +1,7 @@ use std::net::SocketAddr; use axum::body::Body; +use blockifier::context::ChainInfo; use blockifier::test_utils::contracts::FeatureContract; use mempool_test_utils::starknet_api_test_utils::{ external_tx_to_json, @@ -23,18 +24,23 @@ use tokio::net::TcpListener; use crate::integration_test_setup::IntegrationTestSetup; async fn create_gateway_config() -> GatewayConfig { + let socket = get_available_socket().await; + let network_config = GatewayNetworkConfig { ip: socket.ip(), port: socket.port() }; let stateless_tx_validator_config = StatelessTransactionValidatorConfig { validate_non_zero_l1_gas_fee: true, max_calldata_length: 10, max_signature_length: 2, ..Default::default() }; - - let socket = get_available_socket().await; - let network_config = GatewayNetworkConfig { ip: socket.ip(), port: socket.port() }; - let stateful_tx_validator_config = StatefulTransactionValidatorConfig::create_for_testing(); - - GatewayConfig { network_config, stateless_tx_validator_config, stateful_tx_validator_config } + let stateful_tx_validator_config = StatefulTransactionValidatorConfig::default(); + let chain_info = ChainInfo::create_for_testing(); + + GatewayConfig { + network_config, + stateless_tx_validator_config, + stateful_tx_validator_config, + chain_info, + } } pub async fn create_config(rpc_server_addr: SocketAddr) -> MempoolNodeConfig {