Skip to content

Commit

Permalink
Use correct Event type and record it inside process_da method
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchTurner committed Mar 21, 2024
1 parent 8eb2714 commit f6e6e22
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 59 deletions.
2 changes: 1 addition & 1 deletion crates/fuel-core/src/database/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ mod tests {
},
};
let block = PartialFuelBlock::new(header, vec![]);
block.generate(&[], &[])
block.generate(&[], Default::default())
})
.collect::<Vec<_>>();

Expand Down
4 changes: 2 additions & 2 deletions crates/fuel-core/src/query/message/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ async fn can_build_message_proof() {
generated: Default::default(),
},
}
.generate(&[], &[], &[]);
.generate(&[], &[], Default::default());
let commit_block = CompressedBlock::test(commit_block_header, vec![]);
let message_block_header = PartialBlockHeader {
application: ApplicationHeader {
Expand All @@ -157,7 +157,7 @@ async fn can_build_message_proof() {
generated: Default::default(),
},
}
.generate(&[], &message_ids, &[]);
.generate(&[], &message_ids, Default::default());
let message_block = CompressedBlock::test(message_block_header, TXNS.to_vec());

let block_proof = MerkleProof {
Expand Down
2 changes: 1 addition & 1 deletion crates/fuel-core/src/service/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ pub fn create_genesis_block(config: &Config) -> Block {
let da_block_height = config.state_reader.da_block_height();
let transactions = vec![];
let message_ids = &[];
let events = &[];
let events = Default::default();
Block::new(
PartialBlockHeader {
application: ApplicationHeader::<Empty> {
Expand Down
2 changes: 1 addition & 1 deletion crates/services/consensus_module/poa/src/verifier/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn correct() -> Input {
..Default::default()
},
};
let block_header = partial_header.generate(&txs, &[], &[]);
let block_header = partial_header.generate(&txs, &[], Default::default());

Input {
block_header_merkle_root: [2u8; 32],
Expand Down
32 changes: 31 additions & 1 deletion crates/services/executor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ use fuel_core_types::{
RegId,
Word,
},
fuel_merkle::binary::root_calculator::MerkleRootCalculator,
fuel_tx::{
field::{
InputContract,
Expand Down Expand Up @@ -304,6 +305,7 @@ pub struct ExecutionData {
events: Vec<ExecutorEvent>,
changes: Changes,
pub skipped_transactions: Vec<(TxId, ExecutorError)>,
event_inbox_root: Bytes32,
}

/// Per-block execution options
Expand Down Expand Up @@ -499,12 +501,13 @@ where
skipped_transactions,
events,
changes,
event_inbox_root,
..
} = execution_data;

// Now that the transactions have been executed, generate the full header.

let block = block.generate(&message_ids[..], &events);
let block = block.generate(&message_ids[..], event_inbox_root);

let finalized_block_id = block.id();

Expand Down Expand Up @@ -557,6 +560,7 @@ where
events: Vec::new(),
changes: Default::default(),
skipped_transactions: Vec::new(),
event_inbox_root: Default::default(),
};
let execution_data = &mut data;

Expand Down Expand Up @@ -724,13 +728,37 @@ where
return Err(ExecutorError::DaHeightExceededItsLimit)
};

// // Generate the message merkle root.
// let message_outbox_root = message_ids
// .iter()
// .fold(MerkleRootCalculator::new(), |mut tree, id| {
// tree.push(id.as_ref());
// tree
// })
// .root()
// .into();
//
// // Generate the inbound event merkle root.
// let event_inbox_root = events
// .iter()
// .map(|event| event.hash())
// .fold(MerkleRootCalculator::new(), |mut tree, event| {
// tree.push(event.as_ref());
// tree
// })
// .root()
// .into();

let mut root_calculator = MerkleRootCalculator::new();

for da_height in next_unprocessed_da_height..=header.da_height.0 {
let da_height = da_height.into();
let events = self
.relayer
.get_events(&da_height)
.map_err(|err| ExecutorError::RelayerError(err.into()))?;
for event in events {
root_calculator.push(event.hash().as_ref());
match event {
Event::Message(message) => {
if message.da_height() != da_height {
Expand All @@ -747,6 +775,8 @@ where
}
}

execution_data.event_inbox_root = root_calculator.root().into();

Ok(())
}

Expand Down
8 changes: 4 additions & 4 deletions crates/services/producer/src/block_producer/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ mod produce_and_execute_block_txpool {
},
transactions: vec![],
}
.generate(&[], &[])
.generate(&[], Default::default())
.compress(&Default::default());

let db = MockDb {
Expand Down Expand Up @@ -162,7 +162,7 @@ mod produce_and_execute_block_txpool {
},
transactions: vec![],
}
.generate(&[], &[])
.generate(&[], Default::default())
.compress(&Default::default());

// Given
Expand Down Expand Up @@ -215,7 +215,7 @@ mod produce_and_execute_block_txpool {
},
transactions: vec![],
}
.generate(&[], &[])
.generate(&[], Default::default())
.compress(&Default::default());

// Given
Expand Down Expand Up @@ -284,7 +284,7 @@ mod produce_and_execute_block_txpool {
},
transactions: vec![],
}
.generate(&[], &[])
.generate(&[], Default::default())
.compress(&Default::default());

let db = MockDb {
Expand Down
7 changes: 6 additions & 1 deletion crates/services/producer/src/mocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,12 @@ fn to_block(component: &Components<Vec<ArcPoolTx>>) -> Block {
.into_iter()
.map(|tx| tx.as_ref().into())
.collect();
Block::new(component.header_to_produce.clone(), transactions, &[], &[])
Block::new(
component.header_to_produce.clone(),
transactions,
&[],
Default::default(),
)
}

impl Executor<Vec<ArcPoolTx>> for MockExecutor {
Expand Down
2 changes: 1 addition & 1 deletion crates/storage/src/structured_storage/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ mod tests {
},
};
let block = PartialFuelBlock::new(header, vec![]);
block.generate(&[], &[])
block.generate(&[], Default::default())
})
.collect::<Vec<_>>();

Expand Down
15 changes: 10 additions & 5 deletions crates/types/src/blockchain/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ use crate::{
UniqueIdentifier,
},
fuel_types::{
Bytes32,
ChainId,
MessageId,
},
services::executor::Event,
};

/// Version-able block type
Expand Down Expand Up @@ -87,10 +87,10 @@ impl Block<Transaction> {
header: PartialBlockHeader,
transactions: Vec<Transaction>,
message_ids: &[MessageId],
events: &[Event],
event_inbox_root: Bytes32,
) -> Self {
let inner = BlockV1 {
header: header.generate(&transactions, message_ids, events),
header: header.generate(&transactions, message_ids, event_inbox_root),
transactions,
};
Block::V1(inner)
Expand Down Expand Up @@ -220,8 +220,13 @@ impl PartialFuelBlock {
///
/// Message ids are produced by executed the transactions and collecting
/// the ids from the receipts of messages outputs.
pub fn generate(self, message_ids: &[MessageId], events: &[Event]) -> Block {
Block::new(self.header, self.transactions, message_ids, events)
pub fn generate(self, message_ids: &[MessageId], event_inbox_root: Bytes32) -> Block {
Block::new(
self.header,
self.transactions,
message_ids,
event_inbox_root,
)
}
}

Expand Down
14 changes: 1 addition & 13 deletions crates/types/src/blockchain/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use crate::{
Bytes32,
MessageId,
},
services::executor::Event,
};
use tai64::Tai64;

Expand Down Expand Up @@ -369,7 +368,7 @@ impl PartialBlockHeader {
self,
transactions: &[Transaction],
message_ids: &[MessageId],
events: &[Event],
event_inbox_root: Bytes32,
) -> BlockHeader {
// Generate the transaction merkle root.
let transactions_root = generate_txns_root(transactions);
Expand All @@ -384,17 +383,6 @@ impl PartialBlockHeader {
.root()
.into();

// Generate the inbound event merkle root.
let event_inbox_root = events
.iter()
.map(|event| event.hash())
.fold(MerkleRootCalculator::new(), |mut tree, event| {
tree.push(event.as_ref());
tree
})
.root()
.into();

let application = ApplicationHeader {
da_height: self.application.da_height,
consensus_parameters_version: self.application.consensus_parameters_version,
Expand Down
13 changes: 0 additions & 13 deletions crates/types/src/entities/coins/coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use crate::{
fuel_asm::Word,
fuel_crypto,
fuel_tx::{
input::coin::{
CoinPredicate,
Expand All @@ -15,7 +14,6 @@ use crate::{
fuel_types::{
Address,
AssetId,
Bytes32,
},
};

Expand Down Expand Up @@ -46,17 +44,6 @@ impl Coin {
tx_pointer: self.tx_pointer,
})
}

/// Hash the inner values of the coin into a single `Bytes32` hash.
pub fn hash(&self) -> Bytes32 {
let hasher = fuel_crypto::Hasher::default()
.chain(self.owner)
.chain(self.amount.to_be_bytes())
.chain(self.asset_id)
.chain(self.tx_pointer.block_height().to_be_bytes())
.chain(self.tx_pointer.tx_index().to_be_bytes());
hasher.finalize()
}
}

/// The compressed version of the `Coin` with minimum fields required for
Expand Down
17 changes: 1 addition & 16 deletions crates/types/src/services/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ use crate::{
},
services::Uncommitted,
};
use std::{
error::Error as StdError,
ops::Deref,
};
use std::error::Error as StdError;

/// The alias for executor result.
pub type Result<T> = core::result::Result<T, Error>;
Expand Down Expand Up @@ -69,18 +66,6 @@ pub enum Event {
CoinConsumed(Coin),
}

impl Event {
/// Get the hash of the internal values of the event.
pub fn hash(&self) -> Bytes32 {
match self {
Event::MessageImported(message) => (*message.id().deref()).into(),
Event::MessageConsumed(message) => (*message.id().deref()).into(),
Event::CoinCreated(coin) => coin.hash(),
Event::CoinConsumed(coin) => coin.hash(),
}
}
}

/// The status of a transaction after it is executed.
#[derive(Debug, Clone)]
pub struct TransactionExecutionStatus {
Expand Down
9 changes: 9 additions & 0 deletions crates/types/src/services/relayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
use crate::{
blockchain::primitives::DaBlockHeight,
entities::message::Message,
fuel_types::Bytes32,
};
use std::ops::Deref;

/// The event that may come from the relayer.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand All @@ -20,6 +22,13 @@ impl Event {
Event::Message(message) => message.da_height(),
}
}

/// Get hashed value of the event.
pub fn hash(&self) -> Bytes32 {
match self {
Event::Message(message) => (*message.id().deref()).into(),
}
}
}

impl From<Message> for Event {
Expand Down

0 comments on commit f6e6e22

Please sign in to comment.