Skip to content

Commit

Permalink
fault_proving(compression): include commitment to registry in compres…
Browse files Browse the repository at this point in the history
…sed block header
  • Loading branch information
rymnc committed Jan 15, 2025
1 parent 22e2afc commit 6e6e82d
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion crates/compression/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ description = "Compression and decompression of Fuel blocks for DA storage."

[dependencies]
anyhow = { workspace = true }
derive_more = { workspace = true, optional = true }
enum_dispatch = "0.3.13"
fuel-core-types = { workspace = true, features = [
"alloc",
"serde",
"da-compression",
] }
fuel-crypto = { version = "0.59.1", optional = true }
paste = { workspace = true }
rand = { workspace = true, optional = true }
serde = { version = "1.0", features = ["derive"] }
Expand All @@ -43,4 +45,4 @@ test-helpers = [
"fuel-core-types/random",
"fuel-core-types/std",
]
fault-proving = []
fault-proving = ["dep:derive_more", "dep:fuel-crypto"]
1 change: 1 addition & 0 deletions crates/compression/src/compressed_block_payload/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod v0;

#[cfg(feature = "fault-proving")]
pub mod v1;
20 changes: 16 additions & 4 deletions crates/compression/src/compressed_block_payload/v1.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::{
registry::RegistrationsPerTable,
registry::{
RegistrationsPerTable,
RegistryId,
},
VersionedBlockPayload,
};
use fuel_core_types::{
Expand Down Expand Up @@ -31,10 +34,15 @@ pub struct CompressedBlockHeader {
pub consensus: ConsensusHeader<Empty>,
// The block id.
pub block_id: BlockId,
// The registry id.
pub registry_id: RegistryId,
}

impl From<&BlockHeader> for CompressedBlockHeader {
fn from(header: &BlockHeader) -> Self {
impl CompressedBlockHeader {
fn from_header_and_registry(
header: &BlockHeader,
registry: &RegistrationsPerTable,
) -> Self {
let ConsensusHeader {
prev_root,
height,
Expand All @@ -56,6 +64,7 @@ impl From<&BlockHeader> for CompressedBlockHeader {
generated: Empty {},
},
block_id: header.id(),
registry_id: registry.id(),
}
}
}
Expand Down Expand Up @@ -114,7 +123,10 @@ impl CompressedBlockPayloadV1 {
transactions: Vec<CompressedTransaction>,
) -> Self {
Self {
header: CompressedBlockHeader::from(header),
header: CompressedBlockHeader::from_header_and_registry(
header,
&registrations,
),
registrations,
transactions,
}
Expand Down
2 changes: 2 additions & 0 deletions crates/compression/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ mod tests {
generated: Empty,
},
block_id: BlockId::from_str("0xecea85c17070bc2e65f911310dbd01198f4436052ebba96cded9ddf30c58dd1a").unwrap(),
registry_id: registrations.id(),
};


Expand Down Expand Up @@ -302,6 +303,7 @@ mod tests {

if let VersionedCompressedBlock::V1(block) = decompressed {
assert_eq!(block.header.block_id, header.block_id);
assert_eq!(block.header.registry_id, header.registry_id);
} else {
panic!("Expected V1 block, got {:?}", decompressed);
}
Expand Down
56 changes: 56 additions & 0 deletions crates/compression/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,47 @@ use fuel_core_types::{
tai64::Tai64,
};

#[cfg(feature = "fault-proving")]
use derive_more::{
AsRef,
Display,
From,
FromStr,
Into,
LowerHex,
UpperHex,
};
#[cfg(feature = "fault-proving")]
use fuel_core_types::fuel_tx::Bytes32;

#[cfg(feature = "fault-proving")]
#[cfg_attr(
feature = "fault-proving",
derive(
Clone,
Copy,
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Default,
FromStr,
From,
Into,
LowerHex,
UpperHex,
Display,
AsRef,
serde::Deserialize,
serde::Serialize,
)
)]
#[cfg_attr(feature = "fault-proving", serde(transparent))]
#[cfg_attr(feature = "fault-proving", repr(transparent))]
pub struct RegistryId(Bytes32);

macro_rules! tables {
($($ident:ty: $type:ty),*) => { paste::paste! {
#[doc = "RegistryKey namespaces"]
Expand Down Expand Up @@ -90,10 +131,25 @@ macro_rules! tables {

Ok(())
}

#[cfg(feature = "fault-proving")]
pub fn id(&self) -> RegistryId {
let mut hasher = fuel_crypto::Hasher::default();

$(
for (key, value) in self.$ident.iter() {
hasher.input(key);
hasher.input(value);
}
)*

RegistryId(hasher.digest())
}
}
}};
}

// Don't change the order of these, it will affect the order of hashing
tables!(
address: Address,
asset_id: AssetId,
Expand Down

0 comments on commit 6e6e82d

Please sign in to comment.