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

Fetch consensus parameters from the provider #1808

Merged
merged 6 commits into from
Apr 10, 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ Description of the upcoming release here.

- [#1812](https://github.com/FuelLabs/fuel-core/pull/1812): Follow-up PR to simplify the logic around parallel snapshot creation.

### Changed
- [#1808](https://github.com/FuelLabs/fuel-core/pull/1808): Fetch consensus parameters from the database.

## [Version 0.24.2]

### Changed
Expand Down
3 changes: 1 addition & 2 deletions Cargo.lock

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

8 changes: 2 additions & 6 deletions bin/fuel-core/src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl Command {
SnapshotReader::open(metadata)?
}
};
let chain_config = snapshot_reader.chain_config().clone();
let chain_config = snapshot_reader.chain_config();

#[cfg(feature = "relayer")]
let relayer_cfg = relayer_args.into_config();
Expand Down Expand Up @@ -298,7 +298,7 @@ impl Command {
};

let block_importer =
fuel_core::service::config::fuel_core_importer::Config::new(&chain_config);
fuel_core::service::config::fuel_core_importer::Config::new();

let TxPoolArgs {
tx_pool_ttl,
Expand All @@ -317,7 +317,6 @@ impl Command {
tx_blacklist_messages,
tx_blacklist_contracts,
);
let block_gas_limit = chain_config.consensus_parameters.block_gas_limit();

let config = Config {
addr,
Expand All @@ -333,18 +332,15 @@ impl Command {
txpool: TxPoolConfig::new(
tx_max_number,
tx_max_depth,
chain_config,
utxo_validation,
metrics,
tx_pool_ttl.into(),
tx_number_active_subscriptions,
blacklist,
),
block_producer: ProducerConfig {
utxo_validation,
coinbase_recipient,
metrics,
block_gas_limit,
},
static_gas_price: min_gas_price,
block_importer,
Expand Down
2 changes: 1 addition & 1 deletion crates/chain-config/src/config/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use serde::{
use crate as fuel_core_chain_config;
use fuel_core_chain_config::default_consensus_dev_key;

#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[derive(Clone, Copy, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub enum ConsensusConfig {
PoA { signing_key: Address },
}
Expand Down
7 changes: 0 additions & 7 deletions crates/fuel-core/src/graphql_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ use fuel_core_storage::{
Error as StorageError,
IsNotFound,
};
use fuel_core_types::{
blockchain::primitives::SecretKeyWrapper,
fuel_tx::ConsensusParameters,
secrecy::Secret,
};
use std::net::SocketAddr;

pub mod api_service;
Expand All @@ -26,8 +21,6 @@ pub struct Config {
pub max_tx: usize,
pub max_depth: usize,
pub chain_name: String,
pub consensus_parameters: ConsensusParameters,
pub consensus_key: Option<Secret<SecretKeyWrapper>>,
}

pub trait IntoApiResult<T> {
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,
ConsensusProvider as ConsensusProviderTrait,
GasPriceEstimate,
OffChainDatabase,
OnChainDatabase,
Expand Down Expand Up @@ -91,6 +92,8 @@ pub type P2pService = Box<dyn P2pPort>;

pub type GasPriceProvider = Box<dyn GasPriceEstimate>;

pub type ConsensusProvider = Box<dyn ConsensusProviderTrait>;

#[derive(Clone)]
pub struct SharedState {
pub bound_address: SocketAddr,
Expand Down Expand Up @@ -177,6 +180,7 @@ pub fn new_service<OnChain, OffChain>(
consensus_module: ConsensusModule,
p2p_service: P2pService,
gas_price_provider: GasPriceProvider,
consensus_parameters_provider: ConsensusProvider,
log_threshold_ms: Duration,
request_timeout: Duration,
) -> anyhow::Result<Service>
Expand All @@ -197,6 +201,7 @@ where
.data(consensus_module)
.data(p2p_service)
.data(gas_price_provider)
.data(consensus_parameters_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 @@ -32,6 +32,7 @@ use fuel_core_types::{
Message,
},
fuel_tx::{
ConsensusParameters,
Salt,
Transaction,
TxId,
Expand Down Expand Up @@ -285,3 +286,8 @@ pub mod worker {
);
}
}

pub trait ConsensusProvider: Send + Sync {
/// Returns latest consensus parameters.
fn latest_consensus_params(&self) -> Arc<ConsensusParameters>;
}
10 changes: 5 additions & 5 deletions crates/fuel-core/src/schema/balance.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
fuel_core_graphql_api::{
api_service::ConsensusProvider,
database::ReadView,
Config,
},
query::BalanceQueryData,
schema::scalars::{
Expand Down Expand Up @@ -58,8 +58,8 @@ impl BalanceQuery {
) -> async_graphql::Result<Balance> {
let query: &ReadView = ctx.data_unchecked();
let base_asset_id = *ctx
.data_unchecked::<Config>()
.consensus_parameters
.data_unchecked::<ConsensusProvider>()
.latest_consensus_params()
.base_asset_id();
let balance = query.balance(owner.0, asset_id.0, base_asset_id)?.into();
Ok(balance)
Expand All @@ -86,8 +86,8 @@ impl BalanceQuery {
crate::schema::query_pagination(after, before, first, last, |_, direction| {
let owner = filter.owner.into();
let base_asset_id = *ctx
.data_unchecked::<Config>()
.consensus_parameters
.data_unchecked::<ConsensusProvider>()
.latest_consensus_params()
.base_asset_id();
Ok(query
.balances(owner, direction, base_asset_id)
Expand Down
87 changes: 51 additions & 36 deletions crates/fuel-core/src/schema/chain.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
fuel_core_graphql_api::{
api_service::ConsensusProvider,
database::ReadView,
Config as GraphQLConfig,
},
graphql_api::Config,
query::{
Expand Down Expand Up @@ -29,10 +29,13 @@ use fuel_core_types::{
fuel_tx,
fuel_tx::GasCostsValues,
};
use std::ops::Deref;
use std::{
ops::Deref,
sync::Arc,
};

pub struct ChainInfo;
pub struct ConsensusParameters(fuel_tx::ConsensusParameters);
pub struct ConsensusParameters(Arc<fuel_tx::ConsensusParameters>);
pub struct TxParameters(fuel_tx::TxParameters);
pub struct PredicateParameters(fuel_tx::PredicateParameters);
pub struct ScriptParameters(fuel_tx::ScriptParameters);
Expand Down Expand Up @@ -121,94 +124,102 @@ impl Version {
#[Object]
impl ConsensusParameters {
async fn version(&self) -> ConsensusParametersVersion {
match self.0 {
match self.0.as_ref() {
fuel_tx::ConsensusParameters::V1(_) => {
ConsensusParametersVersion::V1(Version(1))
}
}
}

async fn tx_params(&self, ctx: &Context<'_>) -> async_graphql::Result<TxParameters> {
let config = ctx.data_unchecked::<GraphQLConfig>();
let params = ctx
.data_unchecked::<ConsensusProvider>()
.latest_consensus_params();

Ok(TxParameters(
config.consensus_parameters.tx_params().to_owned(),
))
Ok(TxParameters(params.tx_params().to_owned()))
}

async fn predicate_params(
&self,
ctx: &Context<'_>,
) -> async_graphql::Result<PredicateParameters> {
let config = ctx.data_unchecked::<GraphQLConfig>();
let params = ctx
.data_unchecked::<ConsensusProvider>()
.latest_consensus_params();

Ok(PredicateParameters(
config.consensus_parameters.predicate_params().to_owned(),
))
Ok(PredicateParameters(params.predicate_params().to_owned()))
}

async fn script_params(
&self,
ctx: &Context<'_>,
) -> async_graphql::Result<ScriptParameters> {
let config = ctx.data_unchecked::<GraphQLConfig>();
let params = ctx
.data_unchecked::<ConsensusProvider>()
.latest_consensus_params();

Ok(ScriptParameters(
config.consensus_parameters.script_params().to_owned(),
))
Ok(ScriptParameters(params.script_params().to_owned()))
}

async fn contract_params(
&self,
ctx: &Context<'_>,
) -> async_graphql::Result<ContractParameters> {
let config = ctx.data_unchecked::<GraphQLConfig>();
let params = ctx
.data_unchecked::<ConsensusProvider>()
.latest_consensus_params();

Ok(ContractParameters(
config.consensus_parameters.contract_params().to_owned(),
))
Ok(ContractParameters(params.contract_params().to_owned()))
}

async fn fee_params(
&self,
ctx: &Context<'_>,
) -> async_graphql::Result<FeeParameters> {
let config = ctx.data_unchecked::<GraphQLConfig>();
let params = ctx
.data_unchecked::<ConsensusProvider>()
.latest_consensus_params();

Ok(FeeParameters(
config.consensus_parameters.fee_params().to_owned(),
))
Ok(FeeParameters(params.fee_params().to_owned()))
}

async fn base_asset_id(&self, ctx: &Context<'_>) -> async_graphql::Result<AssetId> {
let config = ctx.data_unchecked::<GraphQLConfig>();
let params = ctx
.data_unchecked::<ConsensusProvider>()
.latest_consensus_params();

Ok(AssetId(*config.consensus_parameters.base_asset_id()))
Ok(AssetId(*params.base_asset_id()))
}

async fn block_gas_limit(&self, ctx: &Context<'_>) -> async_graphql::Result<U64> {
let config = ctx.data_unchecked::<GraphQLConfig>();
let params = ctx
.data_unchecked::<ConsensusProvider>()
.latest_consensus_params();

Ok(config.consensus_parameters.block_gas_limit().into())
Ok(params.block_gas_limit().into())
}

async fn chain_id(&self) -> U64 {
(*self.0.chain_id()).into()
}

async fn gas_costs(&self, ctx: &Context<'_>) -> async_graphql::Result<GasCosts> {
let config = ctx.data_unchecked::<GraphQLConfig>();
let params = ctx
.data_unchecked::<ConsensusProvider>()
.latest_consensus_params();

Ok(GasCosts(config.consensus_parameters.gas_costs().clone()))
Ok(GasCosts(params.gas_costs().clone()))
}

async fn privileged_address(
&self,
ctx: &Context<'_>,
) -> async_graphql::Result<Address> {
let config = ctx.data_unchecked::<GraphQLConfig>();
let params = ctx
.data_unchecked::<ConsensusProvider>()
.latest_consensus_params();

Ok(Address(*config.consensus_parameters.privileged_address()))
Ok(Address(*params.privileged_address()))
}
}

Expand Down Expand Up @@ -822,15 +833,19 @@ impl ChainInfo {
&self,
ctx: &Context<'_>,
) -> async_graphql::Result<ConsensusParameters> {
let config = ctx.data_unchecked::<GraphQLConfig>();
let params = ctx
.data_unchecked::<ConsensusProvider>()
.latest_consensus_params();

Ok(ConsensusParameters(config.consensus_parameters.clone()))
Ok(ConsensusParameters(params))
}

async fn gas_costs(&self, ctx: &Context<'_>) -> async_graphql::Result<GasCosts> {
let config = ctx.data_unchecked::<GraphQLConfig>();
let params = ctx
.data_unchecked::<ConsensusProvider>()
.latest_consensus_params();

Ok(GasCosts(config.consensus_parameters.gas_costs().clone()))
Ok(GasCosts(params.gas_costs().clone()))
}
}

Expand Down
Loading
Loading