Skip to content

Commit

Permalink
Create uninitialized task for v1 gas price service (#2442)
Browse files Browse the repository at this point in the history
## Linked Issues/PRs
part of #2140

## Description
Adding the `UninitializedTask` with minimum test coverage.

Because of how we are organizing
#2415, I don't believe we need
to do any syncing of the DA Cost service before startup. If it's
"behind" somehow, then the old values it provides will just be ignored.
It might result in a little error in the profit value early on, but it
will get washed out pretty quickly.

## Checklist
- [x] Breaking changes are clearly marked as such in the PR description
and changelog
- [x] New behavior is reflected in tests
- [ ] [The specification](https://github.com/FuelLabs/fuel-specs/)
matches the implemented behavior (link update PR if changes are needed)

### Before requesting review
- [x] I have reviewed the code myself
- [x] I have created follow-up issues caused by this PR and linked them
here

### After merging, notify other teams

[Add or remove entries as needed]

- [ ] [Rust SDK](https://github.com/FuelLabs/fuels-rs/)
- [ ] [Sway compiler](https://github.com/FuelLabs/sway/)
- [ ] [Platform
documentation](https://github.com/FuelLabs/devrel-requests/issues/new?assignees=&labels=new+request&projects=&template=NEW-REQUEST.yml&title=%5BRequest%5D%3A+)
(for out-of-organization contributors, the person merging the PR will do
this)
- [ ] Someone else?

---------

Co-authored-by: Rafał Chabowski <[email protected]>
Co-authored-by: Green Baneling <[email protected]>
  • Loading branch information
3 people authored Dec 6, 2024
1 parent 95dfeb3 commit e1ff8d4
Show file tree
Hide file tree
Showing 10 changed files with 873 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased]

### Added
- [2442](https://github.com/FuelLabs/fuel-core/pull/2442): Add uninitialized task for V1 gas price service
- [2154](https://github.com/FuelLabs/fuel-core/pull/2154): Added `Unknown` variant to `ConsensusParameters` graphql queries
- [2154](https://github.com/FuelLabs/fuel-core/pull/2154): Added `Unknown` variant to `Block` graphql queries
- [2154](https://github.com/FuelLabs/fuel-core/pull/2154): Added `TransactionType` type in `fuel-client`
Expand Down
2 changes: 1 addition & 1 deletion crates/fuel-gas-price-algorithm/src/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub enum Error {
#[error("Could not calculate cost per byte: {bytes:?} bytes, {cost:?} cost")]
CouldNotCalculateCostPerByte { bytes: u128, cost: u128 },
#[error("Failed to include L2 block data: {0}")]
FailedTooIncludeL2BlockData(String),
FailedToIncludeL2BlockData(String),
#[error("L2 block expected but not found in unrecorded blocks: {height}")]
L2BlockExpectedNotFound { height: u32 },
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub fn get_block_info(
Ok(info)
}

fn mint_values(block: &Block<Transaction>) -> GasPriceResult<(u64, u64)> {
pub(crate) fn mint_values(block: &Block<Transaction>) -> GasPriceResult<(u64, u64)> {
let mint = block
.transactions()
.last()
Expand All @@ -121,6 +121,13 @@ fn mint_values(block: &Block<Transaction>) -> GasPriceResult<(u64, u64)> {
})?;
Ok((*mint.mint_amount(), *mint.gas_price()))
}

// TODO: Don't take a direct dependency on `Postcard` as it's not guaranteed to be the encoding format
// https://github.com/FuelLabs/fuel-core/issues/2443
pub(crate) fn block_bytes(block: &Block<Transaction>) -> u64 {
Postcard::encode(block).len() as u64
}

fn block_used_gas(
fee: u64,
gas_price: u64,
Expand Down
2 changes: 2 additions & 0 deletions crates/services/gas_price_service/src/v0/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,15 @@ where
Metadata: MetadataStorage,
{
async fn run(&mut self, watcher: &mut StateWatcher) -> TaskNextAction {
tracing::trace!("Call of `run` function of the gas price service v0");
tokio::select! {
biased;
_ = watcher.while_started() => {
tracing::debug!("Stopping gas price service");
TaskNextAction::Stop
}
l2_block_res = self.l2_block_source.get_l2_block() => {
tracing::debug!("Received L2 block");
let res = self.process_l2_block_res(l2_block_res).await;
TaskNextAction::always_continue(res)
}
Expand Down
22 changes: 11 additions & 11 deletions crates/services/gas_price_service/src/v0/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,16 @@ async fn next_gas_price__affected_by_new_l2_block() {
algo_updater,
);

tracing::debug!("service created");

let read_algo = service.next_block_algorithm();
let initial = read_algo.next_gas_price();
let mut watcher = StateWatcher::default();
let mut watcher = StateWatcher::started();
tokio::spawn(async move { service.run(&mut watcher).await });

// when
service.run(&mut watcher).await;
l2_block_sender.send(l2_block).await.unwrap();
service.shutdown().await.unwrap();
tokio::time::sleep(std::time::Duration::from_millis(10)).await;

// then
let new = read_algo.next_gas_price();
Expand Down Expand Up @@ -212,18 +214,16 @@ async fn next__new_l2_block_saves_old_metadata() {
algo_updater,
);

// when
let read_algo = service.next_block_algorithm();
let mut watcher = StateWatcher::default();
let start = read_algo.next_gas_price();
let mut watcher = StateWatcher::started();
tokio::spawn(async move { service.run(&mut watcher).await });

service.run(&mut watcher).await;
// when
l2_block_sender.send(l2_block).await.unwrap();
service.shutdown().await.unwrap();
tokio::time::sleep(std::time::Duration::from_millis(10)).await;

// then
let new = read_algo.next_gas_price();
assert_ne!(start, new);
let metadata_has_been_updated = metadata_inner.lock().unwrap().is_some();
assert!(metadata_has_been_updated);
}

#[derive(Clone)]
Expand Down
4 changes: 4 additions & 0 deletions crates/services/gas_price_service/src/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ pub mod algorithm;
pub mod da_source_service;
pub mod metadata;
pub mod service;

#[cfg(test)]
mod tests;
pub mod uninitialized_task;
6 changes: 5 additions & 1 deletion crates/services/gas_price_service/src/v1/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ impl From<&V1AlgorithmConfig> for AlgorithmUpdaterV1 {
.map(|size| u128::from(*size))
.sum();
Self {
new_scaled_exec_price: value.new_exec_gas_price,
// TODO:We don't need this after we implement
// https://github.com/FuelLabs/fuel-core/issues/2481
new_scaled_exec_price: value
.new_exec_gas_price
.saturating_mul(value.gas_price_factor.get()),
l2_block_height: 0,
new_scaled_da_gas_price: value.min_da_gas_price,
gas_price_factor: value.gas_price_factor,
Expand Down
2 changes: 1 addition & 1 deletion crates/services/gas_price_service/src/v1/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ where
async fn shutdown(mut self) -> anyhow::Result<()> {
// handle all the remaining l2 blocks
while let Some(Ok(block)) = self.l2_block_source.get_l2_block().now_or_never() {
tracing::debug!("Updating gas price algorithm");
tracing::debug!("Updating gas price algorithm before shutdown");
self.apply_block_info_to_gas_algorithm(block).await?;
}

Expand Down
Loading

0 comments on commit e1ff8d4

Please sign in to comment.