Skip to content

Commit

Permalink
Add events and fix side effect issues related to #177
Browse files Browse the repository at this point in the history
  • Loading branch information
hackfisher committed Dec 20, 2019
1 parent 07694a3 commit 9af9961
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 34 deletions.
61 changes: 37 additions & 24 deletions srml/eth-backing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ type KtonBalanceOf<T> = <<T as Trait>::Kton as Currency<<T as system::Trait>::Ac
type PositiveImbalanceKton<T> = <<T as Trait>::Kton as Currency<<T as system::Trait>::AccountId>>::PositiveImbalance;
//type NegativeImbalanceKton<T> = <<T as Trait>::Kton as Currency<<T as system::Trait>::AccountId>>::NegativeImbalance;

type EthTransactionIndex = (H256, u64);

#[cfg(all(feature = "std", test))]
mod mock;
#[cfg(all(feature = "std", test))]
Expand All @@ -58,9 +60,9 @@ decl_storage! {
pub RingLocked get(fn ring_locked) config(): RingBalanceOf<T>;
pub KtonLocked get(fn kton_locked) config(): KtonBalanceOf<T>;

pub RingProofVerified get(ring_proof_verfied): map (H256, u64) => Option<EthReceiptProof>;
pub KtonProofVerified get(kton_proof_verfied): map (H256, u64) => Option<EthReceiptProof>;
pub DepositProofVerified get(deposit_proof_verfied): map (H256, u64) => Option<EthReceiptProof>;
pub RingProofVerified get(ring_proof_verfied): map EthTransactionIndex => Option<EthReceiptProof>;
pub KtonProofVerified get(kton_proof_verfied): map EthTransactionIndex => Option<EthReceiptProof>;
pub DepositProofVerified get(deposit_proof_verfied): map EthTransactionIndex => Option<EthReceiptProof>;
}
}

Expand All @@ -69,7 +71,9 @@ decl_event! {
where
<T as system::Trait>::AccountId
{
TODO(AccountId),
RedeemRing(AccountId, Balance, EthTransactionIndex),
RedeemKton(AccountId, Balance, EthTransactionIndex),
RedeemDeposit(AccountId, Balance, EthTransactionIndex),
}
}

