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): initialize v1 metadata #2288

Merged
merged 29 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
8297189
chore(gas_price_service): initialize v1 metadata
rymnc Oct 4, 2024
1b7c91e
chore: changelog
rymnc Oct 4, 2024
56731ca
fix: broken test
rymnc Oct 4, 2024
298de5f
fix: move things around, introduce versioned configuration
rymnc Oct 4, 2024
6d1a673
fix: cyclic dep between service and uninit task
rymnc Oct 7, 2024
c980dfe
Merge branch 'master' into chore/gas-price-service-v1-metadata
rymnc Oct 7, 2024
c04f2c5
Merge branch 'master' into chore/gas-price-service-v1-metadata
MitchTurner Oct 24, 2024
d22c9bb
Fix compilation
MitchTurner Oct 24, 2024
48718ad
Remove unused fields in metadata
MitchTurner Oct 24, 2024
67ccb14
Merge branch 'master' into chore/gas-price-service-v1-metadata
MitchTurner Oct 24, 2024
9e8750f
Fix compilation more, add new field to v1 updater
MitchTurner Oct 24, 2024
5f96e64
remove unneeded fields from v1 metadata because we can get them from …
MitchTurner Oct 24, 2024
e3af4c4
Remove commented code
MitchTurner Oct 24, 2024
d530dd0
Remove more commented code
MitchTurner Oct 24, 2024
30e5bfd
Use uninitialized task instead of the helper function in test
MitchTurner Oct 24, 2024
8a5ab27
Fix the other one
MitchTurner Oct 24, 2024
75b68c3
Fix integration compilation
MitchTurner Oct 25, 2024
4419e4d
Create function for converting old metadata into new metadata
MitchTurner Oct 25, 2024
21ea106
Create function for converting metadata into updater
MitchTurner Oct 25, 2024
3266a17
Add missing fields to config and metadata, include in conversions
MitchTurner Oct 25, 2024
daad75e
Fix test name
MitchTurner Oct 25, 2024
5689168
Merge branch 'master' into chore/gas-price-service-v1-metadata
MitchTurner Oct 28, 2024
eba0dd0
Fix one test
MitchTurner Oct 29, 2024
c57c0ba
Fix the other test and hopefully not break any others
MitchTurner Oct 29, 2024
b11b09c
Lint a little
MitchTurner Oct 29, 2024
10b00f6
Remove redundant type, simplify constructors, other PR suggestions
MitchTurner Oct 29, 2024
068789e
Clarify comment
MitchTurner Oct 29, 2024
5f347ab
Merge branch 'master' into chore/gas-price-service-v1-metadata
MitchTurner Oct 29, 2024
f00b73c
Use `NonZeroU64` in config
MitchTurner Oct 29, 2024
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
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
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)
}

Comment on lines +57 to +59
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I'd complement these constructors with with From<VXAlgorithmConfig> implementations.

/// 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
Loading