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

feat: configurable ETH and STRK fee token addresses #2477

Merged
merged 4 commits into from
Jan 10, 2025
Merged
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
2 changes: 2 additions & 0 deletions crates/crypto/benches/bench.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(unexpected_cfgs)]

use ::pathfinder_crypto::hash::poseidon::poseidon_hash;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use pathfinder_crypto::algebra::curve::{ProjectivePoint, CURVE_G};
Expand Down
2 changes: 1 addition & 1 deletion crates/crypto/src/algebra/curve/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::algebra::curve::AffinePoint;
pub const G_CONSTS_BITS: usize = 4;

#[rustfmt::skip]
pub const G_CONSTS: [AffinePoint; 945] = [
pub static G_CONSTS: [AffinePoint; 945] = [
AffinePoint::from_raw(
[14484022957141291997,5884444832209845738,299981207024966779,232005955912912577],
[6241159653446987914,664812301889158119,18147424675297964973,405578048423154473]
Expand Down
22 changes: 13 additions & 9 deletions crates/executor/src/execution_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use blockifier::context::{BlockContext, ChainInfo};
use blockifier::state::cached_state::CachedState;
use blockifier::versioned_constants::VersionedConstants;
use pathfinder_common::{
contract_address,
BlockHeader,
ChainId,
ContractAddress,
Expand All @@ -20,12 +19,6 @@ use super::pending::PendingStateReader;
use super::state_reader::PathfinderStateReader;
use crate::IntoStarkFelt;

// NOTE: these are the same for _all_ networks
pub const ETH_FEE_TOKEN_ADDRESS: ContractAddress =
contract_address!("0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7");
pub const STRK_FEE_TOKEN_ADDRESS: ContractAddress =
contract_address!("0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d");

mod versioned_constants {
use std::borrow::Cow;
use std::sync::LazyLock;
Expand Down Expand Up @@ -103,6 +96,8 @@ pub struct ExecutionState<'tx> {
pending_state: Option<Arc<StateUpdate>>,
allow_use_kzg_data: bool,
custom_versioned_constants: Option<VersionedConstants>,
eth_fee_address: ContractAddress,
strk_fee_address: ContractAddress,
}

impl<'tx> ExecutionState<'tx> {
Expand Down Expand Up @@ -174,11 +169,11 @@ impl<'tx> ExecutionState<'tx> {

fn chain_info(&self) -> anyhow::Result<ChainInfo> {
let eth_fee_token_address = starknet_api::core::ContractAddress(
PatriciaKey::try_from(ETH_FEE_TOKEN_ADDRESS.0.into_starkfelt())
PatriciaKey::try_from(self.eth_fee_address.0.into_starkfelt())
.expect("ETH fee token address overflow"),
);
let strk_fee_token_address = starknet_api::core::ContractAddress(
PatriciaKey::try_from(STRK_FEE_TOKEN_ADDRESS.0.into_starkfelt())
PatriciaKey::try_from(self.strk_fee_address.0.into_starkfelt())
.expect("STRK fee token address overflow"),
);

Expand Down Expand Up @@ -259,6 +254,8 @@ impl<'tx> ExecutionState<'tx> {
header: BlockHeader,
pending_state: Option<Arc<StateUpdate>>,
custom_versioned_constants: Option<VersionedConstants>,
eth_fee_address: ContractAddress,
strk_fee_address: ContractAddress,
) -> Self {
Self {
transaction,
Expand All @@ -268,16 +265,21 @@ impl<'tx> ExecutionState<'tx> {
execute_on_parent_state: true,
allow_use_kzg_data: true,
custom_versioned_constants,
eth_fee_address,
strk_fee_address,
}
}

#[allow(clippy::too_many_arguments)]
pub fn simulation(
transaction: &'tx pathfinder_storage::Transaction<'tx>,
chain_id: ChainId,
header: BlockHeader,
pending_state: Option<Arc<StateUpdate>>,
l1_blob_data_availability: L1BlobDataAvailability,
custom_versioned_constants: Option<VersionedConstants>,
eth_fee_address: ContractAddress,
strk_fee_address: ContractAddress,
) -> Self {
Self {
transaction,
Expand All @@ -287,6 +289,8 @@ impl<'tx> ExecutionState<'tx> {
execute_on_parent_state: false,
allow_use_kzg_data: l1_blob_data_availability == L1BlobDataAvailability::Enabled,
custom_versioned_constants,
eth_fee_address,
strk_fee_address,
}
}
}
Expand Down
7 changes: 1 addition & 6 deletions crates/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ pub use class::{parse_casm_definition, parse_deprecated_class_definition};
pub use error::{CallError, TransactionExecutionError};
pub use error_stack::{CallFrame, ErrorStack, Frame};
pub use estimate::estimate;
pub use execution_state::{
ExecutionState,
L1BlobDataAvailability,
ETH_FEE_TOKEN_ADDRESS,
STRK_FEE_TOKEN_ADDRESS,
};
pub use execution_state::{ExecutionState, L1BlobDataAvailability};
pub use felt::{IntoFelt, IntoStarkFelt};
pub use simulate::{simulate, trace, TraceCache};
pub use transaction::transaction_hash;
2 changes: 1 addition & 1 deletion crates/gateway-client/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ impl<'a> Request<'a, stage::Params> {
}
}

impl<'a> Request<'a, stage::Final> {
impl Request<'_, stage::Final> {
/// Sends the Sequencer request as a REST `GET` operation and parses the
/// response into `T`.
pub async fn get<T>(self) -> Result<T, SequencerError>
Expand Down
10 changes: 9 additions & 1 deletion crates/gateway-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,15 @@ mod tests {
let (_jh, url) = setup([(
"/feeder_gateway/get_contract_addresses",
(
r#"{"Starknet":"0xde29d060d45901fb19ed6c6e959eb22d8626708e","GpsStatementVerifier":"0xab43ba48c9edf4c2c4bb01237348d1d7b28ef168"}"#,
r#"{
"FriStatementContract": "0x55d049b4C82807808E76e61a08C6764bbf2ffB55",
"GpsStatementVerifier": "0x2046B966994Adcb88D83f467a41b75d64C2a619F",
"MemoryPageFactRegistry": "0x5628E75245Cc69eCA0994F0449F4dDA9FbB5Ec6a",
"MerkleStatementContract": "0xd414f8f535D4a96cB00fFC8E85160b353cb7809c",
"Starknet": "0x4737c0c1B4D5b1A687B42610DdabEE781152359c",
"strk_l2_token_address": "0x4718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d",
"eth_l2_token_address": "0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7"
}"#,
200,
),
)]);
Expand Down
23 changes: 17 additions & 6 deletions crates/gateway-types/src/reply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ use pathfinder_common::{
StateDiffCommitment,
TransactionCommitment,
};
use pathfinder_serde::{EthereumAddressAsHexStr, GasPriceAsHexStr};
use pathfinder_serde::{EthereumAddressAsHexStr, GasPriceAsHexStr, H256AsNoLeadingZerosHexStr};
use primitive_types::H256;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr};
pub use transaction::DataAvailabilityMode;
Expand Down Expand Up @@ -2184,6 +2185,12 @@ pub struct EthContractAddresses {
#[serde(rename = "Starknet")]
#[serde_as(as = "EthereumAddressAsHexStr")]
pub starknet: EthereumAddress,

#[serde_as(as = "H256AsNoLeadingZerosHexStr")]
pub strk_l2_token_address: H256,

#[serde_as(as = "H256AsNoLeadingZerosHexStr")]
pub eth_l2_token_address: H256,
}

pub mod add_transaction {
Expand Down Expand Up @@ -2401,12 +2408,16 @@ mod tests {

#[test]
fn eth_contract_addresses_ignores_extra_fields() {
// Some gateway mocks include extra addresses, check that we can still parse
// these.
// Sepolia integration gateway includes extra fields, check
// that we can still parse these.
let json = serde_json::json!({
"Starknet": "0x12345abcd",
"GpsStatementVerifier": "0xaabdde",
"MemoryPageFactRegistry": "0xdeadbeef"
"FriStatementContract": "0x55d049b4C82807808E76e61a08C6764bbf2ffB55",
"GpsStatementVerifier": "0x2046B966994Adcb88D83f467a41b75d64C2a619F",
"MemoryPageFactRegistry": "0x5628E75245Cc69eCA0994F0449F4dDA9FbB5Ec6a",
"MerkleStatementContract": "0xd414f8f535D4a96cB00fFC8E85160b353cb7809c",
"Starknet": "0x4737c0c1B4D5b1A687B42610DdabEE781152359c",
"strk_l2_token_address": "0x4718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d",
"eth_l2_token_address": "0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7"
});

serde_json::from_value::<crate::reply::EthContractAddresses>(json).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/p2p/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ impl Behaviour {
if self
.peers
.get(peer_id)
.map_or(false, |peer| peer.is_connected())
.is_some_and(|peer| peer.is_connected())
{
tracing::debug!(%peer_id, "Peer already connected, closing");
return Err(ConnectionDenied::new("duplicate connection"));
Expand Down
2 changes: 1 addition & 1 deletion crates/p2p/src/peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl Peer {
pub fn is_relayed(&self) -> bool {
self.addr
.as_ref()
.map_or(false, |addr| addr.iter().any(|p| p == Protocol::P2pCircuit))
.is_some_and(|addr| addr.iter().any(|p| p == Protocol::P2pCircuit))
}

pub fn ip_addr(&self) -> Option<IpAddr> {
Expand Down
11 changes: 10 additions & 1 deletion crates/pathfinder/examples/re_execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use pathfinder_common::receipt::Receipt;
use pathfinder_common::transaction::Transaction;
use pathfinder_common::{BlockHeader, BlockNumber, ChainId};
use pathfinder_executor::ExecutionState;
use pathfinder_rpc::context::{ETH_FEE_TOKEN_ADDRESS, STRK_FEE_TOKEN_ADDRESS};
use pathfinder_storage::{BlockId, Storage};
use rayon::prelude::*;

Expand Down Expand Up @@ -132,7 +133,15 @@ fn execute(storage: &mut Storage, chain_id: ChainId, work: Work) {

let db_tx = connection.transaction().expect("Create transaction");

let execution_state = ExecutionState::trace(&db_tx, chain_id, work.header.clone(), None, None);
let execution_state = ExecutionState::trace(
&db_tx,
chain_id,
work.header.clone(),
None,
None,
ETH_FEE_TOKEN_ADDRESS,
STRK_FEE_TOKEN_ADDRESS,
);

let transactions = work
.transactions
Expand Down
35 changes: 20 additions & 15 deletions crates/pathfinder/src/bin/pathfinder/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ use pathfinder_ethereum::{EthereumApi, EthereumClient};
use pathfinder_lib::monitoring::{self};
use pathfinder_lib::state;
use pathfinder_lib::state::SyncContext;
use pathfinder_rpc::context::WebsocketContext;
use pathfinder_rpc::context::{EthContractAddresses, WebsocketContext};
use pathfinder_rpc::{Notifications, SyncState};
use pathfinder_storage::Storage;
use primitive_types::H160;
use starknet_gateway_client::GatewayApi;
use tokio::signal::unix::{signal, SignalKind};
use tokio::task::JoinError;
Expand Down Expand Up @@ -228,7 +227,7 @@ Hint: This is usually caused by exceeding the file descriptor limit of your syst
execution_storage,
sync_state.clone(),
pathfinder_context.network_id,
pathfinder_context.l1_core_address,
pathfinder_context.contract_addresses,
pathfinder_context.gateway.clone(),
rx_pending.clone(),
notifications.clone(),
Expand Down Expand Up @@ -628,7 +627,7 @@ fn start_feeder_gateway_sync(
ethereum: ethereum_client,
chain: pathfinder_context.network,
chain_id: pathfinder_context.network_id,
core_address: pathfinder_context.l1_core_address,
core_address: pathfinder_context.contract_addresses.l1_contract_address,
sequencer: pathfinder_context.gateway,
state: sync_state.clone(),
head_poll_interval: config.poll_interval,
Expand Down Expand Up @@ -665,7 +664,7 @@ fn start_p2p_sync(
storage,
p2p: p2p_client,
eth_client: ethereum_client,
eth_address: pathfinder_context.l1_core_address,
eth_address: pathfinder_context.contract_addresses.l1_contract_address,
fgw_client: pathfinder_context.gateway,
chain_id: pathfinder_context.network_id,
public_key: gateway_public_key,
Expand Down Expand Up @@ -759,7 +758,7 @@ struct PathfinderContext {
network_id: ChainId,
gateway: starknet_gateway_client::Client,
database: PathBuf,
l1_core_address: H160,
contract_addresses: EthContractAddresses,
}

/// Used to hide private fn's for [PathfinderContext].
Expand All @@ -770,7 +769,7 @@ mod pathfinder_context {
use anyhow::Context;
use pathfinder_common::{Chain, ChainId};
use pathfinder_ethereum::core_addr;
use primitive_types::H160;
use pathfinder_rpc::context::EthContractAddresses;
use reqwest::Url;
use starknet_gateway_client::Client as GatewayClient;

Expand All @@ -790,22 +789,24 @@ mod pathfinder_context {
network_id: ChainId::MAINNET,
gateway: GatewayClient::mainnet(gateway_timeout).with_api_key(api_key),
database: data_directory.join("mainnet.sqlite"),
l1_core_address: H160::from(core_addr::MAINNET),
contract_addresses: EthContractAddresses::new_known(core_addr::MAINNET),
},
NetworkConfig::SepoliaTestnet => Self {
network: Chain::SepoliaTestnet,
network_id: ChainId::SEPOLIA_TESTNET,
gateway: GatewayClient::sepolia_testnet(gateway_timeout).with_api_key(api_key),
database: data_directory.join("testnet-sepolia.sqlite"),
l1_core_address: H160::from(core_addr::SEPOLIA_TESTNET),
contract_addresses: EthContractAddresses::new_known(core_addr::SEPOLIA_TESTNET),
},
NetworkConfig::SepoliaIntegration => Self {
network: Chain::SepoliaIntegration,
network_id: ChainId::SEPOLIA_INTEGRATION,
gateway: GatewayClient::sepolia_integration(gateway_timeout)
.with_api_key(api_key),
database: data_directory.join("integration-sepolia.sqlite"),
l1_core_address: H160::from(core_addr::SEPOLIA_INTEGRATION),
contract_addresses: EthContractAddresses::new_known(
core_addr::SEPOLIA_INTEGRATION,
),
},
NetworkConfig::Custom {
gateway,
Expand Down Expand Up @@ -848,12 +849,16 @@ mod pathfinder_context {
let network_id =
ChainId(Felt::from_be_slice(chain_id.as_bytes()).context("Parsing chain ID")?);

let l1_core_address = gateway
let reply_contract_addresses = gateway
.eth_contract_addresses()
.await
.context("Downloading starknet L1 address from gateway for proxy check")?
.starknet
.0;
.context("Downloading starknet L1 address from gateway for proxy check")?;
let l1_core_address = reply_contract_addresses.starknet.0;
let contract_addresses = EthContractAddresses::new_custom(
l1_core_address,
reply_contract_addresses.eth_l2_token_address,
reply_contract_addresses.strk_l2_token_address,
)?;

// Check for proxies by comparing the core address against those of the known
// networks.
Expand All @@ -873,7 +878,7 @@ mod pathfinder_context {
network_id,
gateway,
database: data_directory.join("custom.sqlite"),
l1_core_address,
contract_addresses,
};

Ok(context)
Expand Down
Loading
Loading