Skip to content

Commit

Permalink
Don't deposit failure event in orml-unknown-tokens. (open-web3-stack#440
Browse files Browse the repository at this point in the history
)

* Don't deposit failure event in orml-unknown-tokens.

* Patch substrate/polkadot/cumulus.

* Fix patch.
  • Loading branch information
shaunxw authored Apr 8, 2021
1 parent d1749f0 commit 8053ddb
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 93 deletions.
83 changes: 64 additions & 19 deletions Cargo.dev.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,67 @@ members = [
]
resolver = "2"

[patch.crates-io]
frame-benchmarking = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1" }
frame-support = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1" }
frame-system = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1" }
pallet-balances = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1" }
pallet-elections-phragmen = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1" }
pallet-scheduler = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1" }
pallet-treasury = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1" }
sp-api = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1" }
sp-application-crypto = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1" }
sp-arithmetic = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1" }
sp-core = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1" }
sp-inherents = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1" }
sp-io = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1" }
sp-runtime = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1" }
sp-runtime-interface = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1" }
sp-std = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1" }
sp-trie = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1" }
sp-version = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1" }
[patch.'https://github.com/paritytech/substrate']
frame-benchmarking = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
frame-support = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
frame-system = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
pallet-balances = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
pallet-elections-phragmen = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
pallet-scheduler = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
pallet-treasury = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
pallet-authorship = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
pallet-vesting = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
pallet-timestamp = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
pallet-session = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
pallet-staking = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
pallet-offences = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
pallet-authority-discovery = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sp-api = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sp-application-crypto = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sp-arithmetic = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sp-core = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sp-inherents = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sp-io = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sp-runtime = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sp-runtime-interface = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sp-std = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sp-trie = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sp-version = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sp-externalities = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sp-state-machine = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sp-wasm-interface = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sp-keystore = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sp-staking = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sp-storage = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sp-panic-handler = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sp-npos-elections = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sp-npos-elections-compact = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sp-debug-derive = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sp-allocator = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sp-authority-discovery = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sc-executor-common = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sc-executor-wasmi = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sc-executor = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sc-client-api = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sp-tasks = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sp-authorship = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sp-consensus = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sp-timestamp = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sp-session = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sp-blockchain = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }
sp-transaction-pool = { git = "https://github.com/paritytech//substrate", rev = "3ec97a31b285181fb1beab426ee2a8e2cb1188ab" }

[patch.'https://github.com/paritytech/cumulus']
cumulus-primitives-core = { git = "https://github.com/paritytech//cumulus", rev = "c5be7306f425be9fcf3c6e0bc598c0f481588cd9" }
cumulus-pallet-parachain-system = { git = "https://github.com/paritytech//cumulus", rev = "c5be7306f425be9fcf3c6e0bc598c0f481588cd9" }
parachain-info = { git = "https://github.com/paritytech//cumulus", rev = "c5be7306f425be9fcf3c6e0bc598c0f481588cd9" }
cumulus-pallet-xcm-handler = { git = "https://github.com/paritytech//cumulus", rev = "c5be7306f425be9fcf3c6e0bc598c0f481588cd9" }

