Skip to content

Commit 262cbd7

Browse files
chore(gas_price_service): initialize v1 metadata (#2288)
> [!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 FuelLabs/fuel-core#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]>
1 parent 9026f3d commit 262cbd7

File tree

16 files changed

+521
-221
lines changed

16 files changed

+521
-221
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
8686
### Fixed
8787
- [2304](https://github.com/FuelLabs/fuel-core/pull/2304): Add initialization for the genesis base asset contract.
8888

89+
### Added
90+
- [2288](https://github.com/FuelLabs/fuel-core/pull/2288): Specify `V1Metadata` for `GasPriceServiceV1`.
91+
8992
## [Version 0.37.0]
9093

9194
### Added

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/fuel-core/src/service/adapters/gas_price_adapters.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ impl GasPriceData for Database<GasPriceDatabase> {
6464

6565
impl From<Config> for GasPriceServiceConfig {
6666
fn from(value: Config) -> Self {
67-
GasPriceServiceConfig::new(
68-
value.min_gas_price,
67+
GasPriceServiceConfig::new_v0(
6968
value.starting_gas_price,
69+
value.min_gas_price,
7070
value.gas_price_change_percent,
7171
value.gas_price_threshold_percent,
7272
)

crates/fuel-core/src/service/sub_services.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ use fuel_core_poa::{
4848
};
4949
use fuel_core_storage::{
5050
self,
51+
structured_storage::StructuredStorage,
5152
transactional::AtomicView,
5253
};
5354
#[cfg(feature = "relayer")]
@@ -181,13 +182,15 @@ pub fn init_sub_services(
181182
let genesis_block_height = *genesis_block.header().height();
182183
let settings = consensus_parameters_provider.clone();
183184
let block_stream = importer_adapter.events_shared_result();
185+
let metadata = StructuredStorage::new(database.gas_price().clone());
184186

185187
let gas_price_service_v0 = new_gas_price_service_v0(
186188
config.clone().into(),
187189
genesis_block_height,
188190
settings,
189191
block_stream,
190192
database.gas_price().clone(),
193+
metadata,
191194
database.on_chain().clone(),
192195
)?;
193196

crates/fuel-gas-price-algorithm/src/v0.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ impl AlgorithmUpdaterV0 {
7474
l2_block_height: u32,
7575
l2_block_fullness_threshold_percent: u64,
7676
) -> Self {
77+
let new_exec_price_corrected = max(new_exec_price, min_exec_gas_price);
7778
Self {
78-
new_exec_price,
79+
new_exec_price: new_exec_price_corrected,
7980
min_exec_gas_price,
8081
exec_gas_price_change_percent,
8182
l2_block_height,
Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1-
use crate::v0::metadata::V0Metadata;
1+
use crate::{
2+
common::utils::Error,
3+
v0::metadata::V0Metadata,
4+
v1::metadata::V1Metadata,
5+
};
26
use fuel_core_types::fuel_types::BlockHeight;
3-
use fuel_gas_price_algorithm::v0::AlgorithmUpdaterV0;
7+
use fuel_gas_price_algorithm::{
8+
v0::AlgorithmUpdaterV0,
9+
v1::AlgorithmUpdaterV1,
10+
};
411

512
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq)]
613
pub enum UpdaterMetadata {
714
V0(V0Metadata),
15+
V1(V1Metadata),
816
}
917

1018
impl UpdaterMetadata {
1119
pub fn l2_block_height(&self) -> BlockHeight {
1220
match self {
13-
UpdaterMetadata::V0(v1) => v1.l2_block_height.into(),
21+
UpdaterMetadata::V0(v0) => v0.l2_block_height.into(),
22+
UpdaterMetadata::V1(v1) => v1.l2_block_height.into(),
1423
}
1524
}
1625
}
@@ -20,3 +29,31 @@ impl From<AlgorithmUpdaterV0> for UpdaterMetadata {
2029
Self::V0(updater.into())
2130
}
2231
}
32+
33+
impl From<AlgorithmUpdaterV1> for UpdaterMetadata {
34+
fn from(updater: AlgorithmUpdaterV1) -> Self {
35+
Self::V1(updater.into())
36+
}
37+
}
38+
39+
impl TryFrom<UpdaterMetadata> for V0Metadata {
40+
type Error = Error;
41+
42+
fn try_from(metadata: UpdaterMetadata) -> Result<Self, Self::Error> {
43+
match metadata {
44+
UpdaterMetadata::V0(v0) => Ok(v0),
45+
_ => Err(Error::CouldNotConvertMetadata),
46+
}
47+
}
48+
}
49+
50+
impl TryFrom<UpdaterMetadata> for V1Metadata {
51+
type Error = Error;
52+
53+
fn try_from(metadata: UpdaterMetadata) -> Result<Self, Self::Error> {
54+
match metadata {
55+
UpdaterMetadata::V1(v1) => Ok(v1),
56+
_ => Err(Error::CouldNotConvertMetadata),
57+
}
58+
}
59+
}

crates/services/gas_price_service/src/common/utils.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ pub enum Error {
1717
},
1818
#[error("Failed to initialize updater: {0:?}")]
1919
CouldNotInitUpdater(anyhow::Error),
20+
#[error("Failed to convert metadata to concrete type. THere is no migration path for this metadata version")]
21+
CouldNotConvertMetadata, // todo(https://github.com/FuelLabs/fuel-core/issues/2286)
2022
}
2123

2224
pub type Result<T, E = Error> = core::result::Result<T, E>;

crates/services/gas_price_service/src/ports.rs

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
use crate::common::{
2-
updater_metadata::UpdaterMetadata,
3-
utils::Result,
1+
use crate::{
2+
common::{
3+
updater_metadata::UpdaterMetadata,
4+
utils::Result,
5+
},
6+
v0::metadata::V0AlgorithmConfig,
7+
v1::metadata::V1AlgorithmConfig,
48
};
59
use fuel_core_storage::Result as StorageResult;
610
use fuel_core_types::{
@@ -30,25 +34,57 @@ pub trait GasPriceData: Send + Sync {
3034
fn latest_height(&self) -> Option<BlockHeight>;
3135
}
3236

33-
pub struct GasPriceServiceConfig {
34-
pub min_gas_price: u64,
35-
pub starting_gas_price: u64,
36-
pub gas_price_change_percent: u64,
37-
pub gas_price_threshold_percent: u64,
37+
pub enum GasPriceServiceConfig {
38+
V0(V0AlgorithmConfig),
39+
V1(V1AlgorithmConfig),
3840
}
3941

4042
impl GasPriceServiceConfig {
41-
pub fn new(
42-
min_gas_price: u64,
43+
pub fn new_v0(
4344
starting_gas_price: u64,
45+
min_gas_price: u64,
4446
gas_price_change_percent: u64,
4547
gas_price_threshold_percent: u64,
4648
) -> Self {
47-
Self {
48-
min_gas_price,
49+
Self::V0(V0AlgorithmConfig {
4950
starting_gas_price,
51+
min_gas_price,
5052
gas_price_change_percent,
5153
gas_price_threshold_percent,
54+
})
55+
}
56+
57+
pub fn new_v1(metadata: V1AlgorithmConfig) -> Self {
58+
Self::V1(metadata)
59+
}
60+
61+
/// Extract V0AlgorithmConfig if it is of V0 version
62+
pub fn v0(self) -> Option<V0AlgorithmConfig> {
63+
if let GasPriceServiceConfig::V0(v0) = self {
64+
Some(v0)
65+
} else {
66+
None
5267
}
5368
}
69+
70+
/// Extract V1AlgorithmConfig if it is of V1 version
71+
pub fn v1(self) -> Option<V1AlgorithmConfig> {
72+
if let GasPriceServiceConfig::V1(v1) = self {
73+
Some(v1)
74+
} else {
75+
None
76+
}
77+
}
78+
}
79+
80+
impl From<V0AlgorithmConfig> for GasPriceServiceConfig {
81+
fn from(value: V0AlgorithmConfig) -> Self {
82+
GasPriceServiceConfig::V0(value)
83+
}
84+
}
85+
86+
impl From<V1AlgorithmConfig> for GasPriceServiceConfig {
87+
fn from(value: V1AlgorithmConfig) -> Self {
88+
GasPriceServiceConfig::V1(value)
89+
}
5490
}

crates/services/gas_price_service/src/v0/algorithm.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use crate::common::gas_price_algorithm::GasPriceAlgorithm;
1+
use crate::common::gas_price_algorithm::{
2+
GasPriceAlgorithm,
3+
SharedGasPriceAlgo,
4+
};
25
use fuel_core_types::fuel_types::BlockHeight;
36
use fuel_gas_price_algorithm::v0::AlgorithmV0;
47

@@ -11,3 +14,5 @@ impl GasPriceAlgorithm for AlgorithmV0 {
1114
self.worst_case(block_height.into())
1215
}
1316
}
17+
18+
pub type SharedV0Algorithm = SharedGasPriceAlgo<AlgorithmV0>;

crates/services/gas_price_service/src/v0/metadata.rs

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,22 @@ use fuel_gas_price_algorithm::v0::AlgorithmUpdaterV0;
44
pub struct V0Metadata {
55
/// The gas price to cover the execution of the next block
66
pub new_exec_price: u64,
7-
// Execution
8-
/// The lowest the algorithm allows the exec gas price to go
9-
pub min_exec_gas_price: u64,
10-
/// The Percentage the execution gas price will change in a single block, either increase or decrease
11-
/// based on the fullness of the last L2 block
12-
pub exec_gas_price_change_percent: u64,
137
/// The height for which the `new_exec_price` is calculated, which should be the _next_ block
148
pub l2_block_height: u32,
15-
/// The threshold of gas usage above and below which the gas price will increase or decrease
16-
/// This is a percentage of the total capacity of the L2 block
17-
pub l2_block_fullness_threshold_percent: u64,
189
}
1910

20-
impl From<V0Metadata> for AlgorithmUpdaterV0 {
21-
fn from(metadata: V0Metadata) -> Self {
22-
Self {
23-
new_exec_price: metadata.new_exec_price,
24-
min_exec_gas_price: metadata.min_exec_gas_price,
25-
exec_gas_price_change_percent: metadata.exec_gas_price_change_percent,
26-
l2_block_height: metadata.l2_block_height,
27-
l2_block_fullness_threshold_percent: metadata
28-
.l2_block_fullness_threshold_percent,
29-
}
30-
}
11+
pub struct V0AlgorithmConfig {
12+
pub starting_gas_price: u64,
13+
pub min_gas_price: u64,
14+
pub gas_price_change_percent: u64,
15+
pub gas_price_threshold_percent: u64,
3116
}
3217

3318
impl From<AlgorithmUpdaterV0> for V0Metadata {
3419
fn from(updater: AlgorithmUpdaterV0) -> Self {
3520
Self {
3621
new_exec_price: updater.new_exec_price,
37-
min_exec_gas_price: updater.min_exec_gas_price,
38-
exec_gas_price_change_percent: updater.exec_gas_price_change_percent,
3922
l2_block_height: updater.l2_block_height,
40-
l2_block_fullness_threshold_percent: updater
41-
.l2_block_fullness_threshold_percent,
4223
}
4324
}
4425
}

0 commit comments

Comments
 (0)