fix for trees

This commit is contained in:
NikVolf 2019-04-09 18:35:20 +03:00
parent 3529542603
commit f3e333f52a
3 changed files with 73 additions and 17 deletions

View File

@ -80,19 +80,7 @@ pub struct ConsensusParams {
}
fn mainnet_pghr_verification_key() -> crypto::Pghr13VerifyingKey {
use crypto::{G1, G2, Group};
// TODO: Actually use group elements from ceremony
crypto::Pghr13VerifyingKey {
a: G2::one(),
b: G1::one(),
c: G2::one(),
z: G2::one(),
gamma: G2::one(),
gamma_beta_1: G1::one(),
gamma_beta_2: G2::one(),
ic: Vec::new(),
}
crypto::json::pghr13::decode(include_bytes!("../../res/sprout-verifying-key.json")).expect("verifying key json invalid").into()
}
fn testnet_pghr_verification_key() -> crypto::Pghr13VerifyingKey {

View File

@ -157,6 +157,13 @@ impl Dim for H32 {
const HEIGHT: usize = 32;
}
#[derive(Clone, Debug, PartialEq)]
pub struct H29;
impl Dim for H29 {
const HEIGHT: usize = 29;
}
#[derive(Clone, Debug, PartialEq)]
pub struct SproutTreeHash;
@ -261,7 +268,7 @@ impl<D: Dim, H: TreeHash> TreeState<D, H> {
}
}
pub type SproutTreeState = TreeState<H32, SproutTreeHash>;
pub type SproutTreeState = TreeState<H29, SproutTreeHash>;
pub type SaplingTreeState = TreeState<H32, SaplingTreeHash>;
impl<D: Dim, H: TreeHash> serialization::Serializable for TreeState<D, H> {
@ -327,10 +334,10 @@ mod tests {
}
#[test]
fn empty_32_root() {
fn empty_29_root() {
assert_eq!(
SproutTreeState::new().root(),
H256::from("ac58cd1388fec290d398f1944b564449a63c815880566bd1d189f7839e3b0c8c"),
H256::from("d7c612c817793191a1e68652121876d6b3bde40f4fa52bc314145ce6e5cdd259"),
)
}
@ -341,7 +348,7 @@ mod tests {
.expect("failed to append to the tree");
assert_eq!(
tree.root(),
H256::from("af3a29c548af2d8314544875fe0a59555bfda3c81ea78da54bd02f89cce68acb")
H256::from("128ebe145c5d6fd81cc7734c90db0e284dd888870d4488314ef4ad70a34232aa")
);
}

View File

@ -0,0 +1,61 @@
use std::{collections::HashMap, sync::Arc};
use parking_lot::RwLock;
use chain::hash::H256;
use storage::{TreeStateProvider, SproutTreeState, SaplingTreeState};
use error::TransactionError;
#[derive(Clone)]
pub struct TreeCache<'a> {
persistent: &'a TreeStateProvider ,
interstitial: Arc<RwLock<HashMap<H256, SproutTreeState>>>,
}
struct NoPersistentStorage;
const NO_PERSISTENT: &'static NoPersistentStorage = &NoPersistentStorage;
impl TreeStateProvider for NoPersistentStorage {
fn sprout_tree_at(&self, _root: &H256) -> Option<SproutTreeState> { None }
fn sapling_tree_at(&self, _root: &H256) -> Option<SaplingTreeState> { None }
fn sprout_block_root(&self, _block_hash: &H256) -> Option<H256> { None }
fn sapling_block_root(&self, _block_hash: &H256) -> Option<H256> { None }
}
impl<'a> TreeCache<'a> {
pub fn new(persistent: &'a TreeStateProvider) -> Self {
TreeCache {
persistent: persistent,
interstitial: Default::default(),
}
}
pub fn new_empty() -> TreeCache<'static> {
TreeCache {
persistent: NO_PERSISTENT,
interstitial: Default::default(),
}
}
pub fn continue_root(&self, root: &H256, commitments: &[[u8; 32]; 2]) -> Result<(), TransactionError> {
let mut tree = match self.interstitial.read().get(root) {
Some(tree) => tree.clone(),
None => {
self.persistent.sprout_tree_at(root).ok_or(TransactionError::UnknownAnchor(*root))?
}
};
tree.append(commitments[0].into()).expect("Unrecoverable error: merkle tree full");
tree.append(commitments[1].into()).expect("Unrecoverable error: merkle tree full");
self.interstitial.write().insert(tree.root(), tree);
Ok(())
}
}