Browse Source

Result<T> to mt.add() function, applied rustfmt

master
arnaucube 5 years ago
parent
commit
57fc963ef4
5 changed files with 717 additions and 506 deletions
  1. +1
    -1
      src/constants.rs
  2. +33
    -31
      src/db.rs
  3. +654
    -453
      src/lib.rs
  4. +10
    -9
      src/node.rs
  5. +19
    -12
      src/utils.rs

+ 1
- 1
src/constants.rs

@ -2,4 +2,4 @@ pub const TYPENODEEMPTY: u8 = 0;
pub const TYPENODENORMAL: u8 = 1; pub const TYPENODENORMAL: u8 = 1;
pub const TYPENODEFINAL: u8 = 2; pub const TYPENODEFINAL: u8 = 2;
pub const TYPENODEVALUE: u8 = 3; pub const TYPENODEVALUE: u8 = 3;
pub const EMPTYNODEVALUE: [u8;32] = [0;32];
pub const EMPTYNODEVALUE: [u8; 32] = [0; 32];

+ 33
- 31
src/db.rs

@ -1,47 +1,49 @@
extern crate rusty_leveldb; extern crate rusty_leveldb;
use self::rusty_leveldb::{DB};
use self::rusty_leveldb::DB;
use constants; use constants;
pub struct Db { pub struct Db {
storage: DB
storage: DB,
} }
impl Db { impl Db {
pub fn new(path: String, in_memory: bool) -> Db {
pub fn new(path: String, in_memory: bool) -> Db {
let opt: rusty_leveldb::Options; let opt: rusty_leveldb::Options;
if in_memory { if in_memory {
opt = rusty_leveldb::in_memory();
opt = rusty_leveldb::in_memory();
} else { } else {
opt = Default::default();
opt = Default::default();
} }
let database = DB::open(path, opt).unwrap(); let database = DB::open(path, opt).unwrap();
Db {
storage: database,
Db { storage: database }
}
pub fn insert(&mut self, k: [u8; 32], t: u8, il: u32, b: Vec<u8>) {
let mut v: Vec<u8>;
v = [t].to_vec();
let il_bytes = il.to_le_bytes();
v.extend(il_bytes.to_vec()); // il_bytes are [u8;4] (4 bytes)
v.extend(&b);
self.storage.put(&k[..], &v[..]).unwrap();
}
pub fn get(&mut self, k: &[u8; 32]) -> (u8, u32, Vec<u8>) {
if k.to_vec() == constants::EMPTYNODEVALUE.to_vec() {
return (0, 0, constants::EMPTYNODEVALUE.to_vec());
}
match self.storage.get(k) {
Some(x) => {
let t = x[0];
let il_bytes: [u8; 4] = [x[1], x[2], x[3], x[4]];
let il = u32::from_le_bytes(il_bytes);
let b = &x[5..];
(t, il, b.to_vec())
}
None => (
constants::TYPENODEEMPTY,
0,
constants::EMPTYNODEVALUE.to_vec(),
),
} }
} }
pub fn insert(&mut self, k: [u8; 32], t: u8, il: u32, b: Vec<u8>) {
let mut v: Vec<u8>;
v = [t].to_vec();
let il_bytes = il.to_le_bytes();
v.extend(il_bytes.to_vec()); // il_bytes are [u8;4] (4 bytes)
v.extend(&b);
self.storage.put(&k[..], &v[..]).unwrap();
}
pub fn get(&mut self, k: &[u8;32]) -> (u8, u32, Vec<u8>) {
if k.to_vec() == constants::EMPTYNODEVALUE.to_vec() {
return (0, 0, constants::EMPTYNODEVALUE.to_vec());
}
match self.storage.get(k) {
Some(x) => {
let t = x[0];
let il_bytes: [u8; 4] = [x[1], x[2], x[3], x[4]];
let il = u32::from_le_bytes(il_bytes);
let b = &x[5..];
(t, il, b.to_vec())
},
None => (constants::TYPENODEEMPTY, 0, constants::EMPTYNODEVALUE.to_vec()),
}
}
}
}

+ 654
- 453
src/lib.rs
File diff suppressed because it is too large
View File


+ 10
- 9
src/node.rs

