-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
MitchTurner
merged 80 commits into
master
from
feature/implement-gas-price-updater-for-service
Jun 25, 2024
Merged
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 bc9efa0
WIP
MitchTurner 31ea5c4
Add some tests to define the updater behavior
7e6546a
Add a couple more tests, WIP getting the second to pass
MitchTurner 6707c3e
Fix test, refactor out single update into two
MitchTurner 09a0c95
Refactor
MitchTurner da3c887
WIP rewrite the chart generation stuff
MitchTurner cdfc206
Add profit stuff to chart, add rewards
MitchTurner 15a45ed
Add some naive profit corrections
MitchTurner 03c6a4a
Remove the old stuff that is not being used
MitchTurner 459f8fd
Add actual algo that the updater generates, fix tests accordingly
MitchTurner b713739
Update gas price with l2 block data
MitchTurner 91fe558
Use algo generation in simulation
MitchTurner 5c3c277
Straighten out the profit chart to be aligned after every da_recording
MitchTurner da7ea98
Fix the noise
MitchTurner f05c594
Refactor to calculate exec gas price eagarly and calculate da gas pri…
MitchTurner 644c2da
Refactor a bit to allow p and d values, rename some stuff
MitchTurner 9c40f42
Add d value to tests
MitchTurner 0783439
WIP
MitchTurner d7d7d55
Use real gas limit values stablize algo a bit.
MitchTurner 106569d
Set params to somewhat steady state
MitchTurner 6db37a6
Add average profit values
MitchTurner 781424f
Cleanup, add naive optimisation
MitchTurner 88793e9
Refactor
MitchTurner b7c10c6
Replace d value :), start adding max change
MitchTurner e245f98
Add change amount tests, remove unused params
MitchTurner 95ed269
Remove avg window
MitchTurner 95ab959
Refactor and cleanup
MitchTurner 53eebfe
Merge remote-tracking branch 'origin' into feature/gas-price-algo
MitchTurner 5d8d7bb
fix fields
MitchTurner 8ecd034
Cleanup, refactor
MitchTurner 72a4ff8
Appease Clippy-sama
MitchTurner 2b29292
Update CHANGELOG
MitchTurner 41ed222
Rename file
MitchTurner 18019ba
Fix CHANGELOG
MitchTurner 8625caa
Actually fix CHANGELOG
MitchTurner 6677f03
Reorder test file
MitchTurner 33fe1a3
Merge branch 'master' into feature/gas-price-algo
MitchTurner 5ea4c26
Try to fix changelog again, minor adjustments
MitchTurner 9d4cda7
Add consts for reused colors
MitchTurner 20a8b11
Add more docs for algorithm
MitchTurner a28c653
Add more docs
MitchTurner 37fa17d
Merge remote-tracking branch 'origin' into feature/gas-price-algo
MitchTurner 220d5e4
Update lock file
MitchTurner 19fe50b
Update version to match
MitchTurner 507e12e
Merge branch 'master' into feature/gas-price-algo
MitchTurner a8bcab1
REmove all arithmetic side effects
MitchTurner 54e48f4
Remove float, fix more clippy complaints
MitchTurner e228fa1
Add minimum gas price
MitchTurner cfc8538
Modulate percent rather than static amount for exec gas price
MitchTurner cc01db7
Move the analysis stuff into its own crate, fix insidious bug
MitchTurner a240657
Remove commented code
MitchTurner 6e9dc0d
Merge remote-tracking branch 'origin' into feature/gas-price-algo
MitchTurner 305ceaa
Update lock file
MitchTurner 1f2be1c
Fix crate names, lint
MitchTurner ebae0f7
Update docs
MitchTurner 8bfd2bc
Create basic updater struct
MitchTurner dd660b3
Implement for struct
MitchTurner c6b1e7b
WIP add tests
MitchTurner 5d1bd21
Fix dep names and versino
MitchTurner b4d65b7
WIP flakey test
MitchTurner 0cc38cb
Move min gas price to exec to avoid error
MitchTurner 209e3b8
Add minimum da gas price
MitchTurner 395a91b
Update crates/fuel-gas-price-algorithm/src/lib.rs
MitchTurner 31450f9
Update crates/fuel-gas-price-algorithm/src/lib.rs
MitchTurner 158b663
Fix unchanged name, get anaysis running again
MitchTurner 6caa7f7
Update bytecode version
MitchTurner 2e76450
Fix test
MitchTurner 1088ff1
Merge branch 'feature/gas-price-algo' into feature/implement-gas-pric…
MitchTurner c28ceea
Merge remote-tracking branch 'origin' into feature/implement-gas-pric…
MitchTurner 3ffe2cb
Fix CHANGELOG
MitchTurner 392054a
Add init test
MitchTurner 975db7d
Add other init test
MitchTurner 0d741b3
Add test for updating metadata
MitchTurner 336993c
Remove DA stuff because we do not need it yet
MitchTurner 284d900
Sort Cargo.toml
MitchTurner 5357520
Appease Clippy-sama
MitchTurner 9d424a1
Merge branch 'master' into feature/implement-gas-price-updater-for-se…
xgreenx e3656a8
Add documentation
MitchTurner 3393573
Merge branch 'master' into feature/implement-gas-price-updater-for-se…
MitchTurner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
crates/services/gas_price_service/src/fuel_gas_price_updater.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
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()) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.