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

Prepare the codebase to use base gas price during block production #1642 #1752

Merged
merged 19 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Description of the upcoming release here.

### Added

- [#1752](https://github.com/FuelLabs/fuel-core/pull/1752): Add `ProducerGasPrice` trait that the `Producer` depends on to get the gas price for the block.
- [#1747](https://github.com/FuelLabs/fuel-core/pull/1747): The DA block height is now included in the genesis state.
- [#1740](https://github.com/FuelLabs/fuel-core/pull/1740): Remove optional fields from genesis configs
- [#1737](https://github.com/FuelLabs/fuel-core/pull/1737): Remove temporary tables for calculating roots during genesis.
Expand Down
2 changes: 1 addition & 1 deletion benches/benches/block_target_gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ fn run_with_service_with_extra_inputs(

let mut sub = shared.block_importer.block_importer.subscribe();
shared
.txpool
.txpool_shared_state
.insert(vec![std::sync::Arc::new(tx)])
.await
.into_iter()
Expand Down
2 changes: 1 addition & 1 deletion benches/benches/transaction_throughput.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ where
test_builder.finalize().await;

// insert all transactions
srv.shared.txpool.insert(transactions).await;
srv.shared.txpool_shared_state.insert(transactions).await;
let _ = client.produce_blocks(1, None).await;

// sanity check block to ensure the transactions were actually processed
Expand Down
2 changes: 1 addition & 1 deletion bin/e2e-test-client/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ fn dev_config() -> Config {
);

config.chain_config = chain_config;
config.block_producer.gas_price = 1;
config.static_gas_price = 1;
config.state_reader = StateReader::in_memory(state_config);

config.block_producer.coinbase_recipient = Some(
Expand Down
3 changes: 1 addition & 2 deletions bin/fuel-core/src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,6 @@ impl Command {
tx_max_number,
tx_max_depth,
chain_conf,
min_gas_price,
utxo_validation,
metrics,
tx_pool_ttl.into(),
Expand All @@ -354,9 +353,9 @@ impl Command {
block_producer: ProducerConfig {
utxo_validation,
coinbase_recipient,
gas_price: min_gas_price,
metrics,
},
static_gas_price: min_gas_price,
block_importer,
#[cfg(feature = "relayer")]
relayer: relayer_cfg,
Expand Down
1 change: 0 additions & 1 deletion crates/client/assets/schema.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,6 @@ type Mutation {
type NodeInfo {
utxoValidation: Boolean!
vmBacktrace: Boolean!
minGasPrice: U64!
maxTx: U64!
maxDepth: U64!
nodeVersion: String!
Expand Down
1 change: 0 additions & 1 deletion crates/client/src/client/schema/node_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use std::{
pub struct NodeInfo {
pub utxo_validation: bool,
pub vm_backtrace: bool,
pub min_gas_price: U64,
pub max_tx: U64,
pub max_depth: U64,
pub node_version: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ query {
nodeInfo {
utxoValidation
vmBacktrace
minGasPrice
maxTx
maxDepth
nodeVersion
Expand Down
2 changes: 0 additions & 2 deletions crates/client/src/client/types/node_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::client::schema;
pub struct NodeInfo {
pub utxo_validation: bool,
pub vm_backtrace: bool,
pub min_gas_price: u64,
pub max_tx: u64,
pub max_depth: u64,
pub node_version: String,
Expand All @@ -16,7 +15,6 @@ impl From<schema::node_info::NodeInfo> for NodeInfo {
Self {
utxo_validation: value.utxo_validation,
vm_backtrace: value.vm_backtrace,
min_gas_price: value.min_gas_price.into(),
max_tx: value.max_tx.into(),
max_depth: value.max_depth.into(),
node_version: value.node_version,
Expand Down
1 change: 0 additions & 1 deletion crates/fuel-core/src/graphql_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ pub struct Config {
pub utxo_validation: bool,
pub debug: bool,
pub vm_backtrace: bool,
pub min_gas_price: u64,
pub max_tx: usize,
pub max_depth: usize,
pub chain_name: String,
Expand Down
5 changes: 5 additions & 0 deletions crates/fuel-core/src/graphql_api/api_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
ports::{
BlockProducerPort,
ConsensusModulePort,
GasPriceEstimate,
OffChainDatabase,
OnChainDatabase,
P2pPort,
Expand Down Expand Up @@ -88,6 +89,8 @@ pub type TxPool = Box<dyn TxPoolPort>;
pub type ConsensusModule = Box<dyn ConsensusModulePort>;
pub type P2pService = Box<dyn P2pPort>;

pub type GasPriceProvider = Box<dyn GasPriceEstimate>;

#[derive(Clone)]
pub struct SharedState {
pub bound_address: SocketAddr,
Expand Down Expand Up @@ -173,6 +176,7 @@ pub fn new_service<OnChain, OffChain>(
producer: BlockProducer,
consensus_module: ConsensusModule,
p2p_service: P2pService,
gas_price_provider: GasPriceProvider,
log_threshold_ms: Duration,
request_timeout: Duration,
) -> anyhow::Result<Service>
Expand All @@ -192,6 +196,7 @@ where
.data(producer)
.data(consensus_module)
.data(p2p_service)
.data(gas_price_provider)
.extension(async_graphql::extensions::Tracing)
.extension(MetricsExtension::new(log_threshold_ms))
.extension(ViewExtension::new())
Expand Down
7 changes: 7 additions & 0 deletions crates/fuel-core/src/graphql_api/ports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,13 @@ pub trait P2pPort: Send + Sync {
async fn all_peer_info(&self) -> anyhow::Result<Vec<PeerInfo>>;
}

/// Trait for defining how to estimate gas price for future blocks
#[async_trait::async_trait]
pub trait GasPriceEstimate: Send + Sync {
/// The worst case scenario for gas price at a given horizon
async fn worst_case_gas_price(&self, height: BlockHeight) -> u64;
}

pub mod worker {
use super::super::storage::blocks::FuelBlockIdsToHeights;
use crate::fuel_core_graphql_api::storage::{
Expand Down
2 changes: 1 addition & 1 deletion crates/fuel-core/src/p2p_test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ impl Node {
let tx_result = self
.node
.shared
.txpool
.txpool_shared_state
.insert(vec![Arc::new(tx.clone())])
.await
.pop()
Expand Down
52 changes: 36 additions & 16 deletions crates/fuel-core/src/schema/gas_price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@ use super::scalars::{
U64,
};
use crate::{
fuel_core_graphql_api::{
database::ReadView,
Config as GraphQLConfig,
fuel_core_graphql_api::database::ReadView,
graphql_api::api_service::GasPriceProvider,
query::{
BlockQueryData,
SimpleTransactionData,
},
query::BlockQueryData,
};
use async_graphql::{
Context,
Object,
};
use fuel_core_types::blockchain::block::Block;
use fuel_core_types::{
blockchain::block::Block,
fuel_tx::{
field::MintGasPrice,
Transaction,
},
};

pub struct LatestGasPrice {
pub gas_price: U64,
Expand All @@ -40,14 +47,18 @@ impl LatestGasPriceQuery {
&self,
ctx: &Context<'_>,
) -> async_graphql::Result<LatestGasPrice> {
let config = ctx.data_unchecked::<GraphQLConfig>();

let query: &ReadView = ctx.data_unchecked();
let latest_block: Block<_> = query.latest_block()?;
let block_height = u32::from(*latest_block.header().height());

let latest_block: Block<_> = query.latest_block()?;
let block_height: u32 = (*latest_block.header().height()).into();
let mut gas_price: U64 = 0.into();
if let Some(tx_id) = latest_block.transactions().last() {
if let Transaction::Mint(mint_tx) = query.transaction(tx_id)? {
gas_price = (*mint_tx.gas_price()).into();
}
}
Ok(LatestGasPrice {
gas_price: config.min_gas_price.into(),
gas_price,
block_height: block_height.into(),
})
}
Expand Down Expand Up @@ -77,13 +88,22 @@ impl EstimateGasPriceQuery {
)]
block_horizon: Option<U32>,
) -> async_graphql::Result<EstimateGasPrice> {
// TODO: implement dynamic calculation based on block horizon
// https://github.com/FuelLabs/fuel-core/issues/1653
let _ = block_horizon;
let query: &ReadView = ctx.data_unchecked();

let config = ctx.data_unchecked::<GraphQLConfig>();
let gas_price = config.min_gas_price.into();
let latest_block_height: u32 = query.latest_block_height()?.into();
let target_block = block_horizon
.and_then(|h| h.0.checked_add(latest_block_height))
.ok_or(async_graphql::Error::new(format!(
"Invalid block horizon. Overflows latest block :{latest_block_height:?}"
)))?;

Ok(EstimateGasPrice { gas_price })
let gas_price_provider = ctx.data_unchecked::<GasPriceProvider>();
let gas_price = gas_price_provider
.worst_case_gas_price(target_block.into())
.await;

Ok(EstimateGasPrice {
gas_price: gas_price.into(),
})
}
}
6 changes: 0 additions & 6 deletions crates/fuel-core/src/schema/node_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use std::time::UNIX_EPOCH;
pub struct NodeInfo {
utxo_validation: bool,
vm_backtrace: bool,
min_gas_price: U64,
max_tx: U64,
max_depth: U64,
node_version: String,
Expand All @@ -28,10 +27,6 @@ impl NodeInfo {
self.vm_backtrace
}

async fn min_gas_price(&self) -> U64 {
self.min_gas_price
}

async fn max_tx(&self) -> U64 {
self.max_tx
}
Expand Down Expand Up @@ -75,7 +70,6 @@ impl NodeQuery {
Ok(NodeInfo {
utxo_validation: config.utxo_validation,
vm_backtrace: config.vm_backtrace,
min_gas_price: config.min_gas_price.into(),
max_tx: (config.max_tx as u64).into(),
max_depth: (config.max_depth as u64).into(),
node_version: VERSION.to_owned(),
Expand Down
4 changes: 3 additions & 1 deletion crates/fuel-core/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use fuel_core_storage::{
};
use std::net::SocketAddr;

use crate::service::adapters::StaticGasPrice;
pub use config::{
Config,
DbType,
Expand All @@ -44,7 +45,8 @@ pub struct SharedState {
/// The PoA adaptor around the shared state of the consensus module.
pub poa_adapter: PoAAdapter,
/// The transaction pool shared state.
pub txpool: fuel_core_txpool::service::SharedState<P2PAdapter, Database>,
pub txpool_shared_state:
fuel_core_txpool::service::SharedState<P2PAdapter, Database, StaticGasPrice>,
/// The P2P network shared state.
#[cfg(feature = "p2p")]
pub network: Option<fuel_core_p2p::service::SharedState>,
Expand Down
19 changes: 15 additions & 4 deletions crates/fuel-core/src/service/adapters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,42 @@ pub mod relayer;
pub mod sync;
pub mod txpool;

#[derive(Debug, Clone)]
pub struct StaticGasPrice {
pub gas_price: u64,
}

impl StaticGasPrice {
pub fn new(gas_price: u64) -> Self {
Self { gas_price }
}
}

#[derive(Clone)]
pub struct PoAAdapter {
shared_state: Option<fuel_core_poa::service::SharedState>,
}

#[derive(Clone)]
pub struct TxPoolAdapter {
service: TxPoolSharedState<P2PAdapter, Database>,
service: TxPoolSharedState<P2PAdapter, Database, StaticGasPrice>,
}

impl TxPoolAdapter {
pub fn new(service: TxPoolSharedState<P2PAdapter, Database>) -> Self {
pub fn new(service: TxPoolSharedState<P2PAdapter, Database, StaticGasPrice>) -> Self {
Self { service }
}
}

#[derive(Clone)]
pub struct TransactionsSource {
txpool: TxPoolSharedState<P2PAdapter, Database>,
txpool: TxPoolSharedState<P2PAdapter, Database, StaticGasPrice>,
_block_height: BlockHeight,
}

impl TransactionsSource {
pub fn new(
txpool: TxPoolSharedState<P2PAdapter, Database>,
txpool: TxPoolSharedState<P2PAdapter, Database, StaticGasPrice>,
block_height: BlockHeight,
) -> Self {
Self {
Expand Down
9 changes: 9 additions & 0 deletions crates/fuel-core/src/service/adapters/graphql_api.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use super::{
BlockImporterAdapter,
BlockProducerAdapter,
StaticGasPrice,
};
use crate::{
database::Database,
fuel_core_graphql_api::ports::{
worker,
BlockProducerPort,
DatabaseMessageProof,
GasPriceEstimate,
P2pPort,
TxPoolPort,
},
Expand Down Expand Up @@ -161,3 +163,10 @@ impl worker::TxPool for TxPoolAdapter {
self.service.send_complete(id, block_height, status)
}
}

#[async_trait::async_trait]
impl GasPriceEstimate for StaticGasPrice {
async fn worst_case_gas_price(&self, _height: BlockHeight) -> u64 {
self.gas_price
}
}
15 changes: 14 additions & 1 deletion crates/fuel-core/src/service/adapters/producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@ use crate::{
BlockProducerAdapter,
ExecutorAdapter,
MaybeRelayerAdapter,
StaticGasPrice,
TransactionsSource,
TxPoolAdapter,
},
sub_services::BlockProducerService,
},
};
use fuel_core_executor::executor::OnceTransactionsSource;
use fuel_core_producer::ports::TxPool;
use fuel_core_producer::{
block_producer::gas_price::{
GasPriceParams,
GasPriceProvider,
},
ports::TxPool,
};
use fuel_core_storage::{
not_found,
tables::FuelBlocks,
Expand Down Expand Up @@ -140,3 +147,9 @@ impl fuel_core_producer::ports::BlockProducerDatabase for Database {
self.storage::<FuelBlocks>().root(height).map(Into::into)
}
}

impl GasPriceProvider for StaticGasPrice {
fn gas_price(&self, _block_height: GasPriceParams) -> Option<u64> {
Some(self.gas_price)
}
}
Loading
Loading