Skip to content

Commit

Permalink
add statetree flush test (#3320)
Browse files Browse the repository at this point in the history
* add statetree missing node test

* fix clippy

* add storage
  • Loading branch information
nkysg authored Apr 8, 2022
1 parent 0986ffc commit 8af1b54
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions state/state-tree/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ serde = { version = "1.0.130" }
forkable-jellyfish-merkle = { path = "../../commons/forkable-jellyfish-merkle"}
starcoin-state-store-api = {path = "../state-store-api"}
bcs-ext = { package="bcs-ext", path = "../../commons/bcs_ext" }

[dev-dependencies]
starcoin-config= { path = "../../config"}
starcoin-storage = { path = "../../storage"}
70 changes: 70 additions & 0 deletions state/state-tree/src/state_tree_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ use crate::mock::MockStateNodeStore;
use anyhow::Result;
use forkable_jellyfish_merkle::blob::Blob;
use forkable_jellyfish_merkle::{HashValueKey, RawKey};
use starcoin_config::RocksdbConfig;
use starcoin_crypto::hash::*;
use starcoin_storage::db_storage::DBStorage;
use starcoin_storage::storage::StorageInstance;
use starcoin_storage::Storage;
use std::collections::HashMap;
use std::sync::Arc;

Expand Down Expand Up @@ -211,3 +215,69 @@ pub fn test_state_storage_dump() -> Result<()> {
assert_eq!(kv1, kv2);
Ok(())
}

#[test]
pub fn test_state_multi_commit_missing_node() -> Result<()> {
let tmpdir = starcoin_config::temp_dir();
let instance = StorageInstance::new_db_instance(DBStorage::new(
tmpdir.path(),
RocksdbConfig::default(),
None,
)?);
let storage = Storage::new(instance)?;
let state = StateTree::new(Arc::new(storage.clone()), None);
let hash_value1 = HashValueKey(HashValue::random());
let value1 = vec![1u8, 2u8];
state.put(hash_value1, value1);
state.commit()?;
let root_hash1 = state.root_hash();
let hash_value2 = HashValueKey(HashValue::random());
let value12 = vec![12u8, 2u8];
let value2 = vec![3u8, 4u8];
state.put(hash_value1, value12.clone());
state.put(hash_value2, value2.clone());
state.commit()?;
state.flush()?;
let root_hash2 = state.root_hash();
let state1 = StateTree::new(Arc::new(storage.clone()), Some(root_hash1));
let result = state1.get(&hash_value1);
assert!(result.is_err(), "Missing node at HashValue");

let state2 = StateTree::new(Arc::new(storage), Some(root_hash2));
assert_eq!(state2.get(&hash_value1)?, Some(value12));
assert_eq!(state2.get(&hash_value2)?, Some(value2));
Ok(())
}

#[test]
pub fn test_state_multi_commit_and_flush() -> Result<()> {
let tmpdir = starcoin_config::temp_dir();
let instance = StorageInstance::new_db_instance(DBStorage::new(
tmpdir.path(),
RocksdbConfig::default(),
None,
)?);
let storage = Storage::new(instance)?;
let state = StateTree::new(Arc::new(storage.clone()), None);
let hash_value1 = HashValueKey(HashValue::random());
let value1 = vec![1u8, 2u8];
state.put(hash_value1, value1.clone());
state.commit()?;
state.flush()?;
let root_hash1 = state.root_hash();
let hash_value2 = HashValueKey(HashValue::random());
let value12 = vec![12u8, 2u8];
let value2 = vec![3u8, 4u8];
state.put(hash_value1, value12.clone());
state.put(hash_value2, value2.clone());
state.commit()?;
state.flush()?;
let root_hash2 = state.root_hash();
let state1 = StateTree::new(Arc::new(storage.clone()), Some(root_hash1));
assert_eq!(state1.get(&hash_value1)?, Some(value1));

let state2 = StateTree::new(Arc::new(storage), Some(root_hash2));
assert_eq!(state2.get(&hash_value1)?, Some(value12));
assert_eq!(state2.get(&hash_value2)?, Some(value2));
Ok(())
}

0 comments on commit 8af1b54

Please sign in to comment.