memory db and key types
This commit is contained in:
parent
2d6e6c3136
commit
9aa90a45e9
|
@ -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<H256, KeyState<TransactionMeta>>,
|
||||
block_number: HashMap<H256, KeyState<u32>>,
|
||||
configuration: HashMap<&'static str, KeyState<Bytes>>,
|
||||
sprout_nullifiers: HashMap<H256, KeyState<()>>,
|
||||
sapling_nullifiers: HashMap<H256, KeyState<()>>,
|
||||
}
|
||||
|
||||
#[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)
|
||||
|
|
|
@ -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)),
|
||||
};
|
||||
|
|
|
@ -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};
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue