Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use BlockHeight as a primary key for the FuelsBlock table #1587

Merged
merged 27 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
df6e1ae
Move storage traits implementation to the `fuel-core-storage` crate
xgreenx Dec 24, 2023
de82e25
Added comments to all newly added stuff. Made self-review and applied…
xgreenx Dec 26, 2023
7ccc722
Merge branch 'master' into feature/move-storage-implementation-to-own…
xgreenx Dec 26, 2023
33661bc
Updated CHANGELOG.md
xgreenx Dec 26, 2023
1b8295a
Merge remote-tracking branch 'origin/feature/move-storage-implementat…
xgreenx Dec 26, 2023
4c3f18c
Apply suggestions from the PR
xgreenx Jan 5, 2024
0eaab98
Merge branch 'master' into feature/move-storage-implementation-to-own…
xgreenx Jan 5, 2024
0cea5bb
Merge branch 'master' into feature/move-storage-implementation-to-own…
xgreenx Jan 6, 2024
c9977a4
Merge branch 'master' into feature/move-storage-implementation-to-own…
xgreenx Jan 6, 2024
b0ed3e9
Fixed compilation
xgreenx Jan 6, 2024
541527e
Merge branch 'master' into feature/move-storage-implementation-to-own…
xgreenx Jan 6, 2024
d2b5504
Use `BlockHeight` as a primary key for the `FuelsBlock` table
xgreenx Jan 6, 2024
1013f0e
Merge branch 'feature/move-storage-implementation-to-own-crate' into …
xgreenx Jan 6, 2024
359192e
Updated CHANGELOG.md
xgreenx Jan 6, 2024
34b5e6a
Merge remote-tracking branch 'origin/feature/block-height-as-primary-…
xgreenx Jan 6, 2024
e27996a
Fix compilation
xgreenx Jan 6, 2024
452418c
Merge remote-tracking branch 'origin/feature/move-storage-implementat…
xgreenx Jan 19, 2024
1fbc318
Merge branch 'master' into feature/move-storage-implementation-to-own…
xgreenx Jan 19, 2024
c5956a8
Use "blueprint" instead of "structure"
xgreenx Jan 19, 2024
cc9966c
Fix documents
xgreenx Jan 19, 2024
0bbeea1
Merge branch 'feature/move-storage-implementation-to-own-crate' into …
xgreenx Jan 19, 2024
2d3e471
Merge latest modifications from move storage PR
xgreenx Jan 19, 2024
f0df4f0
Merge branch 'master' into feature/block-height-as-primary-key
xgreenx Jan 19, 2024
993aa84
Merge branch 'master' into feature/block-height-as-primary-key
xgreenx Jan 19, 2024
36445b7
Merged master
xgreenx Jan 19, 2024
0e2abad
Merge branch 'master' into feature/block-height-as-primary-key
xgreenx Jan 19, 2024
6a503b9
Apply comments
xgreenx Jan 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 107 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,121 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

Description of the upcoming release here.


### Changed

