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

Implement gas price updater for service #1972

Merged
merged 80 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 78 commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
ac1aa41
Add the algorithm code
MitchTurner Jun 7, 2024
bc9efa0
WIP
MitchTurner Jun 7, 2024
31ea5c4
Add some tests to define the updater behavior
Jun 8, 2024
7e6546a
Add a couple more tests, WIP getting the second to pass
MitchTurner Jun 8, 2024
6707c3e
Fix test, refactor out single update into two
MitchTurner Jun 9, 2024
09a0c95
Refactor
MitchTurner Jun 9, 2024
da3c887
WIP rewrite the chart generation stuff
MitchTurner Jun 9, 2024
cdfc206
Add profit stuff to chart, add rewards
MitchTurner Jun 9, 2024
15a45ed
Add some naive profit corrections
MitchTurner Jun 9, 2024
03c6a4a
Remove the old stuff that is not being used
MitchTurner Jun 9, 2024
459f8fd
Add actual algo that the updater generates, fix tests accordingly
MitchTurner Jun 10, 2024
b713739
Update gas price with l2 block data
MitchTurner Jun 10, 2024
91fe558
Use algo generation in simulation
MitchTurner Jun 10, 2024
5c3c277
Straighten out the profit chart to be aligned after every da_recording
MitchTurner Jun 10, 2024
da7ea98
Fix the noise
MitchTurner Jun 11, 2024
f05c594
Refactor to calculate exec gas price eagarly and calculate da gas pri…
MitchTurner Jun 11, 2024
644c2da
Refactor a bit to allow p and d values, rename some stuff
MitchTurner Jun 12, 2024
9c40f42
Add d value to tests
MitchTurner Jun 12, 2024
0783439
WIP
MitchTurner Jun 12, 2024
d7d7d55
Use real gas limit values stablize algo a bit.
MitchTurner Jun 12, 2024
106569d
Set params to somewhat steady state
MitchTurner Jun 12, 2024
6db37a6
Add average profit values
MitchTurner Jun 12, 2024
781424f
Cleanup, add naive optimisation
MitchTurner Jun 13, 2024
88793e9
Refactor
MitchTurner Jun 13, 2024
b7c10c6
Replace d value :), start adding max change
MitchTurner Jun 13, 2024
e245f98
Add change amount tests, remove unused params
MitchTurner Jun 13, 2024
95ed269
Remove avg window
MitchTurner Jun 13, 2024
95ab959
Refactor and cleanup
MitchTurner Jun 13, 2024
53eebfe
Merge remote-tracking branch 'origin' into feature/gas-price-algo
MitchTurner Jun 13, 2024
5d8d7bb
fix fields
MitchTurner Jun 13, 2024
8ecd034
Cleanup, refactor
MitchTurner Jun 13, 2024
72a4ff8
Appease Clippy-sama
MitchTurner Jun 13, 2024
2b29292
Update CHANGELOG
MitchTurner Jun 13, 2024
41ed222
Rename file
MitchTurner Jun 13, 2024
18019ba
Fix CHANGELOG
MitchTurner Jun 13, 2024
8625caa
Actually fix CHANGELOG
MitchTurner Jun 13, 2024
6677f03
Reorder test file
MitchTurner Jun 14, 2024
33fe1a3
Merge branch 'master' into feature/gas-price-algo
MitchTurner Jun 14, 2024
5ea4c26
Try to fix changelog again, minor adjustments
MitchTurner Jun 14, 2024
9d4cda7
Add consts for reused colors
MitchTurner Jun 14, 2024
20a8b11
Add more docs for algorithm
MitchTurner Jun 14, 2024
a28c653
Add more docs
MitchTurner Jun 14, 2024
37fa17d
Merge remote-tracking branch 'origin' into feature/gas-price-algo
MitchTurner Jun 14, 2024
220d5e4
Update lock file
MitchTurner Jun 14, 2024
19fe50b
Update version to match
MitchTurner Jun 14, 2024
507e12e
Merge branch 'master' into feature/gas-price-algo
MitchTurner Jun 15, 2024
a8bcab1
REmove all arithmetic side effects
MitchTurner Jun 16, 2024
54e48f4
Remove float, fix more clippy complaints
MitchTurner Jun 16, 2024
e228fa1
Add minimum gas price
MitchTurner Jun 16, 2024
cfc8538
Modulate percent rather than static amount for exec gas price
MitchTurner Jun 17, 2024
cc01db7
Move the analysis stuff into its own crate, fix insidious bug
MitchTurner Jun 18, 2024
a240657
Remove commented code
MitchTurner Jun 18, 2024
6e9dc0d
Merge remote-tracking branch 'origin' into feature/gas-price-algo
MitchTurner Jun 18, 2024
305ceaa
Update lock file
MitchTurner Jun 18, 2024
1f2be1c
Fix crate names, lint
MitchTurner Jun 18, 2024
ebae0f7
Update docs
MitchTurner Jun 18, 2024
8bfd2bc
Create basic updater struct
MitchTurner Jun 14, 2024
dd660b3
Implement for struct
MitchTurner Jun 14, 2024
c6b1e7b
WIP add tests
MitchTurner Jun 16, 2024
5d1bd21
Fix dep names and versino
MitchTurner Jun 18, 2024
b4d65b7
WIP flakey test
MitchTurner Jun 18, 2024
0cc38cb
Move min gas price to exec to avoid error
MitchTurner Jun 18, 2024
209e3b8
Add minimum da gas price
MitchTurner Jun 18, 2024
395a91b
Update crates/fuel-gas-price-algorithm/src/lib.rs
MitchTurner Jun 18, 2024
31450f9
Update crates/fuel-gas-price-algorithm/src/lib.rs
MitchTurner Jun 18, 2024
158b663
Fix unchanged name, get anaysis running again
MitchTurner Jun 18, 2024
6caa7f7
Update bytecode version
MitchTurner Jun 19, 2024
2e76450
Fix test
MitchTurner Jun 19, 2024
1088ff1
Merge branch 'feature/gas-price-algo' into feature/implement-gas-pric…
MitchTurner Jun 19, 2024
c28ceea
Merge remote-tracking branch 'origin' into feature/implement-gas-pric…
MitchTurner Jun 19, 2024
3ffe2cb
Fix CHANGELOG
MitchTurner Jun 19, 2024
392054a
Add init test
MitchTurner Jun 19, 2024
975db7d
Add other init test
MitchTurner Jun 19, 2024
0d741b3
Add test for updating metadata
MitchTurner Jun 19, 2024
336993c
Remove DA stuff because we do not need it yet
MitchTurner Jun 19, 2024
284d900
Sort Cargo.toml
MitchTurner Jun 19, 2024
5357520
Appease Clippy-sama
MitchTurner Jun 19, 2024
9d424a1
Merge branch 'master' into feature/implement-gas-price-updater-for-se…
xgreenx Jun 24, 2024
e3656a8
Add documentation
MitchTurner Jun 25, 2024
3393573
Merge branch 'master' into feature/implement-gas-price-updater-for-se…
MitchTurner Jun 25, 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
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added
- [1972](https://github.com/FuelLabs/fuel-core/pull/1972): Implement `AlgorithmUpdater` for `GasPriceService`
- [#1948](https://github.com/FuelLabs/fuel-core/pull/1948): Add new `AlgorithmV1` and `AlgorithmUpdaterV1` for the gas price. Include tools for analysis

## [Version 0.30.0]

### Added
Expand All @@ -25,7 +29,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Added
- [#1889](https://github.com/FuelLabs/fuel-core/pull/1889): Add new `FuelGasPriceProvider` that receives the gas price algorithm from a `GasPriceService`
- [#1948](https://github.com/FuelLabs/fuel-core/pull/1948): Add new `AlgorithmV1` and `AlgorithmUpdaterV1' for the gas price. Include tools for analysis

### Changed
- [#1942](https://github.com/FuelLabs/fuel-core/pull/1942): Sequential relayer's commits.
Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion crates/fuel-gas-price-algorithm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub enum Error {
/// The DA portion also uses a moving average of the profits over the last `avg_window` blocks
/// instead of the actual profit. Setting the `avg_window` to 1 will effectively disable the
/// moving average.
#[derive(Debug, Clone, PartialEq)]
pub struct AlgorithmV1 {
/// The lowest the algorithm allows the gas price to go
min_da_gas_price: u64,
Expand Down Expand Up @@ -140,6 +141,7 @@ impl AlgorithmV1 {
///
/// This projection will inevitably lead to error in the gas price calculation. Special care should be taken
/// to account for the worst case scenario when calculating the parameters of the algorithm.
#[derive(Debug, Clone, PartialEq)]
pub struct AlgorithmUpdaterV1 {
/// The gas price to cover the execution of the next block
pub new_exec_price: u64,
Expand Down Expand Up @@ -192,7 +194,7 @@ pub struct RecordedBlock {
pub block_cost: u64,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct BlockBytes {
pub height: u32,
pub block_bytes: u64,
Expand Down
2 changes: 2 additions & 0 deletions crates/services/gas_price_service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ anyhow = { workspace = true }
async-trait = { workspace = true }
fuel-core-services = { workspace = true }
fuel-core-types = { workspace = true }
fuel-gas-price-algorithm = { workspace = true }
futures = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }

Expand Down
132 changes: 132 additions & 0 deletions crates/services/gas_price_service/src/fuel_gas_price_updater.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
use crate::UpdateAlgorithm;
use fuel_core_types::fuel_types::BlockHeight;
use fuel_gas_price_algorithm::{
AlgorithmUpdaterV1,
AlgorithmV1,
RecordedBlock,
};

#[cfg(test)]
mod tests;

pub struct FuelGasPriceUpdater<L2, Metadata> {
inner: AlgorithmUpdaterV1,
l2_block_source: L2,
metadata_storage: Metadata,
}

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Failed to find L2 block at height {block_height:?}: {source_error:?}")]
CouldNotFetchL2Block {
block_height: BlockHeight,
source_error: anyhow::Error,
},
#[error("Failed to find DA records: {0:?}")]
CouldNotFetchDARecord(anyhow::Error),
}

type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, Clone)]
pub struct BlockInfo {
pub height: u32,
pub fullness: (u64, u64),
Copy link
Member

Choose a reason for hiding this comment

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

This fields wants a doc comment, as it's not immediately clear what the tuple does.

pub block_bytes: u64,
pub gas_price: u64,
}
#[async_trait::async_trait]
pub trait L2BlockSource: Send + Sync {
async fn get_l2_block(&self, height: BlockHeight) -> Result<BlockInfo>;
}

#[async_trait::async_trait]
pub trait DARecordSource: Send + Sync {
async fn get_da_record(&self) -> Result<Vec<RecordedBlock>>;
}

#[derive(Debug, Clone)]
pub enum UpdaterMetadata {
V1(AlgorithmUpdaterV1),
}

impl From<UpdaterMetadata> for AlgorithmUpdaterV1 {
fn from(metadata: UpdaterMetadata) -> Self {
match metadata {
UpdaterMetadata::V1(v1) => v1,
}
}
}

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

#[async_trait::async_trait]
pub trait MetadataStorage: Send + Sync {
async fn get_metadata(&self) -> Result<Option<UpdaterMetadata>>;
async fn set_metadata(&self, metadata: UpdaterMetadata) -> Result<()>;
}

impl<L2, Metadata> FuelGasPriceUpdater<L2, Metadata>
where
Metadata: MetadataStorage,
{
pub async fn init(
init_metadata: UpdaterMetadata,
l2_block_source: L2,
metadata_storage: Metadata,
) -> Result<Self> {
let inner = metadata_storage
.get_metadata()
.await?
.unwrap_or(init_metadata)
.into();
let updater = Self {
inner,
l2_block_source,
metadata_storage,
};
Ok(updater)
}
}

#[async_trait::async_trait]
impl<L2, Metadata> UpdateAlgorithm for FuelGasPriceUpdater<L2, Metadata>
where
L2: L2BlockSource,
Metadata: MetadataStorage + Send + Sync,
{
type Algorithm = AlgorithmV1;

fn start(&self, _for_block: BlockHeight) -> Self::Algorithm {
self.inner.algorithm()
}

async fn next(&mut self) -> anyhow::Result<Self::Algorithm> {
tokio::select! {
l2_block = self.l2_block_source.get_l2_block(self.inner.l2_block_height.into()) => {
tracing::info!("Received L2 block: {:?}", l2_block);
let l2_block = l2_block?;
let BlockInfo {
height,
fullness,
block_bytes,
gas_price,
} = l2_block;
self.inner.update_l2_block_data(
height,
fullness,
block_bytes,
gas_price,
)?;
self.metadata_storage
.set_metadata(self.inner.clone().into())
.await?;
Ok(self.inner.algorithm())
}
}
}
}
Loading
Loading