Skip to content

Commit

Permalink
[no ci] Fix after rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
citizen-stig committed Dec 6, 2023
1 parent 243628a commit 5eec43d
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 129 deletions.
9 changes: 4 additions & 5 deletions full-node/db/sov-db/src/native_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,11 @@ impl<Q: QueryManager> NativeDB<Q> {
key: &AccessoryKey,
version: Version,
) -> anyhow::Result<Option<Vec<u8>>> {
let mut iter = self.db.iter::<ModuleAccessoryState>()?;
iter.seek_for_prev(&(key.to_vec(), version))?;
let found = iter.next();
let found = self
.db_snapshot
.get_prev::<ModuleAccessoryState>(&(key.to_vec(), version))?;
match found {
Some(result) => {
let ((found_key, found_version), value) = result?;
Some(((found_key, found_version), value)) => {
if &found_key == key {
anyhow::ensure!(found_version <= version, "Bug! iterator isn't returning expected values. expected a version <= {version:} but found {found_version:}");
Ok(value)
Expand Down
1 change: 1 addition & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ sov-modules-api = { path = "../module-system/sov-modules-api", features = ["arbi
sov-accounts = { path = "../module-system/module-implementations/sov-accounts", features = ["arbitrary", "native"] }
sov-bank = { path = "../module-system/module-implementations/sov-bank", features = ["native"] }
sov-state = { path = "../module-system/sov-state" }
sov-prover-storage-manager = { path = "../full-node/sov-prover-storage-manager" }

# Prevent this from interfering with workspaces.
[workspace]
Expand Down
5 changes: 3 additions & 2 deletions fuzz/fuzz_targets/accounts_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use rand::{RngCore, SeedableRng};
use sov_accounts::{AccountConfig, Accounts, CallMessage, UPDATE_ACCOUNT_MSG};
use sov_modules_api::default_context::DefaultContext;
use sov_modules_api::default_signature::private_key::DefaultPrivateKey;
use sov_modules_api::{Context, Module, PrivateKey, Spec, WorkingSet};
use sov_modules_api::{Context, Module, PrivateKey, WorkingSet};
use sov_prover_storage_manager::new_orphan_storage;

type C = DefaultContext;

Expand Down Expand Up @@ -39,7 +40,7 @@ fuzz_target!(|input: (u16, [u8; 32], Vec<DefaultPrivateKey>)| -> Corpus {
let rng = &mut StdRng::from_seed(seed);
let mut seed = [0u8; 32];
let tmpdir = tempfile::tempdir().unwrap();
let storage = <C as Spec>::Storage::with_path(tmpdir.path()).unwrap();
let storage = new_orphan_storage(tmpdir.path()).unwrap();
let working_set = &mut WorkingSet::new(storage);

let config: AccountConfig<C> = keys.iter().map(|k| k.pub_key()).collect();
Expand Down
5 changes: 3 additions & 2 deletions fuzz/fuzz_targets/accounts_call_random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ use libfuzzer_sys::arbitrary::Unstructured;
use libfuzzer_sys::fuzz_target;
use sov_accounts::{Accounts, CallMessage};
use sov_modules_api::default_context::DefaultContext;
use sov_modules_api::{Module, Spec, WorkingSet};
use sov_modules_api::{Module, WorkingSet};
use sov_prover_storage_manager::new_orphan_storage;

type C = DefaultContext;

// Check arbitrary, random calls
fuzz_target!(|input: (&[u8], Vec<(C, CallMessage<C>)>)| {
let tmpdir = tempfile::tempdir().unwrap();
let storage = <C as Spec>::Storage::with_path(tmpdir.path()).unwrap();
let storage = new_orphan_storage(tmpdir.path()).unwrap();
let working_set = &mut WorkingSet::new(storage);

let (seed, msgs) = input;
Expand Down
7 changes: 3 additions & 4 deletions fuzz/fuzz_targets/bank_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
use libfuzzer_sys::fuzz_target;
use sov_bank::{Bank, CallMessage};
use sov_modules_api::default_context::DefaultContext;
use sov_modules_api::Context;
use sov_modules_api::{Module, WorkingSet};
use sov_state::ProverStorage;
use sov_modules_api::{Context, Module, WorkingSet};
use sov_prover_storage_manager::new_orphan_storage;

type C = DefaultContext;

fuzz_target!(|input: (&[u8], [u8; 32])| {
let (data, sender) = input;
if let Ok(msgs) = serde_json::from_slice::<Vec<CallMessage<C>>>(data) {
let tmpdir = tempfile::tempdir().unwrap();
let mut working_set = WorkingSet::new(ProverStorage::with_path(tmpdir.path()).unwrap());
let mut working_set = WorkingSet::new(new_orphan_storage(tmpdir.path()).unwrap());
let ctx = C::new(sender.into(), 1);
let bank = Bank::default();
for msg in msgs {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use helpers::*;
use sov_bank::{get_genesis_token_address, Amount, Bank, CallMessage, Coins};
use sov_modules_api::default_context::DefaultContext;
use sov_modules_api::{Address, Context, Module, StateReaderAndWriter, WorkingSet};
use sov_prover_storage_manager::{new_orphan_storage, SnapshotManager};
use sov_state::storage::{StorageKey, StorageValue};
use sov_state::{DefaultStorageSpec, ProverStorage, Storage};

Expand All @@ -12,7 +13,7 @@ fn transfer_initial_token() {
let initial_balance = 100;
let bank_config = create_bank_config_with_token(3, initial_balance);
let tmpdir = tempfile::tempdir().unwrap();
let prover_storage = ProverStorage::with_path(tmpdir.path()).unwrap();
let prover_storage = new_orphan_storage(tmpdir.path()).unwrap();
let mut working_set = WorkingSet::new(prover_storage.clone());
let bank = Bank::default();
bank.genesis(&bank_config, &mut working_set).unwrap();
Expand Down Expand Up @@ -261,7 +262,10 @@ fn transfer(
.expect("Transfer call failed");
}

fn commit(working_set: WorkingSet<DefaultContext>, storage: ProverStorage<DefaultStorageSpec>) {
fn commit(
working_set: WorkingSet<DefaultContext>,
storage: ProverStorage<DefaultStorageSpec, SnapshotManager>,
) {
// Save checkpoint
let mut checkpoint = working_set.checkpoint();

Expand Down
228 changes: 114 additions & 114 deletions module-system/sov-modules-api/src/containers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,118 +23,118 @@ pub use vec::StateVec;

#[cfg(test)]
mod test {
use jmt::Version;
use sov_modules_core::{StateReaderAndWriter, Storage, StorageKey, StorageValue, WorkingSet};
use sov_state::{DefaultStorageSpec, ProverStorage};

use crate::default_context::DefaultContext;

#[derive(Clone)]
struct TestCase {
key: StorageKey,
value: StorageValue,
version: Version,
}

fn get_state_db_version(path: &std::path::Path) -> Version {
let state_db = sov_db::state_db::StateDB::with_path(path).unwrap();
state_db.get_next_version()
}

fn create_tests() -> Vec<TestCase> {
vec![
TestCase {
key: StorageKey::from("key_0"),
value: StorageValue::from("value_0"),
version: 1,
},
TestCase {
key: StorageKey::from("key_1"),
value: StorageValue::from("value_1"),
version: 2,
},
TestCase {
key: StorageKey::from("key_2"),
value: StorageValue::from("value_2"),
version: 3,
},
]
}

#[test]
fn test_jmt_storage() {
let tempdir = tempfile::tempdir().unwrap();
let path = tempdir.path();
let tests = create_tests();
{
for test in tests.clone() {
let version_before = get_state_db_version(path);
assert_eq!(version_before, test.version);
{
let prover_storage =
ProverStorage::<DefaultStorageSpec>::with_path(path).unwrap();
let mut working_set: WorkingSet<DefaultContext> =
WorkingSet::new(prover_storage.clone());

working_set.set(&test.key, test.value.clone());
let (cache, witness) = working_set.checkpoint().freeze();
prover_storage
.validate_and_commit(cache, &witness)
.expect("storage is valid");
assert_eq!(
test.value,
prover_storage.get(&test.key, None, &witness).unwrap()
);
}
let version_after = get_state_db_version(path);
assert_eq!(version_after, test.version + 1)
}
}

{
let version_from_db = get_state_db_version(path);
let storage = ProverStorage::<DefaultStorageSpec>::with_path(path).unwrap();
assert_eq!(version_from_db, (tests.len() + 1) as u64);
for test in tests {
assert_eq!(
test.value,
storage.get(&test.key, None, &Default::default()).unwrap()
);
}
}
}

#[test]
fn test_restart_lifecycle() {
let tempdir = tempfile::tempdir().unwrap();
let path = tempdir.path();
{
let prover_storage = ProverStorage::<DefaultStorageSpec>::with_path(path).unwrap();
assert!(prover_storage.is_empty());
}

let key = StorageKey::from("some_key");
let value = StorageValue::from("some_value");
// First restart
{
let prover_storage = ProverStorage::<DefaultStorageSpec>::with_path(path).unwrap();
assert!(prover_storage.is_empty());
let mut storage: WorkingSet<DefaultContext> = WorkingSet::new(prover_storage.clone());
storage.set(&key, value.clone());
let (cache, witness) = storage.checkpoint().freeze();
prover_storage
.validate_and_commit(cache, &witness)
.expect("storage is valid");
}

// Correctly restart from disk
{
let prover_storage = ProverStorage::<DefaultStorageSpec>::with_path(path).unwrap();
assert!(!prover_storage.is_empty());
assert_eq!(
value,
prover_storage.get(&key, None, &Default::default()).unwrap()
);
}
}
// use jmt::Version;
// use sov_modules_core::{StateReaderAndWriter, Storage, StorageKey, StorageValue, WorkingSet};
// use sov_state::{DefaultStorageSpec, ProverStorage};
//
// use crate::default_context::DefaultContext;
//
// #[derive(Clone)]
// struct TestCase {
// key: StorageKey,
// value: StorageValue,
// version: Version,
// }
//
// fn get_state_db_version(path: &std::path::Path) -> Version {
// let state_db = sov_db::state_db::StateDB::with_path(path).unwrap();
// state_db.get_next_version()
// }
//
// fn create_tests() -> Vec<TestCase> {
// vec![
// TestCase {
// key: StorageKey::from("key_0"),
// value: StorageValue::from("value_0"),
// version: 1,
// },
// TestCase {
// key: StorageKey::from("key_1"),
// value: StorageValue::from("value_1"),
// version: 2,
// },
// TestCase {
// key: StorageKey::from("key_2"),
// value: StorageValue::from("value_2"),
// version: 3,
// },
// ]
// }
//
// #[test]
// fn test_jmt_storage() {
// let tempdir = tempfile::tempdir().unwrap();
// let path = tempdir.path();
// let tests = create_tests();
// {
// for test in tests.clone() {
// let version_before = get_state_db_version(path);
// assert_eq!(version_before, test.version);
// {
// let prover_storage =
// ProverStorage::<DefaultStorageSpec>::with_path(path).unwrap();
// let mut working_set: WorkingSet<DefaultContext> =
// WorkingSet::new(prover_storage.clone());
//
// working_set.set(&test.key, test.value.clone());
// let (cache, witness) = working_set.checkpoint().freeze();
// prover_storage
// .validate_and_commit(cache, &witness)
// .expect("storage is valid");
// assert_eq!(
// test.value,
// prover_storage.get(&test.key, None, &witness).unwrap()
// );
// }
// let version_after = get_state_db_version(path);
// assert_eq!(version_after, test.version + 1)
// }
// }
//
// {
// let version_from_db = get_state_db_version(path);
// let storage = ProverStorage::<DefaultStorageSpec>::with_path(path).unwrap();
// assert_eq!(version_from_db, (tests.len() + 1) as u64);
// for test in tests {
// assert_eq!(
// test.value,
// storage.get(&test.key, None, &Default::default()).unwrap()
// );
// }
// }
// }
//
// #[test]
// fn test_restart_lifecycle() {
// let tempdir = tempfile::tempdir().unwrap();
// let path = tempdir.path();
// {
// let prover_storage = ProverStorage::<DefaultStorageSpec>::with_path(path).unwrap();
// assert!(prover_storage.is_empty());
// }
//
// let key = StorageKey::from("some_key");
// let value = StorageValue::from("some_value");
// // First restart
// {
// let prover_storage = ProverStorage::<DefaultStorageSpec>::with_path(path).unwrap();
// assert!(prover_storage.is_empty());
// let mut storage: WorkingSet<DefaultContext> = WorkingSet::new(prover_storage.clone());
// storage.set(&key, value.clone());
// let (cache, witness) = storage.checkpoint().freeze();
// prover_storage
// .validate_and_commit(cache, &witness)
// .expect("storage is valid");
// }
//
// // Correctly restart from disk
// {
// let prover_storage = ProverStorage::<DefaultStorageSpec>::with_path(path).unwrap();
// assert!(!prover_storage.is_empty());
// assert_eq!(
// value,
// prover_storage.get(&key, None, &Default::default()).unwrap()
// );
// }
// }
}

0 comments on commit 5eec43d

Please sign in to comment.