- [#1587](https://github.com/FuelLabs/fuel-core/pull/1587): Use `BlockHeight` as a primary key for the `FuelsBlock` table.
- [#1585](https://github.com/FuelLabs/fuel-core/pull/1585): Let `NetworkBehaviour` macro generate `FuelBehaviorEvent` in p2p
- [#1577](https://github.com/FuelLabs/fuel-core/pull/1577): Moved insertion of sealed blocks into the `BlockImporter` instead of the executor.

#### Breaking

- [#1576](https://github.com/FuelLabs/fuel-core/pull/1576): The change moves the implementation of the storage traits for required tables from `fuel-core` to `fuel-core-storage` crate. The change also adds a more flexible configuration of the encoding/decoding per the table and allows the implementation of specific behaviors for the table in a much easier way. It unifies the encoding between database, SMTs, and iteration, preventing mismatching bytes representation on the Rust type system level. Plus, it increases the re-usage of the code by applying the same structure to other tables.

It is a breaking PR because it changes database encoding/decoding for some tables.

### StructuredStorage

The change adds a new type `StructuredStorage`. It is a wrapper around the key-value storage that implements the storage traits(`StorageInspect`, `StorageMutate`, `StorageRead`, etc) for the tables with structure. This structure works in tandem with the `TableWithStructure` trait. The table may implement `TableWithStructure` specifying the structure, as an example:

```rust
impl TableWithStructure for ContractsRawCode {
type Structure = Plain<Raw, Raw>;

fn column() -> Column {
Column::ContractsRawCode
}
}
```

It is a definition of the structure for the `ContractsRawCode` table. It has a plain structure meaning it simply encodes/decodes bytes and stores/loads them into/from the storage. As a key codec and value codec, it uses a `Raw` encoding/decoding that simplifies writing bytes and loads them back into the memory without applying any serialization or deserialization algorithm.

If the table implements `TableWithStructure` and the selected codec satisfies all structure requirements, the corresponding storage traits for that table are implemented on the `StructuredStorage` type.

### Codecs

Each structure allows customizing the key and value codecs. It allows the use of different codecs for different tables, taking into account the complexity and weight of the data and providing a way of more optimal implementation.

That property may be very useful to perform migration in a more easier way. Plus, it also can be a `no_std` migration potentially allowing its fraud proving.

An example of migration:

```rust
/// Define the table for V1 value encoding/decoding.
impl TableWithStructure for ContractsRawCodeV1 {
type Structure = Plain<Raw, Raw>;

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

/// Define the table for V2 value encoding/decoding.
/// It uses `Postcard` codec for the value instead of `Raw` codec.
///
/// # Dev-note: The columns is the same.
impl TableWithStructure for ContractsRawCodeV2 {
type Structure = Plain<Raw, Postcard>;

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

fn migration(storage: &mut Database) {
let mut iter = storage.iter_all::<ContractsRawCodeV1>(None);
while let Ok((key, value)) = iter.next() {
// Insert into the same table but with another codec.
storage.storage::<ContractsRawCodeV2>().insert(key, value);
}
}
```

### Structures

The structure of the table defines its behavior. As an example, a `Plain` structure simply encodes/decodes bytes and stores/loads them into/from the storage. The `SMT` structure builds a sparse merkle tree on top of the key-value pairs.

Implementing a structure one time, we can apply it to any table satisfying the requirements of this structure. It increases the re-usage of the code and minimizes duplication.

It can be useful if we decide to create global roots for all required tables that are used in fraud proving.

```rust
impl TableWithStructure for SpentMessages {
type Structure = Plain<Raw, Postcard>;

fn column() -> Column {
Column::SpentMessages
}
}
|
|
\|/

impl TableWithStructure for SpentMessages {
type Structure =
Sparse<Raw, Postcard, SpentMessagesMerkleMetadata, SpentMessagesMerkleNodes>;

fn column() -> Column {
Column::SpentMessages
}
}
```

### Side changes

#### `iter_all`
The `iter_all` functionality now accepts the table instead of `K` and `V` generics. It is done to use the correct codec during deserialization. Also, the table definition provides the column.

#### Duplicated unit tests

The `fuel-core-storage` crate provides macros that generate unit tests. Almost all tables had the same test like `get`, `insert`, `remove`, `exist`. All duplicated tests were moved to macros. The unique one still stays at the same place where it was before.

#### `StorageBatchMutate`

Added a new `StorageBatchMutate` trait that we can move to `fuel-storage` crate later. It allows batch operations on the storage. It may be more performant in some cases.

- [#1573](https://github.com/FuelLabs/fuel-core/pull/1573): Remove nested p2p request/response encoding. Only breaks p2p networking compatibility with older fuel-core versions, but is otherwise fully internal.


## [Version 0.22.0]

### Added
Expand Down
43 changes: 39 additions & 4 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ async-trait = "0.1"
cynic = { version = "2.2.1", features = ["http-reqwest"] }
clap = "4.1"
derive_more = { version = "0.99" }
enum-iterator = "1.2"
hyper = { version = "0.14.26" }
primitive-types = { version = "0.12", default-features = false }
rand = "0.8"
Expand All @@ -100,6 +101,8 @@ tracing-attributes = "0.1"
tracing-subscriber = "0.3"
serde = "1.0"
serde_json = "1.0"
strum = "0.25"
strum_macros = "0.25"
# enable cookie store to support L7 sticky sessions
reqwest = { version = "0.11.16", default-features = false, features = ["rustls-tls", "cookies"] }
mockall = "0.11"
Expand Down
2 changes: 1 addition & 1 deletion crates/chain-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fuel-core-storage = { workspace = true }
fuel-core-types = { workspace = true, default-features = false, features = ["serde"] }
hex = { version = "0.4", features = ["serde"] }
itertools = { workspace = true }
postcard = { version = "1.0", features = ["alloc"] }
postcard = { workspace = true, features = ["alloc"] }
rand = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive", "rc"] }
serde_json = { version = "1.0", features = ["raw_value"], optional = true }
Expand Down
8 changes: 3 additions & 5 deletions crates/fuel-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async-trait = { workspace = true }
axum = { workspace = true }
clap = { workspace = true, features = ["derive"] }
derive_more = { version = "0.99" }
enum-iterator = "1.2"
enum-iterator = { workspace = true }
fuel-core-chain-config = { workspace = true }
fuel-core-consensus-module = { workspace = true }
fuel-core-database = { workspace = true }
Expand All @@ -41,16 +41,14 @@ futures = { workspace = true }
hex = { version = "0.4", features = ["serde"] }
hyper = { workspace = true }
itertools = { workspace = true }
postcard = { workspace = true, features = ["use-std"] }
rand = { workspace = true }
rocksdb = { version = "0.21", default-features = false, features = [
"lz4",
"multi-threaded-cf",
], optional = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true, features = ["raw_value"] }
strum = "0.24"
strum_macros = "0.24"
strum = { workspace = true }
strum_macros = { workspace = true }
tempfile = { workspace = true, optional = true }
thiserror = "1.0"
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
Expand Down
Loading
Loading