Skip to content

Commit

Permalink
Make block header a versionable enum
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchTurner committed Jan 24, 2024
1 parent a0b022d commit 44bfe11
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 23 deletions.
2 changes: 1 addition & 1 deletion crates/fuel-core/src/schema/dap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl ConcreteStorage {

let vm_database = VmStorage::new(
storage.as_ref().clone(),
&block.header().consensus,
&block.header().consensus(),
// TODO: Use a real coinbase address
Default::default(),
);
Expand Down
2 changes: 1 addition & 1 deletion crates/services/consensus_module/poa/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub fn verify_block_fields<D: Database>(
);

ensure!(
header.consensus.application_hash == header.application.hash(),
header.consensus().application_hash == header.application().hash(),
"The application hash mismatch."
);

Expand Down
2 changes: 1 addition & 1 deletion crates/services/sync/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ where
skip_all,
fields(
height = **block.entity.header().height(),
id = %block.entity.header().consensus.generated.application_hash
id = %block.entity.header().consensus().generated.application_hash
),
err
)]
Expand Down
4 changes: 2 additions & 2 deletions crates/services/sync/src/import/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ pub fn random_peer() -> PeerId {
pub fn empty_header<I: Into<BlockHeight>>(i: I) -> SealedBlockHeader {
let mut header = BlockHeader::default();
let height = i.into();
header.consensus.height = height;
header.consensus_mut().height = height;
let transaction_tree =
fuel_core_types::fuel_merkle::binary::root_calculator::MerkleRootCalculator::new(
);
header.application.generated.transactions_root = transaction_tree.root().into();
header.application_mut().generated.transactions_root = transaction_tree.root().into();

let consensus = Consensus::default();
Sealed {
Expand Down
5 changes: 3 additions & 2 deletions crates/types/src/blockchain/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use super::{
},
};
use crate::{
blockchain::header::BlockHeaderV1,
fuel_tx::{
Transaction,
TxId,
Expand Down Expand Up @@ -227,7 +228,7 @@ impl From<Block> for PartialFuelBlock {
match block {
Block::V1(BlockV1 {
header:
BlockHeader {
BlockHeader::V1(BlockHeaderV1 {
application: ApplicationHeader { da_height, .. },
consensus:
ConsensusHeader {
Expand All @@ -237,7 +238,7 @@ impl From<Block> for PartialFuelBlock {
..
},
..
},
}),
transactions,
}) => Self {
header: PartialBlockHeader {
Expand Down
96 changes: 80 additions & 16 deletions crates/types/src/blockchain/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,22 @@ use crate::{
};
use tai64::Tai64;

/// Version-able block header type
#[derive(Clone, Debug, derivative::Derivative)]
#[derivative(PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum BlockHeader {
/// V1 BlockHeader
V1(BlockHeaderV1),
}

/// A fuel block header that has all the fields generated because it
/// has been executed.
#[derive(Clone, Debug, derivative::Derivative)]
#[derivative(PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockHeader {
pub struct BlockHeaderV1 {
/// The application header.
pub application: ApplicationHeader<GeneratedApplicationFields>,
/// The consensus header.
Expand All @@ -37,6 +47,58 @@ pub struct BlockHeader {
metadata: Option<BlockHeaderMetadata>,
}

impl From<BlockHeaderV1> for BlockHeader {
fn from(v1: BlockHeaderV1) -> Self {
BlockHeader::V1(v1)
}
}

impl BlockHeader {
/// Getter for consensus portion of header
pub fn consensus(&self) -> &ConsensusHeader<GeneratedConsensusFields> {
match self {
BlockHeader::V1(v1) => &v1.consensus,
}
}

/// Getter for application portion of header
pub fn application(&self) -> &ApplicationHeader<GeneratedApplicationFields> {
match self {
BlockHeader::V1(v1) => &v1.application,
}
}

/// Getter for metadata portion of header
pub fn metadata(&self) -> &Option<BlockHeaderMetadata> {
match self {
BlockHeader::V1(v1) => &v1.metadata,
}
}

/// Mutable getter for consensus portion of header
pub fn consensus_mut(&mut self) -> &mut ConsensusHeader<GeneratedConsensusFields> {
match self {
BlockHeader::V1(v1) => &mut v1.consensus,
}
}

/// Mutable getter for application portion of header
pub fn application_mut(
&mut self,
) -> &mut ApplicationHeader<GeneratedApplicationFields> {
match self {
BlockHeader::V1(v1) => &mut v1.application,
}
}

/// Mutable getter for metadata portion of header
pub fn metadata_mut(&mut self) -> &mut Option<BlockHeaderMetadata> {
match self {
BlockHeader::V1(v1) => &mut v1.metadata,
}
}
}

#[derive(Clone, Debug)]
#[cfg_attr(any(test, feature = "test-helpers"), derive(Default))]
/// A partially complete fuel block header that doesn't not
Expand Down Expand Up @@ -118,11 +180,12 @@ pub struct BlockHeaderMetadata {
#[cfg(any(test, feature = "test-helpers"))]
impl Default for BlockHeader {
fn default() -> Self {
let mut default = Self {
let mut default: BlockHeader = BlockHeaderV1 {
application: Default::default(),
consensus: Default::default(),
metadata: None,
};
}
.into();
default.recalculate_metadata();
default
}
Expand All @@ -134,8 +197,8 @@ impl BlockHeader {
/// The method should be used only for tests.
pub fn new_block(height: BlockHeight, time: Tai64) -> Self {
let mut default = Self::default();
default.consensus.height = height;
default.consensus.time = time;
default.consensus_mut().height = height;
default.consensus_mut().time = time;
default.recalculate_metadata();
default
}
Expand Down Expand Up @@ -189,9 +252,9 @@ impl PartialBlockHeader {
impl BlockHeader {
/// Re-generate the header metadata.
pub fn recalculate_metadata(&mut self) {
let application_hash = self.application.hash();
self.consensus.generated.application_hash = application_hash;
self.metadata = Some(BlockHeaderMetadata { id: self.hash() });
let application_hash = self.application().hash();
self.consensus_mut().generated.application_hash = application_hash;
*self.metadata_mut() = Some(BlockHeaderMetadata { id: self.hash() });
}

/// Get the hash of the fuel header.
Expand All @@ -201,14 +264,14 @@ impl BlockHeader {
// and can't change its final hash on the fly.
//
// This assertion is a double-checks that this behavior is not changed.
debug_assert_eq!(self.consensus.application_hash, self.application.hash());
debug_assert_eq!(self.consensus().application_hash, self.application().hash());
// This internally hashes the hash of the application header.
self.consensus.hash()
self.consensus().hash()
}

/// Get the cached fuel header hash.
pub fn id(&self) -> BlockId {
if let Some(ref metadata) = self.metadata {
if let Some(ref metadata) = self.metadata() {
metadata.id
} else {
self.hash()
Expand All @@ -220,7 +283,7 @@ impl BlockHeader {
// Generate the transaction merkle root.
let transactions_root = generate_txns_root(transactions);

transactions_root == self.application.transactions_root
transactions_root == self.application().transactions_root
}
}

Expand Down Expand Up @@ -263,7 +326,7 @@ impl PartialBlockHeader {
},
};

let mut header = BlockHeader {
let mut header: BlockHeader = BlockHeaderV1 {
application,
consensus: ConsensusHeader {
prev_root: self.consensus.prev_root,
Expand All @@ -275,7 +338,8 @@ impl PartialBlockHeader {
},
},
metadata: None,
};
}
.into();

// Cache the hash.
header.recalculate_metadata();
Expand Down Expand Up @@ -340,7 +404,7 @@ impl core::ops::Deref for BlockHeader {
type Target = ApplicationHeader<GeneratedApplicationFields>;

fn deref(&self) -> &Self::Target {
&self.application
&self.application()
}
}

Expand Down Expand Up @@ -370,7 +434,7 @@ impl core::ops::Deref for ConsensusHeader<GeneratedConsensusFields> {

impl core::convert::AsRef<ConsensusHeader<GeneratedConsensusFields>> for BlockHeader {
fn as_ref(&self) -> &ConsensusHeader<GeneratedConsensusFields> {
&self.consensus
&self.consensus()
}
}

Expand Down

0 comments on commit 44bfe11

Please sign in to comment.