Skip to content

Commit

Permalink
Add Gas Price Updater Service (#1938)
Browse files Browse the repository at this point in the history
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
3 people committed Jun 11, 2024
1 parent 0692cfc commit 6d31acb
Show file tree
Hide file tree
Showing 25 changed files with 641 additions and 63 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]

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

### Changed
- [#1942](https://github.com/FuelLabs/fuel-core/pull/1942): Sequential relayer's commits.

Expand Down
15 changes: 15 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ members = [
"crates/services/consensus_module/bft",
"crates/services/consensus_module/poa",
"crates/services/executor",
"crates/services/gas_price_service",
"crates/services/importer",
"crates/services/p2p",
"crates/services/producer",
Expand Down Expand Up @@ -67,6 +68,7 @@ fuel-core-bft = { version = "0.28.0", path = "./crates/services/consensus_module
fuel-core-poa = { version = "0.28.0", path = "./crates/services/consensus_module/poa" }
fuel-core-executor = { version = "0.28.0", path = "./crates/services/executor", default-features = false }
fuel-core-importer = { version = "0.28.0", path = "./crates/services/importer" }
fuel-core-gas-price-service = { version = "0.28.0", path = "crates/services/gas_price_service" }
fuel-core-p2p = { version = "0.28.0", path = "./crates/services/p2p" }
fuel-core-producer = { version = "0.28.0", path = "./crates/services/producer" }
fuel-core-relayer = { version = "0.28.0", path = "./crates/services/relayer" }
Expand Down
1 change: 1 addition & 0 deletions crates/fuel-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fuel-core-chain-config = { workspace = true, features = ["std"] }
fuel-core-consensus-module = { workspace = true }
fuel-core-database = { workspace = true }
fuel-core-executor = { workspace = true, features = ["std"] }
fuel-core-gas-price-service = { workspace = true }
fuel-core-importer = { workspace = true }
fuel-core-metrics = { workspace = true }
fuel-core-p2p = { workspace = true, optional = true }
Expand Down
4 changes: 2 additions & 2 deletions crates/fuel-core/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,9 @@ mod tests {
i += 1;
}

// current services: graphql, graphql worker, txpool, PoA
// current services: graphql, graphql worker, txpool, PoA, gas price service
#[allow(unused_mut)]
let mut expected_services = 5;
let mut expected_services = 6;

// Relayer service is disabled with `Config::local_node`.
// #[cfg(feature = "relayer")]
Expand Down
2 changes: 2 additions & 0 deletions crates/fuel-core/src/service/adapters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pub mod relayer;
pub mod sync;
pub mod txpool;

pub mod fuel_gas_price_provider;

#[derive(Debug, Clone)]
pub struct ConsensusParametersProvider {
shared_state: consensus_parameters_provider::SharedState,
Expand Down
93 changes: 93 additions & 0 deletions crates/fuel-core/src/service/adapters/fuel_gas_price_provider.rs
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
}
}
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() {}
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);
}
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);
}
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);
}
6 changes: 3 additions & 3 deletions crates/fuel-core/src/service/adapters/producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use fuel_core_executor::executor::OnceTransactionsSource;
use fuel_core_producer::{
block_producer::gas_price::{
ConsensusParametersProvider as ConsensusParametersProviderTrait,
GasPriceParams,
GasPriceProvider,
},
ports::TxPool,
Expand Down Expand Up @@ -224,9 +223,10 @@ impl fuel_core_producer::ports::BlockProducerDatabase for Database {
}
}

#[async_trait::async_trait]
impl GasPriceProvider for StaticGasPrice {
fn gas_price(&self, _block_height: GasPriceParams) -> Option<u64> {
Some(self.gas_price)
async fn next_gas_price(&self, _block_bytes: u64) -> anyhow::Result<u64> {
Ok(self.gas_price)
}
}

Expand Down
7 changes: 4 additions & 3 deletions crates/fuel-core/src/service/adapters/txpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ use fuel_core_types::{
UtxoId,
},
fuel_types::{
BlockHeight,
ContractId,
Nonce,
},
Expand All @@ -49,6 +48,7 @@ use fuel_core_types::{
GossipsubMessageInfo,
TransactionGossipData,
},
txpool::Result as TxPoolResult,
},
};
use std::sync::Arc;
Expand Down Expand Up @@ -141,9 +141,10 @@ impl fuel_core_txpool::ports::TxPoolDb for Database {
}
}

#[async_trait::async_trait]
impl GasPriceProvider for StaticGasPrice {
fn gas_price(&self, _block_height: BlockHeight) -> Option<u64> {
Some(self.gas_price)
async fn last_gas_price(&self) -> TxPoolResult<u64> {
Ok(self.gas_price)
}
}

Expand Down
Loading

0 comments on commit 6d31acb

Please sign in to comment.