Skip to content

Commit

Permalink
chore: add test for gas price service
Browse files Browse the repository at this point in the history
  • Loading branch information
rymnc committed Dec 30, 2024
1 parent dba77da commit 3b60422
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl<Storage> GasPriceServiceAtomicStorage for Storage
where
Storage: 'static,
Storage: GetMetadataStorage + GetLatestRecordedHeight,
Storage: KeyValueInspect<Column = GasPriceColumn> + Modifiable + Send + Sync,
Storage: KeyValueInspect<Column = GasPriceColumn> + Modifiable + Send + Sync + Clone,
{
type Transaction<'a> = StorageTransaction<&'a mut Storage> where Self: 'a;

Expand Down
2 changes: 1 addition & 1 deletion crates/services/gas_price_service/src/ports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub trait GetLatestRecordedHeight: Send + Sync {
fn get_recorded_height(&self) -> Result<Option<BlockHeight>>;
}

pub trait GasPriceServiceAtomicStorage
pub trait GasPriceServiceAtomicStorage: Clone
where
Self: 'static,
Self: Send + Sync,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ where
/// This function polls the source according to a polling interval
/// described by the DaBlockCostsService
async fn run(&mut self, state_watcher: &mut StateWatcher) -> TaskNextAction {
tracing::debug!("111111111111111111111111111111111");
tokio::select! {
biased;
_ = state_watcher.while_started() => {
Expand Down
77 changes: 77 additions & 0 deletions crates/services/gas_price_service/src/v1/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ where
self.apply_block_info_to_gas_algorithm(block).await?;
Ok(())
}

fn storage(&self) -> AtomicStorage {
self.storage_tx_provider.clone()
}
}

impl<L2, DA, AtomicStorage> GasPriceServiceV1<L2, DA, AtomicStorage>
Expand Down Expand Up @@ -389,6 +393,7 @@ mod tests {
},
},
ports::{
GetLatestRecordedHeight,
GetMetadataStorage,
SetMetadataStorage,
},
Expand Down Expand Up @@ -801,4 +806,76 @@ mod tests {

service.shutdown().await.unwrap();
}

#[tokio::test]
async fn run__stores_l2_height_as_recorded_if_previous_doesnt_exist() {
// given
let block_height = 1;
let l2_block = BlockInfo::Block {
height: block_height,
gas_used: 60,
block_gas_capacity: 100,
block_bytes: 100,
block_fees: 100,
};

let (l2_block_sender, l2_block_receiver) = mpsc::channel(1);
let l2_block_source = FakeL2BlockSource {
l2_block: l2_block_receiver,
};

let metadata_storage = FakeMetadata::empty();
let l2_block_height = 0;
let config = V1AlgorithmConfig {
new_exec_gas_price: 100,
min_exec_gas_price: 50,
exec_gas_price_change_percent: 20,
l2_block_fullness_threshold_percent: 20,
gas_price_factor: NonZeroU64::new(10).unwrap(),
min_da_gas_price: 10,
max_da_gas_price_change_percent: 20,
da_p_component: 4,
da_d_component: 2,
normal_range_size: 10,
capped_range_size: 100,
decrease_range_size: 4,
block_activity_threshold: 20,
da_poll_interval: None,
};
let inner = database();
let (algo_updater, shared_algo) =
initialize_algorithm(&config, l2_block_height, &metadata_storage).unwrap();

let notifier = Arc::new(tokio::sync::Notify::new());
let dummy_da_source = DaSourceService::new(
DummyDaBlockCosts::new(
Err(anyhow::anyhow!("unused at the moment")),
notifier.clone(),
),
None,
);
let da_service_runner = ServiceRunner::new(dummy_da_source);
da_service_runner.start_and_await().await.unwrap();

let mut service = GasPriceServiceV1::new(
l2_block_source,
shared_algo,
algo_updater,
da_service_runner,
inner,
);
let read_algo = service.next_block_algorithm();
let mut watcher = StateWatcher::started();
let initial_price = read_algo.next_gas_price();

// when
l2_block_sender.send(l2_block).await.unwrap();
service.run(&mut watcher).await;
let storage_provider = service.storage_tx_provider().clone();
service.shutdown().await.unwrap();

// then
let recorded_height = storage_provider.get_recorded_height().unwrap().unwrap();
assert_eq!(recorded_height, BlockHeight::from(block_height));
}
}
1 change: 1 addition & 0 deletions crates/services/gas_price_service/src/v1/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ impl GetMetadataStorage for FakeMetadata {
}
}

#[derive(Clone)]
struct ErroringPersistedData;

impl SetMetadataStorage for ErroringPersistedData {
Expand Down

0 comments on commit 3b60422

Please sign in to comment.