Skip to content

Commit 124f57d

Browse files
authored
fault_proving(merklized_registry): add merklized registry tables (#2719)
## 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?
1 parent 5f1378e commit 124f57d

File tree

15 files changed

+1000
-8
lines changed

15 files changed

+1000
-8
lines changed

.changes/added/2719.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Merklized DA compression temporal registry tables.

crates/fuel-core/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,5 +127,7 @@ fault-proving = [
127127
"fuel-core-storage/fault-proving",
128128
"fuel-core-chain-config/fault-proving",
129129
"fuel-core-database/fault-proving",
130-
"fuel-core-sync/fault-proving",
130+
"fuel-core-sync?/fault-proving",
131+
"fuel-core-importer/fault-proving",
132+
"fuel-core-poa/fault-proving",
131133
]

crates/fuel-core/src/graphql_api/storage.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,59 @@ pub enum Column {
123123
AssetsInfo = 25,
124124
/// Index of the coins that are available to spend.
125125
CoinsToSpend = 26,
126+
/// See [`DaCompressionTemporalRegistryAddressV2`](da_compression::v2::address::DaCompressionTemporalRegistryAddressV2)
127+
#[cfg(feature = "fault-proving")]
128+
DaCompressionTemporalRegistryAddressV2 = 27,
129+
#[cfg(feature = "fault-proving")]
130+
DaCompressionTemporalAddressMerkleData = 28,
131+
#[cfg(feature = "fault-proving")]
132+
DaCompressionTemporalAddressMerkleMetadata = 29,
133+
// See [`DaCompressionTemporalRegistryAssetIdV2`](da_compression::v2::asset_id::DaCompressionTemporalRegistryAssetIdV2)
134+
#[cfg(feature = "fault-proving")]
135+
DaCompressionTemporalRegistryAssetIdV2 = 30,
136+
#[cfg(feature = "fault-proving")]
137+
DaCompressionTemporalAssetIdMerkleData = 31,
138+
#[cfg(feature = "fault-proving")]
139+
DaCompressionTemporalAssetIdMerkleMetadata = 32,
140+
/// See [`DaCompressionTemporalRegistryContractIdV2`](da_compression::v2::contract_id::DaCompressionTemporalRegistryContractIdV2)
141+
#[cfg(feature = "fault-proving")]
142+
DaCompressionTemporalRegistryContractIdV2 = 33,
143+
#[cfg(feature = "fault-proving")]
144+
DaCompressionTemporalContractIdMerkleData = 34,
145+
#[cfg(feature = "fault-proving")]
146+
DaCompressionTemporalContractIdMerkleMetadata = 35,
147+
/// See [`DaCompressionTemporalRegistryScriptCodeV2`](da_compression::v2::script_code::DaCompressionTemporalRegistryScriptCodeV2)
148+
#[cfg(feature = "fault-proving")]
149+
DaCompressionTemporalRegistryScriptCodeV2 = 36,
150+
#[cfg(feature = "fault-proving")]
151+
DaCompressionTemporalScriptCodeMerkleData = 37,
152+
#[cfg(feature = "fault-proving")]
153+
DaCompressionTemporalScriptCodeMerkleMetadata = 38,
154+
/// See [`DaCompressionTemporalRegistryPredicateCodeV2`](da_compression::v2::predicate_code::DaCompressionTemporalRegistryPredicateCodeV2)
155+
#[cfg(feature = "fault-proving")]
156+
DaCompressionTemporalRegistryPredicateCodeV2 = 39,
157+
#[cfg(feature = "fault-proving")]
158+
DaCompressionTemporalPredicateCodeMerkleData = 40,
159+
#[cfg(feature = "fault-proving")]
160+
DaCompressionTemporalPredicateCodeMerkleMetadata = 41,
161+
#[cfg(feature = "fault-proving")]
162+
DaCompressionTemporalRegistryIndexV2 = 42,
163+
#[cfg(feature = "fault-proving")]
164+
DaCompressionTemporalRegistryIndexMerkleData = 43,
165+
#[cfg(feature = "fault-proving")]
166+
DaCompressionTemporalRegistryIndexMerkleMetadata = 44,
167+
#[cfg(feature = "fault-proving")]
168+
DaCompressionTemporalRegistryTimestampsV2 = 45,
169+
#[cfg(feature = "fault-proving")]
170+
DaCompressionTemporalRegistryTimestampsMerkleData = 46,
171+
#[cfg(feature = "fault-proving")]
172+
DaCompressionTemporalRegistryTimestampsMerkleMetadata = 47,
173+
#[cfg(feature = "fault-proving")]
174+
DaCompressionTemporalRegistryEvictorCacheV2 = 48,
175+
#[cfg(feature = "fault-proving")]
176+
DaCompressionTemporalRegistryEvictorCacheMerkleData = 49,
177+
#[cfg(feature = "fault-proving")]
178+
DaCompressionTemporalRegistryEvictorCacheMerkleMetadata = 50,
126179
}
127180

128181
impl Column {

crates/fuel-core/src/graphql_api/storage/da_compression.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ pub mod reverse_key;
3535
pub mod script_code_codec;
3636
pub mod timestamps;
3737

38+
#[cfg(feature = "fault-proving")]
39+
pub mod v2;
40+
3841
/// The table for the compressed blocks sent to DA.
3942
pub struct DaCompressedBlocks;
4043

crates/fuel-core/src/graphql_api/storage/da_compression/reverse_key.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ impl rand::distributions::Distribution<ReverseKey> for rand::distributions::Stan
7171
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> ReverseKey {
7272
use strum::EnumCount;
7373
match rng.next_u32() as usize % ReverseKey::COUNT {
74-
0 => ReverseKey::Address(Address::default()),
75-
1 => ReverseKey::AssetId(AssetId::default()),
76-
2 => ReverseKey::ContractId(ContractId::default()),
77-
3 => ReverseKey::ScriptCode(Bytes32::default()),
78-
4 => ReverseKey::PredicateCode(Bytes32::default()),
74+
0 => ReverseKey::Address(rng.gen()),
75+
1 => ReverseKey::AssetId(rng.gen()),
76+
2 => ReverseKey::ContractId(rng.gen()),
77+
3 => ReverseKey::ScriptCode(rng.gen()),
78+
4 => ReverseKey::PredicateCode(rng.gen()),
7979
_ => unreachable!("New reverse key is added but not supported here"),
8080
}
8181
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
pub mod address;
2+
pub mod asset_id;
3+
pub mod contract_id;
4+
pub mod evictor_cache;
5+
pub mod predicate_code;
6+
pub mod registry_index;
7+
pub mod script_code;
8+
pub mod timestamps;
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
use crate::graphql_api::storage::{
2+
da_compression::RegistryKey,
3+
Column,
4+
};
5+
use core::borrow::Borrow;
6+
use fuel_core_storage::{
7+
blueprint::{
8+
merklized::Merklized,
9+
plain::Plain,
10+
},
11+
codec::{
12+
postcard::Postcard,
13+
primitive::Primitive,
14+
raw::Raw,
15+
},
16+
structured_storage::TableWithBlueprint,
17+
tables::merkle::{
18+
DenseMerkleMetadata,
19+
DenseMetadataKey,
20+
},
21+
Mappable,
22+
};
23+
use fuel_core_types::{
24+
fuel_merkle::binary,
25+
fuel_tx::{
26+
Address,
27+
Bytes32,
28+
},
29+
};
30+
31+
pub struct TemporalRegistryAddressMerkleData;
32+
33+
impl Mappable for TemporalRegistryAddressMerkleData {
34+
type Key = u64;
35+
type OwnedKey = Self::Key;
36+
type Value = binary::Primitive;
37+
type OwnedValue = Self::Value;
38+
}
39+
40+
impl TableWithBlueprint for TemporalRegistryAddressMerkleData {
41+
type Blueprint = Plain<Primitive<8>, Postcard>;
42+
type Column = Column;
43+
44+
fn column() -> Column {
45+
Column::DaCompressionTemporalAddressMerkleData
46+
}
47+
}
48+
49+
/// The metadata table for [`TemporalRegistryAddressMerkleData`] table.
50+
pub struct TemporalRegistryAddressMerkleMetadata;
51+
52+
impl Mappable for TemporalRegistryAddressMerkleMetadata {
53+
type Key = DenseMetadataKey<RegistryKey>;
54+
type OwnedKey = Self::Key;
55+
type Value = DenseMerkleMetadata;
56+
type OwnedValue = Self::Value;
57+
}
58+
59+
impl TableWithBlueprint for TemporalRegistryAddressMerkleMetadata {
60+
type Blueprint = Plain<Postcard, Postcard>;
61+
type Column = Column;
62+
63+
fn column() -> Column {
64+
Column::DaCompressionTemporalAddressMerkleMetadata
65+
}
66+
}
67+
68+
/// Encoder for the V2 version of the DaCompressionTemporalRegistry for Address.
69+
pub struct DaCompressionTemporalRegistryAddressV2Encoder;
70+
71+
impl fuel_core_storage::codec::Encode<Address>
72+
for DaCompressionTemporalRegistryAddressV2Encoder
73+
{
74+
type Encoder<'a> = [u8; Bytes32::LEN];
75+
76+
fn encode(value: &Address) -> Self::Encoder<'_> {
77+
*Borrow::<[u8; Bytes32::LEN]>::borrow(value)
78+
}
79+
}
80+
81+
/// V2 table for storing Address with Merklized encoding.
82+
pub struct DaCompressionTemporalRegistryAddressV2;
83+
84+
impl Mappable for DaCompressionTemporalRegistryAddressV2 {
85+
type Key = Self::OwnedKey;
86+
type OwnedKey = RegistryKey;
87+
type Value = Self::OwnedValue;
88+
type OwnedValue = Address;
89+
}
90+
91+
impl TableWithBlueprint for DaCompressionTemporalRegistryAddressV2 {
92+
type Blueprint = Merklized<
93+
Postcard,
94+
Raw,
95+
TemporalRegistryAddressMerkleMetadata,
96+
TemporalRegistryAddressMerkleData,
97+
DaCompressionTemporalRegistryAddressV2Encoder,
98+
>;
99+
type Column = Column;
100+
101+
fn column() -> Self::Column {
102+
Column::DaCompressionTemporalRegistryAddressV2
103+
}
104+
}
105+
106+
#[cfg(test)]
107+
mod tests {
108+
use super::*;
109+
use crate::fuel_core_graphql_api::storage::da_compression::tests::generate_key;
110+
111+
#[cfg(test)]
112+
fuel_core_storage::basic_merklelized_storage_tests!(
113+
DaCompressionTemporalRegistryAddressV2,
114+
RegistryKey::ZERO,
115+
<DaCompressionTemporalRegistryAddressV2 as Mappable>::Value::default(),
116+
<DaCompressionTemporalRegistryAddressV2 as Mappable>::Value::default(),
117+
generate_key
118+
);
119+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
use crate::graphql_api::storage::{
2+
da_compression::RegistryKey,
3+
Column,
4+
};
5+
use core::borrow::Borrow;
6+
use fuel_core_storage::{
7+
blueprint::{
8+
merklized::Merklized,
9+
plain::Plain,
10+
},
11+
codec::{
12+
postcard::Postcard,
13+
primitive::Primitive,
14+
raw::Raw,
15+
},
16+
structured_storage::TableWithBlueprint,
17+
tables::merkle::{
18+
DenseMerkleMetadata,
19+
DenseMetadataKey,
20+
},
21+
Mappable,
22+
};
23+
use fuel_core_types::{
24+
fuel_merkle::binary,
25+
fuel_tx::{
26+
AssetId,
27+
Bytes32,
28+
},
29+
};
30+
31+
pub struct TemporalRegistryAssetIdMerkleData;
32+
33+
impl Mappable for TemporalRegistryAssetIdMerkleData {
34+
type Key = u64;
35+
type OwnedKey = Self::Key;
36+
type Value = binary::Primitive;
37+
type OwnedValue = Self::Value;
38+
}
39+
40+
impl TableWithBlueprint for TemporalRegistryAssetIdMerkleData {
41+
type Blueprint = Plain<Primitive<8>, Postcard>;
42+
type Column = Column;
43+
44+
fn column() -> Column {
45+
Column::DaCompressionTemporalAssetIdMerkleData
46+
}
47+
}
48+
49+
/// The metadata table for [`TemporalRegistryAssetIdMerkleData`] table.
50+
pub struct TemporalRegistryAssetIdMerkleMetadata;
51+
52+
impl Mappable for TemporalRegistryAssetIdMerkleMetadata {
53+
type Key = DenseMetadataKey<RegistryKey>;
54+
type OwnedKey = Self::Key;
55+
type Value = DenseMerkleMetadata;
56+
type OwnedValue = Self::Value;
57+
}
58+
59+
impl TableWithBlueprint for TemporalRegistryAssetIdMerkleMetadata {
60+
type Blueprint = Plain<Postcard, Postcard>;
61+
type Column = Column;
62+
63+
fn column() -> Column {
64+
Column::DaCompressionTemporalAssetIdMerkleMetadata
65+
}
66+
}
67+
68+
/// Encoder for the V2 version of the DaCompressionTemporalRegistry for AssetId.
69+
pub struct DaCompressionTemporalRegistryAssetIdV2Encoder;
70+
71+
impl fuel_core_storage::codec::Encode<AssetId>
72+
for DaCompressionTemporalRegistryAssetIdV2Encoder
73+
{
74+
type Encoder<'a> = [u8; Bytes32::LEN];
75+
fn encode(value: &AssetId) -> Self::Encoder<'_> {
76+
*Borrow::<[u8; Bytes32::LEN]>::borrow(value)
77+
}
78+
}
79+
80+
/// V2 table for storing AssetId with Merklized encoding.
81+
pub struct DaCompressionTemporalRegistryAssetIdV2;
82+
83+
impl Mappable for DaCompressionTemporalRegistryAssetIdV2 {
84+
type Key = Self::OwnedKey;
85+
type OwnedKey = RegistryKey;
86+
type Value = Self::OwnedValue;
87+
type OwnedValue = AssetId;
88+
}
89+
90+
impl TableWithBlueprint for DaCompressionTemporalRegistryAssetIdV2 {
91+
type Blueprint = Merklized<
92+
Postcard,
93+
Raw,
94+
TemporalRegistryAssetIdMerkleMetadata,
95+
TemporalRegistryAssetIdMerkleData,
96+
DaCompressionTemporalRegistryAssetIdV2Encoder,
97+
>;
98+
type Column = Column;
99+
fn column() -> Self::Column {
100+
Self::Column::DaCompressionTemporalRegistryAssetIdV2
101+
}
102+
}
103+
104+
#[cfg(test)]
105+
mod tests {
106+
use super::*;
107+
use crate::fuel_core_graphql_api::storage::da_compression::tests::generate_key;
108+
109+
#[cfg(test)]
110+
fuel_core_storage::basic_merklelized_storage_tests!(
111+
DaCompressionTemporalRegistryAssetIdV2,
112+
RegistryKey::ZERO,
113+
<DaCompressionTemporalRegistryAssetIdV2 as Mappable>::Value::default(),
114+
<DaCompressionTemporalRegistryAssetIdV2 as Mappable>::Value::default(),
115+
generate_key
116+
);
117+
}

0 commit comments

Comments
 (0)