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 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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,
GraphQLGasPrice,
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 GraphQLGasPrice>;

#[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
6 changes: 6 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,12 @@ pub trait P2pPort: Send + Sync {
async fn all_peer_info(&self) -> anyhow::Result<Vec<PeerInfo>>;
}

#[async_trait::async_trait]
pub trait GraphQLGasPrice: Send + Sync {
async fn known_gas_price(&self, height: BlockHeight) -> Option<u64>;
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
40 changes: 27 additions & 13 deletions crates/fuel-core/src/schema/gas_price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ 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,
};
use async_graphql::{
Expand Down Expand Up @@ -40,14 +38,20 @@ impl LatestGasPriceQuery {
&self,
ctx: &Context<'_>,
) -> async_graphql::Result<LatestGasPrice> {
let config = ctx.data_unchecked::<GraphQLConfig>();

let gas_price_provider = ctx.data_unchecked::<GasPriceProvider>();
let query: &ReadView = ctx.data_unchecked();

let latest_block: Block<_> = query.latest_block()?;
let block_height = u32::from(*latest_block.header().height());
let gas_price = gas_price_provider
.known_gas_price(block_height.into())
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is that gas price used by the block producer at the last block, or is that the gas price for the next block?

If it is gas price used for the last block, then maybe we can get it from the Mint transaction=)

.await
.ok_or(async_graphql::Error::new(format!(
"Gas price not found for latest block:{block_height:?}"
)))?;

Ok(LatestGasPrice {
gas_price: config.min_gas_price.into(),
gas_price: gas_price.into(),
block_height: block_height.into(),
})
}
Expand Down Expand Up @@ -77,13 +81,23 @@ 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: Block<_> = query.latest_block()?;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Getting Block is super expensive here, maybe we can get the latest height?=D

let latest_block_height = u32::from(*latest_block.header().height());
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:?}"
)))?;

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 })
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
52 changes: 47 additions & 5 deletions crates/fuel-core/src/service/adapters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@ use crate::{
database_description::relayer::Relayer,
Database,
},
graphql_api::ports::GraphQLGasPrice,
service::sub_services::BlockProducerService,
};
use fuel_core_consensus_module::{
block_verifier::Verifier,
RelayerConsensusConfig,
};
use fuel_core_executor::executor::Executor;
use fuel_core_producer::block_producer::gas_price::{
GasPriceParams,
ProducerGasPrice,
};
use fuel_core_services::stream::BoxStream;
use fuel_core_txpool::service::SharedState as TxPoolSharedState;
use fuel_core_txpool::{
service::SharedState as TxPoolSharedState,
txpool::TxPoolGasPrice,
};
#[cfg(feature = "p2p")]
use fuel_core_types::services::p2p::peer_reputation::AppScore;
use fuel_core_types::{
Expand All @@ -33,31 +41,65 @@ 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 }
}
}

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

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

#[async_trait::async_trait]
impl GraphQLGasPrice for StaticGasPrice {
async fn known_gas_price(&self, _height: BlockHeight) -> Option<u64> {
Some(self.gas_price)
}

async fn worst_case_gas_price(&self, _height: BlockHeight) -> u64 {
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