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(katana): starknet gas oracle placeholder #2874

Merged
merged 3 commits into from
Jan 7, 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
14 changes: 6 additions & 8 deletions crates/katana/chain-spec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use katana_primitives::chain::ChainId;
use katana_primitives::class::ClassHash;
use katana_primitives::contract::ContractAddress;
use katana_primitives::da::L1DataAvailabilityMode;
use katana_primitives::eth::{self, Address as EthAddress};
use katana_primitives::genesis::allocation::{DevAllocationsGenerator, GenesisAllocation};
use katana_primitives::genesis::constant::{
get_fee_token_balance_base_storage_address, DEFAULT_ACCOUNT_CLASS_PUBKEY_STORAGE_SLOT,
Expand All @@ -24,7 +23,7 @@ use katana_primitives::genesis::Genesis;
use katana_primitives::state::StateUpdatesWithClasses;
use katana_primitives::utils::split_u256;
use katana_primitives::version::{ProtocolVersion, CURRENT_STARKNET_VERSION};
use katana_primitives::Felt;
use katana_primitives::{eth, Felt};
use lazy_static::lazy_static;
use serde::{Deserialize, Serialize};
use starknet::core::utils::cairo_short_string_to_felt;
Expand Down Expand Up @@ -63,7 +62,7 @@ pub struct FeeContracts {
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum SettlementLayer {
Ethereum {
// The id of the settlement chain.
Expand All @@ -72,11 +71,11 @@ pub enum SettlementLayer {
// url for ethereum rpc provider
rpc_url: Url,

account: EthAddress,
/// account on the ethereum network
account: eth::Address,

// - The core appchain contract used to settlement
// - This is deployed on the L1
core_contract: EthAddress,
core_contract: eth::Address,
},

Starknet {
Expand All @@ -86,11 +85,10 @@ pub enum SettlementLayer {
// url for starknet rpc provider
rpc_url: Url,

// the account address that was used to initialized the l1 deployments
/// account on the starknet network
account: ContractAddress,

// - The core appchain contract used to settlement
// - This is deployed on the L1
core_contract: ContractAddress,
},
}
Expand Down
50 changes: 29 additions & 21 deletions crates/katana/core/src/backend/gas_oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,22 @@
const INTERVAL: Duration = Duration::from_secs(60);
const ONE_GWEI: u128 = 1_000_000_000;

// TODO: implement a proper gas oracle function - sample the l1 gas and data gas prices
// currently this just return the hardcoded value set from the cli or if not set, the default value.
#[derive(Debug)]
pub enum L1GasOracle {
Fixed(FixedL1GasOracle),
Sampled(SampledL1GasOracle),
pub enum GasOracle {
Fixed(FixedGasOracle),
Sampled(EthereumSampledGasOracle),
}

#[derive(Debug)]
pub struct FixedL1GasOracle {
pub struct FixedGasOracle {
gas_prices: GasPrices,
data_gas_prices: GasPrices,
}

#[derive(Debug, Clone)]
pub struct SampledL1GasOracle {
pub struct EthereumSampledGasOracle {
prices: Arc<Mutex<SampledPrices>>,
l1_provider: Url,
provider: Url,
}

#[derive(Debug, Default)]
Expand All @@ -50,29 +48,39 @@
pub data_gas_price_buffer: GasPriceBuffer,
}

impl L1GasOracle {
impl GasOracle {
pub fn fixed(gas_prices: GasPrices, data_gas_prices: GasPrices) -> Self {
L1GasOracle::Fixed(FixedL1GasOracle { gas_prices, data_gas_prices })
GasOracle::Fixed(FixedGasOracle { gas_prices, data_gas_prices })
}

pub fn sampled(l1_provider: Url) -> Self {
/// Creates a new gas oracle that samples the gas prices from an Ethereum chain.
pub fn sampled_ethereum(eth_provider: Url) -> Self {

Check warning on line 57 in crates/katana/core/src/backend/gas_oracle.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/core/src/backend/gas_oracle.rs#L57

Added line #L57 was not covered by tests
let prices: Arc<Mutex<SampledPrices>> = Arc::new(Mutex::new(SampledPrices::default()));
L1GasOracle::Sampled(SampledL1GasOracle { prices, l1_provider })
GasOracle::Sampled(EthereumSampledGasOracle { prices, provider: eth_provider })
}

Check warning on line 60 in crates/katana/core/src/backend/gas_oracle.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/core/src/backend/gas_oracle.rs#L59-L60

Added lines #L59 - L60 were not covered by tests

/// This is just placeholder for now, as Starknet doesn't provide a way to get the L2 gas
/// prices, we just return a fixed gas price values of 0. This is equivalent to calling
/// [`GasOracle::fixed`] with 0 values for both gas and data prices.
///
/// The result of this is the same as running the node with fee disabled.
pub fn sampled_starknet() -> Self {
Self::fixed(GasPrices { eth: 0, strk: 0 }, GasPrices { eth: 0, strk: 0 })

Check warning on line 68 in crates/katana/core/src/backend/gas_oracle.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/core/src/backend/gas_oracle.rs#L67-L68

Added lines #L67 - L68 were not covered by tests
}

/// Returns the current gas prices.
pub fn current_gas_prices(&self) -> GasPrices {
match self {
L1GasOracle::Fixed(fixed) => fixed.current_gas_prices(),
L1GasOracle::Sampled(sampled) => sampled.prices.lock().gas_prices.clone(),
GasOracle::Fixed(fixed) => fixed.current_gas_prices(),
GasOracle::Sampled(sampled) => sampled.prices.lock().gas_prices.clone(),

Check warning on line 75 in crates/katana/core/src/backend/gas_oracle.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/core/src/backend/gas_oracle.rs#L75

Added line #L75 was not covered by tests
}
}

/// Returns the current data gas prices.
pub fn current_data_gas_prices(&self) -> GasPrices {
match self {
L1GasOracle::Fixed(fixed) => fixed.current_data_gas_prices(),
L1GasOracle::Sampled(sampled) => sampled.prices.lock().data_gas_prices.clone(),
GasOracle::Fixed(fixed) => fixed.current_data_gas_prices(),
GasOracle::Sampled(sampled) => sampled.prices.lock().data_gas_prices.clone(),

Check warning on line 83 in crates/katana/core/src/backend/gas_oracle.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/core/src/backend/gas_oracle.rs#L83

Added line #L83 was not covered by tests
}
}

Expand All @@ -81,7 +89,7 @@
Self::Fixed(..) => {}
Self::Sampled(oracle) => {
let prices = oracle.prices.clone();
let l1_provider = oracle.l1_provider.clone();
let l1_provider = oracle.provider.clone();

Check warning on line 92 in crates/katana/core/src/backend/gas_oracle.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/core/src/backend/gas_oracle.rs#L92

Added line #L92 was not covered by tests

task_spawner.build_task().critical().name("L1 Gas Oracle Worker").spawn(
async move {
Expand All @@ -97,7 +105,7 @@
}
}

impl SampledL1GasOracle {
impl EthereumSampledGasOracle {
pub fn current_data_gas_prices(&self) -> GasPrices {
self.prices.lock().data_gas_prices.clone()
}
Expand All @@ -107,7 +115,7 @@
}
}

impl FixedL1GasOracle {
impl FixedGasOracle {
pub fn current_data_gas_prices(&self) -> GasPrices {
self.data_gas_prices.clone()
}
Expand Down Expand Up @@ -289,10 +297,10 @@
#[ignore = "Requires external assumption"]
async fn test_gas_oracle() {
let url = Url::parse("https://eth.merkle.io/").expect("Invalid URL");
let oracle = L1GasOracle::sampled(url.clone());
let oracle = GasOracle::sampled_ethereum(url.clone());

Check warning on line 300 in crates/katana/core/src/backend/gas_oracle.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/core/src/backend/gas_oracle.rs#L300

Added line #L300 was not covered by tests

let shared_prices = match &oracle {
L1GasOracle::Sampled(sampled) => sampled.prices.clone(),
GasOracle::Sampled(sampled) => sampled.prices.clone(),

Check warning on line 303 in crates/katana/core/src/backend/gas_oracle.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/core/src/backend/gas_oracle.rs#L303

Added line #L303 was not covered by tests
_ => panic!("Expected sampled oracle"),
};

Expand Down
4 changes: 2 additions & 2 deletions crates/katana/core/src/backend/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use gas_oracle::L1GasOracle;
use gas_oracle::GasOracle;
use katana_chain_spec::ChainSpec;
use katana_executor::{ExecutionOutput, ExecutionResult, ExecutorFactory};
use katana_primitives::block::{
Expand Down Expand Up @@ -41,7 +41,7 @@ pub struct Backend<EF: ExecutorFactory> {

pub executor_factory: Arc<EF>,

pub gas_oracle: L1GasOracle,
pub gas_oracle: GasOracle,
}

impl<EF: ExecutorFactory> Backend<EF> {
Expand Down
13 changes: 5 additions & 8 deletions crates/katana/node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use hyper::Method;
use jsonrpsee::RpcModule;
use katana_chain_spec::SettlementLayer;
use katana_core::backend::gas_oracle::L1GasOracle;
use katana_core::backend::gas_oracle::GasOracle;
use katana_core::backend::storage::Blockchain;
use katana_core::backend::Backend;
use katana_core::env::BlockContextGenerator;
Expand Down Expand Up @@ -202,20 +202,17 @@
// Check if the user specify a fixed gas price in the dev config.
let gas_oracle = if let Some(fixed_prices) = &config.dev.fixed_gas_prices {
// Use fixed gas prices if provided in the configuration
L1GasOracle::fixed(fixed_prices.gas_price.clone(), fixed_prices.data_gas_price.clone())
GasOracle::fixed(fixed_prices.gas_price.clone(), fixed_prices.data_gas_price.clone())

Check warning on line 205 in crates/katana/node/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/node/src/lib.rs#L205

Added line #L205 was not covered by tests
} else if let Some(settlement) = &config.chain.settlement {
match settlement {
SettlementLayer::Starknet { .. } => GasOracle::sampled_starknet(),

Check warning on line 208 in crates/katana/node/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/node/src/lib.rs#L208

Added line #L208 was not covered by tests
SettlementLayer::Ethereum { rpc_url, .. } => {
// Default to a sampled gas oracle using the given provider
L1GasOracle::sampled(rpc_url.clone())
}
SettlementLayer::Starknet { .. } => {
todo!("starknet gas oracle")
GasOracle::sampled_ethereum(rpc_url.clone())

Check warning on line 210 in crates/katana/node/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/node/src/lib.rs#L210

Added line #L210 was not covered by tests
}
}
} else {
// Use default fixed gas prices if no url and if no fixed prices are provided
L1GasOracle::fixed(
GasOracle::fixed(
GasPrices { eth: DEFAULT_ETH_L1_GAS_PRICE, strk: DEFAULT_STRK_L1_GAS_PRICE },
GasPrices { eth: DEFAULT_ETH_L1_DATA_GAS_PRICE, strk: DEFAULT_STRK_L1_DATA_GAS_PRICE },
)
Expand Down
Loading