Skip to content

Commit

Permalink
Cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
k06a committed Feb 3, 2020
1 parent 9300707 commit 9d32114
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 93 deletions.
52 changes: 25 additions & 27 deletions ethbridge/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::collections::HashMap;
use borsh::{BorshDeserialize, BorshSerialize};
use serde::{Serialize, Deserialize};
use near_bindgen::{near_bindgen};
use eth_types::*;
use near_bindgen::near_bindgen;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[cfg(target_arch = "wasm32")]
#[global_allocator]
Expand Down Expand Up @@ -121,17 +121,12 @@ impl EthBridge {

let mut origin_total_difficulty = U256(0.into());
let mut branch_total_difficulty = U256(0.into());

// Check validity of all the following blocks
for i in 1..block_headers.len() {
let header: BlockHeader = rlp::decode(block_headers[i].as_slice()).unwrap();

assert!(Self::verify_header(
&self,
&header,
&prev,
&dag_nodes[i]
));

assert!(Self::verify_header(&self, &header, &prev, &dag_nodes[i]));

// Compute new chain total difficulty
branch_total_difficulty += header.difficulty;
Expand All @@ -140,20 +135,23 @@ impl EthBridge {
origin_total_difficulty += self.block_difficulties[&header.number];
}

self.block_hashes.insert(header.number, header.hash.unwrap());
self.block_difficulties.insert(header.number, header.difficulty);
self.block_hashes
.insert(header.number, header.hash.unwrap());
self.block_difficulties
.insert(header.number, header.difficulty);
prev = header;
}

if !very_first_blocks {
// Ensure the longest chain rule: https://ethereum.stackexchange.com/a/13750/3032
// https://github.com/ethereum/go-ethereum/blob/525116dbff916825463931361f75e75e955c12e2/core/blockchain.go#L863
assert!(
branch_total_difficulty > origin_total_difficulty ||
(
branch_total_difficulty == origin_total_difficulty &&
prev.difficulty % 2 == U256(0.into()) // hash is good enough random for us
)
branch_total_difficulty > origin_total_difficulty
|| (
branch_total_difficulty == origin_total_difficulty
&& prev.difficulty % 2 == U256(0.into())
// hash is good enough random for us
)
);
}
self.last_block_number = prev.number;
Expand All @@ -179,15 +177,15 @@ impl EthBridge {
// 2. Added condition: header.parent_hash() == prev.hash()
//
ethereum_types::U256::from((result.0).0) < ethash::cross_boundary(header.difficulty.0)
&& header.difficulty < header.difficulty * 101 / 100
&& header.difficulty > header.difficulty * 99 / 100
&& header.gas_used <= header.gas_limit
&& header.gas_limit < prev.gas_limit * 1025 / 1024
&& header.gas_limit > prev.gas_limit * 1023 / 1024
&& header.gas_limit >= U256(5000.into())
&& header.timestamp > prev.timestamp
&& header.number == prev.number + 1
&& header.parent_hash == prev.hash.unwrap()
&& header.difficulty < header.difficulty * 101 / 100
&& header.difficulty > header.difficulty * 99 / 100
&& header.gas_used <= header.gas_limit
&& header.gas_limit < prev.gas_limit * 1025 / 1024
&& header.gas_limit > prev.gas_limit * 1023 / 1024
&& header.gas_limit >= U256(5000.into())
&& header.timestamp > prev.timestamp
&& header.number == prev.number + 1
&& header.parent_hash == prev.hash.unwrap()
}

pub fn hashimoto_merkle(
Expand Down
178 changes: 115 additions & 63 deletions ethbridge/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use futures::future::join_all;
use std::panic;
use futures::future::{join_all};

use crate::{DoubleNodeWithMerkleProof, EthBridge};
use eth_types::*;
use hex::FromHex;
use rlp::RlpStream;
use serde::{Deserialize, Deserializer};
use hex::{FromHex};
use rlp::{RlpStream};
use web3::futures::Future;
use web3::types::{Block};
use crate::{EthBridge, DoubleNodeWithMerkleProof};
use eth_types::*;
use web3::types::Block;

//#[macro_use]
//extern crate lazy_static;
Expand All @@ -26,8 +26,8 @@ struct Hex(pub Vec<u8>);

impl<'de> Deserialize<'de> for Hex {
fn deserialize<D>(deserializer: D) -> Result<Self, <D as Deserializer<'de>>::Error>
where
D: Deserializer<'de>,
where
D: Deserializer<'de>,
{
let mut s = <String as Deserialize>::deserialize(deserializer)?;
if s.starts_with("0x") {
Expand All @@ -36,7 +36,9 @@ impl<'de> Deserialize<'de> for Hex {
if s.len() % 2 == 1 {
s.insert_str(0, "0");
}
Ok(Hex(Vec::from_hex(&s).map_err(|err| serde::de::Error::custom(err.to_string()))?))
Ok(Hex(Vec::from_hex(&s).map_err(|err| {
serde::de::Error::custom(err.to_string())
})?))
}
}

Expand All @@ -53,7 +55,11 @@ struct RootsCollection {
impl From<RootsCollectionRaw> for RootsCollection {
fn from(item: RootsCollectionRaw) -> Self {
Self {
dag_merkle_roots: item.dag_merkle_roots.iter().map(|e| H128::from(&e.0)).collect(),
dag_merkle_roots: item
.dag_merkle_roots
.iter()
.map(|e| H128::from(&e.0))
.collect(),
}
}
}
Expand Down Expand Up @@ -83,33 +89,45 @@ impl From<BlockWithProofsRaw> for BlockWithProofs {
header_rlp: item.header_rlp,
merkle_root: H128::from(&item.merkle_root.0),
elements: item.elements.iter().map(|e| H256::from(&e.0)).collect(),
merkle_proofs: item.merkle_proofs.iter().map(|e| H128::from(&e.0)).collect(),
merkle_proofs: item
.merkle_proofs
.iter()
.map(|e| H128::from(&e.0))
.collect(),
}
}
}

impl BlockWithProofs {
fn combine_dag_h256_to_h512(elements: Vec<H256>) -> Vec<H512> {
elements.iter().zip(elements.iter().skip(1)).enumerate().filter(|(i,_)| {
i % 2 == 0
}).map(|(_,(a,b))| {
let mut buffer = [0u8; 64];
buffer[..32].copy_from_slice(&(a.0).0);
buffer[32..].copy_from_slice(&(b.0).0);
H512(buffer.into())
}).collect()
elements
.iter()
.zip(elements.iter().skip(1))
.enumerate()
.filter(|(i, _)| i % 2 == 0)
.map(|(_, (a, b))| {
let mut buffer = [0u8; 64];
buffer[..32].copy_from_slice(&(a.0).0);
buffer[32..].copy_from_slice(&(b.0).0);
H512(buffer.into())
})
.collect()
}

pub fn to_double_node_with_merkle_proof_vec(&self) -> Vec<DoubleNodeWithMerkleProof> {
let h512s = Self::combine_dag_h256_to_h512(self.elements.clone());
h512s.iter().zip(h512s.iter().skip(1)).enumerate().filter(|(i,_)| {
i % 2 == 0
}).map(|(i,(a,b))| {
DoubleNodeWithMerkleProof {
h512s
.iter()
.zip(h512s.iter().skip(1))
.enumerate()
.filter(|(i, _)| i % 2 == 0)
.map(|(i, (a, b))| DoubleNodeWithMerkleProof {
dag_nodes: vec![*a, *b],
proof: self.merkle_proofs[i/2 * self.proof_length as usize .. (i/2 + 1) * self.proof_length as usize].to_vec(),
}
}).collect()
proof: self.merkle_proofs
[i / 2 * self.proof_length as usize..(i / 2 + 1) * self.proof_length as usize]
.to_vec(),
})
.collect()
}
}

Expand Down Expand Up @@ -140,7 +158,10 @@ use near_bindgen::{testing_env, VMContext};

lazy_static! {
static ref WEB3RS: web3::Web3<web3::transports::Http> = {
let (eloop, transport) = web3::transports::Http::new("https://mainnet.infura.io/v3/b5f870422ee5454fb11937e947154cd2").unwrap();
let (eloop, transport) = web3::transports::Http::new(
"https://mainnet.infura.io/v3/b5f870422ee5454fb11937e947154cd2",
)
.unwrap();
eloop.into_remote();
web3::Web3::new(transport)
};
Expand All @@ -166,13 +187,14 @@ fn get_context(input: Vec<u8>, is_view: bool) -> VMContext {
}
}

fn get_blocks(web3rust: &web3::Web3<web3::transports::Http>, start: usize, stop: usize)
-> (Vec<Vec<u8>>, Vec<H256>)
{

let futures = (start..stop).map(
|i| web3rust.eth().block((i as u64).into())
).collect::<Vec<_>>();
fn get_blocks(
web3rust: &web3::Web3<web3::transports::Http>,
start: usize,
stop: usize,
) -> (Vec<Vec<u8>>, Vec<H256>) {
let futures = (start..stop)
.map(|i| web3rust.eth().block((i as u64).into()))
.collect::<Vec<_>>();

let block_headers = join_all(futures).wait().unwrap();

Expand All @@ -194,23 +216,21 @@ fn read_roots_collection() -> RootsCollection {

fn read_roots_collection_raw() -> RootsCollectionRaw {
serde_json::from_reader(
std::fs::File::open(std::path::Path::new("./src/data/dag_merkle_roots.json")).unwrap()
).unwrap()
std::fs::File::open(std::path::Path::new("./src/data/dag_merkle_roots.json")).unwrap(),
)
.unwrap()
}

fn read_block(filename: String) -> BlockWithProofs {
read_block_raw(filename).into()
}

fn read_block_raw(filename: String) -> BlockWithProofsRaw {
serde_json::from_reader(
std::fs::File::open(std::path::Path::new(&filename)).unwrap()
).unwrap()
serde_json::from_reader(std::fs::File::open(std::path::Path::new(&filename)).unwrap()).unwrap()
}

#[test]
fn add_dags_merkle_roots() {

testing_env!(get_context(vec![], false));

let dmr = read_roots_collection();
Expand All @@ -233,19 +253,25 @@ fn add_blocks_2_and_3() {
let (blocks, hashes) = get_blocks(&WEB3RS, 2, 4);

// $ ../ethrelay/ethashproof/cmd/relayer/relayer 3
let blocks_with_proofs: Vec<BlockWithProofs> = [
"./src/data/2.json",
"./src/data/3.json"
].iter().map(|filename| read_block((&filename).to_string())).collect();
let blocks_with_proofs: Vec<BlockWithProofs> = ["./src/data/2.json", "./src/data/3.json"]
.iter()
.map(|filename| read_block((&filename).to_string()))
.collect();

let mut contract = EthBridge::default();
contract.init(0, read_roots_collection().dag_merkle_roots);

contract.add_block_headers(
blocks,
blocks_with_proofs.iter().map(|b| b.to_double_node_with_merkle_proof_vec()).collect(),
blocks_with_proofs
.iter()
.map(|b| b.to_double_node_with_merkle_proof_vec())
.collect(),
);
assert_eq!(
(hashes[1].0).0,
(contract.block_hash_unsafe(3).unwrap().0).0
);
assert_eq!((hashes[1].0).0, (contract.block_hash_unsafe(3).unwrap().0).0);
}

#[test]
Expand Down Expand Up @@ -287,9 +313,12 @@ fn add_400000_block_only() {

contract.add_block_headers(
blocks,
vec![block_with_proof.to_double_node_with_merkle_proof_vec()]
vec![block_with_proof.to_double_node_with_merkle_proof_vec()],
);
assert_eq!(
(hashes[0].0).0,
(contract.block_hash_unsafe(400_000).unwrap().0).0
);
assert_eq!((hashes[0].0).0, (contract.block_hash_unsafe(400_000).unwrap().0).0);
}

#[test]
Expand All @@ -300,20 +329,30 @@ fn add_two_blocks_from_8996776() {
let (blocks, hashes) = get_blocks(&WEB3RS, 8_996_776, 8_996_778);

// $ ../ethrelay/ethashproof/cmd/relayer/relayer 8996777
let blocks_with_proofs: Vec<BlockWithProofs> = [
"./src/data/8996776.json",
"./src/data/8996777.json"
].iter().map(|filename| read_block((&filename).to_string())).collect();
let blocks_with_proofs: Vec<BlockWithProofs> =
["./src/data/8996776.json", "./src/data/8996777.json"]
.iter()
.map(|filename| read_block((&filename).to_string()))
.collect();

let mut contract = EthBridge::default();
contract.init(0, read_roots_collection().dag_merkle_roots);

contract.add_block_headers(
blocks,
blocks_with_proofs.iter().map(|b| b.to_double_node_with_merkle_proof_vec()).collect()
blocks_with_proofs
.iter()
.map(|b| b.to_double_node_with_merkle_proof_vec())
.collect(),
);
assert_eq!(
(hashes[0].0).0,
(contract.block_hash_unsafe(8_996_776).unwrap().0).0
);
assert_eq!(
(hashes[1].0).0,
(contract.block_hash_unsafe(8_996_777).unwrap().0).0
);
assert_eq!((hashes[0].0).0, (contract.block_hash_unsafe(8_996_776).unwrap().0).0);
assert_eq!((hashes[1].0).0, (contract.block_hash_unsafe(8_996_777).unwrap().0).0);
}

#[test]
Expand All @@ -329,20 +368,33 @@ fn add_2_blocks_from_400000() {
// Proof length: 24
// [400001.json]

let blocks_with_proofs: Vec<BlockWithProofs> = [
"./src/data/400000.json",
"./src/data/400001.json"
].iter().map(|filename| read_block((&filename).to_string())).collect();
let blocks_with_proofs: Vec<BlockWithProofs> =
["./src/data/400000.json", "./src/data/400001.json"]
.iter()
.map(|filename| read_block((&filename).to_string()))
.collect();

let mut contract = EthBridge::default();
contract.init(400_000 / 30000, vec![blocks_with_proofs.first().unwrap().merkle_root]);
contract.init(
400_000 / 30000,
vec![blocks_with_proofs.first().unwrap().merkle_root],
);

contract.add_block_headers(
blocks,
blocks_with_proofs.iter().map(|b| b.to_double_node_with_merkle_proof_vec()).collect(),
blocks_with_proofs
.iter()
.map(|b| b.to_double_node_with_merkle_proof_vec())
.collect(),
);
assert_eq!(
(hashes[0].0).0,
(contract.block_hash_unsafe(400_000).unwrap().0).0
);
assert_eq!(
(hashes[1].0).0,
(contract.block_hash_unsafe(400_001).unwrap().0).0
);
assert_eq!((hashes[0].0).0, (contract.block_hash_unsafe(400_000).unwrap().0).0);
assert_eq!((hashes[1].0).0, (contract.block_hash_unsafe(400_001).unwrap().0).0);
}

// #[test]
Expand Down
Loading

0 comments on commit 9d32114

Please sign in to comment.