Skip to content

Commit 744b765

Browse files
authored
fault_proving(storage_traits): refactor to allow usage of arbitrary TableColumn (#2793)
## Linked Issues/PRs <!-- List of related issues/PRs --> - part of #2775 ## Description <!-- List of detailed changes --> - instead of tying `MerkleizedTableColumn` to the `TableColumn` variant only in use by the global state root service, this refactors the trait to accept an arbitrary `TableColumn` that is returned when `table_column` is called. we've copied all the common functionality required by both destinations (`fuel-core-global-merkle-root-storage` and `fuel-core-compression-service`), you may see that we haven't deleted the implementations from the `fuel-core-global-merkle-root-storage` crate yet, because that crate doesn't use newtypes and we have an orphan rule error. will be addressed in follow up. For the person implementing the follow up, you can remove - - `fuel-core-global-merkle-root/src/merkle.rs` - `fuel-core-global-merkle-root/src/merkle/smt.rs` and reuse the traits & types from `fuel-core-storage` instead ## 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?
1 parent bc78572 commit 744b765

File tree

17 files changed

+344
-107
lines changed

17 files changed

+344
-107
lines changed

.changes/changed/2793.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Moved common merkle storage trait implementations to `fuel-core-storage` and made it easier to setup a set of columns that need merkleization.

crates/proof_system/global_merkle_root/service/src/ports.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use std::future::Future;
22

3-
use fuel_core_global_merkle_root_storage::column::Column;
3+
use fuel_core_global_merkle_root_storage::column::TableColumn;
44
use fuel_core_storage::{
55
kv_store::KeyValueInspect,
6+
merkle::column::MerkleizedColumn,
67
transactional::Modifiable,
78
};
89
use fuel_core_types::blockchain::block::Block;
@@ -17,6 +18,12 @@ pub trait BlockStream {
1718
}
1819

1920
/// The storage requirements for the merkle root service
20-
pub trait ServiceStorage: KeyValueInspect<Column = Column> + Modifiable {}
21+
pub trait ServiceStorage:
22+
KeyValueInspect<Column = MerkleizedColumn<TableColumn>> + Modifiable
23+
{
24+
}
2125

22-
impl<T> ServiceStorage for T where T: KeyValueInspect<Column = Column> + Modifiable {}
26+
impl<T> ServiceStorage for T where
27+
T: KeyValueInspect<Column = MerkleizedColumn<TableColumn>> + Modifiable
28+
{
29+
}
Lines changed: 4 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
use alloc::{
2-
format,
3-
string::{
4-
String,
5-
ToString,
6-
},
7-
};
8-
use fuel_core_storage::kv_store::StorageColumn;
9-
101
/// Almost in the case of all tables we need to prove exclusion of entries,
112
/// in the case of malicious block.
123
#[repr(u32)]
@@ -47,62 +38,11 @@ pub enum TableColumn {
4738

4839
impl TableColumn {
4940
/// The total count of variants in the enum.
50-
const COUNT: usize = <Self as strum::EnumCount>::COUNT;
51-
52-
/// Returns the `usize` representation of the `Column`.
53-
pub fn as_u32(&self) -> u32 {
54-
*self as u32
55-
}
56-
}
57-
58-
/// Almost in the case of all tables we need to prove exclusion of entries,
59-
/// in the case of malicious block.
60-
#[repr(u32)]
61-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
62-
pub enum Column {
63-
/// The column with the specific data.
64-
TableColumn(TableColumn),
65-
/// The column with the merkle tree nodes.
66-
MerkleDataColumn(TableColumn),
67-
/// The merkle metadata column.
68-
MerkleMetadataColumn,
69-
}
70-
71-
impl Column {
72-
/// The total count of variants in the enum.
73-
pub const COUNT: usize = TableColumn::COUNT + TableColumn::COUNT + 1;
74-
75-
/// The start of the merkle data columns.
76-
pub const MERKLE_DATA_COLUMNS_START: u32 = u16::MAX as u32;
77-
78-
/// Returns the `usize` representation of the `Column`.
79-
pub fn as_u32(&self) -> u32 {
80-
match self {
81-
Self::TableColumn(column) => column.as_u32(),
82-
Self::MerkleDataColumn(column) => {
83-
Self::MERKLE_DATA_COLUMNS_START.wrapping_add(column.as_u32())
84-
}
85-
Self::MerkleMetadataColumn => u32::MAX,
86-
}
87-
}
41+
pub const COUNT: usize = <Self as strum::EnumCount>::COUNT;
8842
}
8943

90-
impl StorageColumn for Column {
91-
fn name(&self) -> String {
92-
match self {
93-
Self::TableColumn(column) => {
94-
let str: &str = column.into();
95-
str.to_string()
96-
}
97-
Self::MerkleDataColumn(column) => {
98-
let str: &str = column.into();
99-
format!("Merkle{}", str)
100-
}
101-
Column::MerkleMetadataColumn => "MerkleMetadata".to_string(),
102-
}
103-
}
104-
105-
fn id(&self) -> u32 {
106-
self.as_u32()
44+
impl fuel_core_storage::merkle::column::AsU32 for TableColumn {
45+
fn as_u32(&self) -> u32 {
46+
*self as u32
10747
}
10848
}

crates/proof_system/global_merkle_root/storage/src/compute.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use fuel_core_storage::{
44
KeyValueInspect,
55
StorageColumn,
66
},
7+
merkle::column::MerkleizedColumn,
78
structured_storage::TableWithBlueprint,
89
tables::{
910
BlobData,
@@ -28,7 +29,7 @@ use fuel_core_types::{
2829
};
2930

3031
use crate::{
31-
column::Column,
32+
column::TableColumn,
3233
merkle::{
3334
DummyStorage,
3435
Merkleized,
@@ -44,14 +45,15 @@ pub trait ComputeStateRoot {
4445
/// Compute the merkle root of the specific table
4546
fn merkle_root<Table>(&self) -> Result<MerkleRoot, StorageError>
4647
where
47-
Table: Mappable + MerkleizedTableColumn,
48+
Table: Mappable + MerkleizedTableColumn<TableColumn = TableColumn>,
4849
Table: TableWithBlueprint,
49-
Table::Blueprint: BlueprintInspect<Table, DummyStorage<Column>>;
50+
Table::Blueprint:
51+
BlueprintInspect<Table, DummyStorage<MerkleizedColumn<TableColumn>>>;
5052
}
5153

5254
impl<Storage> ComputeStateRoot for StorageTransaction<Storage>
5355
where
54-
Storage: KeyValueInspect<Column = Column>,
56+
Storage: KeyValueInspect<Column = MerkleizedColumn<TableColumn>>,
5557
{
5658
fn state_root(&self) -> Result<Bytes32, StorageError> {
5759
let roots = [
@@ -77,9 +79,10 @@ where
7779

7880
fn merkle_root<Table>(&self) -> Result<MerkleRoot, StorageError>
7981
where
80-
Table: Mappable + MerkleizedTableColumn,
82+
Table: Mappable + MerkleizedTableColumn<TableColumn = TableColumn>,
8183
Table: TableWithBlueprint,
82-
Table::Blueprint: BlueprintInspect<Table, DummyStorage<Column>>,
84+
Table::Blueprint:
85+
BlueprintInspect<Table, DummyStorage<MerkleizedColumn<TableColumn>>>,
8386
{
8487
<Self as MerkleRootStorage<u32, Merkleized<Table>>>::root(
8588
self,

crates/proof_system/global_merkle_root/storage/src/merkle.rs

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
use crate::column::{
2-
Column,
3-
TableColumn,
4-
};
51
use alloc::borrow::Cow;
62
use fuel_core_storage::{
73
blueprint::{
@@ -16,6 +12,10 @@ use fuel_core_storage::{
1612
StorageColumn,
1713
Value,
1814
},
15+
merkle::column::{
16+
AsU32,
17+
MerkleizedColumn,
18+
},
1919
structured_storage::TableWithBlueprint,
2020
Mappable,
2121
Result as StorageResult,
@@ -50,47 +50,52 @@ pub struct Merkleized<Table>(core::marker::PhantomData<Table>);
5050
/// Implementation of this trait for the table, inherits
5151
/// the Merkle implementation for the [`Merkleized`] table.
5252
pub trait MerkleizedTableColumn {
53+
/// The table column type
54+
type TableColumn;
5355
/// Get the table column
54-
fn table_column() -> TableColumn;
56+
fn table_column() -> Self::TableColumn;
5557
}
5658

57-
impl<Table> Mappable for Merkleized<Table>
59+
impl<Table, TC> Mappable for Merkleized<Table>
5860
where
59-
Table: Mappable + MerkleizedTableColumn,
61+
Table: Mappable + MerkleizedTableColumn<TableColumn = TC>,
6062
{
6163
type Key = <Table as Mappable>::Key;
6264
type OwnedKey = <Table as Mappable>::OwnedKey;
6365
type Value = <Table as Mappable>::Value;
6466
type OwnedValue = <Table as Mappable>::OwnedValue;
6567
}
6668

67-
type KeyCodec<Table> = <<Table as TableWithBlueprint>::Blueprint as BlueprintInspect<
68-
Table,
69-
DummyStorage<Column>,
70-
>>::KeyCodec;
69+
type KeyCodec<Table, TC> =
70+
<<Table as TableWithBlueprint>::Blueprint as BlueprintInspect<
71+
Table,
72+
DummyStorage<MerkleizedColumn<TC>>,
73+
>>::KeyCodec;
7174

72-
type ValueCodec<Table> = <<Table as TableWithBlueprint>::Blueprint as BlueprintInspect<
73-
Table,
74-
DummyStorage<Column>,
75-
>>::ValueCodec;
75+
type ValueCodec<Table, TC> =
76+
<<Table as TableWithBlueprint>::Blueprint as BlueprintInspect<
77+
Table,
78+
DummyStorage<MerkleizedColumn<TC>>,
79+
>>::ValueCodec;
7680

77-
impl<Table> TableWithBlueprint for Merkleized<Table>
81+
impl<Table, TC> TableWithBlueprint for Merkleized<Table>
7882
where
79-
Table: Mappable + MerkleizedTableColumn,
83+
Table: Mappable + MerkleizedTableColumn<TableColumn = TC>,
8084
Table: TableWithBlueprint,
81-
Table::Blueprint: BlueprintInspect<Table, DummyStorage<Column>>,
85+
TC: core::fmt::Debug + Copy + strum::EnumCount + AsU32,
86+
Table::Blueprint: BlueprintInspect<Table, DummyStorage<MerkleizedColumn<TC>>>,
8287
{
8388
type Blueprint = Sparse<
84-
KeyCodec<Table>,
85-
ValueCodec<Table>,
89+
KeyCodec<Table, TC>,
90+
ValueCodec<Table, TC>,
8691
MerkleMetadata,
8792
MerkleData<Table>,
8893
KeyConverter<Table>,
8994
>;
90-
type Column = Column;
95+
type Column = MerkleizedColumn<TC>;
9196

9297
fn column() -> Self::Column {
93-
Column::TableColumn(Table::table_column())
98+
Self::Column::TableColumn(Table::table_column())
9499
}
95100
}
96101

crates/proof_system/global_merkle_root/storage/src/merkle/smt.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
column::Column,
2+
column::TableColumn,
33
merkle::MerkleizedTableColumn,
44
};
55
use fuel_core_storage::{
@@ -9,6 +9,10 @@ use fuel_core_storage::{
99
primitive::Primitive,
1010
raw::Raw,
1111
},
12+
merkle::column::{
13+
AsU32,
14+
MerkleizedColumn,
15+
},
1216
structured_storage::TableWithBlueprint,
1317
tables::merkle::SparseMerkleMetadata,
1418
Mappable,
@@ -25,15 +29,16 @@ impl<Table> Mappable for MerkleData<Table> {
2529
type OwnedValue = Self::Value;
2630
}
2731

28-
impl<Table> TableWithBlueprint for MerkleData<Table>
32+
impl<Table, TC> TableWithBlueprint for MerkleData<Table>
2933
where
30-
Table: MerkleizedTableColumn,
34+
Table: MerkleizedTableColumn<TableColumn = TC>,
35+
TC: core::fmt::Debug + Copy + strum::EnumCount + AsU32,
3136
{
3237
type Blueprint = Plain<Raw, Postcard>;
33-
type Column = Column;
38+
type Column = MerkleizedColumn<TC>;
3439

35-
fn column() -> Column {
36-
Column::MerkleDataColumn(Table::table_column())
40+
fn column() -> Self::Column {
41+
Self::Column::MerkleDataColumn(Table::table_column())
3742
}
3843
}
3944

@@ -49,9 +54,9 @@ impl Mappable for MerkleMetadata {
4954

5055
impl TableWithBlueprint for MerkleMetadata {
5156
type Blueprint = Plain<Primitive<4>, Postcard>;
52-
type Column = Column;
57+
type Column = MerkleizedColumn<TableColumn>;
5358

54-
fn column() -> Column {
55-
Column::MerkleMetadataColumn
59+
fn column() -> Self::Column {
60+
Self::Column::MerkleMetadataColumn
5661
}
5762
}

crates/proof_system/global_merkle_root/storage/src/structured_storage/blobs.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use crate::{
55
use fuel_core_storage::tables::BlobData;
66

77
impl MerkleizedTableColumn for BlobData {
8+
type TableColumn = TableColumn;
9+
810
fn table_column() -> TableColumn {
911
TableColumn::Blobs
1012
}

crates/proof_system/global_merkle_root/storage/src/structured_storage/coins.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use crate::{
55
use fuel_core_storage::tables::Coins;
66

77
impl MerkleizedTableColumn for Coins {
8+
type TableColumn = TableColumn;
9+
810
fn table_column() -> TableColumn {
911
TableColumn::Coins
1012
}

crates/proof_system/global_merkle_root/storage/src/structured_storage/contracts.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ use fuel_core_storage::tables::{
88
};
99

1010
impl MerkleizedTableColumn for ContractsRawCode {
11+
type TableColumn = TableColumn;
12+
1113
fn table_column() -> TableColumn {
1214
TableColumn::ContractsRawCode
1315
}
1416
}
1517

1618
impl MerkleizedTableColumn for ContractsLatestUtxo {
19+
type TableColumn = TableColumn;
20+
1721
fn table_column() -> TableColumn {
1822
TableColumn::ContractsLatestUtxo
1923
}

crates/proof_system/global_merkle_root/storage/src/structured_storage/messages.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use crate::{
55
use fuel_core_storage::tables::Messages;
66

77
impl MerkleizedTableColumn for Messages {
8+
type TableColumn = TableColumn;
9+
810
fn table_column() -> TableColumn {
911
TableColumn::Messages
1012
}

0 commit comments

Comments
 (0)