Skip to content

Commit

Permalink
Create function for converting old metadata into new metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchTurner committed Oct 25, 2024
1 parent 75b68c3 commit 4419e4d
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 58 deletions.
20 changes: 10 additions & 10 deletions crates/services/gas_price_service/src/ports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::{
updater_metadata::UpdaterMetadata,
utils::Result,
},
v0::metadata::V0MetadataInitializer,
v1::metadata::V1MetadataInitializer,
v0::metadata::V0AlgorithmConfig,
v1::metadata::V1AlgorithmConfig,
};
use fuel_core_storage::Result as StorageResult;
use fuel_core_types::{
Expand Down Expand Up @@ -35,8 +35,8 @@ pub trait GasPriceData: Send + Sync {
}

pub enum VersionSpecificConfig {
V0(V0MetadataInitializer),
V1(V1MetadataInitializer),
V0(V0AlgorithmConfig),
V1(V1AlgorithmConfig),
}

impl VersionSpecificConfig {
Expand All @@ -46,20 +46,20 @@ impl VersionSpecificConfig {
gas_price_change_percent: u64,
gas_price_threshold_percent: u64,
) -> Self {
Self::V0(V0MetadataInitializer {
Self::V0(V0AlgorithmConfig {
starting_gas_price,
min_gas_price,
gas_price_change_percent,
gas_price_threshold_percent,
})
}

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

/// Extract V0MetadataInitializer if it is of V0 version
pub fn v0(self) -> Option<V0MetadataInitializer> {
pub fn v0(self) -> Option<V0AlgorithmConfig> {
if let VersionSpecificConfig::V0(v0) = self {
Some(v0)
} else {
Expand All @@ -68,7 +68,7 @@ impl VersionSpecificConfig {
}

/// Extract V1MetadataInitializer if it is of V1 version
pub fn v1(self) -> Option<V1MetadataInitializer> {
pub fn v1(self) -> Option<V1AlgorithmConfig> {
if let VersionSpecificConfig::V1(v1) = self {
Some(v1)
} else {
Expand All @@ -88,11 +88,11 @@ impl GasPriceServiceConfig {
}
}

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

pub fn v1(self) -> Option<V1MetadataInitializer> {
pub fn v1(self) -> Option<V1AlgorithmConfig> {
self.version_specific_config.v1()
}
}
11 changes: 1 addition & 10 deletions crates/services/gas_price_service/src/v0/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,13 @@ pub struct V0Metadata {
pub l2_block_height: u32,
}

pub struct V0MetadataInitializer {
pub struct V0AlgorithmConfig {
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),
l2_block_height,
}
}
}

impl From<AlgorithmUpdaterV0> for V0Metadata {
fn from(updater: AlgorithmUpdaterV0) -> Self {
Self {
Expand Down
4 changes: 2 additions & 2 deletions crates/services/gas_price_service/src/v0/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ mod tests {
ports::MetadataStorage,
v0::{
algorithm::SharedV0Algorithm,
metadata::V0MetadataInitializer,
metadata::V0AlgorithmConfig,
service::GasPriceServiceV0,
uninitialized_task::initialize_algorithm,
},
Expand Down Expand Up @@ -261,7 +261,7 @@ mod tests {
};
let metadata_storage = FakeMetadata::empty();
let l2_block_height = 0;
let config = V0MetadataInitializer {
let config = V0AlgorithmConfig {
starting_gas_price: 100,
min_gas_price: 10,
gas_price_change_percent: 10,
Expand Down
10 changes: 5 additions & 5 deletions crates/services/gas_price_service/src/v0/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use crate::{
},
v0::{
metadata::{
V0AlgorithmConfig,
V0Metadata,
V0MetadataInitializer,
},
service::GasPriceServiceV0,
uninitialized_task::{
Expand Down Expand Up @@ -116,8 +116,8 @@ impl MetadataStorage for ErroringMetadata {
}
}

fn arb_config() -> V0MetadataInitializer {
V0MetadataInitializer {
fn arb_config() -> V0AlgorithmConfig {
V0AlgorithmConfig {
starting_gas_price: 100,
min_gas_price: 0,
gas_price_change_percent: 10,
Expand All @@ -132,8 +132,8 @@ fn arb_metadata() -> V0Metadata {
}
}

fn different_arb_config() -> V0MetadataInitializer {
V0MetadataInitializer {
fn different_arb_config() -> V0AlgorithmConfig {
V0AlgorithmConfig {
starting_gas_price: 200,
min_gas_price: 0,
gas_price_change_percent: 20,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use crate::{
v0::{
algorithm::SharedV0Algorithm,
metadata::{
V0AlgorithmConfig,
V0Metadata,
V0MetadataInitializer,
},
service::GasPriceServiceV0,
},
Expand All @@ -48,7 +48,7 @@ use fuel_gas_price_algorithm::v0::AlgorithmUpdaterV0;
pub use fuel_gas_price_algorithm::v0::AlgorithmV0;

pub struct UninitializedTask<L2DataStoreView, GasPriceStore, Metadata, SettingsProvider> {
pub config: V0MetadataInitializer,
pub config: V0AlgorithmConfig,
pub genesis_block_height: BlockHeight,
pub settings: SettingsProvider,
pub gas_price_db: GasPriceStore,
Expand All @@ -69,7 +69,7 @@ where
SettingsProvider: GasPriceSettingsProvider,
{
pub fn new(
config: V0MetadataInitializer,
config: V0AlgorithmConfig,
genesis_block_height: BlockHeight,
settings: SettingsProvider,
block_stream: BoxStream<SharedImportResult>,
Expand Down Expand Up @@ -188,7 +188,7 @@ where
}

pub fn initialize_algorithm<Metadata>(
config: &V0MetadataInitializer,
config: &V0AlgorithmConfig,
latest_block_height: u32,
metadata_storage: &Metadata,
) -> GasPriceResult<(AlgorithmUpdaterV0, SharedV0Algorithm)>
Expand Down
57 changes: 30 additions & 27 deletions crates/services/gas_price_service/src/v1/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::v0::metadata::V0Metadata;
use fuel_gas_price_algorithm::v1::{
AlgorithmUpdaterV1,
ClampedPercentage,
Expand Down Expand Up @@ -34,46 +35,48 @@ 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)
impl V1Metadata {
pub fn construct_from_v0_metadata(
v0_metadata: V0Metadata,
config: V1AlgorithmConfig,
) -> anyhow::Result<Self> {
let gas_price_factor = NonZeroU64::new(config.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(),
l2_block_height,
#[allow(clippy::arithmetic_side_effects)]
new_scaled_da_gas_price: self.min_da_gas_price * gas_price_factor.get(),
let metadata = Self {
new_scaled_exec_price: v0_metadata
.new_exec_price
.saturating_mul(gas_price_factor.get()),
l2_block_height: v0_metadata.l2_block_height,
new_scaled_da_gas_price: config
.min_da_gas_price
.saturating_mul(gas_price_factor.get()),
gas_price_factor,
total_da_rewards_excess: 0,
da_recorded_block_height: da_block_height,
// TODO: Set to `None` after:
// https://github.com/FuelLabs/fuel-core/issues/2397
da_recorded_block_height: 0,
latest_known_total_da_cost_excess: 0,
projected_total_da_cost: 0,
last_profit: 0,
second_to_last_profit: 0,
latest_da_cost_per_byte: 0,
};

Ok(metadata)
}
}

pub struct V1AlgorithmConfig {
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 From<AlgorithmUpdaterV1> for V1Metadata {
fn from(updater: AlgorithmUpdaterV1) -> Self {
Self {
Expand Down

0 comments on commit 4419e4d

Please sign in to comment.