Skip to content

Commit

Permalink
Fixed #174, and adding tests for staking ledger
Browse files Browse the repository at this point in the history
  • Loading branch information
hackfisher committed Dec 19, 2019
1 parent 408bd24 commit 07694a3
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 34 deletions.
52 changes: 31 additions & 21 deletions srml/eth-backing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
//use codec::{Decode, Encode};
use ethabi::{Event as EthEvent, EventParam as EthEventParam, ParamType, RawLog};
use rstd::{borrow::ToOwned, convert::TryFrom, marker::PhantomData, result, vec}; // fmt::Debug
use sr_primitives::traits::{SaturatedConversion, Saturating};
use support::{decl_event, decl_module, decl_storage, ensure, traits::Currency, traits::OnUnbalanced}; // dispatch::Result,
use sr_primitives::traits::{CheckedSub, SaturatedConversion};
use support::{decl_event, decl_module, decl_storage, ensure, fail, traits::Currency, traits::OnUnbalanced}; // dispatch::Result,
use system::ensure_signed; // Convert,

//use sr_primitives::RuntimeDebug;
Expand Down Expand Up @@ -73,16 +73,6 @@ decl_event! {
}
}

impl<T: Trait> Module<T> {
pub fn adjust_deposit_value() {
unimplemented!()
}

// fn _release(_dest: &T::AccountId, _value: RingBalanceOf<T>) -> Result {
// unimplemented!()
// }
}

decl_module! {
pub struct Module<T: Trait> for enum Call
where
Expand All @@ -105,9 +95,14 @@ decl_module! {
T::RingReward::on_unbalanced(redeemed_positive_imbalance_ring);

RingProofVerified::insert((proof_record.header_hash, proof_record.index), proof_record);
<RingLocked<T>>::mutate(|l| {
*l = l.saturating_sub(redeemed_ring);
});

if let Some(new_ring_locked) = Self::ring_locked().checked_sub(&redeemed_ring) {
<RingLocked<T>>::mutate(|l| {
*l = new_ring_locked;
});
} else {
fail!("RING Locked - NO SUFFICIENT BACKING ASSETS")
}
}

// event KtonBurndropTokens(address indexed token, address indexed owner, uint amount, bytes data)
Expand All @@ -126,9 +121,14 @@ decl_module! {
T::KtonReward::on_unbalanced(redeemed_positive_imbalance_kton);

KtonProofVerified::insert((proof_record.header_hash, proof_record.index), proof_record);
<KtonLocked<T>>::mutate(|l| {
*l = l.saturating_sub(redeemed_kton);
});

if let Some(new_kton_locked) = Self::kton_locked().checked_sub(&redeemed_kton) {
<KtonLocked<T>>::mutate(|l| {
*l = new_kton_locked;
});
} else {
fail!("KTON Locked - NO SUFFICIENT BACKING ASSETS")
}
}

// https://github.com/evolutionlandorg/bank
Expand Down Expand Up @@ -226,9 +226,13 @@ decl_module! {
)?;

DepositProofVerified::insert((proof_record.header_hash, proof_record.index), proof_record);
<RingLocked<T>>::mutate(|l| {
*l = l.saturating_sub(redeemed_ring);
});
if let Some(new_ring_locked) = Self::ring_locked().checked_sub(&redeemed_ring) {
<RingLocked<T>>::mutate(|l| {
*l = new_ring_locked;
});
} else {
fail!("RING Locked - NO SUFFICIENT BACKING ASSETS")
}
}
}
}
Expand Down Expand Up @@ -330,3 +334,9 @@ where
Ok(darwinia_account)
}
}

