-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Gas Price Updater Service (#1938)
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](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/) - [ ] [Swhttps://github.com/FuelLabs/fuel-core/issues/1956ay 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: Hannes Karppila <2204863+Dentosal@users.noreply.github.com> Co-authored-by: Green Baneling <XgreenX9999@gmail.com>
- Loading branch information
1 parent
0692cfc
commit 6d31acb
Showing
25 changed files
with
641 additions
and
63 deletions.
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
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
93 changes: 93 additions & 0 deletions
93
crates/fuel-core/src/service/adapters/fuel_gas_price_provider.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,93 @@ | ||
use crate::fuel_core_graphql_api::ports::GasPriceEstimate as GraphqlGasPriceEstimate; | ||
use fuel_core_gas_price_service::{ | ||
GasPriceAlgorithm, | ||
SharedGasPriceAlgo, | ||
}; | ||
use fuel_core_producer::block_producer::gas_price::GasPriceProvider as ProducerGasPriceProvider; | ||
use fuel_core_txpool::ports::GasPriceProvider as TxPoolGasPricProvider; | ||
use fuel_core_types::{ | ||
fuel_types::BlockHeight, | ||
services::txpool::Result as TxPoolResult, | ||
}; | ||
|
||
pub type Result<T, E = Error> = std::result::Result<T, E>; | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
pub enum Error { | ||
#[error("Requested height is too high. Requested: {requested_height}, latest: {latest_height}")] | ||
AlgorithmNotUpToDate { | ||
requested_height: BlockHeight, | ||
latest_height: BlockHeight, | ||
}, | ||
#[error("Latest block height past requested height. Requested: {requested_height}, latest: {latest_height}")] | ||
RequestedOldBlockHeight { | ||
requested_height: BlockHeight, | ||
latest_height: BlockHeight, | ||
}, | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests; | ||
|
||
#[derive(Debug)] | ||
/// Receives the next gas price algorithm via a shared `BlockGasPriceAlgo` instance | ||
pub struct FuelGasPriceProvider<A> { | ||
algorithm: SharedGasPriceAlgo<A>, | ||
} | ||
|
||
impl<A> Clone for FuelGasPriceProvider<A> { | ||
fn clone(&self) -> Self { | ||
Self { | ||
algorithm: self.algorithm.clone(), | ||
} | ||
} | ||
} | ||
|
||
impl<A> FuelGasPriceProvider<A> { | ||
pub fn new(algorithm: SharedGasPriceAlgo<A>) -> Self { | ||
Self { algorithm } | ||
} | ||
} | ||
|
||
impl<A> FuelGasPriceProvider<A> | ||
where | ||
A: GasPriceAlgorithm + Send + Sync, | ||
{ | ||
async fn next_gas_price(&self, block_bytes: u64) -> u64 { | ||
self.algorithm.next_gas_price(block_bytes).await | ||
} | ||
|
||
async fn last_gas_price(&self) -> u64 { | ||
self.algorithm.last_gas_price().await | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl<A> ProducerGasPriceProvider for FuelGasPriceProvider<A> | ||
where | ||
A: GasPriceAlgorithm + Send + Sync, | ||
{ | ||
async fn next_gas_price(&self, block_bytes: u64) -> anyhow::Result<u64> { | ||
Ok(self.next_gas_price(block_bytes).await) | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl<A> TxPoolGasPricProvider for FuelGasPriceProvider<A> | ||
where | ||
A: GasPriceAlgorithm + Send + Sync, | ||
{ | ||
async fn last_gas_price(&self) -> TxPoolResult<u64> { | ||
Ok(self.last_gas_price().await) | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl<A> GraphqlGasPriceEstimate for FuelGasPriceProvider<A> | ||
where | ||
A: GasPriceAlgorithm + Send + Sync, | ||
{ | ||
async fn worst_case_gas_price(&self, height: BlockHeight) -> u64 { | ||
self.algorithm.worst_case_gas_price(height).await | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
crates/fuel-core/src/service/adapters/fuel_gas_price_provider/tests.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,53 @@ | ||
#![allow(non_snake_case)] | ||
|
||
use super::*; | ||
|
||
#[cfg(test)] | ||
mod producer_gas_price_tests; | ||
|
||
#[cfg(test)] | ||
mod tx_pool_gas_price_tests; | ||
|
||
#[cfg(test)] | ||
mod graph_ql_gas_price_estimate_tests; | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
pub struct TestGasPriceAlgorithm { | ||
last: u64, | ||
multiply: u64, | ||
} | ||
|
||
impl Default for TestGasPriceAlgorithm { | ||
fn default() -> Self { | ||
Self { | ||
last: 100, | ||
multiply: 2, | ||
} | ||
} | ||
} | ||
|
||
impl GasPriceAlgorithm for TestGasPriceAlgorithm { | ||
fn last_gas_price(&self) -> u64 { | ||
self.last | ||
} | ||
|
||
fn next_gas_price(&self, block_bytes: u64) -> u64 { | ||
self.multiply.saturating_mul(block_bytes) | ||
} | ||
|
||
fn worst_case_gas_price(&self, _block_height: BlockHeight) -> u64 { | ||
self.multiply.saturating_mul(10_000_000) // Arbitrary fake bytes | ||
} | ||
} | ||
|
||
fn build_provider<A>(algorithm: A) -> FuelGasPriceProvider<A> | ||
where | ||
A: Send + Sync, | ||
{ | ||
let algorithm = SharedGasPriceAlgo::new(algorithm); | ||
FuelGasPriceProvider::new(algorithm) | ||
} | ||
|
||
#[ignore] | ||
#[test] | ||
fn dummy() {} |
16 changes: 16 additions & 0 deletions
16
...e/src/service/adapters/fuel_gas_price_provider/tests/graph_ql_gas_price_estimate_tests.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,16 @@ | ||
use super::*; | ||
|
||
#[tokio::test] | ||
async fn estimate_gas_price__happy_path() { | ||
// given | ||
let next_height = 432.into(); | ||
let algo = TestGasPriceAlgorithm::default(); | ||
let gas_price_provider = build_provider(algo); | ||
|
||
// when | ||
let expected_price = algo.worst_case_gas_price(next_height); | ||
let actual_price = gas_price_provider.worst_case_gas_price(next_height).await; | ||
|
||
// then | ||
assert_eq!(expected_price, actual_price); | ||
} |
20 changes: 20 additions & 0 deletions
20
.../fuel-core/src/service/adapters/fuel_gas_price_provider/tests/producer_gas_price_tests.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,20 @@ | ||
use crate::service::adapters::fuel_gas_price_provider::tests::{ | ||
build_provider, | ||
TestGasPriceAlgorithm, | ||
}; | ||
use fuel_core_gas_price_service::GasPriceAlgorithm; | ||
|
||
#[tokio::test] | ||
async fn gas_price__if_requested_block_height_is_latest_return_gas_price() { | ||
// given | ||
let algo = TestGasPriceAlgorithm::default(); | ||
let gas_price_provider = build_provider(algo); | ||
let bytes = 10; | ||
|
||
// when | ||
let expected_price = algo.next_gas_price(bytes); | ||
let actual_price = gas_price_provider.next_gas_price(bytes).await; | ||
|
||
// then | ||
assert_eq!(expected_price, actual_price); | ||
} |
19 changes: 19 additions & 0 deletions
19
...s/fuel-core/src/service/adapters/fuel_gas_price_provider/tests/tx_pool_gas_price_tests.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,19 @@ | ||
use crate::service::adapters::fuel_gas_price_provider::tests::{ | ||
build_provider, | ||
TestGasPriceAlgorithm, | ||
}; | ||
use fuel_core_gas_price_service::GasPriceAlgorithm; | ||
|
||
#[tokio::test] | ||
async fn gas_price__if_requested_block_height_is_latest_return_gas_price() { | ||
// given | ||
let algo = TestGasPriceAlgorithm::default(); | ||
let gas_price_provider = build_provider(algo); | ||
|
||
// when | ||
let expected_price = algo.last_gas_price(); | ||
let actual_price = gas_price_provider.last_gas_price().await; | ||
|
||
// then | ||
assert_eq!(expected_price, actual_price); | ||
} |
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
Oops, something went wrong.