Skip to content

Commit

Permalink
fix: move things around, introduce versioned configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
rymnc committed Oct 7, 2024
1 parent 56731ca commit 298de5f
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 28 deletions.
10 changes: 7 additions & 3 deletions crates/fuel-core/src/service/adapters/gas_price_adapters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use fuel_core_gas_price_service::{
GasPriceData,
GasPriceServiceConfig,
L2Data,
VersionSpecificConfig,
},
};
use fuel_core_storage::{
Expand Down Expand Up @@ -64,12 +65,15 @@ impl GasPriceData for Database<GasPriceDatabase> {

impl From<Config> for GasPriceServiceConfig {
fn from(value: Config) -> Self {
GasPriceServiceConfig::new(
value.min_gas_price,
// TODO: we need to alter fuel-core configuration to include versioned config, after which
// we can directly pass it into the constructor.
let version_specific_config = VersionSpecificConfig::new_v0(
value.starting_gas_price,
value.min_gas_price,
value.gas_price_change_percent,
value.gas_price_threshold_percent,
)
);
GasPriceServiceConfig::new(version_specific_config)
}
}

Expand Down
70 changes: 57 additions & 13 deletions crates/services/gas_price_service/src/ports.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::common::{
updater_metadata::UpdaterMetadata,
utils::Result,
use crate::{
common::{
updater_metadata::UpdaterMetadata,
utils::Result,
},
v0::metadata::V0MetadataInitializer,
v1::metadata::V1MetadataInitializer,
};
use fuel_core_storage::Result as StorageResult;
use fuel_core_types::{
Expand Down Expand Up @@ -30,25 +34,65 @@ pub trait GasPriceData: Send + Sync {
fn latest_height(&self) -> Option<BlockHeight>;
}

pub struct GasPriceServiceConfig {
pub min_gas_price: u64,
pub starting_gas_price: u64,
pub gas_price_change_percent: u64,
pub gas_price_threshold_percent: u64,
pub enum VersionSpecificConfig {
V0(V0MetadataInitializer),
V1(V1MetadataInitializer),
}

impl GasPriceServiceConfig {
pub fn new(
min_gas_price: u64,
impl VersionSpecificConfig {
pub fn new_v0(
starting_gas_price: u64,
min_gas_price: u64,
gas_price_change_percent: u64,
gas_price_threshold_percent: u64,
) -> Self {
Self {
min_gas_price,
Self::V0(V0MetadataInitializer {
starting_gas_price,
min_gas_price,
gas_price_change_percent,
gas_price_threshold_percent,
})
}

pub fn new_v1(metadata: V1MetadataInitializer) -> Self {
Self::V1(metadata)
}

/// Extract V0MetadataInitializer if it is of V0 version
pub fn v0(self) -> Option<V0MetadataInitializer> {
if let VersionSpecificConfig::V0(v0) = self {
Some(v0)
} else {
None
}
}

/// Extract V1MetadataInitializer if it is of V1 version
pub fn v1(self) -> Option<V1MetadataInitializer> {
if let VersionSpecificConfig::V1(v1) = self {
Some(v1)
} else {
None
}
}
}

pub struct GasPriceServiceConfig {
version_specific_config: VersionSpecificConfig,
}

impl GasPriceServiceConfig {
pub fn new(version_specific_config: VersionSpecificConfig) -> Self {
Self {
version_specific_config,
}
}

pub fn v0(self) -> Option<V0MetadataInitializer> {
self.version_specific_config.v0()
}

pub fn v1(self) -> Option<V1MetadataInitializer> {
self.version_specific_config.v1()
}
}
19 changes: 19 additions & 0 deletions crates/services/gas_price_service/src/v0/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,25 @@ pub struct V0Metadata {
pub l2_block_fullness_threshold_percent: u64,
}

pub struct V0MetadataInitializer {
pub starting_gas_price: u64,
pub min_gas_price: u64,
pub gas_price_change_percent: u64,
pub gas_price_threshold_percent: u64,
}

impl V0MetadataInitializer {
pub fn initialize(&self, l2_block_height: u32) -> V0Metadata {
V0Metadata {
new_exec_price: self.starting_gas_price.max(self.min_gas_price),
min_exec_gas_price: self.min_gas_price,
exec_gas_price_change_percent: self.gas_price_change_percent,
l2_block_height,
l2_block_fullness_threshold_percent: self.gas_price_threshold_percent,
}
}
}

impl From<V0Metadata> for AlgorithmUpdaterV0 {
fn from(metadata: V0Metadata) -> Self {
Self {
Expand Down
22 changes: 10 additions & 12 deletions crates/services/gas_price_service/src/v0/uninitialized_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ use crate::{
MetadataStorage,
},
v0::{
metadata::V0Metadata,
metadata::{
V0Metadata,
V0MetadataInitializer,
},
service::GasPriceServiceV0,
},
};
Expand Down Expand Up @@ -52,7 +55,7 @@ pub use fuel_gas_price_algorithm::v0::AlgorithmV0;
pub type SharedV0Algorithm = SharedGasPriceAlgo<AlgorithmV0>;

pub struct UninitializedTask<L2DataStoreView, GasPriceStore, SettingsProvider> {
pub config: GasPriceServiceConfig,
pub config: V0MetadataInitializer,
pub genesis_block_height: BlockHeight,
pub settings: SettingsProvider,
pub gas_price_db: GasPriceStore,
Expand All @@ -64,16 +67,10 @@ pub struct UninitializedTask<L2DataStoreView, GasPriceStore, SettingsProvider> {
}

fn get_default_metadata(
config: &GasPriceServiceConfig,
config: &V0MetadataInitializer,
latest_block_height: u32,
) -> V0Metadata {
V0Metadata {
new_exec_price: config.starting_gas_price.max(config.min_gas_price),
min_exec_gas_price: config.min_gas_price,
exec_gas_price_change_percent: config.gas_price_change_percent,
l2_block_height: latest_block_height,
l2_block_fullness_threshold_percent: config.gas_price_threshold_percent,
}
config.initialize(latest_block_height)
}

impl<L2DataStore, L2DataStoreView, GasPriceStore, SettingsProvider>
Expand All @@ -86,7 +83,7 @@ where
SettingsProvider: GasPriceSettingsProvider,
{
pub fn new(
config: GasPriceServiceConfig,
config: V0MetadataInitializer,
genesis_block_height: BlockHeight,
settings: SettingsProvider,
block_stream: BoxStream<SharedImportResult>,
Expand Down Expand Up @@ -359,8 +356,9 @@ where
GasPriceData + Modifiable + KeyValueInspect<Column = GasPriceColumn> + Clone,
SettingsProvider: GasPriceSettingsProvider,
{
let v0_config = config.v0().ok_or(anyhow::anyhow!("Expected V0 config"))?;
let gas_price_init = UninitializedTask::new(
config,
v0_config,
genesis_block_height,
settings,
block_stream,
Expand Down
49 changes: 49 additions & 0 deletions crates/services/gas_price_service/src/v1/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,55 @@ pub struct V1Metadata {
pub latest_da_cost_per_byte: u128,
}

pub struct V1MetadataInitializer {
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: u64,
min_da_gas_price: u64,
max_da_gas_price_change_percent: u16,
da_p_component: i64,
da_d_component: i64,
}

impl V1MetadataInitializer {
pub fn initialize(
&self,
l2_block_height: u32,
da_block_height: u32,
) -> anyhow::Result<V1Metadata> {
let gas_price_factor = NonZeroU64::new(self.gas_price_factor)
.ok_or_else(|| anyhow::anyhow!("gas_price_factor must be non-zero"))?;
let metadata = V1Metadata {
#[allow(clippy::arithmetic_side_effects)]
new_scaled_exec_price: self.new_exec_gas_price * gas_price_factor.get(),
min_exec_gas_price: self.min_exec_gas_price,
exec_gas_price_change_percent: self.exec_gas_price_change_percent,
l2_block_height,
l2_block_fullness_threshold_percent: ClampedPercentage::new(
self.l2_block_fullness_threshold_percent,
),
#[allow(clippy::arithmetic_side_effects)]
new_scaled_da_gas_price: self.min_da_gas_price * gas_price_factor.get(),
gas_price_factor,
min_da_gas_price: self.min_da_gas_price,
max_da_gas_price_change_percent: self.max_da_gas_price_change_percent,
total_da_rewards_excess: 0,
da_recorded_block_height: da_block_height,
latest_known_total_da_cost_excess: 0,
projected_total_da_cost: 0,
da_p_component: self.da_p_component,
da_d_component: self.da_d_component,
last_profit: 0,
second_to_last_profit: 0,
latest_da_cost_per_byte: 0,
};

Ok(metadata)
}
}

impl From<V1Metadata> for AlgorithmUpdaterV1 {
fn from(metadata: V1Metadata) -> Self {
Self {
Expand Down

0 comments on commit 298de5f

Please sign in to comment.