From 9af9961fd0aeda7c0d0ca48f1b98587d2f7777b9 Mon Sep 17 00:00:00 2001 From: HackFisher Date: Fri, 20 Dec 2019 15:27:32 +0800 Subject: [PATCH] Add events and fix side effect issues related to #177 --- srml/eth-backing/src/lib.rs | 61 ++++++++++++++++++++++--------------- srml/eth-relay/src/lib.rs | 19 ++++++------ srml/staking/src/lib.rs | 4 +++ 3 files changed, 50 insertions(+), 34 deletions(-) diff --git a/srml/eth-backing/src/lib.rs b/srml/eth-backing/src/lib.rs index 49b7b6e4b..729ae376d 100644 --- a/srml/eth-backing/src/lib.rs +++ b/srml/eth-backing/src/lib.rs @@ -33,6 +33,8 @@ type KtonBalanceOf = <::Kton as Currency<::Ac type PositiveImbalanceKton = <::Kton as Currency<::AccountId>>::PositiveImbalance; //type NegativeImbalanceKton = <::Kton as Currency<::AccountId>>::NegativeImbalance; +type EthTransactionIndex = (H256, u64); + #[cfg(all(feature = "std", test))] mod mock; #[cfg(all(feature = "std", test))] @@ -58,9 +60,9 @@ decl_storage! { pub RingLocked get(fn ring_locked) config(): RingBalanceOf; pub KtonLocked get(fn kton_locked) config(): KtonBalanceOf; - pub RingProofVerified get(ring_proof_verfied): map (H256, u64) => Option; - pub KtonProofVerified get(kton_proof_verfied): map (H256, u64) => Option; - pub DepositProofVerified get(deposit_proof_verfied): map (H256, u64) => Option; + pub RingProofVerified get(ring_proof_verfied): map EthTransactionIndex => Option; + pub KtonProofVerified get(kton_proof_verfied): map EthTransactionIndex => Option; + pub DepositProofVerified get(deposit_proof_verfied): map EthTransactionIndex => Option; } } @@ -69,7 +71,9 @@ decl_event! { where ::AccountId { - TODO(AccountId), + RedeemRing(AccountId, Balance, EthTransactionIndex), + RedeemKton(AccountId, Balance, EthTransactionIndex), + RedeemDeposit(AccountId, Balance, EthTransactionIndex), } } @@ -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) { @@ -89,17 +95,21 @@ decl_module! { ); let (darwinia_account, redeemed_amount) = Self::parse_token_redeem_proof(&proof_record, "RingBurndropTokens")?; + let redeemed_ring = >::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) { >::mutate(|l| { *l = new_ring_locked; }); + + >::deposit_event(RawEvent::RedeemRing(darwinia_account, redeemed_amount, (proof_record.header_hash, proof_record.index))); } else { fail!("RING Locked - NO SUFFICIENT BACKING ASSETS") } @@ -115,17 +125,20 @@ decl_module! { ); let (darwinia_account, redeemed_amount) = Self::parse_token_redeem_proof(&proof_record, "KtonBurndropTokens")?; + let redeemed_kton = >::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) { >::mutate(|l| { *l = new_kton_locked; }); + + >::deposit_event(RawEvent::RedeemKton(darwinia_account, redeemed_amount, (proof_record.header_hash, proof_record.index))); } else { fail!("KTON Locked - NO SUFFICIENT BACKING ASSETS") } @@ -210,26 +223,26 @@ decl_module! { T::DetermineAccountId::account_id_for(&raw_sub_key)? }; let redeemed_ring = >::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) { >::mutate(|l| { *l = new_ring_locked; }); + + >::deposit_event(RawEvent::RedeemDeposit(darwinia_account, redeemed_amount, (proof_record.header_hash, proof_record.index))); } else { fail!("RING Locked - NO SUFFICIENT BACKING ASSETS") } diff --git a/srml/eth-relay/src/lib.rs b/srml/eth-relay/src/lib.rs index b87fd45b4..48aaa16fa 100644 --- a/srml/eth-relay/src/lib.rs +++ b/srml/eth-relay/src/lib.rs @@ -97,17 +97,17 @@ 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)?; - >::deposit_event(RawEvent::SetGenesisHeader(header, genesis_difficulty)); + >::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) @@ -115,15 +115,15 @@ decl_module! { Self::store_header(&header)?; - >::deposit_event(RawEvent::RelayHeader(header)); + >::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)?; - >::deposit_event(RawEvent::VerifyProof(verified_receipt, proof_record)); + >::deposit_event(RawEvent::VerifyProof(relayer, verified_receipt, proof_record)); } // Assuming that there are at least one honest worker submiting headers @@ -141,10 +141,9 @@ decl_event! { where ::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), diff --git a/srml/staking/src/lib.rs b/srml/staking/src/lib.rs index 060c5eeb0..99273ffff 100644 --- a/srml/staking/src/lib.rs +++ b/srml/staking/src/lib.rs @@ -1961,6 +1961,10 @@ impl OnDepositRedeem for Module { 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); >::mutate(|r| *r += extra);