From 9aa90a45e9ef78341ccf66e50e82284891f5979c Mon Sep 17 00:00:00 2001 From: NikVolf Date: Sun, 2 Dec 2018 20:58:01 +0300 Subject: [PATCH] memory db and key types --- db/src/kv/memorydb.rs | 12 +++++++++++- db/src/kv/transaction.rs | 14 ++++++++++++-- storage/src/lib.rs | 2 +- storage/src/nullifier.rs | 14 ++++++++++++++ 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/db/src/kv/memorydb.rs b/db/src/kv/memorydb.rs index 5c9c3c49..47af420e 100644 --- a/db/src/kv/memorydb.rs +++ b/db/src/kv/memorydb.rs @@ -7,7 +7,7 @@ use bytes::Bytes; use ser::List; use chain::{Transaction as ChainTransaction, BlockHeader}; use kv::{Transaction, Key, KeyState, Operation, Value, KeyValueDatabase, KeyValue}; -use storage::{TransactionMeta}; +use storage::{TransactionMeta, NullifierTag}; #[derive(Default, Debug)] struct InnerDatabase { @@ -19,6 +19,8 @@ struct InnerDatabase { transaction_meta: HashMap>, block_number: HashMap>, configuration: HashMap<&'static str, KeyState>, + sprout_nullifiers: HashMap>, + sapling_nullifiers: HashMap>, } #[derive(Default, Debug)] @@ -91,6 +93,10 @@ impl KeyValueDatabase for MemoryDatabase { Key::TransactionMeta(key) => { db.transaction_meta.insert(key, KeyState::Delete); } Key::BlockNumber(key) => { db.block_number.insert(key, KeyState::Delete); } Key::Configuration(key) => { db.configuration.insert(key, KeyState::Delete); } + Key::Nullifier(key) => match key.tag() { + NullifierTag::Sprout => { db.sprout_nullifiers.insert(*key.hash(), KeyState::Delete); }, + NullifierTag::Sapling => { db.sapling_nullifiers.insert(*key.hash(), KeyState::Delete); }, + }, } } } @@ -108,6 +114,10 @@ impl KeyValueDatabase for MemoryDatabase { Key::TransactionMeta(ref key) => db.transaction_meta.get(key).cloned().unwrap_or_default().map(Value::TransactionMeta), Key::BlockNumber(ref key) => db.block_number.get(key).cloned().unwrap_or_default().map(Value::BlockNumber), Key::Configuration(ref key) => db.configuration.get(key).cloned().unwrap_or_default().map(Value::Configuration), + Key::Nullifier(ref key) => match key.tag() { + NullifierTag::Sprout => db.sprout_nullifiers.get(key.hash()).cloned().unwrap_or_default().map(|_| Value::Empty), + NullifierTag::Sapling => db.sapling_nullifiers.get(key.hash()).cloned().unwrap_or_default().map(|_| Value::Empty), + } }; Ok(result) diff --git a/db/src/kv/transaction.rs b/db/src/kv/transaction.rs index d7d1f0df..4c4439f8 100644 --- a/db/src/kv/transaction.rs +++ b/db/src/kv/transaction.rs @@ -2,7 +2,7 @@ use bytes::Bytes; use hash::H256; use ser::{serialize, List, deserialize}; use chain::{Transaction as ChainTransaction, BlockHeader}; -use storage::{TransactionMeta}; +use storage::{TransactionMeta, Nullifier, NullifierTag}; pub const COL_COUNT: u32 = 10; pub const COL_META: u32 = 0; @@ -12,7 +12,9 @@ pub const COL_BLOCK_TRANSACTIONS: u32 = 3; pub const COL_TRANSACTIONS: u32 = 4; pub const COL_TRANSACTIONS_META: u32 = 5; pub const COL_BLOCK_NUMBERS: u32 = 6; -pub const COL_CONFIGURATION: u32 = 7; +pub const COL_SPROUT_NULLIFIERS: u32 = 7; +pub const COL_SAPLING_NULLIFIERS: u32 = 8; +pub const COL_CONFIGURATION: u32 = 9; #[derive(Debug)] pub enum Operation { @@ -42,6 +44,7 @@ pub enum Key { TransactionMeta(H256), BlockNumber(H256), Configuration(&'static str), + Nullifier(Nullifier), } #[derive(Debug, Clone)] @@ -54,6 +57,8 @@ pub enum Value { TransactionMeta(TransactionMeta), BlockNumber(u32), Configuration(Bytes), + Bloom(Bytes), + Empty, } impl Value { @@ -67,6 +72,7 @@ impl Value { Key::TransactionMeta(_) => deserialize(bytes).map(Value::TransactionMeta), Key::BlockNumber(_) => deserialize(bytes).map(Value::BlockNumber), Key::Configuration(_) => deserialize(bytes).map(Value::Configuration), + Key::Nullifier(_) => Ok(Value::Empty), }.map_err(|e| format!("{:?}", e)) } @@ -261,6 +267,10 @@ impl<'a> From<&'a Key> for RawKey { Key::BlockTransactions(ref key) => (COL_BLOCK_TRANSACTIONS, serialize(key)), Key::Transaction(ref key) => (COL_TRANSACTIONS, serialize(key)), Key::TransactionMeta(ref key) => (COL_TRANSACTIONS_META, serialize(key)), + Key::Nullifier(ref key) => match key.tag() { + NullifierTag::Sprout => (COL_SPROUT_NULLIFIERS, serialize(key.hash())), + NullifierTag::Sapling => (COL_SAPLING_NULLIFIERS, serialize(key.hash())), + }, Key::BlockNumber(ref key) => (COL_BLOCK_NUMBERS, serialize(key)), Key::Configuration(ref key) => (COL_CONFIGURATION, serialize(key)), }; diff --git a/storage/src/lib.rs b/storage/src/lib.rs index ad999890..e063ce12 100644 --- a/storage/src/lib.rs +++ b/storage/src/lib.rs @@ -36,4 +36,4 @@ pub use error::Error; pub use store::{AsSubstore, Store, SharedStore, CanonStore, ConfigStore}; pub use transaction_meta::TransactionMeta; pub use transaction_provider::{TransactionProvider, TransactionOutputProvider, TransactionMetaProvider}; -pub use nullifier::{Nullifier, NullifierTracker}; +pub use nullifier::{Nullifier, NullifierTracker, Tag as NullifierTag}; diff --git a/storage/src/nullifier.rs b/storage/src/nullifier.rs index fea3ee6c..d8909575 100644 --- a/storage/src/nullifier.rs +++ b/storage/src/nullifier.rs @@ -6,7 +6,9 @@ use hash::H256; /// even if they have the same bit pattern. #[derive(Debug, Clone, Copy, PartialEq)] pub enum Tag { + /// Sprout nullifier. Sprout, + /// Sapling nullifier. Sapling, } @@ -17,17 +19,29 @@ pub struct Nullifier { hash: H256, } +/// Trait to query existing nullifier. pub trait NullifierTracker { fn contains(&self, nullifier: Nullifier) -> bool; } impl Nullifier { + /// New nullifer. pub fn new(tag: Tag, hash: H256) -> Self { Nullifier { tag: tag, hash: hash, } } + + /// Nullifer tag + pub fn tag(&self) -> Tag { + self.tag + } + + /// Nullifer hash + pub fn hash(&self) -> &H256 { + &self.hash + } } impl From<(Tag, H256)> for Nullifier {