Skip to content

Commit

Permalink
chore(gas_price_service): initialize v1 metadata (#2288)
Browse files Browse the repository at this point in the history
> [!NOTE]
> This is PR 1/n in introducing `GasPriceServiceV1` which links to `v1`
of the `gas-price-algorithm`. This PR is mainly used to start the
discussion around v1 metadata and the fields we need.


## Linked Issues/PRs
<!-- List of related issues/PRs -->
fixes #2286

## Description
<!-- List of detailed changes -->
- [x] Specifies a `V1Metadata` that will be stored along `V0Metadata`
wrapped within the `UpdaterMetadata` struct.
- [x] We use fallible conversions between the two, `v0` => `v1` should
be possible
- [x] Constructor for `AlgorithmUpdaterV1` from `V1Metadata`

Bonus:
- Also fixed the tests that were testing `initialize_algorithm` to test
the `UninitializedTask` instead--a public interface.

## 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
- [ ] 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: Mitchell Turner <[email protected]>
  • Loading branch information
rymnc and MitchTurner authored Oct 30, 2024
1 parent aaee83a commit 7605a2a
Show file tree
Hide file tree
Showing 16 changed files with 521 additions and 221 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed
- [2304](https://github.com/FuelLabs/fuel-core/pull/2304): Add initialization for the genesis base asset contract.

### Added
- [2288](https://github.com/FuelLabs/fuel-core/pull/2288): Specify `V1Metadata` for `GasPriceServiceV1`.

## [Version 0.37.0]

### Added
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/fuel-core/src/service/adapters/gas_price_adapters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ impl GasPriceData for Database<GasPriceDatabase> {

impl From<Config> for GasPriceServiceConfig {
fn from(value: Config) -> Self {
GasPriceServiceConfig::new(
value.min_gas_price,
GasPriceServiceConfig::new_v0(
value.starting_gas_price,
value.min_gas_price,
value.gas_price_change_percent,
value.gas_price_threshold_percent,
)
Expand Down
3 changes: 3 additions & 0 deletions crates/fuel-core/src/service/sub_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ use fuel_core_poa::{
};
use fuel_core_storage::{
self,
structured_storage::StructuredStorage,
transactional::AtomicView,
};
#[cfg(feature = "relayer")]
Expand Down Expand Up @@ -181,13 +182,15 @@ pub fn init_sub_services(
let genesis_block_height = *genesis_block.header().height();
let settings = consensus_parameters_provider.clone();
let block_stream = importer_adapter.events_shared_result();
let metadata = StructuredStorage::new(database.gas_price().clone());

let gas_price_service_v0 = new_gas_price_service_v0(
config.clone().into(),
genesis_block_height,
settings,
block_stream,
database.gas_price().clone(),
metadata,
database.on_chain().clone(),
)?;

Expand Down
3 changes: 2 additions & 1 deletion crates/fuel-gas-price-algorithm/src/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ impl AlgorithmUpdaterV0 {
l2_block_height: u32,
l2_block_fullness_threshold_percent: u64,
) -> Self {
let new_exec_price_corrected = max(new_exec_price, min_exec_gas_price);
Self {
new_exec_price,
new_exec_price: new_exec_price_corrected,
min_exec_gas_price,
exec_gas_price_change_percent,
l2_block_height,
Expand Down
43 changes: 40 additions & 3 deletions crates/services/gas_price_service/src/common/updater_metadata.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
use crate::v0::metadata::V0Metadata;
use crate::{
common::utils::Error,
v0::metadata::V0Metadata,
v1::metadata::V1Metadata,
};
use fuel_core_types::fuel_types::BlockHeight;
use fuel_gas_price_algorithm::v0::AlgorithmUpdaterV0;
use fuel_gas_price_algorithm::{
v0::AlgorithmUpdaterV0,
v1::AlgorithmUpdaterV1,
};

#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq)]
pub enum UpdaterMetadata {
V0(V0Metadata),
V1(V1Metadata),
}

impl UpdaterMetadata {
pub fn l2_block_height(&self) -> BlockHeight {
match self {
UpdaterMetadata::V0(v1) => v1.l2_block_height.into(),
UpdaterMetadata::V0(v0) => v0.l2_block_height.into(),
UpdaterMetadata::V1(v1) => v1.l2_block_height.into(),
}
}
}
Expand All @@ -20,3 +29,31 @@ impl From<AlgorithmUpdaterV0> for UpdaterMetadata {
Self::V0(updater.into())
}
}

impl From<AlgorithmUpdaterV1> for UpdaterMetadata {
fn from(updater: AlgorithmUpdaterV1) -> Self {
Self::V1(updater.into())
}
}

impl TryFrom<UpdaterMetadata> for V0Metadata {
type Error = Error;

fn try_from(metadata: UpdaterMetadata) -> Result<Self, Self::Error> {
match metadata {
UpdaterMetadata::V0(v0) => Ok(v0),
_ => Err(Error::CouldNotConvertMetadata),
}
}
}

impl TryFrom<UpdaterMetadata> for V1Metadata {
type Error = Error;

fn try_from(metadata: UpdaterMetadata) -> Result<Self, Self::Error> {
match metadata {
UpdaterMetadata::V1(v1) => Ok(v1),
_ => Err(Error::CouldNotConvertMetadata),
}
}
}
2 changes: 2 additions & 0 deletions crates/services/gas_price_service/src/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub enum Error {
},
#[error("Failed to initialize updater: {0:?}")]
CouldNotInitUpdater(anyhow::Error),
#[error("Failed to convert metadata to concrete type. THere is no migration path for this metadata version")]
CouldNotConvertMetadata, // todo(https://github.com/FuelLabs/fuel-core/issues/2286)
}

pub type Result<T, E = Error> = core::result::Result<T, E>;
Expand Down
60 changes: 48 additions & 12 deletions crates/services/gas_price_service/src/ports.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::common::{
updater_metadata::UpdaterMetadata,
utils::Result,
use crate::{
common::{
updater_metadata::UpdaterMetadata,
utils::Result,
},
v0::metadata::V0AlgorithmConfig,
v1::metadata::V1AlgorithmConfig,
};
use fuel_core_storage::Result as StorageResult;
use fuel_core_types::{
Expand Down Expand Up @@ -30,25 +34,57 @@ pub trait GasPriceData: Send + Sync {
fn latest_height(&self) -> Option<BlockHeight>;
}

pub struct GasPriceServiceConfig {
pub min_gas_price: u64,
pub starting_gas_price: u64,
pub gas_price_change_percent: u64,
pub gas_price_threshold_percent: u64,
pub enum GasPriceServiceConfig {
V0(V0AlgorithmConfig),
V1(V1AlgorithmConfig),
}

impl GasPriceServiceConfig {
pub fn new(
min_gas_price: u64,
pub fn new_v0(
starting_gas_price: u64,
min_gas_price: u64,
gas_price_change_percent: u64,
gas_price_threshold_percent: u64,
) -> Self {
Self {
min_gas_price,
Self::V0(V0AlgorithmConfig {
starting_gas_price,
min_gas_price,
gas_price_change_percent,
gas_price_threshold_percent,
})
}

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

/// Extract V0AlgorithmConfig if it is of V0 version
pub fn v0(self) -> Option<V0AlgorithmConfig> {
if let GasPriceServiceConfig::V0(v0) = self {
Some(v0)
} else {
None
}
}

/// Extract V1AlgorithmConfig if it is of V1 version
pub fn v1(self) -> Option<V1AlgorithmConfig> {
if let GasPriceServiceConfig::V1(v1) = self {
Some(v1)
} else {
None
}
}
}

impl From<V0AlgorithmConfig> for GasPriceServiceConfig {
fn from(value: V0AlgorithmConfig) -> Self {
GasPriceServiceConfig::V0(value)
}
}

impl From<V1AlgorithmConfig> for GasPriceServiceConfig {
fn from(value: V1AlgorithmConfig) -> Self {
GasPriceServiceConfig::V1(value)
}
}
7 changes: 6 additions & 1 deletion crates/services/gas_price_service/src/v0/algorithm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::common::gas_price_algorithm::GasPriceAlgorithm;
use crate::common::gas_price_algorithm::{
GasPriceAlgorithm,
SharedGasPriceAlgo,
};
use fuel_core_types::fuel_types::BlockHeight;
use fuel_gas_price_algorithm::v0::AlgorithmV0;

Expand All @@ -11,3 +14,5 @@ impl GasPriceAlgorithm for AlgorithmV0 {
self.worst_case(block_height.into())
}
}

pub type SharedV0Algorithm = SharedGasPriceAlgo<AlgorithmV0>;
29 changes: 5 additions & 24 deletions crates/services/gas_price_service/src/v0/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,22 @@ use fuel_gas_price_algorithm::v0::AlgorithmUpdaterV0;
pub struct V0Metadata {
/// The gas price to cover the execution of the next block
pub new_exec_price: u64,
// Execution
/// The lowest the algorithm allows the exec gas price to go
pub min_exec_gas_price: u64,
/// The Percentage the execution gas price will change in a single block, either increase or decrease
/// based on the fullness of the last L2 block
pub exec_gas_price_change_percent: u64,
/// The height for which the `new_exec_price` is calculated, which should be the _next_ block
pub l2_block_height: u32,
/// The threshold of gas usage above and below which the gas price will increase or decrease
/// This is a percentage of the total capacity of the L2 block
pub l2_block_fullness_threshold_percent: u64,
}

impl From<V0Metadata> for AlgorithmUpdaterV0 {
fn from(metadata: V0Metadata) -> Self {
Self {
new_exec_price: metadata.new_exec_price,
min_exec_gas_price: metadata.min_exec_gas_price,
exec_gas_price_change_percent: metadata.exec_gas_price_change_percent,
l2_block_height: metadata.l2_block_height,
l2_block_fullness_threshold_percent: metadata
.l2_block_fullness_threshold_percent,
}
}
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 From<AlgorithmUpdaterV0> for V0Metadata {
fn from(updater: AlgorithmUpdaterV0) -> Self {
Self {
new_exec_price: updater.new_exec_price,
min_exec_gas_price: updater.min_exec_gas_price,
exec_gas_price_change_percent: updater.exec_gas_price_change_percent,
l2_block_height: updater.l2_block_height,
l2_block_fullness_threshold_percent: updater
.l2_block_fullness_threshold_percent,
}
}
}
24 changes: 11 additions & 13 deletions crates/services/gas_price_service/src/v0/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
utils::BlockInfo,
},
ports::MetadataStorage,
v0::uninitialized_task::SharedV0Algorithm,
v0::algorithm::SharedV0Algorithm,
};
use anyhow::anyhow;
use async_trait::async_trait;
Expand Down Expand Up @@ -165,12 +165,10 @@ mod tests {
},
ports::MetadataStorage,
v0::{
metadata::V0Metadata,
algorithm::SharedV0Algorithm,
metadata::V0AlgorithmConfig,
service::GasPriceServiceV0,
uninitialized_task::{
initialize_algorithm,
SharedV0Algorithm,
},
uninitialized_task::initialize_algorithm,
},
};
use fuel_core_services::{
Expand Down Expand Up @@ -262,15 +260,15 @@ mod tests {
l2_block: l2_block_receiver,
};
let metadata_storage = FakeMetadata::empty();
let starting_metadata = V0Metadata {
min_exec_gas_price: 10,
exec_gas_price_change_percent: 10,
new_exec_price: 100,
l2_block_fullness_threshold_percent: 0,
l2_block_height: 0,
let l2_block_height = 0;
let config = V0AlgorithmConfig {
starting_gas_price: 100,
min_gas_price: 10,
gas_price_change_percent: 10,
gas_price_threshold_percent: 0,
};
let (algo_updater, shared_algo) =
initialize_algorithm(starting_metadata.clone(), &metadata_storage).unwrap();
initialize_algorithm(&config, l2_block_height, &metadata_storage).unwrap();

let service = GasPriceServiceV0::new(
l2_block_source,
Expand Down
Loading

0 comments on commit 7605a2a

Please sign in to comment.