Skip to content

Commit

Permalink
fix: make async
Browse files Browse the repository at this point in the history
  • Loading branch information
rymnc committed Sep 30, 2024
1 parent fb926c4 commit 9d17cfa
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 85 deletions.
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

0 comments on commit 9d17cfa

Please sign in to comment.