@ -1,17 +1,16 @@
use utils;
use constants; use constants;
use utils;
pub struct TreeNode { pub struct TreeNode {
pub child_l: [u8;32],
pub child_r: [u8;32],
pub child_l: [u8; 32],
pub child_r: [u8; 32],
} }
impl TreeNode { impl TreeNode {
pub fn bytes(&self) -> Vec<u8> { pub fn bytes(&self) -> Vec<u8> {
concatenate_arrays(&self.child_l, &self.child_r) concatenate_arrays(&self.child_l, &self.child_r)
} }
pub fn ht(&self) -> [u8;32] {
pub fn ht(&self) -> [u8; 32] {
utils::hash_vec(self.bytes()) utils::hash_vec(self.bytes())
} }
} }
@ -19,12 +18,12 @@ impl TreeNode {
fn concatenate_arrays<T: Clone>(x: &[T], y: &[T]) -> Vec<T> { fn concatenate_arrays<T: Clone>(x: &[T], y: &[T]) -> Vec<T> {
let mut concat = x.to_vec(); let mut concat = x.to_vec();
concat.extend_from_slice(y); concat.extend_from_slice(y);
concat concat
} }
pub fn parse_node_bytes(b: Vec<u8>) -> TreeNode { pub fn parse_node_bytes(b: Vec<u8>) -> TreeNode {
if b==constants::EMPTYNODEVALUE {
if b == constants::EMPTYNODEVALUE {
let n = TreeNode { let n = TreeNode {
child_l: constants::EMPTYNODEVALUE, child_l: constants::EMPTYNODEVALUE,
child_r: constants::EMPTYNODEVALUE, child_r: constants::EMPTYNODEVALUE,
@ -39,7 +38,6 @@ pub fn parse_node_bytes(b: Vec) -> TreeNode {
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -51,6 +49,9 @@ mod tests {
child_l: constants::EMPTYNODEVALUE, child_l: constants::EMPTYNODEVALUE,
child_r: constants::EMPTYNODEVALUE, child_r: constants::EMPTYNODEVALUE,
}; };
assert_eq!("ad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb5", n.ht().to_hex())
assert_eq!(
"ad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb5",
n.ht().to_hex()
)
} }
} }

+ 19
- 12
src/utils.rs

@ -1,6 +1,6 @@
use constants;
use node; use node;
use tiny_keccak::Keccak; use tiny_keccak::Keccak;
use constants;
pub fn hash_vec(b: Vec<u8>) -> [u8; 32] { pub fn hash_vec(b: Vec<u8>) -> [u8; 32] {
let mut sha3 = Keccak::new_keccak256(); let mut sha3 = Keccak::new_keccak256();
@ -10,15 +10,19 @@ pub fn hash_vec(b: Vec) -> [u8; 32] {
res res
} }
pub fn get_path(num_levels: u32, hi: [u8;32]) -> Vec<bool> {
pub fn get_path(num_levels: u32, hi: [u8; 32]) -> Vec<bool> {
let mut path = Vec::new(); let mut path = Vec::new();
for i in (0..=num_levels as usize-2).rev() {
path.push((hi[hi.len()-i/8-1] & (1 << (i%8))) > 0);
for i in (0..=num_levels as usize - 2).rev() {
path.push((hi[hi.len() - i / 8 - 1] & (1 << (i % 8))) > 0);
} }
path path
} }
pub fn calc_hash_from_leaf_and_level(until_level: u32, path: &[bool], leaf_hash: [u8;32]) -> [u8;32] {
pub fn calc_hash_from_leaf_and_level(
until_level: u32,
path: &[bool],
leaf_hash: [u8; 32],
) -> [u8; 32] {
let mut node_curr_lvl = leaf_hash; let mut node_curr_lvl = leaf_hash;
for i in 0..until_level { for i in 0..until_level {
if path[i as usize] { if path[i as usize] {
@ -41,24 +45,24 @@ pub fn calc_hash_from_leaf_and_level(until_level: u32, path: &[bool], leaf_hash:
pub fn cut_path(path: &[bool], i: usize) -> Vec<bool> { pub fn cut_path(path: &[bool], i: usize) -> Vec<bool> {
let mut path_res: Vec<bool> = Vec::new(); let mut path_res: Vec<bool> = Vec::new();
for j in 0..path.len() { for j in 0..path.len() {
if j>=i {
if j >= i {
path_res.push(path[j]); path_res.push(path[j]);
} }
} }
path_res path_res
} }
pub fn compare_paths(a: &[bool], b: &[bool]) -> u32 {
pub fn compare_paths(a: &[bool], b: &[bool]) -> i32 {
for i in (0..a.len()).rev() { for i in (0..a.len()).rev() {
if a[i] != b[i] { if a[i] != b[i] {
return i as u32;
return i as i32;
} }
} }
999
-1
} }
pub fn get_empties_between_i_and_pos(i: u32, pos: u32) -> Vec<[u8;32]> {
let mut sibl: Vec<[u8;32]> = Vec::new();
pub fn get_empties_between_i_and_pos(i: u32, pos: u32) -> Vec<[u8; 32]> {
let mut sibl: Vec<[u8; 32]> = Vec::new();
for _ in (pos..=i).rev() { for _ in (pos..=i).rev() {
sibl.push(constants::EMPTYNODEVALUE); sibl.push(constants::EMPTYNODEVALUE);
} }
@ -75,6 +79,9 @@ mod tests {
let a: Vec<u8> = From::from("test"); let a: Vec<u8> = From::from("test");
assert_eq!("74657374", a.to_hex()); assert_eq!("74657374", a.to_hex());
let h = hash_vec(a); let h = hash_vec(a);
assert_eq!("9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658", h.to_hex());
assert_eq!(
"9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658",
h.to_hex()
);
} }
} }

Loading…
Cancel
Save