impl<T: Trait> Module<T> {
pub fn adjust_deposit_value() {
unimplemented!()
}
}
4 changes: 2 additions & 2 deletions srml/eth-backing/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ impl ExtBuilder {
ring_redeem_address: hex!["dbc888d701167cbfb86486c516aafbefc3a4de6e"].into(),
kton_redeem_address: hex!["dbc888d701167cbfb86486c516aafbefc3a4de6e"].into(),
deposit_redeem_address: hex!["6ef538314829efa8386fc43386cb13b4e0a67d1e"].into(),
ring_locked: 2000000000,
kton_locked: 50000,
ring_locked: 20000000000000,
kton_locked: 5000000000000,
}
.assimilate_storage(&mut t)
.unwrap();
Expand Down
52 changes: 44 additions & 8 deletions srml/eth-backing/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ use support::{assert_ok, assert_err};
use sr_primitives::{AccountId32, traits::Dispatchable};

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

use darwinia_support::StakingLock;

use crate::{mock::*, *};

#[test]
Expand Down Expand Up @@ -56,7 +58,7 @@ fn verify_parse_token_redeem_proof() {

assert_ok!(EthRelay::init_genesis_header(&header, 0x68de130d2c02a8_u64));

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

assert_eq!(EthBacking::parse_token_redeem_proof(&proof_record, "RingBurndropTokens"), Ok((expect_account_id, 1234567891)));
});
Expand Down Expand Up @@ -100,17 +102,23 @@ fn verify_redeem_ring() {

assert_ok!(EthRelay::init_genesis_header(&header, 0x68de130d2c02a8_u64));

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

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


let ring_locked_before = EthBacking::ring_locked();

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

assert_eq!(Ring::free_balance(&expect_account_id), 1234567891 + 1);

let ring_locked_after = EthBacking::ring_locked();

assert_eq!(ring_locked_after + 1234567891, ring_locked_before);

// shouldn't redeem twice
assert_err!(EthBacking::redeem_ring(Origin::signed(id1.clone()), proof_record.clone()), "Ring For This Proof - ALREADY BEEN REDEEMED");
});
Expand Down Expand Up @@ -159,20 +167,25 @@ 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")).ok().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)));

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");

let kton_locked_before = EthBacking::kton_locked();

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

assert_eq!(Kton::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");
});
Expand Down Expand Up @@ -228,14 +241,18 @@ fn verify_redeem_deposit() {
// totalDifficulty
assert_ok!(EthRelay::init_genesis_header(&header, 0x68e58ae1c31caf_u64));

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

let ring_locked_before = EthBacking::ring_locked();

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

let id1 = AccountId32::from([0; 32]);

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

let _ = Ring::deposit_creating(&expect_account_id, 1);
assert_ok!(
staking::Call::<Test>::bond(
AccountId32::from([1; 32]),
controller.clone(),
StakingBalances::Ring(1),
RewardDestination::Controller,
0).dispatch(Origin::signed(expect_account_id.clone()))
Expand All @@ -244,8 +261,27 @@ fn verify_redeem_deposit() {

assert_eq!(Ring::free_balance(&expect_account_id), 1234000000000 + 1);

let ring_locked_after = EthBacking::ring_locked();
assert_eq!(ring_locked_after + 1234000000000, ring_locked_before);

let staking_ledger = Staking::ledger(&controller);

assert_eq!(staking_ledger, Some(StakingLedger {
stash: expect_account_id,
active_ring: 1234000000001,
active_deposit_ring: 1234000000000,
deposit_items: vec![TimeDepositItem { value: 1234000000000, start_time: 1576664555000, expire_time: 1607768555000 }],
ring_staking_lock: StakingLock { staking_amount: 1234000000001, unbondings: vec![] },
..Default::default()
}));

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

#[test]
fn verify_insurficient_backing_assets() {
// TODO
}

6 changes: 3 additions & 3 deletions srml/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,11 @@ where
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
pub struct TimeDepositItem<Ring: HasCompact, Moment> {
#[codec(compact)]
value: Ring,
pub value: Ring,
#[codec(compact)]
start_time: Moment,
pub start_time: Moment,
#[codec(compact)]
expire_time: Moment,
pub expire_time: Moment,
}

/// The ledger of a (bonded) stash.
Expand Down

0 comments on commit 07694a3

Please sign in to comment.