Skip to content

Commit

Permalink
Add concept of DA event index to header, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchTurner committed Mar 28, 2024
1 parent 30ad24e commit 6b80189
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 10 deletions.
46 changes: 36 additions & 10 deletions crates/services/producer/src/block_producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ where
// use the same configuration as the last block -> the same DA height.
// It is deterministic from the result perspective, plus it is more performant
// because we don't need to wait for the relayer to sync.
let header = self._new_header(height, Tai64::now())?;
// TODO: This might not work, it was using the `_new_header` method before, which didn't
// update the DA height. If the tests fail here, that's probably why.
let header = self.new_header(height, Tai64::now()).await?;
let component = Components {
header_to_produce: header,
transactions_source: transactions.clone(),
Expand Down Expand Up @@ -272,18 +274,37 @@ where
height: BlockHeight,
block_time: Tai64,
) -> anyhow::Result<PartialBlockHeader> {
let mut block_header = self._new_header(height, block_time)?;
let new_da_height = self.select_new_da_height(block_header.da_height).await?;
let view = self.view_provider.latest_view();

let last_block = self.previous_block_info(height, &view)?;

block_header.application.da_height = new_da_height;
let PreviousBlockInfo {
prev_root,
da_height: last_da_height,
da_event_index: last_da_event_index,
} = last_block;

Ok(block_header)
let (new_da_height, new_da_event_index) = self
.select_new_da_height(last_da_height, last_da_event_index)
.await?;

let partial_block_header = self._new_header(
height,
block_time,
prev_root,
new_da_height,
new_da_event_index,
)?;

Ok(partial_block_header)
}

async fn select_new_da_height(
&self,
previous_da_height: DaBlockHeight,
) -> anyhow::Result<DaBlockHeight> {
_previous_da_event_index: u64,
) -> anyhow::Result<(DaBlockHeight, u64)> {
// TODO: Use the previous_da_event_index to select the new da_event_index
let best_height = self.relayer.wait_for_at_least(&previous_da_height).await?;
if best_height < previous_da_height {
// If this happens, it could mean a block was erroneously imported
Expand All @@ -294,29 +315,32 @@ where
}
.into())
}
Ok(best_height)
Ok((best_height, 0))
}

fn _new_header(
&self,
height: BlockHeight,
block_time: Tai64,
prev_root: Bytes32,
da_height: DaBlockHeight,
da_event_index: u64,
) -> anyhow::Result<PartialBlockHeader> {
let view = self.view_provider.latest_view();
let previous_block_info = self.previous_block_info(height, &view)?;
let consensus_parameters_version = view.latest_consensus_parameters_version()?;
let state_transition_bytecode_version =
view.latest_state_transition_bytecode_version()?;

Ok(PartialBlockHeader {
application: ApplicationHeader {
da_height: previous_block_info.da_height,
da_height,
da_event_index,
consensus_parameters_version,
state_transition_bytecode_version,
generated: Default::default(),
},
consensus: ConsensusHeader {
prev_root: previous_block_info.prev_root,
prev_root,
height,
time: block_time,
generated: Default::default(),
Expand Down Expand Up @@ -349,6 +373,7 @@ where
Ok(PreviousBlockInfo {
prev_root,
da_height: previous_block.header().da_height,
da_event_index: previous_block.header().da_event_index,
})
}
}
Expand All @@ -357,4 +382,5 @@ where
struct PreviousBlockInfo {
prev_root: Bytes32,
da_height: DaBlockHeight,
da_event_index: u64,
}
5 changes: 5 additions & 0 deletions crates/services/producer/src/block_producer/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,11 @@ mod produce_and_execute_block_txpool {
);
}

#[tokio::test]
async fn only_advances_da_height_if_txs_can_fit() {
todo!()
}

#[tokio::test]
async fn production_fails_on_execution_error() {
let ctx = TestContext::default_from_executor(FailingMockExecutor(Mutex::new(
Expand Down
8 changes: 8 additions & 0 deletions crates/services/producer/src/mocks.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::ports::{
BlockProducerDatabase,
DaBlockCosts,
Executor,
Relayer,
TxPool,
Expand Down Expand Up @@ -68,6 +69,13 @@ impl Relayer for MockRelayer {
) -> anyhow::Result<DaBlockHeight> {
Ok(self.best_finalized_height)
}

async fn get_costs_for_block(
&self,
_height: &DaBlockHeight,
) -> anyhow::Result<Option<DaBlockCosts>> {
todo!()
}
}

#[derive(Default)]
Expand Down
13 changes: 13 additions & 0 deletions crates/services/producer/src/ports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ pub trait TxPool: Send + Sync {
) -> Self::TxSource;
}

pub struct DaBlockCosts {
/// The block height.
pub block_height: DaBlockHeight,
/// The cost of each of the events in the block.
pub event_costs: Vec<Option<u64>>,
}

#[async_trait::async_trait]
pub trait Relayer: Send + Sync {
/// Wait for the relayer to reach at least this height and return the
Expand All @@ -67,6 +74,12 @@ pub trait Relayer: Send + Sync {
&self,
height: &DaBlockHeight,
) -> anyhow::Result<DaBlockHeight>;

/// Get the costs for the block at the given height.
async fn get_costs_for_block(
&self,
height: &DaBlockHeight,
) -> anyhow::Result<Option<DaBlockCosts>>;
}

pub trait Executor<TxSource>: Send + Sync {
Expand Down
2 changes: 2 additions & 0 deletions crates/types/src/blockchain/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ impl From<Block> for PartialFuelBlock {
application:
ApplicationHeader {
da_height,
da_event_index,
consensus_parameters_version,
state_transition_bytecode_version,
..
Expand All @@ -261,6 +262,7 @@ impl From<Block> for PartialFuelBlock {
header: PartialBlockHeader {
application: ApplicationHeader {
da_height,
da_event_index,
consensus_parameters_version,
state_transition_bytecode_version,
generated: Empty {},
Expand Down
3 changes: 3 additions & 0 deletions crates/types/src/blockchain/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ pub struct ApplicationHeader<Generated> {
/// layer 1 chain. They should also verify that the block number isn't too stale and is increasing.
/// Some similar concerns are noted in this issue: <https://github.com/FuelLabs/fuel-specs/issues/220>
pub da_height: DaBlockHeight,
/// The index of the last event included from L1
pub da_event_index: u64,
/// The version of the consensus parameters used to execute this block.
pub consensus_parameters_version: ConsensusParametersVersion,
/// The version of the state transition bytecode used to execute this block.
Expand Down Expand Up @@ -386,6 +388,7 @@ impl PartialBlockHeader {

let application = ApplicationHeader {
da_height: self.application.da_height,
da_event_index: self.da_event_index,
consensus_parameters_version: self.application.consensus_parameters_version,
state_transition_bytecode_version: self
.application
Expand Down

0 comments on commit 6b80189

Please sign in to comment.