Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: code dedup of chain info in gateway config #27

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions crates/blockifier/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use serde::{Deserialize, Serialize};
use starknet_api::core::{ChainId, ContractAddress};

use crate::blockifier::block::BlockInfo;
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
61 changes: 23 additions & 38 deletions crates/gateway/src/config.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<ChainInfoConfig> 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<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.0.serialize(serializer)
}
}

impl From<ChainInfo> 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<D>(deserializer: D) -> Result<Self, D::Error>
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<ParamPath, SerializedParam> {
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,
),
Expand All @@ -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 {
Expand All @@ -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(),
}
}
}
Expand Down Expand Up @@ -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(),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/gateway/src/stateful_transaction_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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(),
);
Expand Down
6 changes: 3 additions & 3 deletions crates/gateway/src/stateful_transaction_validator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()),
},
}
}
Expand Down Expand Up @@ -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);
Expand Down
Loading