From 1598c2d33deff8fc77695037e157b6dfd2bc5e0c Mon Sep 17 00:00:00 2001 From: Mitch Turner Date: Mon, 2 Dec 2024 11:58:04 -0700 Subject: [PATCH] WIP add new config values --- .../tests/integration_tests.rs | 2 +- bin/fuel-core/src/cli/run.rs | 38 ++++++--- .../service/adapters/gas_price_adapters.rs | 19 +++-- crates/fuel-core/src/service/config.rs | 55 +++++++++---- crates/fuel-core/src/service/sub_services.rs | 2 +- .../services/gas_price_service/src/ports.rs | 33 +++++++- .../block_committer_costs.rs | 54 ++++++++----- .../gas_price_service/src/v1/metadata.rs | 4 +- tests/test-helpers/src/builder.rs | 2 +- tests/test-helpers/src/fuel_core_driver.rs | 4 +- tests/tests/chain.rs | 2 +- tests/tests/gas_price.rs | 80 ++++++++++++------- 12 files changed, 208 insertions(+), 87 deletions(-) diff --git a/bin/e2e-test-client/tests/integration_tests.rs b/bin/e2e-test-client/tests/integration_tests.rs index bb62d4e8dc3..720a4be4939 100644 --- a/bin/e2e-test-client/tests/integration_tests.rs +++ b/bin/e2e-test-client/tests/integration_tests.rs @@ -136,7 +136,7 @@ fn dev_config() -> Config { let reader = reader.with_chain_config(chain_config); let mut config = Config::local_node_with_reader(reader); - config.starting_gas_price = 1; + config.starting_exec_gas_price = 1; config.block_producer.coinbase_recipient = Some( ContractId::from_str( "0x7777777777777777777777777777777777777777777777777777777777777777", diff --git a/bin/fuel-core/src/cli/run.rs b/bin/fuel-core/src/cli/run.rs index 06d00219b50..cc5ef9765bb 100644 --- a/bin/fuel-core/src/cli/run.rs +++ b/bin/fuel-core/src/cli/run.rs @@ -190,12 +190,12 @@ pub struct Command { pub native_executor_version: Option, /// The starting gas price for the network - #[arg(long = "starting-gas-price", default_value = "0", env)] - pub starting_gas_price: u64, + #[arg(long = "starting-exec-gas-price", default_value = "0", env)] + pub starting_exec_gas_price: u64, /// The percentage change in gas price per block #[arg(long = "gas-price-change-percent", default_value = "0", env)] - pub gas_price_change_percent: u64, + pub gas_price_change_percent: u16, /// The minimum allowed gas price #[arg(long = "min-gas-price", default_value = "0", env)] @@ -203,7 +203,15 @@ pub struct Command { /// The percentage threshold for gas price increase #[arg(long = "gas-price-threshold-percent", default_value = "50", env)] - pub gas_price_threshold_percent: u64, + pub gas_price_threshold_percent: u8, + + // Minimum DA gas price + #[arg(long = "min-da-gas-price", default_value = "0", env)] + pub min_da_gas_price: u64, + + /// The URL for the DA Block Committer info + #[arg(long = "da-committer-url", env)] + pub da_committer_url: Option, /// The signing key used when producing blocks. /// Setting via the `CONSENSUS_KEY_SECRET` ENV var is preferred. @@ -299,10 +307,12 @@ impl Command { debug, utxo_validation, native_executor_version, - starting_gas_price, + starting_exec_gas_price: starting_gas_price, gas_price_change_percent, min_gas_price, gas_price_threshold_percent, + min_da_gas_price, + da_committer_url, consensus_key, #[cfg(feature = "aws-kms")] consensus_aws_kms, @@ -588,10 +598,10 @@ impl Command { coinbase_recipient, metrics: disabled_metrics.is_enabled(Module::Producer), }, - starting_gas_price, - gas_price_change_percent, - min_gas_price, - gas_price_threshold_percent, + starting_exec_gas_price: starting_gas_price, + exec_gas_price_change_percent: gas_price_change_percent, + min_exec_gas_price: min_gas_price, + exec_gas_price_threshold_percent: gas_price_threshold_percent, block_importer, da_compression, #[cfg(feature = "relayer")] @@ -606,6 +616,16 @@ impl Command { min_connected_reserved_peers, time_until_synced: time_until_synced.into(), memory_pool_size, + da_gas_price_factor: NonZeroU64::new(100).expect("100 is not zero"), + min_da_gas_price, + max_da_gas_price_change_percent: 0, + da_p_component: 0, + da_d_component: 0, + activity_normal_range_size: 100, + activity_capped_range_size: 0, + activity_decrease_range_size: 0, + da_committer_url, + block_activity_threshold: 0, }; Ok(config) } diff --git a/crates/fuel-core/src/service/adapters/gas_price_adapters.rs b/crates/fuel-core/src/service/adapters/gas_price_adapters.rs index 857c6ded988..2af4c597528 100644 --- a/crates/fuel-core/src/service/adapters/gas_price_adapters.rs +++ b/crates/fuel-core/src/service/adapters/gas_price_adapters.rs @@ -64,11 +64,20 @@ impl GasPriceData for Database { impl From for GasPriceServiceConfig { fn from(value: Config) -> Self { - GasPriceServiceConfig::new_v0( - value.starting_gas_price, - value.min_gas_price, - value.gas_price_change_percent, - value.gas_price_threshold_percent, + GasPriceServiceConfig::new_v1( + value.starting_exec_gas_price, + value.min_exec_gas_price, + value.exec_gas_price_change_percent, + value.exec_gas_price_threshold_percent, + value.da_gas_price_factor, + value.min_da_gas_price, + value.max_da_gas_price_change_percent, + value.da_p_component, + value.da_d_component, + value.activity_normal_range_size, + value.activity_capped_range_size, + value.activity_decrease_range_size, + value.block_activity_threshold, ) } } diff --git a/crates/fuel-core/src/service/config.rs b/crates/fuel-core/src/service/config.rs index 0e093c2b8b6..069b9dd40c7 100644 --- a/crates/fuel-core/src/service/config.rs +++ b/crates/fuel-core/src/service/config.rs @@ -1,10 +1,10 @@ +use clap::ValueEnum; +use fuel_core_poa::signer::SignMode; use std::{ + num::NonZeroU64, path::PathBuf, time::Duration, }; - -use clap::ValueEnum; -use fuel_core_poa::signer::SignMode; use strum_macros::{ Display, EnumString, @@ -57,10 +57,11 @@ pub struct Config { pub vm: VMConfig, pub txpool: TxPoolConfig, pub block_producer: fuel_core_producer::Config, - pub starting_gas_price: u64, - pub gas_price_change_percent: u64, - pub min_gas_price: u64, - pub gas_price_threshold_percent: u64, + pub starting_exec_gas_price: u64, + pub exec_gas_price_change_percent: u16, + pub min_exec_gas_price: u64, + pub exec_gas_price_threshold_percent: u8, + pub da_committer_url: Option, pub da_compression: DaCompressionConfig, pub block_importer: fuel_core_importer::Config, #[cfg(feature = "relayer")] @@ -78,6 +79,15 @@ pub struct Config { pub time_until_synced: Duration, /// The size of the memory pool in number of `MemoryInstance`s. pub memory_pool_size: usize, + pub da_gas_price_factor: NonZeroU64, + pub min_da_gas_price: u64, + pub max_da_gas_price_change_percent: u16, + pub da_p_component: i64, + pub da_d_component: i64, + pub activity_normal_range_size: u16, + pub activity_capped_range_size: u16, + pub activity_decrease_range_size: u16, + pub block_activity_threshold: u8, } impl Config { @@ -96,14 +106,17 @@ impl Config { chain_config: ChainConfig, state_config: StateConfig, ) -> Self { - Self::local_node_with_reader(SnapshotReader::new_in_memory( - chain_config, - state_config, - )) + Self::local_node_with_reader( + SnapshotReader::new_in_memory(chain_config, state_config), + None, + ) } #[cfg(feature = "test-helpers")] - pub fn local_node_with_reader(snapshot_reader: SnapshotReader) -> Self { + pub fn local_node_with_reader( + snapshot_reader: SnapshotReader, + da_committer_url: Option, + ) -> Self { let block_importer = fuel_core_importer::Config::new(false); let latest_block = snapshot_reader.last_block_config(); // In tests, we always want to use the native executor as a default configuration. @@ -171,10 +184,10 @@ impl Config { ..Default::default() }, da_compression: DaCompressionConfig::Disabled, - starting_gas_price, - gas_price_change_percent, - min_gas_price, - gas_price_threshold_percent, + starting_exec_gas_price: starting_gas_price, + exec_gas_price_change_percent: gas_price_change_percent, + min_exec_gas_price: min_gas_price, + exec_gas_price_threshold_percent: gas_price_threshold_percent, block_importer, #[cfg(feature = "relayer")] relayer: None, @@ -190,6 +203,16 @@ impl Config { min_connected_reserved_peers: 0, time_until_synced: Duration::ZERO, memory_pool_size: 4, + da_gas_price_factor: NonZeroU64::new(100).expect("100 is not zero"), + min_da_gas_price: 0, + max_da_gas_price_change_percent: 0, + da_p_component: 0, + da_d_component: 0, + activity_normal_range_size: 0, + activity_capped_range_size: 0, + activity_decrease_range_size: 0, + da_committer_url, + block_activity_threshold: 0, } } diff --git a/crates/fuel-core/src/service/sub_services.rs b/crates/fuel-core/src/service/sub_services.rs index 82fbc4835f8..8f8314ff6f5 100644 --- a/crates/fuel-core/src/service/sub_services.rs +++ b/crates/fuel-core/src/service/sub_services.rs @@ -188,7 +188,7 @@ pub fn init_sub_services( let block_stream = importer_adapter.events_shared_result(); let metadata = StructuredStorage::new(database.gas_price().clone()); - let committer = BlockCommitterHttpApi::new("lolz".to_string()); + let committer = BlockCommitterHttpApi::new(config.da_committer_url.clone()); let da_source = BlockCommitterDaBlockCosts::new(committer, None); let on_chain_db = database.on_chain().clone(); diff --git a/crates/services/gas_price_service/src/ports.rs b/crates/services/gas_price_service/src/ports.rs index cb829ab21fb..c9e2524e87b 100644 --- a/crates/services/gas_price_service/src/ports.rs +++ b/crates/services/gas_price_service/src/ports.rs @@ -12,6 +12,7 @@ use fuel_core_types::{ fuel_tx::Transaction, fuel_types::BlockHeight, }; +use std::num::NonZeroU64; pub trait L2Data: Send + Sync { fn latest_height(&self) -> StorageResult; @@ -54,8 +55,36 @@ impl GasPriceServiceConfig { }) } - pub fn new_v1(metadata: V1AlgorithmConfig) -> Self { - Self::V1(metadata) + pub fn new_v1( + new_exec_gas_price: u64, + min_exec_gas_price: u64, + exec_gas_price_change_percent: u16, + l2_block_fullness_threshold_percent: u8, + gas_price_factor: NonZeroU64, + min_da_gas_price: u64, + max_da_gas_price_change_percent: u16, + da_p_component: i64, + da_d_component: i64, + normal_range_size: u16, + capped_range_size: u16, + decrease_range_size: u16, + block_activity_threshold: u8, + ) -> Self { + Self::V1(V1AlgorithmConfig { + new_exec_gas_price, + min_exec_gas_price, + exec_gas_price_change_percent, + l2_block_fullness_threshold_percent, + gas_price_factor, + min_da_gas_price, + max_da_gas_price_change_percent, + da_p_component, + da_d_component, + normal_range_size, + capped_range_size, + decrease_range_size, + block_activity_threshold, + }) } /// Extract V0AlgorithmConfig if it is of V0 version diff --git a/crates/services/gas_price_service/src/v1/da_source_service/block_committer_costs.rs b/crates/services/gas_price_service/src/v1/da_source_service/block_committer_costs.rs index 3387041f2f9..9d3cbf6f503 100644 --- a/crates/services/gas_price_service/src/v1/da_source_service/block_committer_costs.rs +++ b/crates/services/gas_price_service/src/v1/da_source_service/block_committer_costs.rs @@ -120,11 +120,11 @@ where pub struct BlockCommitterHttpApi { client: reqwest::Client, - url: String, + url: Option, } impl BlockCommitterHttpApi { - pub fn new(url: String) -> Self { + pub fn new(url: Option) -> Self { Self { client: reqwest::Client::new(), url, @@ -135,37 +135,49 @@ impl BlockCommitterHttpApi { #[async_trait::async_trait] impl BlockCommitterApi for BlockCommitterHttpApi { async fn get_latest_costs(&self) -> DaBlockCostsResult> { - let val = self.client.get(&self.url).send().await?; - let response = val.json::().await?; - Ok(Some(response)) + if let Some(url) = &self.url { + let val = self.client.get(url).send().await?; + let response = val.json::().await?; + Ok(Some(response)) + } else { + Ok(None) + } } async fn get_costs_by_seqno( &self, number: u32, ) -> DaBlockCostsResult> { - let response = self - .client - .get(&format!("{}/{}", self.url, number)) - .send() - .await? - .json::() - .await?; - Ok(Some(response)) + if let Some(url) = &self.url { + let response = self + .client + .get(&format!("{}/{}", url, number)) + .send() + .await? + .json::() + .await?; + Ok(Some(response)) + } else { + Ok(None) + } } async fn get_cost_bundles_by_range( &self, range: core::ops::Range, ) -> DaBlockCostsResult>> { - let response = self - .client - .get(&format!("{}/{}-{}", self.url, range.start, range.end)) - .send() - .await? - .json::>() - .await?; - Ok(response.into_iter().map(Some).collect()) + if let Some(url) = &self.url { + let response = self + .client + .get(&format!("{}/{}-{}", url, range.start, range.end)) + .send() + .await? + .json::>() + .await?; + Ok(response.into_iter().map(Some).collect()) + } else { + Ok(vec![]) + } } } diff --git a/crates/services/gas_price_service/src/v1/metadata.rs b/crates/services/gas_price_service/src/v1/metadata.rs index 914eddf7c7d..08e4fbf5cc5 100644 --- a/crates/services/gas_price_service/src/v1/metadata.rs +++ b/crates/services/gas_price_service/src/v1/metadata.rs @@ -95,7 +95,9 @@ pub fn updater_from_config( .new_exec_gas_price .saturating_mul(value.gas_price_factor.get()), l2_block_height: 0, - new_scaled_da_gas_price: value.min_da_gas_price, + new_scaled_da_gas_price: value + .min_da_gas_price + .saturating_mul(value.gas_price_factor.get()), gas_price_factor: value.gas_price_factor, total_da_rewards_excess: 0, latest_known_total_da_cost_excess: 0, diff --git a/tests/test-helpers/src/builder.rs b/tests/test-helpers/src/builder.rs index 0d16bc48dc6..ec0e0eb6c53 100644 --- a/tests/test-helpers/src/builder.rs +++ b/tests/test-helpers/src/builder.rs @@ -235,7 +235,7 @@ impl TestSetupBuilder { utxo_validation: self.utxo_validation, txpool: fuel_core_txpool::config::Config::default(), block_production: self.trigger, - starting_gas_price: self.starting_gas_price, + starting_exec_gas_price: self.starting_gas_price, ..Config::local_node_with_configs(chain_conf, state) }; assert_eq!(config.combined_db_config.database_type, DbType::RocksDb); diff --git a/tests/test-helpers/src/fuel_core_driver.rs b/tests/test-helpers/src/fuel_core_driver.rs index a290712e5d2..4f2f3f8363b 100644 --- a/tests/test-helpers/src/fuel_core_driver.rs +++ b/tests/test-helpers/src/fuel_core_driver.rs @@ -22,7 +22,7 @@ impl FuelCoreDriver { pub async fn spawn_feeless(extra_args: &[&str]) -> anyhow::Result { let mut args = vec![ - "--starting-gas-price", + "--starting-exec-gas-price", "0", "--gas-price-change-percent", "0", @@ -36,7 +36,7 @@ impl FuelCoreDriver { extra_args: &[&str], ) -> anyhow::Result { let mut args = vec![ - "--starting-gas-price", + "--starting-exec-gas-price", "0", "--gas-price-change-percent", "0", diff --git a/tests/tests/chain.rs b/tests/tests/chain.rs index 43470f7c0d3..7e9b627a3f8 100644 --- a/tests/tests/chain.rs +++ b/tests/tests/chain.rs @@ -141,7 +141,7 @@ async fn network_operates_with_non_zero_base_asset_id() { let node_config = Config { debug: true, utxo_validation: true, - starting_gas_price, + starting_exec_gas_price: starting_gas_price, ..Config::local_node_with_configs(chain_config, state_config) }; diff --git a/tests/tests/gas_price.rs b/tests/tests/gas_price.rs index 4165cbff410..af46a03db6a 100644 --- a/tests/tests/gas_price.rs +++ b/tests/tests/gas_price.rs @@ -104,7 +104,7 @@ async fn latest_gas_price__for_single_block_should_be_starting_gas_price() { // given let mut config = Config::local_node(); let starting_gas_price = 982; - config.starting_gas_price = starting_gas_price; + config.starting_exec_gas_price = starting_gas_price; let srv = FuelService::from_database(Database::default(), config.clone()) .await .unwrap(); @@ -138,9 +138,9 @@ async fn produce_block__raises_gas_price() { let percent = 10; let threshold = 50; node_config.block_producer.coinbase_recipient = Some([5; 32].into()); - node_config.starting_gas_price = starting_gas_price; - node_config.gas_price_change_percent = percent; - node_config.gas_price_threshold_percent = threshold; + node_config.starting_exec_gas_price = starting_gas_price; + node_config.exec_gas_price_change_percent = percent; + node_config.exec_gas_price_threshold_percent = threshold; node_config.block_production = Trigger::Never; let srv = FuelService::new_node(node_config.clone()).await.unwrap(); @@ -159,7 +159,7 @@ async fn produce_block__raises_gas_price() { let _ = client.produce_blocks(1, None).await.unwrap(); // then - let change = starting_gas_price * percent / 100; + let change = starting_gas_price * percent as u64 / 100; let expected = starting_gas_price + change; let latest = client.latest_gas_price().await.unwrap(); let actual = latest.gas_price; @@ -183,9 +183,9 @@ async fn produce_block__lowers_gas_price() { let percent = 10; let threshold = 50; node_config.block_producer.coinbase_recipient = Some([5; 32].into()); - node_config.starting_gas_price = starting_gas_price; - node_config.gas_price_change_percent = percent; - node_config.gas_price_threshold_percent = threshold; + node_config.starting_exec_gas_price = starting_gas_price; + node_config.exec_gas_price_change_percent = percent; + node_config.exec_gas_price_threshold_percent = threshold; node_config.block_production = Trigger::Never; let srv = FuelService::new_node(node_config.clone()).await.unwrap(); @@ -204,7 +204,7 @@ async fn produce_block__lowers_gas_price() { let _ = client.produce_blocks(1, None).await.unwrap(); // then - let change = starting_gas_price * percent / 100; + let change = starting_gas_price * percent as u64 / 100; let expected = starting_gas_price - change; let latest = client.latest_gas_price().await.unwrap(); let actual = latest.gas_price; @@ -217,10 +217,10 @@ async fn estimate_gas_price__is_greater_than_actual_price_at_desired_height() { let mut node_config = Config::local_node(); let starting_gas_price = 1000; let percent = 10; - node_config.starting_gas_price = starting_gas_price; - node_config.gas_price_change_percent = percent; + node_config.starting_exec_gas_price = starting_gas_price; + node_config.exec_gas_price_change_percent = percent; // Always increase - node_config.gas_price_threshold_percent = 0; + node_config.exec_gas_price_threshold_percent = 0; let srv = FuelService::new_node(node_config.clone()).await.unwrap(); let client = FuelClient::from(srv.bound_address); @@ -248,8 +248,8 @@ async fn estimate_gas_price__returns_min_gas_price_if_starting_gas_price_is_zero // Given let mut node_config = Config::local_node(); - node_config.min_gas_price = MIN_GAS_PRICE; - node_config.starting_gas_price = 0; + node_config.min_exec_gas_price = MIN_GAS_PRICE; + node_config.starting_exec_gas_price = 0; let srv = FuelService::new_node(node_config.clone()).await.unwrap(); let client = FuelClient::from(srv.bound_address); @@ -268,7 +268,7 @@ async fn latest_gas_price__if_node_restarts_gets_latest_value() { "--debug", "--poa-instant", "true", - "--starting-gas-price", + "--starting-exec-gas-price", "1000", "--gas-price-change-percent", "10", @@ -276,7 +276,7 @@ async fn latest_gas_price__if_node_restarts_gets_latest_value() { "0", ]; let driver = FuelCoreDriver::spawn(&args).await.unwrap(); - let starting = driver.node.shared.config.starting_gas_price; + let starting = driver.node.shared.config.starting_exec_gas_price; let arb_blocks_to_produce = 10; for _ in 0..arb_blocks_to_produce { driver.client.produce_blocks(1, None).await.unwrap(); @@ -418,22 +418,48 @@ async fn startup__can_override_gas_price_values_by_changing_config() { recovered_driver.kill().await; } -use fuel_core_gas_price_service::v1::da_source_service::block_committer_costs::{ - BlockCommitterApi, - BlockCommitterHttpApi, - RawDaBlockCosts, -}; +use fuel_core_gas_price_service::v1::da_source_service::block_committer_costs::RawDaBlockCosts; #[test] fn produce_block__l1_committed_block_effects_gas_price() { + let rt = tokio::runtime::Runtime::new().unwrap(); + // set up chain with single unrecorded block + let args = vec![ + "--debug", + "--poa-instant", + "true", + "--min-da-gas-price", + "100", + ]; + let (first_gas_price, temp_dir) = rt.block_on(async { + let driver = FuelCoreDriver::spawn(&args).await.unwrap(); + driver.client.produce_blocks(1, None).await.unwrap(); + let first_gas_price = driver.client.latest_gas_price().await.unwrap().gas_price; + let temp_dir = driver.kill().await; + (first_gas_price, temp_dir) + }); + + assert_eq!(100, first_gas_price); + + // set up chain with single recorded block let mock = FakeServer::new(); let url = mock.url(); - let api = BlockCommitterHttpApi::new(url); - let rt = tokio::runtime::Runtime::new().unwrap(); - let res = rt.block_on(api.get_latest_costs()); - dbg!(&res); - let costs = res.unwrap(); - dbg!(costs); + + let args = vec![ + "--debug", + "--poa-instant", + "true", + "--da-committer-url", + &url, + "--min-da-gas-price", + "100", + ]; + + rt.block_on(async { + let _driver = FuelCoreDriver::spawn_with_directory(temp_dir, &args) + .await + .unwrap(); + }); } struct FakeServer {