Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chainrelay #89

Merged
merged 23 commits into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ windows-x86_64
windows-x86_64.tar.gz

# Macro expand file
expand.rs
expand.rs

# For develop
purge_run.sh
32 changes: 27 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 12 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ substrate-phragmen = { git = 'https://github.com/paritytech/substrate.git' }
substrate-session = { git = 'https://github.com/paritytech/substrate.git' }
substrate-wasm-interface = { git = 'https://github.com/paritytech/substrate.git' }


[build-dependencies]
vergen = "3.0.4"

Expand All @@ -60,20 +59,27 @@ path = 'node/src/main.rs'

[workspace]
members = [
# "darwinia-client",
"core/merkle-mountain-range",
"core/fly-client",
"core/sr-eth-primitives",
"core/sr-rlp",

# "darwinia-client",

"node/cli",
"node/executor",
"node/primitives",
"node/runtime",
"node/rpc-client",
"srml/staking",
"srml/kton",

"srml/support",
"srml/try",
"srml/chainrelay",
"srml/chainrelay/poa"

"srml/chainrelay/bridge/eos",
"srml/chainrelay/bridge/ethereum",

"srml/kton",
"srml/staking",
]

exclude = ["node/runtime/wasm"]
Expand All @@ -84,4 +90,3 @@ build = 'build.rs'
edition = '2018'
name = 'darwinia'
version = '0.3.2'

26 changes: 0 additions & 26 deletions build.sh

This file was deleted.

13 changes: 13 additions & 0 deletions core/fly-client/Cargo.toml
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 = []
1 change: 1 addition & 0 deletions core/fly-client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#![cfg_attr(not(feature = "std"), no_std)]
21 changes: 21 additions & 0 deletions core/merkle-mountain-range/Cargo.toml
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",
]
131 changes: 131 additions & 0 deletions core/merkle-mountain-range/src/common.rs
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()
}
17 changes: 17 additions & 0 deletions core/merkle-mountain-range/src/lib.rs
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;
Loading