-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
79f9852
commit 14cfe8e
Showing
10 changed files
with
199 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
module-system/module-implementations/sov-blob-storage/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
[package] | ||
name = "sov-blob-storage" | ||
description = "A Sovereign SDK module for holding blobs from Data Availability Layer" | ||
authors = { workspace = true } | ||
edition = { workspace = true } | ||
homepage = { workspace = true } | ||
license = { workspace = true } | ||
repository = { workspace = true } | ||
rust-version = { workspace = true } | ||
version = { workspace = true } | ||
readme = "README.md" | ||
|
||
resolver = "2" | ||
|
||
|
||
[dependencies] | ||
anyhow = { workspace = true } | ||
bincode = { workspace = true } | ||
sov-modules-api = { path = "../../sov-modules-api", version = "0.1", default-features = false } | ||
sov-modules-macros = { path = "../../sov-modules-macros", version = "0.1" } | ||
sov-state = { path = "../../sov-state", version = "0.1", default-features = false } | ||
sov-rollup-interface = { path = "../../../rollup-interface", version = "0.1" } | ||
|
||
[dev-dependencies] | ||
sov-modules-api = { path = "../../sov-modules-api", version = "0.1" } | ||
tempfile = { workspace = true } | ||
|
||
[features] | ||
default = ["native"] | ||
native = ["sov-modules-api/native", "sov-state/native"] |
5 changes: 5 additions & 0 deletions
5
module-system/module-implementations/sov-blob-storage/src/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# sov-blob-storage | ||
|
||
Blob storage module allows to store arbitrary blobs in state of the module | ||
|
||
Caller is responsible for correct match between block number and actual blob, as well as for order of blobs. |
62 changes: 62 additions & 0 deletions
62
module-system/module-implementations/sov-blob-storage/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#![deny(missing_docs)] | ||
|
||
//! Blob storage module allows to save DA blobs in the state | ||
use sov_modules_api::Module; | ||
use sov_modules_macros::ModuleInfo; | ||
use sov_rollup_interface::da::BlobTransactionTrait; | ||
use sov_state::{StateMap, WorkingSet}; | ||
|
||
/// Blob storage contains only address and vector of blobs | ||
#[derive(ModuleInfo, Clone)] | ||
pub struct BlobStorage<C: sov_modules_api::Context> { | ||
/// The address of blob storage module | ||
/// Note: this is address is generated by the module framework and the corresponding private key is unknown. | ||
#[address] | ||
pub(crate) address: C::Address, | ||
|
||
/// Actual storage of blobs | ||
/// DA block number => vector of blobs | ||
/// Caller controls the order of blobs in the vector | ||
#[state] | ||
pub(crate) blobs: StateMap<u64, Vec<Vec<u8>>>, | ||
} | ||
|
||
/// Non standard methods for blob storage | ||
impl<C: sov_modules_api::Context> BlobStorage<C> { | ||
/// Store blobs for given block number, overwrite if already exists | ||
pub fn store_blobs<B: BlobTransactionTrait>( | ||
&self, | ||
block_number: u64, | ||
blobs: &[B], | ||
working_set: &mut WorkingSet<C::Storage>, | ||
) -> anyhow::Result<()> { | ||
let mut raw_blobs: Vec<Vec<u8>> = Vec::with_capacity(blobs.len()); | ||
for blob in blobs { | ||
raw_blobs.push(bincode::serialize(blob)?); | ||
} | ||
self.blobs.set(&block_number, &raw_blobs, working_set); | ||
Ok(()) | ||
} | ||
|
||
/// Take all blobs for given block number, return empty vector if not exists | ||
/// Returned blobs are removed from the storage | ||
pub fn take_blobs_for_block_number<B: BlobTransactionTrait>( | ||
&self, | ||
block_number: u64, | ||
working_set: &mut WorkingSet<C::Storage>, | ||
) -> Vec<B> { | ||
self.blobs | ||
.remove(&block_number, working_set) | ||
.unwrap_or_default() | ||
.iter() | ||
.map(|b| bincode::deserialize(b).expect("malformed blob was stored previously")) | ||
.collect() | ||
} | ||
} | ||
|
||
/// Empty module implementation | ||
impl<C: sov_modules_api::Context> Module for BlobStorage<C> { | ||
type Context = C; | ||
type Config = (); | ||
} |
90 changes: 90 additions & 0 deletions
90
module-system/module-implementations/sov-blob-storage/tests/blob_storage_tests.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use sov_blob_storage::BlobStorage; | ||
use sov_modules_api::default_context::DefaultContext; | ||
use sov_modules_api::Genesis; | ||
use sov_rollup_interface::mocks::{MockAddress, TestBlob}; | ||
use sov_state::{ProverStorage, WorkingSet}; | ||
|
||
type C = DefaultContext; | ||
type B = TestBlob<MockAddress>; | ||
|
||
#[test] | ||
fn empty_test() { | ||
let tmpdir = tempfile::tempdir().unwrap(); | ||
let mut working_set = WorkingSet::new(ProverStorage::with_path(tmpdir.path()).unwrap()); | ||
let blob_storage = BlobStorage::<C>::default(); | ||
|
||
blob_storage.genesis(&(), &mut working_set).unwrap(); | ||
|
||
let blobs: Vec<B> = blob_storage.take_blobs_for_block_number(1, &mut working_set); | ||
|
||
assert!(blobs.is_empty()); | ||
} | ||
|
||
#[test] | ||
fn store_and_retrieve_standard() { | ||
let tmpdir = tempfile::tempdir().unwrap(); | ||
let mut working_set = WorkingSet::new(ProverStorage::with_path(tmpdir.path()).unwrap()); | ||
let blob_storage = BlobStorage::<C>::default(); | ||
|
||
blob_storage.genesis(&(), &mut working_set).unwrap(); | ||
|
||
assert!(blob_storage | ||
.take_blobs_for_block_number::<B>(1, &mut working_set) | ||
.is_empty()); | ||
assert!(blob_storage | ||
.take_blobs_for_block_number::<B>(2, &mut working_set) | ||
.is_empty()); | ||
assert!(blob_storage | ||
.take_blobs_for_block_number::<B>(3, &mut working_set) | ||
.is_empty()); | ||
assert!(blob_storage | ||
.take_blobs_for_block_number::<B>(4, &mut working_set) | ||
.is_empty()); | ||
|
||
let sender = MockAddress::from([1u8; 32]); | ||
let dummy_hash = [2u8; 32]; | ||
|
||
let blob_1 = B::new(vec![1, 2, 3], sender, dummy_hash); | ||
let blob_2 = B::new(vec![3, 4, 5], sender, dummy_hash); | ||
let blob_3 = B::new(vec![6, 7, 8], sender, dummy_hash); | ||
let blob_4 = B::new(vec![9, 9, 9], sender, dummy_hash); | ||
let blob_5 = B::new(vec![0, 1, 0], sender, dummy_hash); | ||
|
||
let block_2_blobs = vec![blob_1, blob_2, blob_3]; | ||
let block_3_blobs = vec![blob_4]; | ||
let block_4_blobs = vec![blob_5]; | ||
|
||
blob_storage | ||
.store_blobs(2, &block_2_blobs, &mut working_set) | ||
.unwrap(); | ||
blob_storage | ||
.store_blobs(3, &block_3_blobs, &mut working_set) | ||
.unwrap(); | ||
blob_storage | ||
.store_blobs(4, &block_4_blobs, &mut working_set) | ||
.unwrap(); | ||
|
||
assert_eq!( | ||
block_2_blobs, | ||
blob_storage.take_blobs_for_block_number(2, &mut working_set) | ||
); | ||
assert!(blob_storage | ||
.take_blobs_for_block_number::<B>(2, &mut working_set) | ||
.is_empty()); | ||
|
||
assert_eq!( | ||
block_3_blobs, | ||
blob_storage.take_blobs_for_block_number(3, &mut working_set) | ||
); | ||
assert!(blob_storage | ||
.take_blobs_for_block_number::<B>(3, &mut working_set) | ||
.is_empty()); | ||
|
||
assert_eq!( | ||
block_4_blobs, | ||
blob_storage.take_blobs_for_block_number(4, &mut working_set) | ||
); | ||
assert!(blob_storage | ||
.take_blobs_for_block_number::<B>(4, &mut working_set) | ||
.is_empty()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ sov-accounts | |
sov-bank | ||
sov-sequencer-registry | ||
sov-prover-incentives | ||
sov-blob-storage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters