Skip to content

Commit 2f3644b

Browse files
authored
Merge pull request #182 from wuminzhe/develop
Fix a side effect caused by Err in eth relay
2 parents a980333 + deafa99 commit 2f3644b

File tree

2 files changed

+24
-25
lines changed

2 files changed

+24
-25
lines changed

srml/eth-backing/src/tests.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ use sr_eth_primitives::{
88
Bloom, EthAddress, H64, U128,
99
};
1010
use std::str::FromStr;
11-
use support::{assert_ok, assert_err};
11+
use support::{assert_err, assert_ok};
1212

13-
use sr_primitives::{AccountId32, traits::Dispatchable};
13+
use sr_primitives::{traits::Dispatchable, AccountId32};
1414

15-
use staking::{
16-
RewardDestination, StakingBalances, StakingLedger, TimeDepositItem
17-
};
15+
use staking::{RewardDestination, StakingBalances, StakingLedger, TimeDepositItem};
1816

1917
use darwinia_support::StakingLock;
2018

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

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

172170
// 0.123456789123456789 KTON
173-
assert_eq!(EthBacking::parse_token_redeem_proof(&proof_record, "KtonBurndropTokens"), Ok((expect_account_id.clone(), 123456789)));
174-
171+
assert_eq!(EthBacking::parse_token_redeem_proof(&proof_record, "KtonBurndropTokens"), Ok((expect_account_id.clone(), 123456789)));
172+
175173
let id1 = AccountId32::from([0; 32]);
176-
// If expect_account_id doesn't exist, redeem should fail
177-
assert_err!(EthBacking::redeem_kton(Origin::signed(id1.clone()), proof_record.clone()), "beneficiary account must pre-exist");
174+
// If expect_account_id doesn't exist, redeem should fail
175+
assert_err!(EthBacking::redeem_kton(Origin::signed(id1.clone()), proof_record.clone()), "beneficiary account must pre-exist");
178176

179177
let kton_locked_before = EthBacking::kton_locked();
180-
181-
let _ = KtonModule::deposit_creating(&expect_account_id, 1);
182-
assert_ok!(EthBacking::redeem_kton(Origin::signed(id1.clone()), proof_record.clone()));
183178

184-
assert_eq!(KtonModule::free_balance(&expect_account_id), 123456789 + 1);
179+
let _ = KtonModule::deposit_creating(&expect_account_id, 1);
180+
assert_ok!(EthBacking::redeem_kton(Origin::signed(id1.clone()), proof_record.clone()));
181+
182+
assert_eq!(KtonModule::free_balance(&expect_account_id), 123456789 + 1);
185183

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

189-
// shouldn't redeem twice
190-
assert_err!(EthBacking::redeem_kton(Origin::signed(id1.clone()), proof_record.clone()), "Kton For This Proof - ALREADY BEEN REDEEMED");
187+
// shouldn't redeem twice
188+
assert_err!(EthBacking::redeem_kton(Origin::signed(id1.clone()), proof_record.clone()), "Kton For This Proof - ALREADY BEEN REDEEMED");
191189
});
192190
}
193191

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

250248
let controller = AccountId32::from([1; 32]);
251-
249+
252250
let _ = RingModule::deposit_creating(&expect_account_id, 1);
253251
assert_ok!(
254252
staking::Call::<Test>::bond(
255253
controller.clone(),
256254
StakingBalances::Ring(1),
257255
RewardDestination::Controller,
258256
0).dispatch(Origin::signed(expect_account_id.clone()))
259-
);
257+
);
260258
assert_ok!(EthBacking::redeem_deposit(Origin::signed(id1.clone()), proof_record.clone()));
261259

262260
assert_eq!(RingModule::free_balance(&expect_account_id), 1234000000000 + 1);
@@ -284,4 +282,3 @@ fn verify_redeem_deposit() {
284282
fn verify_insurficient_backing_assets() {
285283
// TODO
286284
}
287-

srml/eth-relay/src/lib.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,15 @@ impl<T: Trait> Module<T> {
243243
let header_hash = header.hash();
244244
let block_number = header.number();
245245

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

248-
let prev_total_difficulty = Self::header_details_of(header.parent_hash()).unwrap().total_difficulty;
254+
HeaderOf::insert(header_hash, header);
249255

250256
HeaderDetailsOf::insert(
251257
header_hash,
@@ -256,10 +262,6 @@ impl<T: Trait> Module<T> {
256262
},
257263
);
258264

259-
let best_header_hash = Self::best_header_hash();
260-
// let best_header = Self::header_of(best_header_hash).ok_or("Can not find best header.");
261-
let best_header_details = Self::header_details_of(best_header_hash).unwrap();
262-
263265
// TODO: Check total difficulty and reorg if necessary.
264266
if prev_total_difficulty + header.difficulty() > best_header_details.total_difficulty {
265267
BestHeaderHash::mutate(|hash| {

0 commit comments

Comments
 (0)