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

Fix testing wrong data #159

Merged
merged 3 commits into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions core/sr-eth-primitives/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ impl EthHeader {
hash
}

pub fn re_compute_hash(&self) -> H256 {
keccak_hash::keccak(self.rlp(Seal::With))
}

/// Get the hash of this header (keccak of the RLP with seal).
pub fn hash(&self) -> H256 {
self.hash.unwrap_or_else(|| keccak_hash::keccak(self.rlp(Seal::With)))
Expand Down
22 changes: 18 additions & 4 deletions srml/eth-relay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ decl_storage! {
if let Some(h) = &config.header {
let header: EthHeader = rlp::decode(&h).expect("Deserialize Genesis Header - FAILED");

<Module<T>>::init_genesis_header(&header,config.genesis_difficulty);
// Discard the result even it fail.
let _ = <Module<T>>::init_genesis_header(&header,config.genesis_difficulty);

// TODO: initialize other parameters.
}
Expand All @@ -106,7 +107,7 @@ decl_module! {
// TODO: Check authority

// TODO: Just for easy testing.
Self::init_genesis_header(&header, genesis_difficulty);
Self::init_genesis_header(&header, genesis_difficulty)?;

<Module<T>>::deposit_event(RawEvent::NewHeader(header));
}
Expand Down Expand Up @@ -157,8 +158,14 @@ decl_event! {

impl<T: Trait> Module<T> {
// TOOD: what is the total difficulty for genesis/begin header
pub fn init_genesis_header(header: &EthHeader, genesis_difficulty: u64) {
pub fn init_genesis_header(header: &EthHeader, genesis_difficulty: u64) -> result::Result<(), &'static str> {
let header_hash = header.hash();

ensure!(
header_hash == header.re_compute_hash(),
"Header Hash Check Failed - Hash NOT MATCH",
hackfisher marked this conversation as resolved.
Show resolved Hide resolved
);

let block_number = header.number();

HeaderOf::insert(&header_hash, header);
Expand All @@ -180,6 +187,8 @@ impl<T: Trait> Module<T> {

// Initialize the header.
BeginHeader::put(header.clone());

Ok(())
}

fn verify_receipt(proof_record: &ActionRecord) -> result::Result<Receipt, &'static str> {
Expand All @@ -201,7 +210,11 @@ impl<T: Trait> Module<T> {
/// 2. proof of pow (mixhash)
/// 3. challenge
fn verify_header(header: &EthHeader) -> Result {
// TODO: check parent hash,
ensure!(
header.hash() == header.re_compute_hash(),
"Header Hash Check Failed - Hash NOT MATCH",
hackfisher marked this conversation as resolved.
Show resolved Hide resolved
);

let parent_hash = header.parent_hash();

let number = header.number();
Expand All @@ -211,6 +224,7 @@ impl<T: Trait> Module<T> {
"Block Number - TOO SMALL",
);

// TODO: check parent hash is the last header, ignore or reorg
let prev_header = Self::header_of(parent_hash).ok_or("Previous Header - NOT EXISTED")?;
ensure!((prev_header.number() + 1) == number, "Block Number - NOT MATCHED");

Expand Down
4 changes: 2 additions & 2 deletions srml/eth-relay/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn verify_receipt_proof() {
hash: Some(H256::from(hex!("f1a5bc27877e219b859b0bb1f2f440134553019f9bb5a2eca7a4703263e736c9"))),
};

EthRelay::init_genesis_header(&header, 0x624c22d93f8e59_u64);
EthRelay::init_genesis_header(&header, 0x624c22d93f8e59_u64).expect("Initialize Genesis Failed.");
hackfisher marked this conversation as resolved.
Show resolved Hide resolved

assert_eq!(EthRelay::verify_receipt(&proof_record), Ok(receipt));
});
Expand Down Expand Up @@ -126,7 +126,7 @@ fn relay_header() {
};


EthRelay::init_genesis_header(&header1, 0x624c22d93f8e59_u64);
EthRelay::init_genesis_header(&header1, 0x624c22d93f8e59_u64).expect("Initialize Genesis Failed.");
hackfisher marked this conversation as resolved.
Show resolved Hide resolved

// let light_dag2 = DAG::new(header2.number().into());
// let partial_header_hash2 = header2.bare_hash();
Expand Down