block roots in db

This commit is contained in:
NikVolf 2019-01-16 17:39:54 +03:00
parent 1103ba8d43
commit c833d392c4
2 changed files with 16 additions and 1 deletions

View File

@ -13,6 +13,7 @@ use storage::{TransactionMeta, EpochTag, RegularTreeState, Nullifier};
struct InnerDatabase {
meta: HashMap<&'static str, KeyState<Bytes>>,
block_hash: HashMap<u32, KeyState<H256>>,
block_root: HashMap<H256, KeyState<H256>>,
block_header: HashMap<H256, KeyState<BlockHeader>>,
block_transactions: HashMap<H256, KeyState<List<H256>>>,
transaction: HashMap<H256, KeyState<ChainTransaction>>,
@ -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)

View File

@ -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)),
};