[patch.'https://github.com/paritytech/polkadot']
xcm = { git = "https://github.com/paritytech//polkadot", rev = "6b5b4a58a3f6a3fb647fec926609e04b7d5a336a" }
xcm-executor = { git = "https://github.com/paritytech//polkadot", rev = "6b5b4a58a3f6a3fb647fec926609e04b7d5a336a" }
xcm-builder = { git = "https://github.com/paritytech//polkadot", rev = "6b5b4a58a3f6a3fb647fec926609e04b7d5a336a" }
polkadot-core-primitives = { git = "https://github.com/paritytech//polkadot", rev = "6b5b4a58a3f6a3fb647fec926609e04b7d5a336a" }
polkadot-runtime-parachains = { git = "https://github.com/paritytech//polkadot", rev = "6b5b4a58a3f6a3fb647fec926609e04b7d5a336a" }
polkadot-parachain = { git = "https://github.com/paritytech//polkadot", rev = "6b5b4a58a3f6a3fb647fec926609e04b7d5a336a" }
polkadot-primitives = { git = "https://github.com/paritytech//polkadot", rev = "6b5b4a58a3f6a3fb647fec926609e04b7d5a336a" }
28 changes: 8 additions & 20 deletions unknown-tokens/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,8 @@ pub mod module {
pub enum Event {
/// Deposit success. [asset, to]
Deposited(MultiAsset, MultiLocation),
/// Deposit failed. [asset, to, error]
DepositFailed(MultiAsset, MultiLocation, DispatchError),
/// Withdraw success. [asset, from]
Withdrawn(MultiAsset, MultiLocation),
/// Withdraw failed. [asset, from, error]
WithdrawFailed(MultiAsset, MultiLocation, DispatchError),
}

#[pallet::error]
Expand Down Expand Up @@ -74,7 +70,7 @@ pub mod module {

impl<T: Config> UnknownAsset for Pallet<T> {
fn deposit(asset: &MultiAsset, to: &MultiLocation) -> DispatchResult {
let result = match asset {
match asset {
MultiAsset::ConcreteFungible { id, amount } => {
ConcreteFungibleBalances::<T>::try_mutate(to, id, |b| -> DispatchResult {
*b = b.checked_add(*amount).ok_or(Error::<T>::BalanceOverflow)?;
Expand All @@ -88,19 +84,15 @@ impl<T: Config> UnknownAsset for Pallet<T> {
})
}
_ => Err(Error::<T>::UnhandledAsset.into()),
};
}?;

if let Err(err) = result {
Self::deposit_event(Event::DepositFailed(asset.clone(), to.clone(), err));
} else {
Self::deposit_event(Event::Deposited(asset.clone(), to.clone()));
}
Self::deposit_event(Event::Deposited(asset.clone(), to.clone()));

result
Ok(())
}

fn withdraw(asset: &MultiAsset, from: &MultiLocation) -> DispatchResult {
let result = match asset {
match asset {
MultiAsset::ConcreteFungible { id, amount } => {
ConcreteFungibleBalances::<T>::try_mutate(from, id, |b| -> DispatchResult {
*b = b.checked_sub(*amount).ok_or(Error::<T>::BalanceTooLow)?;
Expand All @@ -114,14 +106,10 @@ impl<T: Config> UnknownAsset for Pallet<T> {
})
}
_ => Err(Error::<T>::UnhandledAsset.into()),
};
}?;

if let Err(err) = result {
Self::deposit_event(Event::WithdrawFailed(asset.clone(), from.clone(), err));
} else {
Self::deposit_event(Event::Withdrawn(asset.clone(), from.clone()));
}
Self::deposit_event(Event::Withdrawn(asset.clone(), from.clone()));

result
Ok(())
}
}
54 changes: 0 additions & 54 deletions unknown-tokens/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,6 @@ fn deposit_concrete_fungible_asset_works() {
UnknownTokens::deposit(&max_asset, &MOCK_RECIPIENT),
Error::<Runtime>::BalanceOverflow
);

let deposit_failed_event = Event::unknown_tokens(crate::Event::DepositFailed(
max_asset,
MOCK_RECIPIENT,
Error::<Runtime>::BalanceOverflow.into(),
));
assert!(System::events()
.iter()
.any(|record| record.event == deposit_failed_event));
});
}

Expand All @@ -83,15 +74,6 @@ fn deposit_abstract_fungible_asset() {
UnknownTokens::abstract_fungible_balances(&MOCK_RECIPIENT, &mock_abstract_fungible_id()),
3
);

let deposit_failed_event = Event::unknown_tokens(crate::Event::DepositFailed(
max_asset,
MOCK_RECIPIENT,
Error::<Runtime>::BalanceOverflow.into(),
));
assert!(System::events()
.iter()
.any(|record| record.event == deposit_failed_event));
});
}

Expand All @@ -102,15 +84,6 @@ fn deposit_unhandled_asset_should_fail() {
UnknownTokens::deposit(&MultiAsset::All, &MOCK_RECIPIENT),
Error::<Runtime>::UnhandledAsset
);

let deposit_failed_event = Event::unknown_tokens(crate::Event::DepositFailed(
MultiAsset::All,
MOCK_RECIPIENT,
Error::<Runtime>::UnhandledAsset.into(),
));
assert!(System::events()
.iter()
.any(|record| record.event == deposit_failed_event));
});
}

Expand All @@ -134,15 +107,6 @@ fn withdraw_concrete_fungible_asset_works() {
UnknownTokens::withdraw(&asset, &MOCK_RECIPIENT),
Error::<Runtime>::BalanceTooLow
);

let withdraw_failed_event = Event::unknown_tokens(crate::Event::WithdrawFailed(
asset,
MOCK_RECIPIENT,
Error::<Runtime>::BalanceTooLow.into(),
));
assert!(System::events()
.iter()
.any(|record| record.event == withdraw_failed_event));
});
}

Expand All @@ -166,15 +130,6 @@ fn withdraw_abstract_fungible_asset_works() {
UnknownTokens::withdraw(&asset, &MOCK_RECIPIENT),
Error::<Runtime>::BalanceTooLow
);

let withdraw_failed_event = Event::unknown_tokens(crate::Event::WithdrawFailed(
asset,
MOCK_RECIPIENT,
Error::<Runtime>::BalanceTooLow.into(),
));
assert!(System::events()
.iter()
.any(|record| record.event == withdraw_failed_event));
});
}

Expand All @@ -185,14 +140,5 @@ fn withdraw_unhandled_asset_should_fail() {
UnknownTokens::withdraw(&MultiAsset::All, &MOCK_RECIPIENT),
Error::<Runtime>::UnhandledAsset
);

let withdraw_failed_event = Event::unknown_tokens(crate::Event::WithdrawFailed(
MultiAsset::All,
MOCK_RECIPIENT,
Error::<Runtime>::UnhandledAsset.into(),
));
assert!(System::events()
.iter()
.any(|record| record.event == withdraw_failed_event));
});
}

0 comments on commit 8053ddb

Please sign in to comment.