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
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
102 commits
Select commit Hold shift + click to select a range
378dae8
Introduce type with dummy test
MitchTurner May 8, 2024
cbebddc
Replace test with historical gas price test
MitchTurner May 8, 2024
bb24903
Get test passing
MitchTurner May 8, 2024
fb58594
Refactor
MitchTurner May 8, 2024
dc193d5
Add more logic to when it looks up historical prices, add tests
MitchTurner May 8, 2024
4af3946
Update CHANGELOG
MitchTurner May 9, 2024
398884e
Remove extra traits
MitchTurner May 9, 2024
96f20d9
Rethink ports
MitchTurner May 9, 2024
86f49a7
Write test to show that increase is called in non-profitable case
MitchTurner May 9, 2024
672f6cb
Add algo abstraction
MitchTurner May 10, 2024
2e80dec
Remove index trait
MitchTurner May 10, 2024
1134fd5
Add test for capped price
MitchTurner May 10, 2024
a818b9b
Add updating logic
MitchTurner May 10, 2024
edf1cb3
WIP add Result to functions
MitchTurner May 10, 2024
a389a13
Add errors to all the failure modes
MitchTurner May 10, 2024
19a41ac
Start writing real algo
MitchTurner May 10, 2024
0f8edce
Introduce real `BlockFullness`
MitchTurner May 11, 2024
613c6bb
Switch `BlockFullness` to ints
MitchTurner May 11, 2024
4b141b5
Add some naive tests to algo impl with tests
MitchTurner May 11, 2024
c3bae38
Merge branch 'master' into feature/fuel-gas-price-provider
MitchTurner May 13, 2024
b693a2a
WIP add benches
MitchTurner May 13, 2024
e9bf624
Add WIP algo testing to its own crate for compilation reasons
MitchTurner May 13, 2024
ada5897
Beef up graph
MitchTurner May 13, 2024
473a1a1
Add missed stuff
MitchTurner May 13, 2024
8e5224d
Fix some maths
MitchTurner May 13, 2024
883c695
Cleanup a little
MitchTurner May 13, 2024
b92ba83
Add tuned p value
MitchTurner May 13, 2024
ca4568d
Add p value
MitchTurner May 14, 2024
a8cf8e1
Introduce "more realistic" noise to cost signal
MitchTurner May 14, 2024
c51e15e
Simplify algo to ignore block fullness and add d value
MitchTurner May 14, 2024
9119cc0
Tune some
MitchTurner May 14, 2024
509d64a
Add cap to gas-price change
MitchTurner May 15, 2024
0cfb486
Make a little more stable
MitchTurner May 15, 2024
1526069
Add new chart for execution vs da vs total, implement naive exec calc
MitchTurner May 24, 2024
cc7f5e8
Refactor to split da and exec gas prices and have storage for both
MitchTurner May 25, 2024
6df0036
Use algo in adapter
MitchTurner May 27, 2024
f4941b1
Remove benchmarking stuff
MitchTurner May 27, 2024
9cb29c7
Merge branch 'master' into feature/fuel-gas-price-provider
MitchTurner May 27, 2024
a7eff14
Remove buggy/unneeded while loop
MitchTurner May 27, 2024
0c3b0e0
Have gas price provider return result instead of option
MitchTurner May 27, 2024
6349835
Merge branch 'master' into feature/fuel-gas-price-provider
MitchTurner May 27, 2024
c03caf1
Fix typo
MitchTurner May 27, 2024
b22ab44
Lint toml file
MitchTurner May 27, 2024
1b70458
Reformat
MitchTurner May 27, 2024
a0f0a74
Clean up
MitchTurner May 27, 2024
ba03b02
Use old da_gas_price if not caught up
MitchTurner May 27, 2024
fd3fb69
Fix bug in when da gas prices are recalculated
MitchTurner May 28, 2024
102bdc9
Make da price more stepwise to account for fewer blocks on da layer
MitchTurner May 28, 2024
80835e6
Increase max change for da gas price because of lower frequency, fix …
MitchTurner May 28, 2024
8ca7fe6
Merge remote-tracking branch 'origin' into feature/fuel-gas-price-pro…
MitchTurner May 28, 2024
29a5025
Add some rough concept of determining a good p and d value
MitchTurner May 28, 2024
d45d246
Appease Clippy-sama
MitchTurner May 28, 2024
daa2c68
Merge branch 'master' into feature/fuel-gas-price-provider
MitchTurner May 29, 2024
f5986aa
Remove file, use anyhow error
MitchTurner May 31, 2024
7746674
Fix bug that was not caught by tests :/
MitchTurner Jun 1, 2024
d5f4509
Fix name
MitchTurner Jun 1, 2024
3964a8d
Remove old stuff, reconstruct provider
MitchTurner Jun 3, 2024
7fb2d10
Update deps
MitchTurner Jun 4, 2024
7098569
WIP
MitchTurner Jun 5, 2024
bbc3d1f
Add new service and provider for producer
MitchTurner Jun 5, 2024
88f75cc
Remove algo stuff
MitchTurner Jun 5, 2024
4997a38
Rename service
MitchTurner Jun 5, 2024
8b53010
Remove graph that is not part of current code
MitchTurner Jun 5, 2024
398ac29
Fix impls for tests
MitchTurner Jun 5, 2024
b43bf26
Add test for gas price service
MitchTurner Jun 6, 2024
6c545e2
Fix cargo deps
MitchTurner Jun 6, 2024
f794dbb
Merge remote-tracking branch 'origin' into feature/fuel-gas-price-ser…
MitchTurner Jun 6, 2024
fe76f27
Fix dep, add new_service method
MitchTurner Jun 6, 2024
f0927e8
Add new shared state type
MitchTurner Jun 6, 2024
b8b4d22
Use new provider for tx pool
MitchTurner Jun 6, 2024
9017580
Fully remove old gas provider
MitchTurner Jun 6, 2024
22ce7e6
Merge branch 'master' into feature/fuel-gas-price-service
MitchTurner Jun 6, 2024
23cc079
Fix spelling
MitchTurner Jun 6, 2024
629abf8
Lint cargo.toml
MitchTurner Jun 6, 2024
0784fc3
Use max bytes in producer gas price
MitchTurner Jun 6, 2024
7c44a26
Add simple sanity check UT for estimating gas price
MitchTurner Jun 6, 2024
c5b237d
Update CHANGELOG
MitchTurner Jun 6, 2024
3d81f5b
Remove gas price params type because YAGNI
MitchTurner Jun 6, 2024
e62c969
Lint cargo.toml
MitchTurner Jun 6, 2024
d924ae9
Cleanup
MitchTurner Jun 6, 2024
9a004dc
Merge branch 'master' into feature/fuel-gas-price-service
MitchTurner Jun 6, 2024
baa5f3a
Fix some impls
MitchTurner Jun 6, 2024
0814849
Appease Clippy-sama
MitchTurner Jun 6, 2024
eb40414
Fix test
MitchTurner Jun 7, 2024
4a57128
Remove block height as input for gas price because of bug it was caus…
MitchTurner Jun 7, 2024
703b890
Use max block bytes in dry run as well
MitchTurner Jun 7, 2024
c1764d6
Merge remote-tracking branch 'origin' into feature/fuel-gas-price-ser…
MitchTurner Jun 7, 2024
a455bd1
Merge branch 'master' into feature/fuel-gas-price-service
MitchTurner Jun 10, 2024
9f9d7ba
Update crates/services/gas_price_service/Cargo.toml
MitchTurner Jun 11, 2024
dac6d29
Add some docs and better errors
MitchTurner Jun 11, 2024
a4037ea
Fmt
MitchTurner Jun 11, 2024
9438dfc
Merge branch 'master' into feature/fuel-gas-price-service
MitchTurner Jun 11, 2024
4470ab7
Add changes from review suggestions
MitchTurner Jun 11, 2024
2d0932a
Add constraint to shared algo for cleaner access
MitchTurner Jun 11, 2024
46c1969
Merge branch 'master' into feature/fuel-gas-price-service
MitchTurner Jun 11, 2024
d4150e9
Fmt
MitchTurner Jun 11, 2024
1e1848b
Rename a couple functions
MitchTurner Jun 11, 2024
843c480
Fmt
MitchTurner Jun 11, 2024
e8399f5
Fix compilation, appease Clippy-sama
MitchTurner Jun 11, 2024
8ad3260
More cleanup
MitchTurner Jun 11, 2024
ab475e5
Update crates/services/producer/src/block_producer.rs
xgreenx Jun 11, 2024
421dc45
Update crates/services/txpool/src/ports.rs
xgreenx Jun 11, 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
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
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
Loading