Skip to content

Commit

Permalink
Merge f1bf334 into 8af1b54
Browse files Browse the repository at this point in the history
  • Loading branch information
nkysg authored Apr 9, 2022
2 parents 8af1b54 + f1bf334 commit 413d366
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 13 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion contrib-contracts/src/starcoin_merkle_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn test_starcoin_merkle() -> Result<()> {
}

{
// change to previout state root.
// change to previous state root.
let old_chain_state = chain_state.fork_at(state_root);
// let state_root = chain_state.state_root();
let _expected_root = MoveValue::vector_u8(state_root.to_vec());
Expand Down
5 changes: 0 additions & 5 deletions executor/src/block_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@ pub fn block_execute(
let txn_state_root = chain_state
.commit()
.map_err(BlockExecutorError::BlockChainStateErr)?;
//every transaction's state tree root and tree nodes should save to storage
//TODO merge database flush.
chain_state
.flush()
.map_err(BlockExecutorError::BlockChainStateErr)?;

executed_data.txn_infos.push(TransactionInfo::new(
txn_hash,
Expand Down
1 change: 1 addition & 0 deletions state/state-tree/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +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" }
logger = { path = "../../commons/logger", package="starcoin-logger"}

[dev-dependencies]
starcoin-config= { path = "../../config"}
Expand Down
34 changes: 30 additions & 4 deletions state/state-tree/src/state_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use forkable_jellyfish_merkle::proof::SparseMerkleProof;
use forkable_jellyfish_merkle::{
JellyfishMerkleTree, RawKey, StaleNodeIndex, TreeReader, TreeUpdateBatch,
};
use logger::prelude::debug;
use parking_lot::{Mutex, RwLock};
use starcoin_crypto::hash::*;
use starcoin_state_store_api::*;
Expand All @@ -24,6 +25,8 @@ use std::sync::Arc;
pub struct StateCache<K: RawKey> {
root_hash: HashValue,
change_set: TreeUpdateBatch<K>,
change_set_list: Vec<(HashValue, TreeUpdateBatch<K>)>,
split_off_idx: Option<usize>,
}

impl<K> StateCache<K>
Expand All @@ -34,12 +37,19 @@ where
Self {
root_hash: initial_root,
change_set: TreeUpdateBatch::default(),
change_set_list: Vec::new(),
split_off_idx: None,
}
}

fn reset(&mut self, root_hash: HashValue) {
self.root_hash = root_hash;
self.change_set = TreeUpdateBatch::default();
self.change_set_list = if let Some(split_idx) = self.split_off_idx {
self.change_set_list.split_off(split_idx)
} else {
Vec::new()
};
}

fn add_changeset(&mut self, root_hash: HashValue, cs: TreeUpdateBatch<K>) {
Expand Down Expand Up @@ -70,7 +80,8 @@ where
cur_change_set.num_new_leaves += 1;
}
}

self.change_set_list
.push((root_hash, cur_change_set.clone()));
self.root_hash = root_hash;
}
}
Expand Down Expand Up @@ -196,11 +207,25 @@ where

/// commit the state change into underline storage.
pub fn flush(&self) -> Result<()> {
let (root_hash, change_sets) = self.change_sets();
let change_set_list = {
let mut cache_guard = self.cache.lock();
cache_guard.split_off_idx = Some(cache_guard.change_set_list.len());
cache_guard.change_set_list.clone()
};

debug!("change_set_list len {}", change_set_list.len());
// when self::commit call self::updates(&self, updates: Vec<(K, Option<Blob>)>)
// the param updates is empty cause this situation
if change_set_list.is_empty() {
return Ok(());
}
let mut root_hash = HashValue::default();
let mut node_map = BTreeMap::new();
for (nk, n) in change_sets.node_batch.into_iter() {
node_map.insert(nk, n.try_into()?);
for (hash, change_sets) in change_set_list.into_iter() {
for (nk, n) in change_sets.node_batch.into_iter() {
node_map.insert(nk, n.try_into()?);
}
root_hash = hash;
}
self.storage.write_nodes(node_map)?;
// and then advance the storage root hash
Expand Down Expand Up @@ -300,6 +325,7 @@ where
let cache_guard = self.cache.lock();
(cache_guard.root_hash, cache_guard.change_set.clone())
}

// TODO: to keep atomic with other commit.
// TODO: think about the WriteBatch trait position.
// pub fn save<T>(&self, batch: &mut T) -> Result<()>
Expand Down
5 changes: 2 additions & 3 deletions state/state-tree/src/state_tree_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ pub fn test_state_multi_commit_missing_node() -> Result<()> {
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.put(hash_value1, value1.clone());
state.commit()?;
let root_hash1 = state.root_hash();
let hash_value2 = HashValueKey(HashValue::random());
Expand All @@ -240,8 +240,7 @@ pub fn test_state_multi_commit_missing_node() -> Result<()> {
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");
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));
Expand Down

0 comments on commit 413d366

Please sign in to comment.