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

Move block producer requirements into corresponding crates #814

Merged
merged 4 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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 Cargo.lock

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

1 change: 1 addition & 0 deletions fuel-block-producer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ anyhow = "1.0"
async-trait = "0.1"
fuel-core-interfaces = { path = "../fuel-core-interfaces", version = "0.14.1" }
parking_lot = "0.12"
thiserror = "1.0"
tokio = { version = "1.21", features = ["full"] }
tracing = { version = "0.1" }

Expand Down
50 changes: 31 additions & 19 deletions fuel-block-producer/src/block_producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@ use anyhow::{
Result,
};
use fuel_core_interfaces::{
block_producer::{
BlockProducer as Trait,
Error::{
GenesisBlock,
InvalidDaFinalizationState,
MissingBlock,
},
Relayer,
},
common::{
crypto::ephemeral_merkle_root,
fuel_tx::{
Expand All @@ -43,6 +34,7 @@ use fuel_core_interfaces::{
},
};
use std::sync::Arc;
use thiserror::Error;
use tokio::{
sync::{
Mutex,
Expand All @@ -55,6 +47,27 @@ use tracing::debug;
#[cfg(test)]
mod tests;

#[derive(Error, Debug)]
pub enum Error {
ControlCplusControlV marked this conversation as resolved.
Show resolved Hide resolved
#[error(
"0 is an invalid block height for production. It is reserved for genesis data."
)]
GenesisBlock,
#[error("Previous block height {0} doesn't exist")]
MissingBlock(BlockHeight),
#[error("Best finalized da_height {best} is behind previous block da_height {previous_block}")]
InvalidDaFinalizationState {
best: DaBlockHeight,
previous_block: DaBlockHeight,
},
}

#[async_trait::async_trait]
pub trait Relayer: Sync + Send {
Copy link
Member

@Voxelot Voxelot Dec 5, 2022

Choose a reason for hiding this comment

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

could we move this to a ports module? Just so we have a consistent pattern for locating these kinds of external dep interfaces within each crate.

/// Get the best finalized height from the DA layer
async fn get_best_finalized_da_height(&self) -> Result<DaBlockHeight>;
}

pub struct Producer {
pub config: Config,
pub db: Box<dyn BlockProducerDatabase>,
Expand All @@ -67,10 +80,9 @@ pub struct Producer {
pub dry_run_semaphore: Semaphore,
}

#[async_trait::async_trait]
impl Trait for Producer {
impl Producer {
/// Produces and execute block for the specified height
async fn produce_and_execute_block(
pub async fn produce_and_execute_block(
&self,
height: BlockHeight,
max_gas: Word,
Expand Down Expand Up @@ -111,10 +123,10 @@ impl Trait for Producer {
Ok(result)
}

// simulate a transaction without altering any state. Does not aquire the production lock
// since it is basically a "read only" operation and shouldn't get in the way of normal
// production.
async fn dry_run(
/// Simulate a transaction without altering any state. Does not aquire the production lock
/// since it is basically a "read only" operation and shouldn't get in the way of normal
/// production.
pub async fn dry_run(
&self,
transaction: Transaction,
height: Option<BlockHeight>,
Expand Down Expand Up @@ -184,7 +196,7 @@ impl Producer {
if best_height < previous_da_height {
// If this happens, it could mean a block was erroneously imported
// without waiting for our relayer's da_height to catch up to imported da_height.
return Err(InvalidDaFinalizationState {
return Err(Error::InvalidDaFinalizationState {
best: best_height,
previous_block: previous_da_height,
}
Expand All @@ -196,7 +208,7 @@ impl Producer {
fn previous_block_info(&self, height: BlockHeight) -> Result<PreviousBlockInfo> {
// block 0 is reserved for genesis
if height == 0u32.into() {
Err(GenesisBlock.into())
Err(Error::GenesisBlock.into())
}
// if this is the first block, fill in base metadata from genesis
else if height == 1u32.into() {
Expand All @@ -211,7 +223,7 @@ impl Producer {
let previous_block = self
.db
.get_block(prev_height)?
.ok_or(MissingBlock(prev_height))?;
.ok_or(Error::MissingBlock(prev_height))?;
// TODO: this should use a proper BMT MMR
let hash = previous_block.id();
let prev_root = ephemeral_merkle_root(
Expand Down
5 changes: 1 addition & 4 deletions fuel-block-producer/src/block_producer/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
block_producer::Error,
mocks::{
FailingMockExecutor,
MockDb,
Expand All @@ -10,10 +11,6 @@ use crate::{
Producer,
};
use fuel_core_interfaces::{
block_producer::{
BlockProducer,
Error,
},
executor::Executor,
model::{
FuelApplicationHeader,
Expand Down
6 changes: 4 additions & 2 deletions fuel-block-producer/src/mocks.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use super::db::BlockProducerDatabase;
use crate::ports::TxPool;
use crate::{
block_producer::Relayer,
ports::TxPool,
};
use anyhow::Result;
use fuel_core_interfaces::{
block_producer::Relayer,
common::{
fuel_storage::StorageInspect,
fuel_tx::{
Expand Down
1 change: 0 additions & 1 deletion fuel-block-producer/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use fuel_block_producer::{
};
use fuel_core_interfaces::{
block_importer::ImportBlockBroadcast,
block_producer::BlockProducer as Trait,
common::{
fuel_asm::Opcode,
fuel_crypto::{
Expand Down
3 changes: 0 additions & 3 deletions fuel-core-bft/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ use fuel_core_interfaces::{
ImportBlockBroadcast,
ImportBlockMpsc,
},
block_producer::BlockProducer,
p2p::P2pRequestEvent,
};
use parking_lot::Mutex;
use std::sync::Arc;
use tokio::{
sync::{
broadcast,
Expand All @@ -35,7 +33,6 @@ impl Service {
pub async fn start(
&self,
_p2p_consensus: mpsc::Sender<P2pRequestEvent>,
_block_producer: Arc<dyn BlockProducer>,
_block_importer_sender: mpsc::Sender<ImportBlockMpsc>,
_block_importer_broadcast: broadcast::Receiver<ImportBlockBroadcast>,
) {
Expand Down
71 changes: 0 additions & 71 deletions fuel-core-interfaces/src/block_producer.rs

This file was deleted.

1 change: 0 additions & 1 deletion fuel-core-interfaces/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod bft;
pub mod block_importer;
pub mod block_producer;
pub mod db;
pub mod executor;
pub mod model;
Expand Down
3 changes: 1 addition & 2 deletions fuel-core/src/schema/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ use async_graphql::{
Subscription,
};
use fuel_core_interfaces::{
block_producer::BlockProducer,
common::{
fuel_storage::StorageAsRef,
fuel_tx::{
Expand Down Expand Up @@ -335,7 +334,7 @@ impl TxMutation {
// for read-only calls.
utxo_validation: Option<bool>,
) -> async_graphql::Result<Vec<receipt::Receipt>> {
let block_producer = ctx.data_unchecked::<Arc<dyn BlockProducer>>();
let block_producer = ctx.data_unchecked::<Arc<fuel_block_producer::Producer>>();

let mut tx = FuelTx::from_bytes(&tx.0)?;
tx.precompute();
Expand Down
48 changes: 40 additions & 8 deletions fuel-core/src/service/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ use anyhow::Result;
use fuel_core_interfaces::p2p::P2pDb;
use fuel_core_interfaces::{
self,
block_producer::{
BlockProducer,
Relayer as BlockProducerRelayer,
common::{
fuel_tx::Receipt,
prelude::{
Transaction,
Word,
},
},
common::fuel_tx::Receipt,
executor::{
Error,
ExecutionBlock,
ExecutionResult,
Executor as ExecutorTrait,
},
model::BlockHeight,
relayer::RelayerDb,
txpool::{
Sender,
Expand All @@ -45,7 +48,7 @@ use tokio::{
pub struct Modules {
pub txpool: Arc<fuel_txpool::Service>,
pub block_importer: Arc<fuel_block_importer::Service>,
pub block_producer: Arc<dyn BlockProducer>,
pub block_producer: Arc<fuel_block_producer::Producer>,
pub coordinator: Arc<CoordinatorService>,
pub sync: Arc<fuel_sync::Service>,
#[cfg(feature = "relayer")]
Expand Down Expand Up @@ -202,15 +205,16 @@ pub async fn start_modules(config: &Config, database: &Database) -> Result<Modul
txpool_builder.tx_status_subscribe(),
txpool_builder.sender().clone(),
block_import_tx,
block_producer.clone(),
PoACoordinatorAdapter {
block_producer: block_producer.clone(),
},
database.clone(),
)
.await;
}
CoordinatorService::Bft(bft) => {
bft.start(
p2p_request_event_sender.clone(),
block_producer.clone(),
block_importer.sender().clone(),
block_importer.subscribe(),
)
Expand Down Expand Up @@ -283,7 +287,7 @@ struct MaybeRelayerAdapter {
}

#[async_trait::async_trait]
impl BlockProducerRelayer for MaybeRelayerAdapter {
impl fuel_block_producer::block_producer::Relayer for MaybeRelayerAdapter {
async fn get_best_finalized_da_height(
&self,
) -> Result<fuel_core_interfaces::model::DaBlockHeight> {
Expand All @@ -301,3 +305,31 @@ impl BlockProducerRelayer for MaybeRelayerAdapter {
.unwrap_or_default())
}
}

struct PoACoordinatorAdapter {
Copy link
Member

@Voxelot Voxelot Dec 5, 2022

Choose a reason for hiding this comment

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

block_producer: Arc<fuel_block_producer::Producer>,
}

#[async_trait::async_trait]
impl fuel_poa_coordinator::service::BlockProducer for PoACoordinatorAdapter {
async fn produce_and_execute_block(
&self,
height: BlockHeight,
max_gas: Word,
) -> anyhow::Result<ExecutionResult> {
self.block_producer
.produce_and_execute_block(height, max_gas)
.await
}

async fn dry_run(
&self,
transaction: Transaction,
height: Option<BlockHeight>,
utxo_validation: Option<bool>,
) -> anyhow::Result<Vec<Receipt>> {
ControlCplusControlV marked this conversation as resolved.
Show resolved Hide resolved
self.block_producer
.dry_run(transaction, height, utxo_validation)
.await
}
}
2 changes: 1 addition & 1 deletion fuel-poa-coordinator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ description = "Fuel Core PoA Coordinator"

[dependencies]
anyhow = "1.0"
async-trait = "0.1"
fuel-core-interfaces = { path = "../fuel-core-interfaces", version = "0.14.1" }
humantime-serde = "1.1.1"
parking_lot = "0.12"
Expand All @@ -19,7 +20,6 @@ tokio = { version = "1.21", features = ["full"] }
tracing = "0.1"

[dev-dependencies]
async-trait = "0.1"
fuel-core-interfaces = { path = "../fuel-core-interfaces", features = ["test-helpers"] }
mockall = "0.11"
rand = "0.8"
Expand Down
Loading