Skip to content

Commit

Permalink
Move block producer requirements into corresponding crates (#814)
Browse files Browse the repository at this point in the history
* Move block-producer requirements into corresponding crates.
Reused patter of adapter in the `fuel-core`.

* Apply nits from the review
  • Loading branch information
xgreenx authored Dec 6, 2022
1 parent 1c4d4dd commit d731735
Show file tree
Hide file tree
Showing 20 changed files with 219 additions and 204 deletions.
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
49 changes: 29 additions & 20 deletions fuel-block-producer/src/block_producer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::{
db::BlockProducerDatabase,
ports::TxPool,
ports::{
Relayer,
TxPool,
},
Config,
};
use anyhow::{
Expand All @@ -9,15 +12,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 +37,7 @@ use fuel_core_interfaces::{
},
};
use std::sync::Arc;
use thiserror::Error;
use tokio::{
sync::{
Mutex,
Expand All @@ -55,6 +50,21 @@ use tracing::debug;
#[cfg(test)]
mod tests;

#[derive(Error, Debug)]
pub enum Error {
#[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,
},
}

pub struct Producer {
pub config: Config,
pub db: Box<dyn BlockProducerDatabase>,
Expand All @@ -67,10 +77,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 +120,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 +193,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 +205,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 +220,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::ports::{
Relayer,
TxPool,
};
use anyhow::Result;
use fuel_core_interfaces::{
block_producer::Relayer,
common::{
fuel_storage::StorageInspect,
fuel_tx::{
Expand Down
10 changes: 8 additions & 2 deletions fuel-block-producer/src/ports.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use anyhow::Result;
use async_trait::async_trait;
use fuel_core_interfaces::model::{
ArcPoolTx,
BlockHeight,
DaBlockHeight,
};

#[async_trait]
Expand All @@ -13,5 +13,11 @@ pub trait TxPool: Sync + Send {
block_height: BlockHeight,
// The upper limit for the total amount of gas of these txs
max_gas: u64,
) -> Result<Vec<ArcPoolTx>>;
) -> anyhow::Result<Vec<ArcPoolTx>>;
}

#[async_trait::async_trait]
pub trait Relayer: Sync + Send {
/// Get the best finalized height from the DA layer
async fn get_best_finalized_da_height(&self) -> anyhow::Result<DaBlockHeight>;
}
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
1 change: 1 addition & 0 deletions fuel-core/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub use config::{
VMConfig,
};

pub mod adapters;
pub mod config;
pub(crate) mod genesis;
pub mod graph_api;
Expand Down
Loading

0 comments on commit d731735

Please sign in to comment.