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

chore(gas_price_service): split into v0 and v1 and squash FuelGasPriceUpdater type into GasPriceService #2256

Merged
merged 6 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
67 changes: 16 additions & 51 deletions crates/services/gas_price_service/src/v0/service.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
use crate::{
common::{
gas_price_algorithm::SharedGasPriceAlgo,
l2_block_source::L2BlockSource,
updater_metadata::UpdaterMetadata,
utils::{
BlockInfo,
Error as GasPriceError,
Result as GasPriceResult,
},
utils::BlockInfo,
},
ports::MetadataStorage,
v0::{
metadata::V0Metadata,
uninitialized_task::SharedV0Algorithm,
},
v0::uninitialized_task::SharedV0Algorithm,
};
use anyhow::anyhow;
use async_trait::async_trait;
Expand Down Expand Up @@ -48,48 +40,15 @@ where
pub fn new(
l2_block_source: L2,
metadata_storage: Metadata,
starting_metadata: V0Metadata,
) -> GasPriceResult<Self> {
let V0Metadata {
min_exec_gas_price,
exec_gas_price_change_percent,
new_exec_price,
l2_block_fullness_threshold_percent,
l2_block_height,
} = starting_metadata;

let algorithm_updater;
if let Some(old_metadata) = metadata_storage
.get_metadata(&l2_block_height.into())
.map_err(|err| GasPriceError::CouldNotInitUpdater(anyhow::anyhow!(err)))?
{
algorithm_updater = match old_metadata {
UpdaterMetadata::V0(old) => AlgorithmUpdaterV0::new(
old.new_exec_price,
min_exec_gas_price,
exec_gas_price_change_percent,
old.l2_block_height,
l2_block_fullness_threshold_percent,
),
};
} else {
algorithm_updater = AlgorithmUpdaterV0::new(
new_exec_price,
min_exec_gas_price,
exec_gas_price_change_percent,
l2_block_height,
l2_block_fullness_threshold_percent,
);
}

let shared_algo =
SharedGasPriceAlgo::new_with_algorithm(algorithm_updater.algorithm());
Ok(Self {
shared_algo: SharedV0Algorithm,
algorithm_updater: AlgorithmUpdaterV0,
) -> Self {
Self {
shared_algo,
l2_block_source,
metadata_storage,
algorithm_updater,
})
}
}

pub fn algorithm_updater(&self) -> &AlgorithmUpdaterV0 {
Expand Down Expand Up @@ -235,6 +194,7 @@ mod tests {
v0::{
metadata::V0Metadata,
service::GasPriceServiceV0,
uninitialized_task::initialize_algorithm,
},
};
use fuel_core_services::{
Expand Down Expand Up @@ -305,10 +265,15 @@ mod tests {
l2_block_fullness_threshold_percent: 0,
l2_block_height: 0,
};
let (algo_updater, shared_algo) =
initialize_algorithm(starting_metadata.clone(), &metadata_storage).unwrap();

let service =
GasPriceServiceV0::new(l2_block_source, metadata_storage, starting_metadata)
.unwrap();
let service = GasPriceServiceV0::new(
l2_block_source,
metadata_storage,
shared_algo,
algo_updater,
);
let read_algo = service.next_block_algorithm();
let service = ServiceRunner::new(service);
let prev = read_algo.next_gas_price().await;
Expand Down
39 changes: 27 additions & 12 deletions crates/services/gas_price_service/src/v0/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
v0::{
metadata::V0Metadata,
service::GasPriceServiceV0,
uninitialized_task::initialize_algorithm,
},
};
use anyhow::anyhow;
Expand Down Expand Up @@ -129,10 +130,14 @@ async fn next_gas_price__affected_by_new_l2_block() {
let metadata_storage = FakeMetadata::empty();

let starting_metadata = arb_metadata();
let service =
GasPriceServiceV0::new(l2_block_source, metadata_storage, starting_metadata)
.unwrap();

let (algo_updater, shared_algo) =
initialize_algorithm(starting_metadata.clone(), &metadata_storage).unwrap();
let service = GasPriceServiceV0::new(
l2_block_source,
metadata_storage,
shared_algo,
algo_updater,
);
let service = ServiceRunner::new(service);
let shared = service.shared.clone();
let initial = shared.next_gas_price().await;
Expand Down Expand Up @@ -166,10 +171,15 @@ async fn next__new_l2_block_saves_old_metadata() {
};

let starting_metadata = arb_metadata();
let (algo_updater, shared_algo) =
initialize_algorithm(starting_metadata.clone(), &metadata_storage).unwrap();

let service =
GasPriceServiceV0::new(l2_block_source, metadata_storage, starting_metadata)
.unwrap();
let service = GasPriceServiceV0::new(
l2_block_source,
metadata_storage,
shared_algo,
algo_updater,
);

// when
let service = ServiceRunner::new(service);
Expand Down Expand Up @@ -205,10 +215,16 @@ async fn new__if_exists_already_reload_old_values_with_overrides() {
min_exec_gas_price: new_min_exec_gas_price,
l2_block_height: original.l2_block_height().into(),
};
let (algo_updater, shared_algo) =
initialize_algorithm(new_metadata, &metadata_storage).unwrap();

// when
let service =
GasPriceServiceV0::new(l2_block_source, metadata_storage, new_metadata).unwrap();
let service = GasPriceServiceV0::new(
l2_block_source,
metadata_storage,
shared_algo,
algo_updater,
);

// then
let expected = original;
Expand All @@ -217,14 +233,13 @@ async fn new__if_exists_already_reload_old_values_with_overrides() {
}

#[tokio::test]
async fn new__if_couldnt_fetch_metadata_should_fail() {
async fn initialize_algorithm__should_fail_if_cannot_fetch_metadata() {
// given
let metadata_storage = ErroringMetadata;
let l2_block_source = PendingL2BlockSource;

// when
let metadata = different_arb_metadata();
let res = GasPriceServiceV0::new(l2_block_source, metadata_storage, metadata);
let res = initialize_algorithm(metadata, &metadata_storage);

// then
assert!(matches!(res, Err(GasPriceError::CouldNotInitUpdater(_))));
Expand Down
Loading
Loading