Skip to content

Commit

Permalink
Overhaul events (paritytech#293)
Browse files Browse the repository at this point in the history
* Overhaul Events

* Refactor bitcoin events

* Rename BlockedAccounts to Blacklist in xsystem

* Refactor gateway records

* Nits

* Fix test

* Using apply_withdraw is not appropriate

* Remove ReservedXrc20 asset type

* Simplify the trait bound of Trait

* Apply reviews

* Add TrusteeSetChanged event

* Fix the typo of module name

* .

* Fix test

* .
  • Loading branch information
liuchengxu authored Oct 16, 2020
1 parent eb1b978 commit 222bf9c
Show file tree
Hide file tree
Showing 27 changed files with 191 additions and 178 deletions.
3 changes: 1 addition & 2 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,7 @@ impl SignedExtension for BaseFilter {
FORBIDDEN_CALL,
)));
}

if XSystem::blocked_accounts(who) {
if XSystem::blacklist(who) {
return Err(TransactionValidityError::from(InvalidTransaction::Custom(
FORBIDDEN_ACCOUNT,
)));
Expand Down
3 changes: 1 addition & 2 deletions scripts/chainx-js/res/chainx_types.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"Locked",
"Reserved",
"ReservedWithdrawal",
"ReservedDexSpot",
"ReservedXRC20"
"ReservedDexSpot"
]
},
"Chain": {
Expand Down
20 changes: 10 additions & 10 deletions xpallets/assets-registrar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ pub trait Trait: frame_system::Trait {
decl_event!(
/// Event for the XAssetRegistrar Module
pub enum Event {
/// A new asset is registered. [asset_id, has_mining_rights]
Register(AssetId, bool),
/// A deregistered asset is recovered. [asset_id, has_mining_rights]
Recover(AssetId, bool),
/// An asset is invalid now. [asset_id]
Deregister(AssetId),
/// A new asset was registered. [asset_id, has_mining_rights]
Registered(AssetId, bool),
/// A deregistered asset was recovered. [asset_id, has_mining_rights]
Recovered(AssetId, bool),
/// An asset was deregistered. [asset_id]
Deregistered(AssetId),
}
);

Expand Down Expand Up @@ -144,8 +144,8 @@ decl_module! {

Self::apply_register(asset_id, asset)?;

Self::deposit_event(Event::Registered(asset_id, has_mining_rights));
T::RegistrarHandler::on_register(&asset_id, has_mining_rights)?;
Self::deposit_event(Event::Register(asset_id, has_mining_rights));

if !is_online {
let _ = Self::deregister(frame_system::RawOrigin::Root.into(), asset_id);
Expand All @@ -166,9 +166,9 @@ decl_module! {
ensure!(Self::is_valid(&id), Error::<T>::AssetIsInvalid);

AssetOnline::remove(id);
T::RegistrarHandler::on_deregister(&id)?;

Self::deposit_event(Event::Deregister(id));
Self::deposit_event(Event::Deregistered(id));
T::RegistrarHandler::on_deregister(&id)?;

Ok(())
}
Expand All @@ -187,8 +187,8 @@ decl_module! {

AssetOnline::insert(id, true);

Self::deposit_event(Event::Recovered(id, has_mining_rights));
T::RegistrarHandler::on_register(&id, has_mining_rights)?;
Self::deposit_event(Event::Recover(id, has_mining_rights));
Ok(())
}

Expand Down
1 change: 0 additions & 1 deletion xpallets/assets/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ benchmarks! {
balances.insert(AssetType::Reserved, 1000.into());
balances.insert(AssetType::ReservedWithdrawal, 1000.into());
balances.insert(AssetType::ReservedDexSpot, 1000.into());
balances.insert(AssetType::ReservedXRC20, 1000.into());
}: set_balance(RawOrigin::Root, user_lookup, ASSET_ID, balances.clone())
verify {
assert_eq!(XAssets::<T>::asset_balance(&user, &ASSET_ID), balances);
Expand Down
16 changes: 10 additions & 6 deletions xpallets/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub trait WeightInfo {
/// The module's config trait.
///
/// `frame_system::Trait` should always be included in our implied traits.
pub trait Trait: frame_system::Trait + xpallet_assets_registrar::Trait {
pub trait Trait: xpallet_assets_registrar::Trait {
/// The overarching event type.
type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;

Expand Down Expand Up @@ -124,11 +124,15 @@ decl_event!(
<T as frame_system::Trait>::AccountId,
Balance = BalanceOf<T>,
{
Move(AssetId, AccountId, AssetType, AccountId, AssetType, Balance),
Issue(AssetId, AccountId, Balance),
Destroy(AssetId, AccountId, Balance),
Set(AssetId, AccountId, AssetType, Balance),
/// Set restrictions for an asset
/// Some balances of an asset was moved from one to another. [asset_id, from, from_type, to, to_type, amount]
Moved(AssetId, AccountId, AssetType, AccountId, AssetType, Balance),
/// New balances of an asset were issued. [asset_id, receiver, amount]
Issued(AssetId, AccountId, Balance),
/// Some balances of an asset were destoryed. [asset_id, who, amount]
Destroyed(AssetId, AccountId, Balance),
/// Set asset balance of an account by root. [asset_id, who, asset_type, amount]
SetBalance(AssetId, AccountId, AssetType, Balance),
/// Set restrictions for an asset by root. [asset_id, assets_restrictions]
SetRestrictions(AssetId, AssetRestrictions),
}
);
Expand Down
6 changes: 3 additions & 3 deletions xpallets/assets/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ fn test_balance_btree_map() {
&a,
AssetType::Usable,
&a,
AssetType::ReservedXRC20,
AssetType::ReservedWithdrawal,
30,
);
assert_eq!(AssetBalance::<Test>::get(&a, &btc_id).len(), 2);
Expand All @@ -380,15 +380,15 @@ fn test_balance_btree_map() {
let _ = XAssets::move_balance(
&X_BTC,
&a,
AssetType::ReservedXRC20,
AssetType::ReservedWithdrawal,
&a,
AssetType::Usable,
10,
);
let _ = XAssets::move_balance(
&X_BTC,
&a,
AssetType::ReservedXRC20,
AssetType::ReservedWithdrawal,
&b,
AssetType::Usable,
20,
Expand Down
2 changes: 1 addition & 1 deletion xpallets/assets/src/tests_multicurrency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ fn transfer_should_work() {
assert_eq!(XAssets::free_balance(X_BTC, &BOB), 150);
assert_eq!(XAssets::total_issuance(&X_BTC), 200 + 800);

let transferred_event = MetaEvent::assets(RawEvent::Move(
let transferred_event = MetaEvent::assets(RawEvent::Moved(
X_BTC,
ALICE,
AssetType::Usable,
Expand Down
10 changes: 5 additions & 5 deletions xpallets/assets/src/trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{BalanceOf, Event, Module, Trait};

impl<AccountId, Balance> OnAssetChanged<AccountId, Balance> for () {}

pub struct AssetChangedTrigger<T: Trait>(::sp_std::marker::PhantomData<T>);
pub struct AssetChangedTrigger<T: Trait>(sp_std::marker::PhantomData<T>);

impl<T: Trait> AssetChangedTrigger<T> {
pub fn on_move_pre(
Expand All @@ -33,7 +33,7 @@ impl<T: Trait> AssetChangedTrigger<T> {
to_type: AssetType,
value: BalanceOf<T>,
) -> result::Result<(), AssetErr> {
Module::<T>::deposit_event(Event::<T>::Move(
Module::<T>::deposit_event(Event::<T>::Moved(
*id,
from.clone(),
from_type,
Expand All @@ -50,7 +50,7 @@ impl<T: Trait> AssetChangedTrigger<T> {
}

pub fn on_issue_post(id: &AssetId, who: &T::AccountId, value: BalanceOf<T>) -> DispatchResult {
Module::<T>::deposit_event(Event::<T>::Issue(*id, who.clone(), value));
Module::<T>::deposit_event(Event::<T>::Issued(*id, who.clone(), value));
T::OnAssetChanged::on_issue_post(id, who, value)?;
Ok(())
}
Expand All @@ -64,7 +64,7 @@ impl<T: Trait> AssetChangedTrigger<T> {
who: &T::AccountId,
value: BalanceOf<T>,
) -> DispatchResult {
Module::<T>::deposit_event(Event::<T>::Destroy(*id, who.clone(), value));
Module::<T>::deposit_event(Event::<T>::Destroyed(*id, who.clone(), value));
T::OnAssetChanged::on_destroy_post(id, who, value)?;
Ok(())
}
Expand All @@ -75,7 +75,7 @@ impl<T: Trait> AssetChangedTrigger<T> {
type_: AssetType,
value: BalanceOf<T>,
) -> DispatchResult {
Module::<T>::deposit_event(Event::<T>::Set(*id, who.clone(), type_, value));
Module::<T>::deposit_event(Event::<T>::SetBalance(*id, who.clone(), type_, value));
T::OnAssetChanged::on_set_balance(id, who, type_, value)?;
Ok(())
}
Expand Down
4 changes: 1 addition & 3 deletions xpallets/assets/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ use frame_support::traits::LockIdentifier;

use crate::{Error, Trait};

const ASSET_TYPES: [AssetType; 6] = [
const ASSET_TYPES: [AssetType; 5] = [
AssetType::Usable,
AssetType::Locked,
AssetType::Reserved,
AssetType::ReservedWithdrawal,
AssetType::ReservedDexSpot,
AssetType::ReservedXRC20,
];

#[derive(PartialEq, PartialOrd, Ord, Eq, Clone, Copy, Encode, Decode, RuntimeDebug)]
Expand All @@ -34,7 +33,6 @@ pub enum AssetType {
Reserved,
ReservedWithdrawal,
ReservedDexSpot,
ReservedXRC20,
}

impl AssetType {
Expand Down
10 changes: 5 additions & 5 deletions xpallets/dex/spot/src/execution/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl<T: Trait> Module<T> {
// The order count of user should be increased after a new order is created.
<OrderCountOf<T>>::insert(&who, order_id + 1);

Self::deposit_event(RawEvent::NewOrder(order.clone()));
Self::deposit_event(Event::<T>::NewOrder(order.clone()));

order
}
Expand Down Expand Up @@ -382,9 +382,9 @@ impl<T: Trait> Module<T> {
Self::insert_executed_order(taker_order);

// FIXME: The information delivered by these events seems be redundant.
Self::deposit_event(RawEvent::MakerOrderUpdated(maker_order.clone()));
Self::deposit_event(RawEvent::TakerOrderUpdated(taker_order.clone()));
Self::deposit_event(RawEvent::OrderExecuted(OrderExecutedInfo::new(
Self::deposit_event(Event::<T>::MakerOrderUpdated(maker_order.clone()));
Self::deposit_event(Event::<T>::TakerOrderUpdated(taker_order.clone()));
Self::deposit_event(Event::<T>::OrderExecuted(OrderExecutedInfo::new(
trading_history_idx,
pair_id,
price,
Expand Down Expand Up @@ -416,7 +416,7 @@ impl<T: Trait> Module<T> {

OrderInfoOf::<T>::insert(order.submitter(), order.id(), order.clone());

Self::deposit_event(RawEvent::CanceledOrderUpdated(order.clone()));
Self::deposit_event(Event::<T>::CanceledOrderUpdated(order.clone()));

Ok(())
}
Expand Down
24 changes: 12 additions & 12 deletions xpallets/dex/spot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,21 +157,21 @@ decl_event!(
<T as frame_system::Trait>::BlockNumber,
<T as Trait>::Price,
{
/// A new order is created.
/// A new order was created. [order_info]
NewOrder(Order<TradingPairId, AccountId, Balance, Price, BlockNumber>),
/// There is an update to the order due to it gets executed.
/// There was an update to the order due to it gets executed. [maker_order_info]
MakerOrderUpdated(Order<TradingPairId, AccountId, Balance, Price, BlockNumber>),
/// There is an update to the order due to it gets executed.
/// There was an update to the order due to it gets executed. [taker_order_info]
TakerOrderUpdated(Order<TradingPairId, AccountId, Balance, Price, BlockNumber>),
/// Overall information about the maker and taker orders when there is an order execution.
/// Overall information about the maker and taker orders when there was an order execution. [order_executed_info]
OrderExecuted(OrderExecutedInfo<AccountId, Balance, BlockNumber, Price>),
/// There is an update to the order due to it gets canceled.
/// There is an update to the order due to it gets canceled. [order_info]
CanceledOrderUpdated(Order<TradingPairId, AccountId, Balance, Price, BlockNumber>),
/// A new trading pair is added.
/// A new trading pair is added. [pair_profile]
TradingPairAdded(TradingPairProfile),
/// Trading pair profile has been updated.
/// Trading pair profile has been updated. [pair_profile]
TradingPairUpdated(TradingPairProfile),
/// Price fluctuation of trading pair has been updated.
/// Price fluctuation of trading pair has been updated. [pair_id, price_fluctuation]
PriceFluctuationUpdated(TradingPairId, PriceFluctuation),
}
);
Expand Down Expand Up @@ -301,7 +301,7 @@ decl_module! {
) {
ensure_root(origin)?;
PriceFluctuationOf::insert(pair_id, new);
Self::deposit_event(RawEvent::PriceFluctuationUpdated(pair_id, new));
Self::deposit_event(Event::<T>::PriceFluctuationUpdated(pair_id, new));
}

/// Add a new trading pair.
Expand Down Expand Up @@ -340,7 +340,7 @@ decl_module! {
let pair = Self::trading_pair(pair_id)?;
ensure!(tick_decimals >= pair.tick_decimals, Error::<T>::InvalidTickdecimals);
Self::apply_update_trading_pair(pair_id, tick_decimals, tradable);
Self::deposit_event(RawEvent::TradingPairUpdated(pair));
Self::deposit_event(Event::<T>::TradingPairUpdated(pair));
}
}
}
Expand Down Expand Up @@ -401,7 +401,7 @@ impl<T: Trait> Module<T> {

TradingPairCount::put(pair_id + 1);

Self::deposit_event(RawEvent::TradingPairAdded(pair));
Self::deposit_event(Event::<T>::TradingPairAdded(pair));
}

fn apply_update_trading_pair(pair_id: TradingPairId, tick_decimals: u32, tradable: bool) {
Expand Down Expand Up @@ -503,7 +503,7 @@ impl<T: Trait> xpallet_assets_registrar::RegistrarHandler for Module<T> {
if pair.base().eq(token) || pair.quote().eq(token) {
pair.tradable = false;
TradingPairOf::insert(i, &pair);
Self::deposit_event(RawEvent::TradingPairUpdated(pair));
Self::deposit_event(Event::<T>::TradingPairUpdated(pair));
}
}
}
Expand Down
44 changes: 22 additions & 22 deletions xpallets/gateway/bitcoin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,28 +166,28 @@ decl_event!(
<T as frame_system::Trait>::AccountId,
Balance = BalanceOf<T>
{
/// block hash
InsertHeader(H256),
/// tx hash, block hash, tx type
ProcessTx(H256, H256, BtcTxState),
/// Unclaimed deposit tx
/// A Bitcoin header was validated and inserted. [btc_header_hash]
HeaderInserted(H256),
/// A Bitcoin transaction was processed. [tx_hash, block_hash, tx_state]
TxProcessed(H256, H256, BtcTxState),
/// An account deposited some token. [tx_hash, who, amount]
Deposited(H256, AccountId, Balance),
/// A list of withdrawal applications were processed successfully. [tx_hash, withdrawal_ids, total_withdrawn]
Withdrawn(H256, Vec<u32>, Balance),
/// A new record of unclaimed deposit. [tx_hash]
UnclaimedDeposit(H256),
/// who, balance, txhsah, Chain Addr
DepositPending(AccountId, Balance, H256, AddrStr),
/// create withdraw tx, who proposal, withdrawal list id
CreateWithdrawalProposal(AccountId, Vec<u32>),
/// Sign withdraw tx
SignWithdrawalProposal(AccountId, bool),
/// finish proposal and wait for broadcasting
FinishProposal(H256),
/// WithdrawalFatalErr, tx hash, Proposal hash,
/// A unclaimed deposit record was removed. [depositor, deposit_amount, tx_hash, chain_addr]
PendingDepositRemoved(AccountId, Balance, H256, AddrStr),
/// A new withdrawal proposal was created. [proposer, withdrawal_ids]
WithdrawalProposalCreated(AccountId, Vec<u32>),
/// A trustee voted/vetoed a withdrawal proposal. [trustee, vote_status]
WithdrawalProposalVoted(AccountId, bool),
/// A withdrawal proposal was dropped. [reject_count, total_count, withdrawal_ids]
WithdrawalProposalDropped(u32, u32, Vec<u32>),
/// The proposal has been processed successfully and is waiting for broadcasting. [tx_hash]
WithdrawalProposalCompleted(H256),
/// A fatal error happened during the withdrwal process. [tx_hash, proposal_hash]
WithdrawalFatalErr(H256, H256),
/// reject_count, sum_count, withdrawal id list
DropWithdrawalProposal(u32, u32, Vec<u32>),
/// Deposit token for a account.
DepositToken(H256, AccountId, Balance),
/// Withdraw token for a list of withdrawal applications.
WithdrawToken(H256, Vec<u32>, Balance),
}
);

Expand Down Expand Up @@ -560,7 +560,7 @@ impl<T: Trait> Module<T> {
info!("[apply_push_header]|best index larger than this height|best height:{:}|this height{:}", best_index.height, header_info.height);
header::check_confirmed_header::<T>(&header_info)?;
};
Self::deposit_event(RawEvent::InsertHeader(hash));
Self::deposit_event(Event::<T>::HeaderInserted(hash));
Ok(())
})
}
Expand Down Expand Up @@ -603,7 +603,7 @@ impl<T: Trait> Module<T> {

let state = tx::process_tx::<T>(tx.raw, prev)?;
TxState::insert(&tx_hash, state);
Self::deposit_event(RawEvent::ProcessTx(tx_hash, block_hash, state));
Self::deposit_event(Event::<T>::TxProcessed(tx_hash, block_hash, state));
match state.result {
BtcTxResult::Success => Ok(()),
BtcTxResult::Failed => Err(Error::<T>::ProcessTxFailed.into()),
Expand Down
Loading

0 comments on commit 222bf9c

Please sign in to comment.