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 a side effect caused by Err in eth relay #182

Merged
merged 2 commits into from
Dec 24, 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
35 changes: 16 additions & 19 deletions srml/eth-backing/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ use sr_eth_primitives::{
Bloom, EthAddress, H64, U128,
};
use std::str::FromStr;
use support::{assert_ok, assert_err};
use support::{assert_err, assert_ok};

use sr_primitives::{AccountId32, traits::Dispatchable};
use sr_primitives::{traits::Dispatchable, AccountId32};

use staking::{
RewardDestination, StakingBalances, StakingLedger, TimeDepositItem
};
use staking::{RewardDestination, StakingBalances, StakingLedger, TimeDepositItem};

use darwinia_support::StakingLock;

Expand Down Expand Up @@ -167,27 +165,27 @@ fn verify_redeem_kton() {
// totalDifficulty
assert_ok!(EthRelay::init_genesis_header(&header, 0x68e4ea361f7a78_u64));

let expect_account_id = <Test as Trait>::DetermineAccountId::account_id_for(&hex!("2a92ae5b41feba5ee68a61449c557efa9e3b894a6461c058ec2de45429adb44546")).unwrap();
let expect_account_id = <Test as Trait>::DetermineAccountId::account_id_for(&hex!("2a92ae5b41feba5ee68a61449c557efa9e3b894a6461c058ec2de45429adb44546")).unwrap();

// 0.123456789123456789 KTON
assert_eq!(EthBacking::parse_token_redeem_proof(&proof_record, "KtonBurndropTokens"), Ok((expect_account_id.clone(), 123456789)));
assert_eq!(EthBacking::parse_token_redeem_proof(&proof_record, "KtonBurndropTokens"), Ok((expect_account_id.clone(), 123456789)));

let id1 = AccountId32::from([0; 32]);
// If expect_account_id doesn't exist, redeem should fail
assert_err!(EthBacking::redeem_kton(Origin::signed(id1.clone()), proof_record.clone()), "beneficiary account must pre-exist");
// If expect_account_id doesn't exist, redeem should fail
assert_err!(EthBacking::redeem_kton(Origin::signed(id1.clone()), proof_record.clone()), "beneficiary account must pre-exist");

let kton_locked_before = EthBacking::kton_locked();

let _ = KtonModule::deposit_creating(&expect_account_id, 1);
assert_ok!(EthBacking::redeem_kton(Origin::signed(id1.clone()), proof_record.clone()));

assert_eq!(KtonModule::free_balance(&expect_account_id), 123456789 + 1);
let _ = KtonModule::deposit_creating(&expect_account_id, 1);
assert_ok!(EthBacking::redeem_kton(Origin::signed(id1.clone()), proof_record.clone()));

assert_eq!(KtonModule::free_balance(&expect_account_id), 123456789 + 1);

let kton_locked_after = EthBacking::kton_locked();
assert_eq!(kton_locked_after + 123456789, kton_locked_before);

// shouldn't redeem twice
assert_err!(EthBacking::redeem_kton(Origin::signed(id1.clone()), proof_record.clone()), "Kton For This Proof - ALREADY BEEN REDEEMED");
// shouldn't redeem twice
assert_err!(EthBacking::redeem_kton(Origin::signed(id1.clone()), proof_record.clone()), "Kton For This Proof - ALREADY BEEN REDEEMED");
});
}

Expand Down Expand Up @@ -248,15 +246,15 @@ fn verify_redeem_deposit() {
let id1 = AccountId32::from([0; 32]);

let controller = AccountId32::from([1; 32]);

let _ = RingModule::deposit_creating(&expect_account_id, 1);
assert_ok!(
staking::Call::<Test>::bond(
controller.clone(),
StakingBalances::Ring(1),
RewardDestination::Controller,
0).dispatch(Origin::signed(expect_account_id.clone()))
);
);
assert_ok!(EthBacking::redeem_deposit(Origin::signed(id1.clone()), proof_record.clone()));

assert_eq!(RingModule::free_balance(&expect_account_id), 1234000000000 + 1);
Expand Down Expand Up @@ -284,4 +282,3 @@ fn verify_redeem_deposit() {
fn verify_insurficient_backing_assets() {
// TODO
}

14 changes: 8 additions & 6 deletions srml/eth-relay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,15 @@ impl<T: Trait> Module<T> {
let header_hash = header.hash();
let block_number = header.number();

HeaderOf::insert(header_hash, header);
let prev_total_difficulty = Self::header_details_of(header.parent_hash())
.ok_or("Previous Header Detail - NOT EXISTED")?
.total_difficulty;
let best_header_hash = Self::best_header_hash();
// let best_header = Self::header_of(best_header_hash).ok_or("Can not find best header.");
let best_header_details =
Self::header_details_of(best_header_hash).ok_or("Best Header Detail - NOT EXISTED")?;

let prev_total_difficulty = Self::header_details_of(header.parent_hash()).unwrap().total_difficulty;
HeaderOf::insert(header_hash, header);

HeaderDetailsOf::insert(
header_hash,
Expand All @@ -256,10 +262,6 @@ impl<T: Trait> Module<T> {
},
);

let best_header_hash = Self::best_header_hash();
// let best_header = Self::header_of(best_header_hash).ok_or("Can not find best header.");
let best_header_details = Self::header_details_of(best_header_hash).unwrap();

// TODO: Check total difficulty and reorg if necessary.
if prev_total_difficulty + header.difficulty() > best_header_details.total_difficulty {
BestHeaderHash::mutate(|hash| {
Expand Down