Skip to content

Commit

Permalink
Merge branch 'master' into feature/use-min-gas-price-if-starting-gas-…
Browse files Browse the repository at this point in the history
…price-zero
  • Loading branch information
xgreenx authored Aug 9, 2024
2 parents 91923e9 + 6758990 commit 84f3022
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Changed
-[2064](https://github.com/FuelLabs/fuel-core/pull/2064): Allow gas price metadata values to be overridden with config

### Fixes
- [2060](https://github.com/FuelLabs/fuel-core/pull/2060): Use `min-gas-price` as a starting point if `start-gas-price` is zero.
- [2059](https://github.com/FuelLabs/fuel-core/pull/2059): Remove unwrap that is breaking backwards compatibility
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ pub fn get_synced_gas_price_updater(
latest_block_height.into(),
l2_block_source,
metadata_storage,
config.min_gas_price,
config.gas_price_change_percent,
config.gas_price_threshold_percent,
)
.map_err(|e| anyhow::anyhow!("Could not initialize gas price updater: {e:?}"))
}
Expand Down
16 changes: 16 additions & 0 deletions crates/fuel-gas-price-algorithm/src/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ pub struct BlockBytes {
}

impl AlgorithmUpdaterV0 {
pub fn new(
new_exec_price: u64,
min_exec_gas_price: u64,
exec_gas_price_change_percent: u64,
l2_block_height: u32,
l2_block_fullness_threshold_percent: u64,
) -> Self {
Self {
new_exec_price,
min_exec_gas_price,
exec_gas_price_change_percent,
l2_block_height,
l2_block_fullness_threshold_percent,
}
}

pub fn update_l2_block_data(
&mut self,
height: u32,
Expand Down
24 changes: 19 additions & 5 deletions crates/services/gas_price_service/src/fuel_gas_price_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,28 @@ where
target_block_height: BlockHeight,
l2_block_source: L2,
metadata_storage: Metadata,
min_exec_gas_price: u64,
exec_gas_price_change_percent: u64,
l2_block_fullness_threshold_percent: u64,
) -> Result<Self> {
let inner = metadata_storage
.get_metadata(&target_block_height)?
.ok_or(Error::CouldNotInitUpdater(anyhow::anyhow!(
let old_metadata = metadata_storage.get_metadata(&target_block_height)?.ok_or(
Error::CouldNotInitUpdater(anyhow::anyhow!(
"No metadata found for block height: {:?}",
target_block_height
)))?
.into();
)),
)?;
let inner = match old_metadata {
UpdaterMetadata::V0(old) => {
let v0 = AlgorithmUpdaterV0::new(
old.new_exec_price,
min_exec_gas_price,
exec_gas_price_change_percent,
old.l2_block_height,
l2_block_fullness_threshold_percent,
);
AlgorithmUpdater::V0(v0)
}
};
let updater = Self {
inner,
l2_block_source,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,39 @@ async fn next__fetches_l2_block() {
}

#[tokio::test]
async fn init__if_exists_already_reload() {
async fn init__if_exists_already_reload_old_values_with_overrides() {
// given
let metadata = arb_metadata();
let metadata_inner = Arc::new(std::sync::Mutex::new(Some(metadata.clone())));
let original = arb_metadata();
let metadata_inner = Arc::new(std::sync::Mutex::new(Some(original.clone())));
let metadata_storage = FakeMetadata {
inner: metadata_inner,
};
let l2_block_source = PendingL2BlockSource;
let new_min_exec_gas_price = 99;
let new_exec_gas_price_change_percent = 88;
let new_l2_block_fullness_threshold_percent = 77;

// when
let height = metadata.l2_block_height();
let updater =
FuelGasPriceUpdater::init(height, l2_block_source, metadata_storage).unwrap();
let height = original.l2_block_height();
let updater = FuelGasPriceUpdater::init(
height,
l2_block_source,
metadata_storage,
new_min_exec_gas_price,
new_exec_gas_price_change_percent,
new_l2_block_fullness_threshold_percent,
)
.unwrap();

// then
let expected: AlgorithmUpdater = metadata.into();
let UpdaterMetadata::V0(original_inner) = original;
let expected: AlgorithmUpdater = UpdaterMetadata::V0(V0Metadata {
min_exec_gas_price: new_min_exec_gas_price,
exec_gas_price_change_percent: new_exec_gas_price_change_percent,
l2_block_fullness_threshold_percent: new_l2_block_fullness_threshold_percent,
..original_inner
})
.into();
let actual = updater.inner;
assert_eq!(expected, actual);
}
Expand All @@ -137,7 +154,14 @@ async fn init__if_it_does_not_exist_fail() {
// when
let metadata = different_arb_metadata();
let height = u32::from(metadata.l2_block_height()) + 1;
let res = FuelGasPriceUpdater::init(height.into(), l2_block_source, metadata_storage);
let res = FuelGasPriceUpdater::init(
height.into(),
l2_block_source,
metadata_storage,
0,
0,
0,
);

// then
assert!(matches!(res, Err(Error::CouldNotInitUpdater(_))));
Expand Down
75 changes: 75 additions & 0 deletions tests/tests/gas_price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@ use fuel_core_client::client::{
types::gas_price::LatestGasPrice,
FuelClient,
};
use fuel_core_gas_price_service::fuel_gas_price_updater::{
fuel_core_storage_adapter::storage::GasPriceMetadata,
UpdaterMetadata,
V0Metadata,
};
use fuel_core_poa::Trigger;
use fuel_core_storage::{
transactional::AtomicView,
StorageAsRef,
};
use fuel_core_types::{
fuel_asm::*,
fuel_crypto::{
Expand All @@ -38,6 +47,7 @@ use fuel_core_types::{
use rand::Rng;
use std::{
iter::repeat,
ops::Deref,
time::Duration,
};
use test_helpers::fuel_core_driver::FuelCoreDriver;
Expand Down Expand Up @@ -347,3 +357,68 @@ async fn dry_run_opt__zero_gas_price_equal_to_none_gas_price() {

assert_eq!(total_gas, total_gas_zero_gas_price);
}

#[tokio::test]
async fn startup__can_override_gas_price_values_by_changing_config() {
// given
let args = vec![
"--debug",
"--poa-instant",
"true",
"--gas-price-change-percent",
"0",
"--gas-price-threshold-percent",
"0",
"--min-gas-price",
"0",
];
let driver = FuelCoreDriver::spawn(&args).await.unwrap();
driver.client.produce_blocks(1, None).await.unwrap();
let temp_dir = driver.kill().await;

// when
let new_args = vec![
"--debug",
"--poa-instant",
"true",
"--gas-price-change-percent",
"11",
"--gas-price-threshold-percent",
"22",
"--min-gas-price",
"33",
];
let recovered_driver = FuelCoreDriver::spawn_with_directory(temp_dir, &new_args)
.await
.unwrap();

// then
recovered_driver
.client
.produce_blocks(1, None)
.await
.unwrap();
let new_height = 2;

let recovered_database = &recovered_driver.node.shared.database;
let recovered_view = recovered_database.gas_price().latest_view().unwrap();
let new_metadata = recovered_view
.storage::<GasPriceMetadata>()
.get(&new_height.into())
.unwrap()
.unwrap()
.deref()
.clone();

let UpdaterMetadata::V0(V0Metadata {
min_exec_gas_price,
exec_gas_price_change_percent,
l2_block_height,
l2_block_fullness_threshold_percent,
..
}) = new_metadata;
assert_eq!(exec_gas_price_change_percent, 11);
assert_eq!(l2_block_fullness_threshold_percent, 22);
assert_eq!(min_exec_gas_price, 33);
assert_eq!(l2_block_height, new_height);
}

0 comments on commit 84f3022

Please sign in to comment.