Allow root to force transfers (#3475)

* Allow root to force transfers

* Bump version

* Avoid changing pre-existing encodings
This commit is contained in:
Gavin Wood
2019-08-24 20:12:29 +02:00
committed by GitHub
parent c14afe4352
commit 672e62fe0f
3 changed files with 32 additions and 2 deletions
+1 -1
View File
@@ -79,7 +79,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to equal spec_version. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 150,
spec_version: 151,
impl_version: 151,
apis: RUNTIME_API_VERSIONS,
};
+16 -1
View File
@@ -462,7 +462,7 @@ decl_module! {
/// - Independent of the arguments.
/// - Contains a limited number of reads and writes.
/// # </weight>
#[weight = SimpleDispatchInfo::FixedOperational(500_000)]
#[weight = SimpleDispatchInfo::FixedOperational(50_000)]
fn set_balance(
origin,
who: <T::Lookup as StaticLookup>::Source,
@@ -488,6 +488,21 @@ decl_module! {
}
Self::set_reserved_balance(&who, new_reserved);
}
/// Exactly as `transfer`, except the origin must be root and the source account may be
/// specified.
#[weight = SimpleDispatchInfo::FixedNormal(1_000_000)]
pub fn force_transfer(
origin,
source: <T::Lookup as StaticLookup>::Source,
dest: <T::Lookup as StaticLookup>::Source,
#[compact] value: T::Balance
) {
ensure_root(origin)?;
let source = T::Lookup::lookup(source)?;
let dest = T::Lookup::lookup(dest)?;
<Self as Currency<_>>::transfer(&source, &dest, value)?;
}
}
}
+15
View File
@@ -26,6 +26,7 @@ use srml_support::{
traits::{LockableCurrency, LockIdentifier, WithdrawReason, WithdrawReasons,
Currency, ReservableCurrency}
};
use system::RawOrigin;
const ID_1: LockIdentifier = *b"1 ";
const ID_2: LockIdentifier = *b"2 ";
@@ -352,6 +353,20 @@ fn balance_transfer_works() {
});
}
#[test]
fn force_transfer_works() {
with_externalities(&mut ExtBuilder::default().build(), || {
let _ = Balances::deposit_creating(&1, 111);
assert_noop!(
Balances::force_transfer(Some(2).into(), 1, 2, 69),
"bad origin: expected to be a root origin"
);
assert_ok!(Balances::force_transfer(RawOrigin::Root.into(), 1, 2, 69));
assert_eq!(Balances::total_balance(&1), 42);
assert_eq!(Balances::total_balance(&2), 69);
});
}
#[test]
fn reserving_balance_should_work() {
with_externalities(&mut ExtBuilder::default().build(), || {