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

Add Gas Price Updater Service #1938

Merged
merged 102 commits into from
Jun 11, 2024
Merged

Conversation

MitchTurner
Copy link
Member

@MitchTurner MitchTurner commented Jun 5, 2024

Closes: #1956

This is a subtask of #1624

In this PR we add the generic service that will post an algorithm for the providers to use. Not bothering with a real algorithm, that will be implemented later. For now, just show that the provider can get the value generated by the service.

Checklist

  • Breaking changes are clearly marked as such in the PR description and changelog
  • New behavior is reflected in tests
  • The specification matches the implemented behavior (link update PR if changes are needed)

Before requesting review

  • 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]

@MitchTurner MitchTurner requested a review from a team June 11, 2024 02:14
Dentosal
Dentosal previously approved these changes Jun 11, 2024
CHANGELOG.md Outdated
@@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- [#1888](https://github.com/FuelLabs/fuel-core/pull/1888): Upgraded `fuel-vm` to `0.51.0`. See [release](https://github.com/FuelLabs/fuel-vm/releases/tag/v0.51.0) for more information.

### Added
- [#1889](https://github.com/FuelLabs/fuel-core/pull/1889): Add new `FuelGasPriceProvider` that receives the gas price algorithm from a `GasPriceService`
Copy link
Collaborator

Choose a reason for hiding this comment

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

We need to move this into unreleased section

Self(Arc::new(RwLock::new(algo)))
}

pub async fn read(&self) -> RwLockReadGuard<A> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It is a bad idea to return RwLockReadGuard from the service. This means anyone outside the service can lock it. Could you add two separate methods like execute and estimate?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah. You're right. I don't like it just for code cleanliness either.

To add an execute and estimate I'll have to constrain A somehow with a new trait.

The other approach would be just make A Clone, and clone it out of the lock.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it is okay to add constrains for the algorithm=) Gas provider expects that algorithm will provide next gas price=D

A: GasPriceEstimate + Send + Sync,
{
async fn worst_case_gas_price(&self, height: BlockHeight) -> u64 {
self.algorithm.read().await.estimate(height)
Copy link
Collaborator

Choose a reason for hiding this comment

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

The estimate naming is too vague. It would be nice to include the idea that it is a prediction of the price change for some period.

Copy link
Member Author

Choose a reason for hiding this comment

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

I changed it to match the provider: worst_case_gas_price.

where
A: GasPriceAlgorithm + Send + Sync,
{
async fn execute_algorithm(&self, block_bytes: u64) -> u64 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

[nit] next_gas_price sounds more clear to me=)

Comment on lines 4 to 21
pub struct BlockFullness {
used: u64,
capacity: u64,
}

impl BlockFullness {
pub fn new(used: u64, capacity: u64) -> Self {
Self { used, capacity }
}

pub fn used(&self) -> u64 {
self.used
}

pub fn capacity(&self) -> u64 {
self.capacity
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems is not used anywhere

crates/services/gas_price_service/src/lib.rs Show resolved Hide resolved
Comment on lines +32 to +35
/// The algorithm that can be used in the next block
next_block_algorithm: SharedGasPriceAlgo<A>,
/// The code that is run to update your specific algorithm
update_algorithm: U,
Copy link
Collaborator

Choose a reason for hiding this comment

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

For me, it looks like the updater and algorithm should be one entity since only the algorithm knows how it works inside and what data it requires.

But we can leave it for now as it is and see how it will look like with a nonstatic algorithm.

Copy link
Member Author

@MitchTurner MitchTurner Jun 11, 2024

Choose a reason for hiding this comment

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

I'm leaning to this approach so that the consumer of the algo doesn't need to have all the generics for the updater to read the values.

Could also use dyn but that's gross :)

Comment on lines 216 to 217
let (watch_sender, watch_receiver) = tokio::sync::watch::channel(State::Started);
let watcher = StateWatcher::from(watch_receiver);
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can re-use helper function
image

let gas_price = if let Some(inner) = gas_price {
inner
} else {
self.gas_price_provider.gas_price(max_block_bytes).await?
Copy link
Collaborator

Choose a reason for hiding this comment

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

It is better to move all calculations here=) Could you move the duplicated code into a small function that we could reuse in both places, please?=)

Plus, maybe for dry_run, we could just use the previous gas price without.

async fn gas_price(
&self,
block_bytes: u64,
) -> fuel_core_types::services::txpool::Result<GasPrice>;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
) -> fuel_core_types::services::txpool::Result<GasPrice>;
) -> TxPoolResult<GasPrice>;

@MitchTurner MitchTurner requested a review from xgreenx June 11, 2024 17:49
Copy link
Collaborator

@xgreenx xgreenx left a comment

Choose a reason for hiding this comment

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

LGTM!=)

