From e4dd3b023516fd16c1de7509cec26cf3f3a16669 Mon Sep 17 00:00:00 2001 From: Arnon Hod Date: Wed, 31 Jul 2024 11:30:32 +0300 Subject: [PATCH] chore: use chain info in stateful validator config (#99) --- config/mempool/default_config.json | 6 +- crates/gateway/src/config.rs | 76 ++----------------- .../src/stateful_transaction_validator.rs | 2 +- .../stateful_transaction_validator_test.rs | 4 +- 4 files changed, 11 insertions(+), 77 deletions(-) diff --git a/config/mempool/default_config.json b/config/mempool/default_config.json index 162161a4e4..e6cf5f0a75 100644 --- a/config/mempool/default_config.json +++ b/config/mempool/default_config.json @@ -24,12 +24,12 @@ "privacy": "Public", "value": "0x0" }, - "gateway_config.stateful_tx_validator_config.chain_info.eth_fee_token_address": { + "gateway_config.stateful_tx_validator_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.strk_fee_token_address": { + "gateway_config.stateful_tx_validator_config.chain_info.fee_token_addresses.strk_fee_token_address": { "description": "Address of the STRK fee token.", "privacy": "Public", "value": "0x0" @@ -119,4 +119,4 @@ "privacy": "Public", "value": "" } -} +} \ No newline at end of file diff --git a/crates/gateway/src/config.rs b/crates/gateway/src/config.rs index 5afb8eb18a..07bf214938 100644 --- a/crates/gateway/src/config.rs +++ b/crates/gateway/src/config.rs @@ -1,11 +1,11 @@ use std::collections::BTreeMap; use std::net::IpAddr; -use blockifier::context::{BlockContext, ChainInfo, FeeTokenAddresses}; +use blockifier::context::ChainInfo; use papyrus_config::dumping::{append_sub_config_name, ser_param, SerializeConfig}; use papyrus_config::{ParamPath, ParamPrivacyInput, SerializedParam}; use serde::{Deserialize, Serialize}; -use starknet_api::core::{ChainId, ContractAddress, Nonce}; +use starknet_api::core::Nonce; use starknet_types_core::felt::Felt; use validator::Validate; @@ -174,78 +174,12 @@ impl SerializeConfig for RpcStateReaderConfig { } } -// TODO(Arni): Remove this struct once Chain info supports Papyrus serialization. -#[derive(Clone, Debug, Serialize, Deserialize, Validate, PartialEq)] -pub struct ChainInfoConfig { - pub chain_id: ChainId, - pub strk_fee_token_address: ContractAddress, - pub eth_fee_token_address: ContractAddress, -} - -impl From for ChainInfo { - fn from(chain_info: ChainInfoConfig) -> Self { - Self { - chain_id: chain_info.chain_id, - fee_token_addresses: FeeTokenAddresses { - strk_fee_token_address: chain_info.strk_fee_token_address, - eth_fee_token_address: chain_info.eth_fee_token_address, - }, - } - } -} - -impl From for ChainInfoConfig { - fn from(chain_info: ChainInfo) -> Self { - let FeeTokenAddresses { strk_fee_token_address, eth_fee_token_address } = - chain_info.fee_token_addresses; - Self { chain_id: chain_info.chain_id, strk_fee_token_address, eth_fee_token_address } - } -} - -impl Default for ChainInfoConfig { - fn default() -> Self { - ChainInfo::default().into() - } -} - -impl ChainInfoConfig { - #[cfg(any(test, feature = "testing"))] - pub fn create_for_testing() -> Self { - BlockContext::create_for_testing().chain_info().clone().into() - } -} - -impl SerializeConfig for ChainInfoConfig { - fn dump(&self) -> BTreeMap { - BTreeMap::from_iter([ - ser_param( - "chain_id", - &self.chain_id, - "The chain ID of the StarkNet chain.", - ParamPrivacyInput::Public, - ), - ser_param( - "strk_fee_token_address", - &self.strk_fee_token_address, - "Address of the STRK fee token.", - ParamPrivacyInput::Public, - ), - ser_param( - "eth_fee_token_address", - &self.eth_fee_token_address, - "Address of the ETH fee token.", - ParamPrivacyInput::Public, - ), - ]) - } -} - #[derive(Clone, Debug, Serialize, Deserialize, Validate, PartialEq)] pub struct StatefulTransactionValidatorConfig { pub max_nonce_for_validation_skip: Nonce, pub validate_max_n_steps: u32, pub max_recursion_depth: usize, - pub chain_info: ChainInfoConfig, + pub chain_info: ChainInfo, } impl Default for StatefulTransactionValidatorConfig { @@ -254,7 +188,7 @@ 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: ChainInfoConfig::default(), + chain_info: ChainInfo::default(), } } } @@ -293,7 +227,7 @@ impl StatefulTransactionValidatorConfig { max_nonce_for_validation_skip: Default::default(), validate_max_n_steps: 1000000, max_recursion_depth: 50, - chain_info: ChainInfoConfig::create_for_testing(), + 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 e9da6120cd..19d6be27cd 100644 --- a/crates/gateway/src/stateful_transaction_validator.rs +++ b/crates/gateway/src/stateful_transaction_validator.rs @@ -101,7 +101,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().into(), + self.config.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 d48d646abf..028bca11bb 100644 --- a/crates/gateway/src/stateful_transaction_validator_test.rs +++ b/crates/gateway/src/stateful_transaction_validator_test.rs @@ -43,7 +43,7 @@ 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().into(), + chain_info: block_context.chain_info().clone(), }, } } @@ -124,7 +124,7 @@ 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().into(), + chain_info: block_context.chain_info().clone(), }, }; let blockifier_validator = stateful_validator.instantiate_validator(&mock_state_reader_factory);