Skip to content

Commit

Permalink
fault_proving(compression): include block_id in da compressed block h…
Browse files Browse the repository at this point in the history
…eaders (#2551)

## Linked Issues/PRs
<!-- List of related issues/PRs -->

- closes #2567

## Description
<!-- List of detailed changes -->
Defines a new version of the `CompressedBlockPayload` which has a header
that contains the block_id, relevant for fault proving.

Also refactored the proptest strategy generation and moved the macro
internally so that tests can be ran from IDEs easily.

## Checklist
- [x] Breaking changes are clearly marked as such in the PR description
and changelog
- [x] New behavior is reflected in tests
- [x] [The specification](https://github.com/FuelLabs/fuel-specs/)
matches the implemented behavior (link update PR if changes are needed)

### Before requesting review
- [x] I have reviewed the code myself
- [x] I have created follow-up issues caused by this PR and linked them
here

### After merging, notify other teams

[Add or remove entries as needed]

- [ ] [Rust SDK](https://github.com/FuelLabs/fuels-rs/)
- [ ] [Sway compiler](https://github.com/FuelLabs/sway/)
- [ ] [Platform
documentation](https://github.com/FuelLabs/devrel-requests/issues/new?assignees=&labels=new+request&projects=&template=NEW-REQUEST.yml&title=%5BRequest%5D%3A+)
(for out-of-organization contributors, the person merging the PR will do
this)
- [ ] Someone else?
  • Loading branch information
rymnc authored Jan 15, 2025
1 parent 2287292 commit ab816ec
Show file tree
Hide file tree
Showing 11 changed files with 442 additions and 84 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added
- [2551](https://github.com/FuelLabs/fuel-core/pull/2551): Enhanced the DA compressed block header to include block id.

## [Version 0.41.0]

### Added
Expand Down
13 changes: 13 additions & 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 bin/fuel-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,4 @@ production = [
"parquet",
"aws-kms",
]
fault-proving = ["fuel-core-compression/fault-proving"]
2 changes: 2 additions & 0 deletions crates/compression/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ description = "Compression and decompression of Fuel blocks for DA storage."

[dependencies]
anyhow = { workspace = true }
enum_dispatch = "0.3.13"
fuel-core-types = { workspace = true, features = [
"alloc",
"serde",
Expand All @@ -42,3 +43,4 @@ test-helpers = [
"fuel-core-types/random",
"fuel-core-types/std",
]
fault-proving = []
7 changes: 3 additions & 4 deletions crates/compression/src/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use crate::{
RegistrationsPerTable,
TemporalRegistryAll,
},
CompressedBlockPayloadV0,
VersionedCompressedBlock,
};
use anyhow::Context;
Expand Down Expand Up @@ -70,11 +69,11 @@ where
let transactions = target.compress_with(&mut ctx).await?;
let registrations: RegistrationsPerTable = ctx.finalize()?;

Ok(VersionedCompressedBlock::V0(CompressedBlockPayloadV0 {
Ok(VersionedCompressedBlock::new(
block.header(),
registrations,
header: block.header().into(),
transactions,
}))
))
}

/// Preparation pass through the block to collect all keys accessed during compression.
Expand Down
3 changes: 3 additions & 0 deletions crates/compression/src/compressed_block_payload/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod v0;

pub mod v1;
70 changes: 70 additions & 0 deletions crates/compression/src/compressed_block_payload/v0.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use crate::{
registry::RegistrationsPerTable,
VersionedBlockPayload,
};
use fuel_core_types::{
blockchain::{
header::{
ApplicationHeader,
BlockHeader,
ConsensusHeader,
PartialBlockHeader,
},
primitives::Empty,
},
fuel_tx::CompressedTransaction,
fuel_types::BlockHeight,
};

/// Compressed block, without the preceding version byte.
#[derive(Debug, Default, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct CompressedBlockPayloadV0 {
/// Temporal registry insertions
pub registrations: RegistrationsPerTable,
/// Compressed block header
pub header: PartialBlockHeader,
/// Compressed transactions
pub transactions: Vec<CompressedTransaction>,
}

impl VersionedBlockPayload for CompressedBlockPayloadV0 {
fn height(&self) -> &BlockHeight {
self.header.height()
}

fn consensus_header(&self) -> &ConsensusHeader<Empty> {
&self.header.consensus
}

fn application_header(&self) -> &ApplicationHeader<Empty> {
&self.header.application
}

fn registrations(&self) -> &RegistrationsPerTable {
&self.registrations
}

fn transactions(&self) -> Vec<CompressedTransaction> {
self.transactions.clone()
}

fn partial_block_header(&self) -> PartialBlockHeader {
self.header
}
}

impl CompressedBlockPayloadV0 {
/// Create a new compressed block payload V0.
#[allow(unused)]
pub(crate) fn new(
header: &BlockHeader,
registrations: RegistrationsPerTable,
transactions: Vec<CompressedTransaction>,
) -> Self {
Self {
header: PartialBlockHeader::from(header),
registrations,
transactions,
}
}
}
122 changes: 122 additions & 0 deletions crates/compression/src/compressed_block_payload/v1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
use crate::{
registry::RegistrationsPerTable,
VersionedBlockPayload,
};
use fuel_core_types::{
blockchain::{
header::{
ApplicationHeader,
BlockHeader,
ConsensusHeader,
PartialBlockHeader,
},
primitives::{
BlockId,
Empty,
},
},
fuel_tx::CompressedTransaction,
fuel_types::BlockHeight,
};

/// A partially complete fuel block header that does not
/// have any generated fields because it has not been executed yet.
#[derive(
Copy, Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize,
)]
pub struct CompressedBlockHeader {
/// The application header.
pub application: ApplicationHeader<Empty>,
/// The consensus header.
pub consensus: ConsensusHeader<Empty>,
// The block id.
pub block_id: BlockId,
}

impl From<&BlockHeader> for CompressedBlockHeader {
fn from(header: &BlockHeader) -> Self {
let ConsensusHeader {
prev_root,
height,
time,
..
} = *header.consensus();
CompressedBlockHeader {
application: ApplicationHeader {
da_height: header.da_height,
consensus_parameters_version: header.consensus_parameters_version,
state_transition_bytecode_version: header
.state_transition_bytecode_version,
generated: Empty {},
},
consensus: ConsensusHeader {
prev_root,
height,
time,
generated: Empty {},
},
block_id: header.id(),
}
}
}

impl From<&CompressedBlockHeader> for PartialBlockHeader {
fn from(value: &CompressedBlockHeader) -> Self {
PartialBlockHeader {
application: value.application,
consensus: value.consensus,
}
}
}

#[derive(Debug, Default, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct CompressedBlockPayloadV1 {
/// Temporal registry insertions
pub registrations: RegistrationsPerTable,
/// Compressed block header
pub header: CompressedBlockHeader,
/// Compressed transactions
pub transactions: Vec<CompressedTransaction>,
}

impl VersionedBlockPayload for CompressedBlockPayloadV1 {
fn height(&self) -> &BlockHeight {
&self.header.consensus.height
}

fn consensus_header(&self) -> &ConsensusHeader<Empty> {
&self.header.consensus
}

fn application_header(&self) -> &ApplicationHeader<Empty> {
&self.header.application
}

fn registrations(&self) -> &RegistrationsPerTable {
&self.registrations
}

fn transactions(&self) -> Vec<CompressedTransaction> {
self.transactions.clone()
}

fn partial_block_header(&self) -> PartialBlockHeader {
PartialBlockHeader::from(&self.header)
}
}

impl CompressedBlockPayloadV1 {
/// Create a new compressed block payload V1.
#[allow(unused)]
pub(crate) fn new(
header: &BlockHeader,
registrations: RegistrationsPerTable,
transactions: Vec<CompressedTransaction>,
) -> Self {
Self {
header: CompressedBlockHeader::from(header),
registrations,
transactions,
}
}
}
15 changes: 7 additions & 8 deletions crates/compression/src/decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
TemporalRegistry,
},
registry::TemporalRegistryAll,
VersionedBlockPayload,
VersionedCompressedBlock,
};
use fuel_core_types::{
Expand Down Expand Up @@ -55,28 +56,26 @@ pub async fn decompress<D>(
where
D: DecompressDb,
{
let VersionedCompressedBlock::V0(compressed) = block;

// TODO: merkle root verification: https://github.com/FuelLabs/fuel-core/issues/2232

compressed
.registrations
.write_to_registry(&mut db, compressed.header.consensus.time)?;
block
.registrations()
.write_to_registry(&mut db, block.consensus_header().time)?;

let ctx = DecompressCtx {
config,
timestamp: compressed.header.consensus.time,
timestamp: block.consensus_header().time,
db,
};

let transactions = <Vec<Transaction> as DecompressibleBy<_>>::decompress_with(
compressed.transactions,
block.transactions(),
&ctx,
)
.await?;

Ok(PartialFuelBlock {
header: compressed.header,
header: block.partial_block_header(),
transactions,
})
}
Expand Down
Loading

0 comments on commit ab816ec

Please sign in to comment.