Expand All @@ -78,6 +82,8 @@ decl_module! {
where
origin: T::Origin
{
fn deposit_event() = default;

// event RingBurndropTokens(address indexed token, address indexed owner, uint amount, bytes data)
// https://ropsten.etherscan.io/tx/0x81f699c93b00ab0b7db701f87b6f6045c1e0692862fcaaf8f06755abb0536800
pub fn redeem_ring(origin, proof_record: EthReceiptProof) {
Expand All @@ -89,17 +95,21 @@ decl_module! {
);

let (darwinia_account, redeemed_amount) = Self::parse_token_redeem_proof(&proof_record, "RingBurndropTokens")?;

let redeemed_ring = <RingBalanceOf<T>>::saturated_from(redeemed_amount);
let redeemed_positive_imbalance_ring = T::Ring::deposit_into_existing(&darwinia_account, redeemed_ring)?;
if let Some(new_ring_locked) = Self::ring_locked().checked_sub(&redeemed_ring) {

T::RingReward::on_unbalanced(redeemed_positive_imbalance_ring);
let redeemed_positive_imbalance_ring = T::Ring::deposit_into_existing(&darwinia_account, redeemed_ring)?;

RingProofVerified::insert((proof_record.header_hash, proof_record.index), proof_record);
T::RingReward::on_unbalanced(redeemed_positive_imbalance_ring);

RingProofVerified::insert((proof_record.header_hash, proof_record.index), &proof_record);

if let Some(new_ring_locked) = Self::ring_locked().checked_sub(&redeemed_ring) {
<RingLocked<T>>::mutate(|l| {
*l = new_ring_locked;
});

<Module<T>>::deposit_event(RawEvent::RedeemRing(darwinia_account, redeemed_amount, (proof_record.header_hash, proof_record.index)));
} else {
fail!("RING Locked - NO SUFFICIENT BACKING ASSETS")
}
Expand All @@ -115,17 +125,20 @@ decl_module! {
);

let (darwinia_account, redeemed_amount) = Self::parse_token_redeem_proof(&proof_record, "KtonBurndropTokens")?;

let redeemed_kton = <KtonBalanceOf<T>>::saturated_from(redeemed_amount);
let redeemed_positive_imbalance_kton = T::Kton::deposit_into_existing(&darwinia_account, redeemed_kton)?;
if let Some(new_kton_locked) = Self::kton_locked().checked_sub(&redeemed_kton) {
let redeemed_positive_imbalance_kton = T::Kton::deposit_into_existing(&darwinia_account, redeemed_kton)?;

T::KtonReward::on_unbalanced(redeemed_positive_imbalance_kton);
T::KtonReward::on_unbalanced(redeemed_positive_imbalance_kton);

KtonProofVerified::insert((proof_record.header_hash, proof_record.index), proof_record);
KtonProofVerified::insert((proof_record.header_hash, proof_record.index), &proof_record);

if let Some(new_kton_locked) = Self::kton_locked().checked_sub(&redeemed_kton) {
<KtonLocked<T>>::mutate(|l| {
*l = new_kton_locked;
});

<Module<T>>::deposit_event(RawEvent::RedeemKton(darwinia_account, redeemed_amount, (proof_record.header_hash, proof_record.index)));
} else {
fail!("KTON Locked - NO SUFFICIENT BACKING ASSETS")
}
Expand Down Expand Up @@ -210,26 +223,26 @@ decl_module! {
T::DetermineAccountId::account_id_for(&raw_sub_key)?
};
let redeemed_ring = <RingBalanceOf<T>>::saturated_from(redeemed_amount);
let redeemed_positive_imbalance_ring = T::Ring::deposit_into_existing(&darwinia_account, redeemed_ring)?;

T::RingReward::on_unbalanced(redeemed_positive_imbalance_ring);
if let Some(new_ring_locked) = Self::ring_locked().checked_sub(&redeemed_ring) {
T::OnDepositRedeem::on_deposit_redeem(
month.saturated_into(),
start_at.saturated_into(),
redeemed_amount,
&darwinia_account,
)?;

// TODO: check deposit_id duplication
// TODO: check deposit_id duplication

// TODO: Ignore Unit Interest for now
// TODO: Ignore Unit Interest for now

T::OnDepositRedeem::on_deposit_redeem(
month.saturated_into(),
start_at.saturated_into(),
redeemed_amount,
&darwinia_account,
)?;
DepositProofVerified::insert((proof_record.header_hash, proof_record.index), &proof_record);

DepositProofVerified::insert((proof_record.header_hash, proof_record.index), proof_record);
if let Some(new_ring_locked) = Self::ring_locked().checked_sub(&redeemed_ring) {
<RingLocked<T>>::mutate(|l| {
*l = new_ring_locked;
});

<Module<T>>::deposit_event(RawEvent::RedeemDeposit(darwinia_account, redeemed_amount, (proof_record.header_hash, proof_record.index)));
} else {
fail!("RING Locked - NO SUFFICIENT BACKING ASSETS")
}
Expand Down
19 changes: 9 additions & 10 deletions srml/eth-relay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,33 +97,33 @@ decl_module! {
fn deposit_event() = default;

pub fn reset_genesis_header(origin, header: EthHeader, genesis_difficulty: u64) {
let _relayer = ensure_signed(origin)?;
let relayer = ensure_signed(origin)?;
// TODO: Check authority

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

<Module<T>>::deposit_event(RawEvent::SetGenesisHeader(header, genesis_difficulty));
<Module<T>>::deposit_event(RawEvent::SetGenesisHeader(relayer, header, genesis_difficulty));
}

pub fn relay_header(origin, header: EthHeader) {
let _relayer = ensure_signed(origin)?;
let relayer = ensure_signed(origin)?;
// 1. There must be a corresponding parent hash
// 2. Update best hash if the current block number is larger than current best block's number (Chain reorg)

Self::verify_header(&header)?;

Self::store_header(&header)?;

<Module<T>>::deposit_event(RawEvent::RelayHeader(header));
<Module<T>>::deposit_event(RawEvent::RelayHeader(relayer, header));
}

pub fn check_receipt(origin, proof_record: EthReceiptProof) {
let _relayer = ensure_signed(origin)?;
let relayer = ensure_signed(origin)?;

let verified_receipt = Self::verify_receipt(&proof_record)?;

<Module<T>>::deposit_event(RawEvent::VerifyProof(verified_receipt, proof_record));
<Module<T>>::deposit_event(RawEvent::VerifyProof(relayer, verified_receipt, proof_record));
}

// Assuming that there are at least one honest worker submiting headers
Expand All @@ -141,10 +141,9 @@ decl_event! {
where
<T as system::Trait>::AccountId
{
SetGenesisHeader(EthHeader, u64),
RelayHeader(EthHeader),
VerifyProof(Receipt, EthReceiptProof),
TODO(AccountId),
SetGenesisHeader(AccountId, EthHeader, u64),
RelayHeader(AccountId, EthHeader),
VerifyProof(AccountId, Receipt, EthReceiptProof),

// Develop
// Print(u64),
Expand Down
4 changes: 4 additions & 0 deletions srml/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1961,6 +1961,10 @@ impl<T: Trait> OnDepositRedeem<T::AccountId> for Module<T> {
if let Some(extra) = stash_balance.checked_sub(&ledger.active_ring) {
let extra = extra.min(r);

let redeemed_positive_imbalance_ring = T::Ring::deposit_into_existing(&stash, r)?;

T::RingReward::on_unbalanced(redeemed_positive_imbalance_ring);

Self::bond_helper_in_ring_for_deposit_redeem(&stash, &controller, extra, start, promise_month, ledger);

<RingPool<T>>::mutate(|r| *r += extra);
Expand Down

0 comments on commit 9af9961

Please sign in to comment.