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

feat: Impl StorageRead and StorageWrite #1713

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Added

- [#1713](https://github.com/FuelLabs/fuel-core/pull/1713): Added automatic `impl` of traits `StorageWrite` and `StorageRead` for `StructuredStorage`. Tables that use a `Blueprint` can be read and written using these interfaces provided by structured storage types.
- [#1671](https://github.com/FuelLabs/fuel-core/pull/1671): Added a new `Merklized` blueprint that maintains the binary Merkle tree over the storage data. It supports only the insertion of the objects without removing them.
- [#1657](https://github.com/FuelLabs/fuel-core/pull/1657): Moved `ContractsInfo` table from `fuel-vm` to on-chain tables, and created version-able `ContractsInfoType` to act as the table's data type.

Expand Down
89 changes: 88 additions & 1 deletion crates/storage/src/structured_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ use crate::{
SupportsBatching,
SupportsMerkle,
},
codec::{
raw::Raw,
Encode,
Encoder,
},
kv_store::{
BatchOperations,
KeyValueStore,
Expand All @@ -19,9 +24,14 @@ use crate::{
StorageBatchMutate,
StorageInspect,
StorageMutate,
StorageRead,
StorageSize,
StorageWrite,
};
use std::{
borrow::Cow,
ops::Deref,
};
use std::borrow::Cow;

pub mod balances;
pub mod blocks;
Expand Down Expand Up @@ -173,6 +183,83 @@ where
}
}

impl<Column, S, M> StorageRead<M> for StructuredStorage<S>
where
S: KeyValueStore<Column = Column>,
M: Mappable + TableWithBlueprint<Column = Column, Value = [u8]>,
M::Blueprint: Blueprint<M, S, ValueCodec = Raw>,
{
fn read(
&self,
key: &<M as Mappable>::Key,
buf: &mut [u8],
) -> Result<Option<usize>, Self::Error> {
let key_encoder = <M::Blueprint as Blueprint<M, S>>::KeyCodec::encode(key);
let key_bytes = key_encoder.as_bytes();
self.storage
.read(key_bytes.as_ref(), <M as TableWithBlueprint>::column(), buf)
}

fn read_alloc(
&self,
key: &<M as Mappable>::Key,
) -> Result<Option<Vec<u8>>, Self::Error> {
let key_encoder = <M::Blueprint as Blueprint<M, S>>::KeyCodec::encode(key);
let key_bytes = key_encoder.as_bytes();
self.storage
.get(key_bytes.as_ref(), <M as TableWithBlueprint>::column())
// TODO: Return `Value` instead of cloned `Vec<u8>`.
.map(|value| value.map(|value| value.deref().clone()))
}
}

impl<Column, S, M> StorageWrite<M> for StructuredStorage<S>
where
S: KeyValueStore<Column = Column>,
M: TableWithBlueprint<Column = Column, Value = [u8]>,
M::Blueprint: Blueprint<M, S, ValueCodec = Raw>,
// TODO: Add new methods to the `Blueprint` that allows work with bytes directly
// without deserialization into `OwnedValue`.
M::OwnedValue: Into<Vec<u8>>,
{
fn write(&mut self, key: &M::Key, buf: Vec<u8>) -> Result<usize, Self::Error> {
<M as TableWithBlueprint>::Blueprint::put(
&mut self.storage,
key,
M::column(),
buf.as_slice(),
)
.map(|_| buf.len())
}

fn replace(
&mut self,
key: &M::Key,
buf: Vec<u8>,
) -> Result<(usize, Option<Vec<u8>>), Self::Error> {
let bytes_written = buf.len();
let prev = <M as TableWithBlueprint>::Blueprint::replace(
&mut self.storage,
key,
M::column(),
buf.as_slice(),
)?
.map(|prev| prev.into());
let result = (bytes_written, prev);
Ok(result)
}

fn take(&mut self, key: &M::Key) -> Result<Option<Vec<u8>>, Self::Error> {
let take = <M as TableWithBlueprint>::Blueprint::take(
&mut self.storage,
key,
M::column(),
)?
.map(|value| value.into());
Ok(take)
}
}

/// The module that provides helper macros for testing the structured storage.
#[cfg(feature = "test-helpers")]
pub mod test {
Expand Down
29 changes: 1 addition & 28 deletions crates/storage/src/structured_storage/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,13 @@ use crate::{
raw::Raw,
},
column::Column,
kv_store::KeyValueStore,
structured_storage::{
StructuredStorage,
TableWithBlueprint,
},
structured_storage::TableWithBlueprint,
tables::{
ContractsInfo,
ContractsLatestUtxo,
ContractsRawCode,
},
StorageRead,
};
use core::ops::Deref;
use fuel_core_types::fuel_tx::ContractId;

// # Dev-note: The value of the `ContractsRawCode` has a unique implementation of serialization
// and deserialization and uses `Raw` codec. Because the value is a contract byte code represented
Expand All @@ -35,26 +28,6 @@ impl TableWithBlueprint for ContractsRawCode {
}
}

impl<S> StorageRead<ContractsRawCode> for StructuredStorage<S>
where
S: KeyValueStore<Column = Column>,
{
fn read(
&self,
key: &ContractId,
buf: &mut [u8],
) -> Result<Option<usize>, Self::Error> {
self.storage
.read(key.as_ref(), Column::ContractsRawCode, buf)
}

fn read_alloc(&self, key: &ContractId) -> Result<Option<Vec<u8>>, Self::Error> {
self.storage
.get(key.as_ref(), Column::ContractsRawCode)
.map(|value| value.map(|value| value.deref().clone()))
}
}

impl TableWithBlueprint for ContractsInfo {
type Blueprint = Plain<Raw, Postcard>;
type Column = Column;
Expand Down
Loading