From 2ff43cd7fd18528321de446fa730736c17ed6c47 Mon Sep 17 00:00:00 2001 From: Arni Hod Date: Mon, 22 Jul 2024 16:39:10 +0300 Subject: [PATCH] chore: code dedup of chain info in gateway config --- crates/blockifier/src/context.rs | 5 +- crates/gateway/src/config.rs | 61 +++++++------------ .../src/stateful_transaction_validator.rs | 4 +- .../stateful_transaction_validator_test.rs | 6 +- 4 files changed, 31 insertions(+), 45 deletions(-) diff --git a/crates/blockifier/src/context.rs b/crates/blockifier/src/context.rs index 383a14d7e6..8f27c3fbdf 100644 --- a/crates/blockifier/src/context.rs +++ b/crates/blockifier/src/context.rs @@ -1,3 +1,4 @@ +use serde::{Deserialize, Serialize}; use starknet_api::core::{ChainId, ContractAddress}; use crate::blockifier::block::BlockInfo; @@ -68,7 +69,7 @@ impl BlockContext { } } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] pub struct ChainInfo { pub chain_id: ChainId, pub fee_token_addresses: FeeTokenAddresses, @@ -92,7 +93,7 @@ impl Default for ChainInfo { } } -#[derive(Clone, Debug, Default)] +#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)] pub struct FeeTokenAddresses { pub strk_fee_token_address: ContractAddress, pub eth_fee_token_address: ContractAddress, diff --git a/crates/gateway/src/config.rs b/crates/gateway/src/config.rs index 49970fa7a0..fa18218da6 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::{BlockContext, ChainInfo as BlockifierChainInfo}; 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 serde::{Deserialize, Deserializer, Serialize}; +use starknet_api::core::Nonce; use starknet_types_core::felt::Felt; use validator::Validate; @@ -175,63 +175,48 @@ 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, -} +#[derive(Clone, Debug, Default, PartialEq)] +pub struct ChainInfo(pub BlockifierChainInfo); -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 Serialize for ChainInfo { + fn serialize(&self, serializer: S) -> Result { + self.0.serialize(serializer) } } -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<'de> Deserialize<'de> for ChainInfo { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + Ok(ChainInfo(BlockifierChainInfo::deserialize(deserializer)?)) } } -impl ChainInfoConfig { +impl ChainInfo { pub fn create_for_testing() -> Self { - BlockContext::create_for_testing().chain_info().clone().into() + Self(BlockContext::create_for_testing().chain_info().clone()) } } -impl SerializeConfig for ChainInfoConfig { +impl SerializeConfig for ChainInfo { fn dump(&self) -> BTreeMap { BTreeMap::from_iter([ ser_param( "chain_id", - &self.chain_id, + &self.0.chain_id, "The chain ID of the StarkNet chain.", ParamPrivacyInput::Public, ), ser_param( "strk_fee_token_address", - &self.strk_fee_token_address, + &self.0.fee_token_addresses.strk_fee_token_address, "Address of the STRK fee token.", ParamPrivacyInput::Public, ), ser_param( "eth_fee_token_address", - &self.eth_fee_token_address, + &self.0.fee_token_addresses.eth_fee_token_address, "Address of the ETH fee token.", ParamPrivacyInput::Public, ), @@ -244,7 +229,7 @@ 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 { @@ -253,7 +238,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(), } } } @@ -291,7 +276,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 db131a2759..cbbcfbe55c 100644 --- a/crates/gateway/src/stateful_transaction_validator.rs +++ b/crates/gateway/src/stateful_transaction_validator.rs @@ -35,7 +35,7 @@ impl StatefulTransactionValidator { let account_tx = external_tx_to_account_tx( external_tx, optional_class_info, - &self.config.chain_info.chain_id, + &self.config.chain_info.0.chain_id, )?; let tx_hash = get_tx_hash(&account_tx); @@ -68,7 +68,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.0.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 58075f2ccf..8397b61a8a 100644 --- a/crates/gateway/src/stateful_transaction_validator_test.rs +++ b/crates/gateway/src/stateful_transaction_validator_test.rs @@ -24,7 +24,7 @@ use starknet_api::transaction::TransactionHash; use starknet_types_core::felt::Felt; use crate::compilation::GatewayCompiler; -use crate::config::{GatewayCompilerConfig, StatefulTransactionValidatorConfig}; +use crate::config::{ChainInfo, GatewayCompilerConfig, StatefulTransactionValidatorConfig}; use crate::errors::{StatefulTransactionValidatorError, StatefulTransactionValidatorResult}; use crate::state_reader_test_utils::{ local_test_state_reader_factory, @@ -46,7 +46,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: ChainInfo(block_context.chain_info().clone()), }, } } @@ -125,7 +125,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: ChainInfo(block_context.chain_info().clone()), }, }; let blockifier_validator = stateful_validator.instantiate_validator(&state_reader_factory);