crates/services/txpool/src/ports.rs Outdated Show resolved Hide resolved
crates/services/producer/src/block_producer.rs Outdated Show resolved Hide resolved
@MitchTurner MitchTurner enabled auto-merge (squash) June 11, 2024 19:32
@MitchTurner MitchTurner merged commit 6d31acb into master Jun 11, 2024
29 checks passed
@MitchTurner MitchTurner deleted the feature/fuel-gas-price-service branch June 11, 2024 19:39
@xgreenx xgreenx mentioned this pull request Jun 14, 2024
xgreenx added a commit that referenced this pull request Jun 14, 2024
## Version v0.29.0

### Added
- [#1889](#1889): Add new
`FuelGasPriceProvider` that receives the gas price algorithm from a
`GasPriceService`

### Changed
- [#1942](#1942): Sequential
relayer's commits.
- [#1952](#1952): Change tip
sorting to ratio between tip and max gas sorting in txpool
- [#1960](#1960): Update
fuel-vm to v0.53.0.

### Fixed
- [#1950](#1950): Fix cursor
`BlockHeight` encoding in `SortedTXCursor`

## What's Changed
* Fix code coverage compilation and tests by @Dentosal in
#1943
* Weekly `cargo update` by @github-actions in
#1949
* Fix cursor block height decoding in SortedTXCursor by @AurelienFT in
#1950
* Sequential relayer's commits by @xgreenx in
#1942
* Add Gas Price Updater Service by @MitchTurner in
#1938
* Change tip sorting to ratio between tip and max gas sorting in txpool
by @AurelienFT in #1952
* deps(client): update eventsource-client to fix CVE(s) by @Br1ght0ne in
#1954
* Update fuel-vm to v0.53.0 by @Dentosal in
#1960

## New Contributors
* @AurelienFT made their first contribution in
#1950

**Full Changelog**:
v0.28.0...v0.29.0
GoldenPath1109 added a commit to GoldenPath1109/fuel-core that referenced this pull request Sep 7, 2024
## Version v0.29.0

### Added
- [#1889](FuelLabs/fuel-core#1889): Add new
`FuelGasPriceProvider` that receives the gas price algorithm from a
`GasPriceService`

### Changed
- [#1942](FuelLabs/fuel-core#1942): Sequential
relayer's commits.
- [#1952](FuelLabs/fuel-core#1952): Change tip
sorting to ratio between tip and max gas sorting in txpool
- [#1960](FuelLabs/fuel-core#1960): Update
fuel-vm to v0.53.0.

### Fixed
- [#1950](FuelLabs/fuel-core#1950): Fix cursor
`BlockHeight` encoding in `SortedTXCursor`

## What's Changed
* Fix code coverage compilation and tests by @Dentosal in
FuelLabs/fuel-core#1943
* Weekly `cargo update` by @github-actions in
FuelLabs/fuel-core#1949
* Fix cursor block height decoding in SortedTXCursor by @AurelienFT in
FuelLabs/fuel-core#1950
* Sequential relayer's commits by @xgreenx in
FuelLabs/fuel-core#1942
* Add Gas Price Updater Service by @MitchTurner in
FuelLabs/fuel-core#1938
* Change tip sorting to ratio between tip and max gas sorting in txpool
by @AurelienFT in FuelLabs/fuel-core#1952
* deps(client): update eventsource-client to fix CVE(s) by @Br1ght0ne in
FuelLabs/fuel-core#1954
* Update fuel-vm to v0.53.0 by @Dentosal in
FuelLabs/fuel-core#1960

## New Contributors
* @AurelienFT made their first contribution in
FuelLabs/fuel-core#1950

**Full Changelog**:
FuelLabs/fuel-core@v0.28.0...v0.29.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create Gas Price Service
3 participants