-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from AurevoirXavier/chainrelay
Chainrelay
- Loading branch information
Showing
34 changed files
with
1,151 additions
and
355 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,7 @@ windows-x86_64 | |
windows-x86_64.tar.gz | ||
|
||
# Macro expand file | ||
expand.rs | ||
expand.rs | ||
|
||
# For develop | ||
purge_run.sh |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "fly-client" | ||
version = "0.1.0" | ||
authors = ["Xavier Lau <c.estlavie@icloud.com>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
|
||
[features] | ||
default = ["std"] | ||
std = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#![cfg_attr(not(feature = "std"), no_std)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[package] | ||
name = "merkle-mountain-range" | ||
version = "0.1.0" | ||
authors = ["Xavier Lau <c.estlavie@icloud.com>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
blake2 = { version = "0.8.1", default-features = false } | ||
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] } | ||
rstd = { package = "sr-std", git = 'https://github.com/paritytech/substrate.git', default-features = false } | ||
system = { package = "srml-system", git = 'https://github.com/paritytech/substrate.git', default-features = false } | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"codec/std", | ||
"blake2/std", | ||
"rstd/std", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
use blake2::Digest; | ||
// for `vec![]` macro | ||
use rstd::vec; | ||
use rstd::vec::Vec; | ||
|
||
const ALL_ONES: usize = usize::max_value(); | ||
|
||
pub type Hash = Vec<u8>; | ||
|
||
pub fn peak_map_height(mut index: usize) -> (usize, usize) { | ||
if index == 0 { | ||
return (0, 0); | ||
} | ||
|
||
let mut peak_size = ALL_ONES >> index.leading_zeros(); | ||
let mut bitmap = 0; | ||
while peak_size != 0 { | ||
bitmap <<= 1; | ||
if index >= peak_size { | ||
index -= peak_size; | ||
bitmap |= 1; | ||
} | ||
|
||
peak_size >>= 1; | ||
} | ||
|
||
(bitmap, index) | ||
} | ||
|
||
pub fn peak_indexes(size: usize) -> Vec<usize> { | ||
if size == 0 { | ||
return vec![]; | ||
} | ||
|
||
let mut peak_size = ALL_ONES >> size.leading_zeros(); | ||
let mut num_left = size; | ||
let mut sum_prev_peaks = 0; | ||
let mut peaks = vec![]; | ||
|
||
while peak_size != 0 { | ||
if num_left >= peak_size { | ||
sum_prev_peaks += peak_size; | ||
num_left -= peak_size; | ||
|
||
peaks.push(sum_prev_peaks - 1); | ||
} | ||
|
||
peak_size >>= 1; | ||
} | ||
|
||
if num_left > 0 { | ||
vec![] | ||
} else { | ||
peaks | ||
} | ||
} | ||
|
||
#[inline] | ||
pub fn is_leaf(index: usize) -> bool { | ||
bintree_height(index) == 0 | ||
} | ||
|
||
#[inline] | ||
pub fn bintree_height(index: usize) -> usize { | ||
if index == 0 { | ||
0 | ||
} else { | ||
peak_map_height(index).1 | ||
} | ||
} | ||
|
||
pub fn family_branch(index: usize, last_index: usize) -> Vec<(usize, usize)> { | ||
let (peak_map, height) = peak_map_height(index); | ||
let mut peak = 1 << height; | ||
let mut branch = vec![]; | ||
let mut current = index; | ||
let mut sibling; | ||
while current < last_index { | ||
if (peak_map & peak) != 0 { | ||
current += 1; | ||
sibling = current - 2 * peak; | ||
} else { | ||
current += 2 * peak; | ||
sibling = current - 1; | ||
} | ||
if current > last_index { | ||
break; | ||
} | ||
|
||
branch.push((current, sibling)); | ||
peak <<= 1; | ||
} | ||
|
||
branch | ||
} | ||
|
||
pub fn family(index: usize) -> (usize, usize) { | ||
let (peak_map, height) = peak_map_height(index); | ||
let peak = 1 << height; | ||
|
||
if (peak_map & peak) != 0 { | ||
(index + 1, index + 1 - 2 * peak) | ||
} else { | ||
(index + 2 * peak, index + 2 * peak - 1) | ||
} | ||
} | ||
|
||
#[inline] | ||
pub fn is_left_sibling(index: usize) -> bool { | ||
let (peak_map, height) = peak_map_height(index); | ||
let peak = 1 << height; | ||
(peak_map & peak) == 0 | ||
} | ||
|
||
#[inline] | ||
pub fn leaf_index(n: usize) -> usize { | ||
if n == 0 { | ||
0 | ||
} else { | ||
2 * n - n.count_ones() as usize | ||
} | ||
} | ||
|
||
#[inline] | ||
pub fn chain_two_hash<D, H>(left: H, right: H) -> Hash | ||
where | ||
D: Digest, | ||
H: AsRef<[u8]>, | ||
{ | ||
D::new().chain(left).chain(right).result().to_vec() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#![cfg_attr(not(feature = "std"), no_std)] | ||
#![feature(test)] | ||
|
||
#[cfg(all(feature = "std", test))] | ||
extern crate test; | ||
|
||
mod common; | ||
mod merkle_mountain_range; | ||
mod merkle_proof; | ||
|
||
#[allow(unused)] | ||
#[cfg(all(feature = "std", test))] | ||
mod tests; | ||
|
||
pub use common::*; | ||
pub use merkle_mountain_range::MerkleMountainRange; | ||
pub use merkle_proof::MerkleProof; |
Oops, something went wrong.