Skip to content

Commit

Permalink
fault_proving(merklized_registry): add merklized registry tables (#2719)
Browse files Browse the repository at this point in the history
## Linked Issues/PRs
<!-- List of related issues/PRs -->
part 1/4 of #2568

## Description
<!-- List of detailed changes -->

Implementation of various V2 temporal registry tables with Merklized
encoding.

New modules and features for fault-proofing:

*
[`crates/fuel-core/src/graphql_api/storage/da_compression.rs`](diffhunk://#diff-950254edead884ff443b7cc612f20d04acba8e37d8e6decab435348f84d0c51dR38-R40):
Added a new module `v2` under the `da_compression` directory to support
fault-proofing.
*
[`crates/fuel-core/src/graphql_api/storage/da_compression/v2.rs`](diffhunk://#diff-9fb44668f23048acd6709f3532aab5afbdf1297855a03d3bd258b26cf9e9c06aR1-R11):
Introduced new modules for `address`, `asset_id`, `contract_id`,
`predicate_code`, and `script_code` to handle fault-proofing.

Updates to `Column` enum:

*
[`crates/fuel-core/src/graphql_api/storage.rs`](diffhunk://#diff-f40129b7382c84ff5de9ccbc254350dbdb9cd62c5e23e0f653cdb49d32f2e5e7R126-R160):
Added new entries to the `Column` enum to support the new data
compression types for fault-proofing.

Implementation of V2 tables with Merklized encoding:

*
[`crates/fuel-core/src/graphql_api/storage/da_compression/v2/address.rs`](diffhunk://#diff-86681ac54c3c34d2cc3c942db5d24cc5002f2d439ed94e3a784a19dd3d259a57R1-R119):
Implemented `DaCompressionTemporalRegistryAddressV2` and related
structures with Merklized encoding.
*
[`crates/fuel-core/src/graphql_api/storage/da_compression/v2/asset_id.rs`](diffhunk://#diff-14903aaebdaf9e608a2f2c1fb245001f44f54d09c75c5609cebe0133dbc42efcR1-R117):
Implemented `DaCompressionTemporalRegistryAssetIdV2` and related
structures with Merklized encoding.
*
[`crates/fuel-core/src/graphql_api/storage/da_compression/v2/contract_id.rs`](diffhunk://#diff-81f055d34308e9e2b9f0dacc8806ca34068e5c4b4e9d1991c86f898a0a80b254R1-R118):
Implemented `DaCompressionTemporalRegistryContractIdV2` and related
structures with Merklized encoding.
*
[`crates/fuel-core/src/graphql_api/storage/da_compression/v2/predicate_code.rs`](diffhunk://#diff-93ccf013d8d2c5d19d7a29ce71e5043249228c48409477290bf753badf25377dR1-R116):
Implemented `DaCompressionTemporalRegistryPredicateCodeV2` and related
structures with Merklized encoding.
*
[`crates/fuel-core/src/graphql_api/storage/da_compression/v2/script_code.rs`](diffhunk://#diff-e08f9a8999be7e426ce3ac5688d987be33883998d960073a976dd7248184d29dR1-R118):
Implemented `DaCompressionTemporalRegistryScriptCodeV2` and related
structures with Merklized encoding.

Other changes:

*
[`crates/fuel-core/Cargo.toml`](diffhunk://#diff-7fc200bcb8e651840f8692da4101cfcdba429acc82847ed81d1d0ed63e0a5bd2R131-R132):
Added new fault-proofing dependencies for `fuel-core-importer` and
`fuel-core-poa`.
*
[`crates/storage/src/blueprint/merklized.rs`](diffhunk://#diff-0173b542fbd232931eb6bbbfabbaf4c10ad64b4fb96c76483ac9973607ab0f59L437-R437):
Updated the seed value in `basic_merklelized_storage_tests` macro for
consistency in tests.
[[1]](diffhunk://#diff-0173b542fbd232931eb6bbbfabbaf4c10ad64b4fb96c76483ac9973607ab0f59L437-R437)
[[2]](diffhunk://#diff-0173b542fbd232931eb6bbbfabbaf4c10ad64b4fb96c76483ac9973607ab0f59L480-R480)

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

### Before requesting review
- [ ] I have reviewed the code myself
- [ ] 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 Feb 20, 2025
1 parent 5f1378e commit 124f57d
Show file tree
Hide file tree
Showing 15 changed files with 1,000 additions and 8 deletions.
1 change: 1 addition & 0 deletions .changes/added/2719.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Merklized DA compression temporal registry tables.
4 changes: 3 additions & 1 deletion crates/fuel-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,7 @@ fault-proving = [
"fuel-core-storage/fault-proving",
"fuel-core-chain-config/fault-proving",
"fuel-core-database/fault-proving",
"fuel-core-sync/fault-proving",
"fuel-core-sync?/fault-proving",
"fuel-core-importer/fault-proving",
"fuel-core-poa/fault-proving",
]
53 changes: 53 additions & 0 deletions crates/fuel-core/src/graphql_api/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,59 @@ pub enum Column {
AssetsInfo = 25,
/// Index of the coins that are available to spend.
CoinsToSpend = 26,
/// See [`DaCompressionTemporalRegistryAddressV2`](da_compression::v2::address::DaCompressionTemporalRegistryAddressV2)
#[cfg(feature = "fault-proving")]
DaCompressionTemporalRegistryAddressV2 = 27,
#[cfg(feature = "fault-proving")]
DaCompressionTemporalAddressMerkleData = 28,
#[cfg(feature = "fault-proving")]
DaCompressionTemporalAddressMerkleMetadata = 29,
// See [`DaCompressionTemporalRegistryAssetIdV2`](da_compression::v2::asset_id::DaCompressionTemporalRegistryAssetIdV2)
#[cfg(feature = "fault-proving")]
DaCompressionTemporalRegistryAssetIdV2 = 30,
#[cfg(feature = "fault-proving")]
DaCompressionTemporalAssetIdMerkleData = 31,
#[cfg(feature = "fault-proving")]
DaCompressionTemporalAssetIdMerkleMetadata = 32,
/// See [`DaCompressionTemporalRegistryContractIdV2`](da_compression::v2::contract_id::DaCompressionTemporalRegistryContractIdV2)
#[cfg(feature = "fault-proving")]
DaCompressionTemporalRegistryContractIdV2 = 33,
#[cfg(feature = "fault-proving")]
DaCompressionTemporalContractIdMerkleData = 34,
#[cfg(feature = "fault-proving")]
DaCompressionTemporalContractIdMerkleMetadata = 35,
/// See [`DaCompressionTemporalRegistryScriptCodeV2`](da_compression::v2::script_code::DaCompressionTemporalRegistryScriptCodeV2)
#[cfg(feature = "fault-proving")]
DaCompressionTemporalRegistryScriptCodeV2 = 36,
#[cfg(feature = "fault-proving")]
DaCompressionTemporalScriptCodeMerkleData = 37,
#[cfg(feature = "fault-proving")]
DaCompressionTemporalScriptCodeMerkleMetadata = 38,
/// See [`DaCompressionTemporalRegistryPredicateCodeV2`](da_compression::v2::predicate_code::DaCompressionTemporalRegistryPredicateCodeV2)
#[cfg(feature = "fault-proving")]
DaCompressionTemporalRegistryPredicateCodeV2 = 39,
#[cfg(feature = "fault-proving")]
DaCompressionTemporalPredicateCodeMerkleData = 40,
#[cfg(feature = "fault-proving")]
DaCompressionTemporalPredicateCodeMerkleMetadata = 41,
#[cfg(feature = "fault-proving")]
DaCompressionTemporalRegistryIndexV2 = 42,
#[cfg(feature = "fault-proving")]
DaCompressionTemporalRegistryIndexMerkleData = 43,
#[cfg(feature = "fault-proving")]
DaCompressionTemporalRegistryIndexMerkleMetadata = 44,
#[cfg(feature = "fault-proving")]
DaCompressionTemporalRegistryTimestampsV2 = 45,
#[cfg(feature = "fault-proving")]
DaCompressionTemporalRegistryTimestampsMerkleData = 46,
#[cfg(feature = "fault-proving")]
DaCompressionTemporalRegistryTimestampsMerkleMetadata = 47,
#[cfg(feature = "fault-proving")]
DaCompressionTemporalRegistryEvictorCacheV2 = 48,
#[cfg(feature = "fault-proving")]
DaCompressionTemporalRegistryEvictorCacheMerkleData = 49,
#[cfg(feature = "fault-proving")]
DaCompressionTemporalRegistryEvictorCacheMerkleMetadata = 50,
}

impl Column {
Expand Down
3 changes: 3 additions & 0 deletions crates/fuel-core/src/graphql_api/storage/da_compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ pub mod reverse_key;
pub mod script_code_codec;
pub mod timestamps;

#[cfg(feature = "fault-proving")]
pub mod v2;

/// The table for the compressed blocks sent to DA.
pub struct DaCompressedBlocks;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ impl rand::distributions::Distribution<ReverseKey> for rand::distributions::Stan
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> ReverseKey {
use strum::EnumCount;
match rng.next_u32() as usize % ReverseKey::COUNT {
0 => ReverseKey::Address(Address::default()),
1 => ReverseKey::AssetId(AssetId::default()),
2 => ReverseKey::ContractId(ContractId::default()),
3 => ReverseKey::ScriptCode(Bytes32::default()),
4 => ReverseKey::PredicateCode(Bytes32::default()),
0 => ReverseKey::Address(rng.gen()),
1 => ReverseKey::AssetId(rng.gen()),
2 => ReverseKey::ContractId(rng.gen()),
3 => ReverseKey::ScriptCode(rng.gen()),
4 => ReverseKey::PredicateCode(rng.gen()),
_ => unreachable!("New reverse key is added but not supported here"),
}
}
Expand Down
8 changes: 8 additions & 0 deletions crates/fuel-core/src/graphql_api/storage/da_compression/v2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pub mod address;
pub mod asset_id;
pub mod contract_id;
pub mod evictor_cache;
pub mod predicate_code;
pub mod registry_index;
pub mod script_code;
pub mod timestamps;
119 changes: 119 additions & 0 deletions crates/fuel-core/src/graphql_api/storage/da_compression/v2/address.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
use crate::graphql_api::storage::{
da_compression::RegistryKey,
Column,
};
use core::borrow::Borrow;
use fuel_core_storage::{
blueprint::{
merklized::Merklized,
plain::Plain,
},
codec::{
postcard::Postcard,
primitive::Primitive,
raw::Raw,
},
structured_storage::TableWithBlueprint,
tables::merkle::{
DenseMerkleMetadata,
DenseMetadataKey,
},
Mappable,
};
use fuel_core_types::{
fuel_merkle::binary,
fuel_tx::{
Address,
Bytes32,
},
};

pub struct TemporalRegistryAddressMerkleData;

impl Mappable for TemporalRegistryAddressMerkleData {
type Key = u64;
type OwnedKey = Self::Key;
type Value = binary::Primitive;
type OwnedValue = Self::Value;
}

impl TableWithBlueprint for TemporalRegistryAddressMerkleData {
type Blueprint = Plain<Primitive<8>, Postcard>;
type Column = Column;

fn column() -> Column {
Column::DaCompressionTemporalAddressMerkleData
}
}

/// The metadata table for [`TemporalRegistryAddressMerkleData`] table.
pub struct TemporalRegistryAddressMerkleMetadata;

impl Mappable for TemporalRegistryAddressMerkleMetadata {
type Key = DenseMetadataKey<RegistryKey>;
type OwnedKey = Self::Key;
type Value = DenseMerkleMetadata;
type OwnedValue = Self::Value;
}

impl TableWithBlueprint for TemporalRegistryAddressMerkleMetadata {
type Blueprint = Plain<Postcard, Postcard>;
type Column = Column;

fn column() -> Column {
Column::DaCompressionTemporalAddressMerkleMetadata
}
}

/// Encoder for the V2 version of the DaCompressionTemporalRegistry for Address.
pub struct DaCompressionTemporalRegistryAddressV2Encoder;

impl fuel_core_storage::codec::Encode<Address>
for DaCompressionTemporalRegistryAddressV2Encoder
{
type Encoder<'a> = [u8; Bytes32::LEN];

fn encode(value: &Address) -> Self::Encoder<'_> {
*Borrow::<[u8; Bytes32::LEN]>::borrow(value)
}
}

/// V2 table for storing Address with Merklized encoding.
pub struct DaCompressionTemporalRegistryAddressV2;

impl Mappable for DaCompressionTemporalRegistryAddressV2 {
type Key = Self::OwnedKey;
type OwnedKey = RegistryKey;
type Value = Self::OwnedValue;
type OwnedValue = Address;
}

impl TableWithBlueprint for DaCompressionTemporalRegistryAddressV2 {
type Blueprint = Merklized<
Postcard,
Raw,
TemporalRegistryAddressMerkleMetadata,
TemporalRegistryAddressMerkleData,
DaCompressionTemporalRegistryAddressV2Encoder,
>;
type Column = Column;

fn column() -> Self::Column {
Column::DaCompressionTemporalRegistryAddressV2
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::fuel_core_graphql_api::storage::da_compression::tests::generate_key;

#[cfg(test)]
fuel_core_storage::basic_merklelized_storage_tests!(
DaCompressionTemporalRegistryAddressV2,
RegistryKey::ZERO,
<DaCompressionTemporalRegistryAddressV2 as Mappable>::Value::default(),
<DaCompressionTemporalRegistryAddressV2 as Mappable>::Value::default(),
generate_key
);
}
117 changes: 117 additions & 0 deletions crates/fuel-core/src/graphql_api/storage/da_compression/v2/asset_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
use crate::graphql_api::storage::{
da_compression::RegistryKey,
Column,
};
use core::borrow::Borrow;
use fuel_core_storage::{
blueprint::{
merklized::Merklized,
plain::Plain,
},
codec::{
postcard::Postcard,
primitive::Primitive,
raw::Raw,
},
structured_storage::TableWithBlueprint,
tables::merkle::{
DenseMerkleMetadata,
DenseMetadataKey,
},
Mappable,
};
use fuel_core_types::{
fuel_merkle::binary,
fuel_tx::{
AssetId,
Bytes32,
},
};

pub struct TemporalRegistryAssetIdMerkleData;

impl Mappable for TemporalRegistryAssetIdMerkleData {
type Key = u64;
type OwnedKey = Self::Key;
type Value = binary::Primitive;
type OwnedValue = Self::Value;
}

impl TableWithBlueprint for TemporalRegistryAssetIdMerkleData {
type Blueprint = Plain<Primitive<8>, Postcard>;
type Column = Column;

fn column() -> Column {
Column::DaCompressionTemporalAssetIdMerkleData
}
}

/// The metadata table for [`TemporalRegistryAssetIdMerkleData`] table.
pub struct TemporalRegistryAssetIdMerkleMetadata;

impl Mappable for TemporalRegistryAssetIdMerkleMetadata {
type Key = DenseMetadataKey<RegistryKey>;
type OwnedKey = Self::Key;
type Value = DenseMerkleMetadata;
type OwnedValue = Self::Value;
}

impl TableWithBlueprint for TemporalRegistryAssetIdMerkleMetadata {
type Blueprint = Plain<Postcard, Postcard>;
type Column = Column;

fn column() -> Column {
Column::DaCompressionTemporalAssetIdMerkleMetadata
}
}

/// Encoder for the V2 version of the DaCompressionTemporalRegistry for AssetId.
pub struct DaCompressionTemporalRegistryAssetIdV2Encoder;

impl fuel_core_storage::codec::Encode<AssetId>
for DaCompressionTemporalRegistryAssetIdV2Encoder
{
type Encoder<'a> = [u8; Bytes32::LEN];
fn encode(value: &AssetId) -> Self::Encoder<'_> {
*Borrow::<[u8; Bytes32::LEN]>::borrow(value)
}
}

/// V2 table for storing AssetId with Merklized encoding.
pub struct DaCompressionTemporalRegistryAssetIdV2;

impl Mappable for DaCompressionTemporalRegistryAssetIdV2 {
type Key = Self::OwnedKey;
type OwnedKey = RegistryKey;
type Value = Self::OwnedValue;
type OwnedValue = AssetId;
}

impl TableWithBlueprint for DaCompressionTemporalRegistryAssetIdV2 {
type Blueprint = Merklized<
Postcard,
Raw,
TemporalRegistryAssetIdMerkleMetadata,
TemporalRegistryAssetIdMerkleData,
DaCompressionTemporalRegistryAssetIdV2Encoder,
>;
type Column = Column;
fn column() -> Self::Column {
Self::Column::DaCompressionTemporalRegistryAssetIdV2
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::fuel_core_graphql_api::storage::da_compression::tests::generate_key;

#[cfg(test)]
fuel_core_storage::basic_merklelized_storage_tests!(
DaCompressionTemporalRegistryAssetIdV2,
RegistryKey::ZERO,
<DaCompressionTemporalRegistryAssetIdV2 as Mappable>::Value::default(),
<DaCompressionTemporalRegistryAssetIdV2 as Mappable>::Value::default(),
generate_key
);
}
Loading

0 comments on commit 124f57d

Please sign in to comment.