Skip to content

Commit

Permalink
refactor: share the chain info as a config for the gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Aug 19, 2024
1 parent c081dd8 commit 51cef37
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 54 deletions.
26 changes: 13 additions & 13 deletions config/mempool/default_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
24 changes: 4 additions & 20 deletions crates/gateway/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
Expand Down Expand Up @@ -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 {
Expand All @@ -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<ParamPath, SerializedParam> {
let members = BTreeMap::from_iter([
BTreeMap::from_iter([
ser_param(
"max_nonce_for_validation_skip",
&self.max_nonce_for_validation_skip,
Expand All @@ -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(),
}
])
}
}
14 changes: 8 additions & 6 deletions crates/gateway/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -38,6 +39,7 @@ pub struct AppState {
pub state_reader_factory: Arc<dyn StateReaderFactory>,
pub gateway_compiler: GatewayCompiler,
pub mempool_client: SharedMempoolClient,
pub chain_info: ChainInfo,
}

impl Gateway {
Expand All @@ -57,6 +59,7 @@ impl Gateway {
state_reader_factory,
gateway_compiler,
mempool_client,
chain_info: config.chain_info.clone(),
};
Gateway { config, app_state }
}
Expand Down Expand Up @@ -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,
)
})
Expand All @@ -121,20 +125,18 @@ fn process_tx(
stateful_tx_validator: &StatefulTransactionValidator,
state_reader_factory: &dyn StateReaderFactory,
gateway_compiler: GatewayCompiler,
chain_info: &ChainInfo,
tx: RpcTransaction,
) -> GatewayResult<MempoolInput> {
// TODO(Arni, 1/5/2024): Perform congestion control.

// 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)?;
Expand Down
3 changes: 2 additions & 1 deletion crates/gateway/src/gateway_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}

Expand Down
5 changes: 3 additions & 2 deletions crates/gateway/src/stateful_transaction_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -89,6 +89,7 @@ impl StatefulTransactionValidator {
pub fn instantiate_validator(
&self,
state_reader_factory: &dyn StateReaderFactory,
chain_info: &ChainInfo,
) -> StatefulTransactionValidatorResult<BlockifierStatefulValidator> {
// 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.
Expand All @@ -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(),
);
Expand Down
11 changes: 5 additions & 6 deletions crates/gateway/src/stateful_transaction_validator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
},
}
}
Expand All @@ -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();

Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -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();

Expand Down
18 changes: 12 additions & 6 deletions crates/tests-integration/src/integration_test_utils.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 {
Expand Down

0 comments on commit 51cef37

Please sign in to comment.