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

Make BlockHeader a versionable enum #1616

Merged
merged 7 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Description of the upcoming release here.
- [#1601](https://github.com/FuelLabs/fuel-core/pull/1601): Fix formatting in docs and check that `cargo doc` passes in the CI.

#### Breaking
- [#1616](https://github.com/FuelLabs/fuel-core/pull/1616) Make `BlockHeader` type a version-able enum
- [#1614](https://github.com/FuelLabs/fuel-core/pull/#1614): Use the default consensus key regardless of trigger mode. The change is breaking because it removes the `--dev-keys` argument. If the `debug` flag is set, the default consensus key will be used, regardless of the trigger mode.
- [#1596](https://github.com/FuelLabs/fuel-core/pull/1596) Make `Consensus` type a version-able enum
- [#1593](https://github.com/FuelLabs/fuel-core/pull/1593) Make `Block` type a version-able enum
Expand Down
6 changes: 3 additions & 3 deletions crates/fuel-core/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ mod tests {
let invalid_duplicate_tx = script.clone().into();

let mut block = Block::default();
block.header_mut().consensus.height = 1.into();
block.header_mut().set_block_height(1.into());
*block.transactions_mut() = vec![script.into(), invalid_duplicate_tx];
block.header_mut().recalculate_metadata();

Expand Down Expand Up @@ -439,7 +439,7 @@ mod tests {
.max_fee();

let mut block = Block::default();
block.header_mut().consensus.height = 2.into();
block.header_mut().set_block_height(2.into());
*block.transactions_mut() = vec![script.into()];
block.header_mut().recalculate_metadata();

Expand Down Expand Up @@ -1174,7 +1174,7 @@ mod tests {
.unwrap();

// randomize transaction commitment
block.header_mut().application.generated.transactions_root = rng.gen();
block.header_mut().set_transaction_root(rng.gen());
block.header_mut().recalculate_metadata();

let verify_result = verifier
Expand Down
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/fuel-core/src/service/adapters/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl BlockHeightImporter for BlockImporterAdapter {
Box::pin(
BroadcastStream::new(self.block_importer.subscribe())
.filter_map(|result| result.ok())
.map(|result| result.sealed_block.entity.header().consensus.height),
.map(|result| *result.sealed_block.entity.header().height()),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ async fn can_manually_produce_block(
.expect_produce_and_execute_block()
.returning(|_, time, _, _| {
let mut block = Block::default();
block.header_mut().consensus.time = time;
block.header_mut().set_time(time);
block.header_mut().recalculate_metadata();
Ok(UncommittedResult::new(
ExecutionResult {
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.application_hash() == &header.application().hash(),
"The application hash mismatch."
);

Expand Down
8 changes: 4 additions & 4 deletions crates/services/consensus_module/poa/src/verifier/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ fn test_verify_genesis_block_fields(input: Input) -> anyhow::Result<()> {
.returning(move |_| Ok(block_header_merkle_root.into()));
d.expect_block_header().returning(move |_| {
let mut h = BlockHeader::default();
h.consensus.time = prev_header_time;
h.application.da_height = prev_header_da_height.into();
h.set_time(prev_header_time);
h.set_da_height(prev_header_da_height.into());
Ok(h)
});
let mut b = Block::default();
b.header_mut().consensus = ch;
b.header_mut().application = ah;
b.header_mut().set_consensus_header(ch);
b.header_mut().set_application_header(ah);
verify_block_fields(&d, &b)
}
36 changes: 18 additions & 18 deletions crates/services/consensus_module/src/block_verifier/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,59 @@ use test_case::test_case;
#[test_case(
{
let mut h = BlockHeader::default();
h.consensus.prev_root = Bytes32::zeroed();
h.consensus.time = Tai64::UNIX_EPOCH;
h.consensus.height = 0u32.into();
h.set_previous_root(Bytes32::zeroed());
h.set_time(Tai64::UNIX_EPOCH);
h.set_block_height(0u32.into());
h
},
0 => matches Ok(_) ; "Correct header at `0`"
)]
#[test_case(
{
let mut h = BlockHeader::default();
h.consensus.prev_root = Bytes32::zeroed();
h.consensus.time = Tai64::UNIX_EPOCH;
h.consensus.height = 113u32.into();
h.set_previous_root(Bytes32::zeroed());
h.set_time(Tai64::UNIX_EPOCH);
h.set_block_height(113u32.into());
h
},
113 => matches Ok(_) ; "Correct header at `113`"
)]
#[test_case(
{
let mut h = BlockHeader::default();
h.consensus.prev_root = Bytes32::zeroed();
h.consensus.time = Tai64::UNIX_EPOCH;
h.consensus.height = 0u32.into();
h.set_previous_root(Bytes32::zeroed());
h.set_time(Tai64::UNIX_EPOCH);
h.set_block_height(0u32.into());
h
},
10 => matches Err(_) ; "wrong expected height"
)]
#[test_case(
{
let mut h = BlockHeader::default();
h.consensus.prev_root = Bytes32::zeroed();
h.consensus.time = Tai64::UNIX_EPOCH;
h.consensus.height = 5u32.into();
h.set_previous_root(Bytes32::zeroed());
h.set_time(Tai64::UNIX_EPOCH);
h.set_block_height(5u32.into());
h
},
0 => matches Err(_) ; "wrong header height"
)]
#[test_case(
{
let mut h = BlockHeader::default();
h.consensus.prev_root = Bytes32::zeroed();
h.consensus.time = Tai64(0);
h.consensus.height = 0u32.into();
h.set_previous_root(Bytes32::zeroed());
h.set_time(Tai64(0));
h.set_block_height(0u32.into());
h
},
0 => matches Err(_) ; "wrong time"
)]
#[test_case(
{
let mut h = BlockHeader::default();
h.consensus.prev_root = Bytes32::from([1u8; 32]);
h.consensus.time = Tai64::UNIX_EPOCH;
h.consensus.height = 0u32.into();
h.set_previous_root(Bytes32::from([1u8; 32]));
h.set_time(Tai64::UNIX_EPOCH);
h.set_block_height(0u32.into());
h
},
0 => matches Err(_) ; "wrong root"
Expand Down
6 changes: 3 additions & 3 deletions crates/services/importer/src/importer/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ struct MockExecutionResult {

fn genesis(height: u32) -> SealedBlock {
let mut block = Block::default();
block.header_mut().consensus.height = height.into();
block.header_mut().set_block_height(height.into());
block.header_mut().recalculate_metadata();

SealedBlock {
Expand All @@ -99,7 +99,7 @@ fn genesis(height: u32) -> SealedBlock {

fn poa_block(height: u32) -> SealedBlock {
let mut block = Block::default();
block.header_mut().consensus.height = height.into();
block.header_mut().set_block_height(height.into());
block.header_mut().recalculate_metadata();

SealedBlock {
Expand Down Expand Up @@ -514,7 +514,7 @@ where

// We tested commit part in the `commit_result_and_execute_and_commit_poa` so setup the
// databases to always pass the committing part.
let expected_height: u32 = sealed_block.entity.header().consensus.height.into();
let expected_height: u32 = (*sealed_block.entity.header().height()).into();
let previous_height = expected_height.checked_sub(1).unwrap_or_default();
let execute_and_commit_result = execute_and_commit_assert(
sealed_block,
Expand Down
6 changes: 3 additions & 3 deletions crates/services/p2p/src/p2p_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1463,7 +1463,7 @@ mod tests {
let mut blocks = Vec::new();
for i in range {
let mut header: BlockHeader = Default::default();
header.consensus.height = i.into();
header.set_block_height(i.into());

let sealed_block = SealedBlockHeader {
entity: header,
Expand All @@ -1476,8 +1476,8 @@ mod tests {

// Metadata gets skipped during serialization, so this is the fuzzy way to compare blocks
fn eq_except_metadata(a: &SealedBlockHeader, b: &SealedBlockHeader) -> bool {
a.entity.application == b.entity.application
&& a.entity.consensus == b.entity.consensus
a.entity.application() == b.entity.application()
&& a.entity.consensus() == b.entity.consensus()
}

async fn request_response_works_with(request_msg: RequestMessage) {
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
5 changes: 3 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,12 @@ 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.set_block_height(height);
let transaction_tree =
fuel_core_types::fuel_merkle::binary::root_calculator::MerkleRootCalculator::new(
);
header.application.generated.transactions_root = transaction_tree.root().into();
let root = transaction_tree.root().into();
header.set_transaction_root(root);

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