From c833d392c40320f1bb123d0696f360b8aec06c0b Mon Sep 17 00:00:00 2001 From: NikVolf Date: Wed, 16 Jan 2019 17:39:54 +0300 Subject: [PATCH] block roots in db --- db/src/kv/memorydb.rs | 8 ++++++++ db/src/kv/transaction.rs | 9 ++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/db/src/kv/memorydb.rs b/db/src/kv/memorydb.rs index f6632894..4bf5df50 100644 --- a/db/src/kv/memorydb.rs +++ b/db/src/kv/memorydb.rs @@ -13,6 +13,7 @@ use storage::{TransactionMeta, EpochTag, RegularTreeState, Nullifier}; struct InnerDatabase { meta: HashMap<&'static str, KeyState>, block_hash: HashMap>, + block_root: HashMap>, block_header: HashMap>, block_transactions: HashMap>>, transaction: HashMap>, @@ -75,6 +76,9 @@ impl MemoryDatabase { let tree_state = replace(&mut db.tree_state, HashMap::default()).into_iter() .flat_map(|(key, state)| state.into_operation(key, KeyValue::TreeState, Key::TreeRoot)); + let block_root = replace(&mut db.block_root, HashMap::default()).into_iter() + .flat_map(|(key, state)| state.into_operation(key, KeyValue::BlockRoot, Key::BlockRoot)); + Transaction { operations: meta .chain(block_hash) @@ -85,6 +89,7 @@ impl MemoryDatabase { .chain(block_number) .chain(configuration) .chain(tree_state) + .chain(block_root) .chain(sprout_nullifiers) .chain(sapling_nullifiers) .collect() @@ -111,6 +116,7 @@ impl KeyValueDatabase for MemoryDatabase { EpochTag::Sapling => { db.sapling_nullifiers.insert(*key.hash(), KeyState::Insert(())); }, }, KeyValue::TreeState(key, value) => { db.tree_state.insert(key, KeyState::Insert(value)); }, + KeyValue::BlockRoot(key, value) => { db.block_root.insert(key, KeyState::Insert(value)); }, }, Operation::Delete(delete) => match delete { Key::Meta(key) => { db.meta.insert(key, KeyState::Delete); } @@ -126,6 +132,7 @@ impl KeyValueDatabase for MemoryDatabase { EpochTag::Sapling => { db.sapling_nullifiers.insert(*key.hash(), KeyState::Delete); }, }, Key::TreeRoot(key) => { db.tree_state.insert(key, KeyState::Delete); }, + Key::BlockRoot(key) => { db.block_root.insert(key, KeyState::Delete); }, }, } } @@ -148,6 +155,7 @@ impl KeyValueDatabase for MemoryDatabase { EpochTag::Sapling => db.sapling_nullifiers.get(key.hash()).cloned().unwrap_or_default().map(|_| Value::Empty), }, Key::TreeRoot(ref key) => db.tree_state.get(key).cloned().unwrap_or_default().map(Value::TreeState), + Key::BlockRoot(ref key) => db.block_root.get(key).cloned().unwrap_or_default().map(Value::TreeRoot), }; Ok(result) diff --git a/db/src/kv/transaction.rs b/db/src/kv/transaction.rs index 7a29fc86..89ad58e2 100644 --- a/db/src/kv/transaction.rs +++ b/db/src/kv/transaction.rs @@ -15,7 +15,8 @@ pub const COL_BLOCK_NUMBERS: u32 = 6; pub const COL_SPROUT_NULLIFIERS: u32 = 7; pub const COL_SAPLING_NULLIFIERS: u32 = 8; pub const COL_TREESTATES: u32 = 9; -pub const COL_CONFIGURATION: u32 = 10; +pub const COL_BLOCK_ROOTS: u32 = 10; +pub const COL_CONFIGURATION: u32 = 11; #[derive(Debug)] pub enum Operation { @@ -35,6 +36,7 @@ pub enum KeyValue { Configuration(&'static str, Bytes), Nullifier(Nullifier), TreeState(H256, RegularTreeState), + BlockRoot(H256, H256), } #[derive(Debug)] @@ -49,6 +51,7 @@ pub enum Key { Configuration(&'static str), Nullifier(Nullifier), TreeRoot(H256), + BlockRoot(H256), } #[derive(Debug, Clone)] @@ -63,6 +66,7 @@ pub enum Value { Configuration(Bytes), Empty, TreeState(RegularTreeState), + TreeRoot(H256), } impl Value { @@ -78,6 +82,7 @@ impl Value { Key::Configuration(_) => deserialize(bytes).map(Value::Configuration), Key::Nullifier(_) => Ok(Value::Empty), Key::TreeRoot(_) => deserialize(bytes).map(Value::TreeState), + Key::BlockRoot(_) => deserialize(bytes).map(Value::TreeRoot), }.map_err(|e| format!("{:?}", e)) } @@ -243,6 +248,7 @@ impl<'a> From<&'a KeyValue> for RawKeyValue { }, KeyValue::BlockNumber(ref key, ref value) => (COL_BLOCK_NUMBERS, serialize(key), serialize(value)), KeyValue::TreeState(ref key, ref value) => (COL_TREESTATES, serialize(key), serialize(value)), + KeyValue::BlockRoot(ref key, ref value) => (COL_BLOCK_ROOTS, serialize(key), serialize(value)), KeyValue::Configuration(ref key, ref value) => (COL_CONFIGURATION, serialize(key), serialize(value)), }; @@ -283,6 +289,7 @@ impl<'a> From<&'a Key> for RawKey { }, Key::TreeRoot(ref key) => (COL_TREESTATES, serialize(key)), Key::BlockNumber(ref key) => (COL_BLOCK_NUMBERS, serialize(key)), + Key::BlockRoot(ref key) => (COL_BLOCK_ROOTS, serialize(key)), Key::Configuration(ref key) => (COL_CONFIGURATION, serialize(key)), };