Split Indices module from Balances (#1404)

* Indices module

* Remove indices stuff from balances

* Rejob node, move Lookup into system.

* Fix up some modules.

* Fix democracy tests

* Fix staking tests

* Fix more tests

* Final test fixes

* Bump runtime versions

* Assets uses compact dispatchers

* Contracts module uses indexed addressing

* Democracy has more compact encoding

* Example now demonstrates compact eencoding

* Sudo uses indexed address

* Upgrade key also uses indexed lookups

* Assets more compact types.

* Fix test

* Rebuild runtime, whitespace

* Remove TOODs

* Remove TODOs

* Add a couple of tests back to balances.

* Update lib.rs

* Update lib.rs
This commit is contained in:
Gav Wood
2019-01-16 15:57:19 +01:00
committed by GitHub
parent 04175ddc83
commit c9f047fe84
44 changed files with 907 additions and 619 deletions
+39 -24
View File
@@ -46,7 +46,8 @@ extern crate sr_primitives as primitives;
extern crate srml_system as system;
use runtime_support::{StorageValue, StorageMap, Parameter};
use primitives::traits::{Member, SimpleArithmetic, Zero};
use codec::{Compact, HasCompact};
use primitives::traits::{Member, SimpleArithmetic, Zero, StaticLookup};
use system::ensure_signed;
pub trait Trait: system::Trait {
@@ -66,8 +67,9 @@ decl_module! {
/// Issue a new class of fungible assets. There are, and will only ever be, `total`
/// such assets and they'll all belong to the `origin` initially. It will have an
/// identifier `AssetId` instance: this will be specified in the `Issued` event.
fn issue(origin, total: T::Balance) {
fn issue(origin, total: <T::Balance as HasCompact>::Type) {
let origin = ensure_signed(origin)?;
let total = total.into();
let id = Self::next_asset_id();
<NextAssetId<T>>::mutate(|id| *id += 1);
@@ -79,10 +81,17 @@ decl_module! {
}
/// Move some assets from one holder to another.
fn transfer(origin, id: AssetId, target: T::AccountId, amount: T::Balance) {
fn transfer(origin,
id: Compact<AssetId>,
target: <T::Lookup as StaticLookup>::Source,
amount: <T::Balance as HasCompact>::Type
) {
let origin = ensure_signed(origin)?;
let id = id.into();
let origin_account = (id, origin.clone());
let origin_balance = <Balances<T>>::get(&origin_account);
let target = T::Lookup::lookup(target)?;
let amount = amount.into();
ensure!(!amount.is_zero(), "transfer amount should be non-zero");
ensure!(origin_balance >= amount, "origin account balance must be greater than or equal to the transfer amount");
@@ -92,8 +101,9 @@ decl_module! {
}
/// Destroy any assets of `id` owned by `origin`.
fn destroy(origin, id: AssetId) {
fn destroy(origin, id: Compact<AssetId>) {
let origin = ensure_signed(origin)?;
let id = id.into();
let balance = <Balances<T>>::take((id, origin.clone()));
ensure!(!balance.is_zero(), "origin balance should be non-zero");
@@ -151,7 +161,11 @@ mod tests {
use substrate_primitives::{H256, Blake2Hasher};
// The testing primitives are very useful for avoiding having to work with signatures
// or public keys. `u64` is used as the `AccountId` and no `Signature`s are required.
use primitives::{BuildStorage, traits::{BlakeTwo256}, testing::{Digest, DigestItem, Header}};
use primitives::{
BuildStorage,
traits::{BlakeTwo256, IdentityLookup},
testing::{Digest, DigestItem, Header}
};
impl_outer_origin! {
pub enum Origin for Test {}
@@ -170,6 +184,7 @@ mod tests {
type Hashing = BlakeTwo256;
type Digest = Digest;
type AccountId = u64;
type Lookup = IdentityLookup<u64>;
type Header = Header;
type Event = ();
type Log = DigestItem;
@@ -189,7 +204,7 @@ mod tests {
#[test]
fn issuing_asset_units_to_issuer_should_work() {
with_externalities(&mut new_test_ext(), || {
assert_ok!(Assets::issue(Origin::signed(1), 100));
assert_ok!(Assets::issue(Origin::signed(1), 100.into()));
assert_eq!(Assets::balance(0, 1), 100);
});
}
@@ -197,16 +212,16 @@ mod tests {
#[test]
fn querying_total_supply_should_work() {
with_externalities(&mut new_test_ext(), || {
assert_ok!(Assets::issue(Origin::signed(1), 100));
assert_ok!(Assets::issue(Origin::signed(1), 100.into()));
assert_eq!(Assets::balance(0, 1), 100);
assert_ok!(Assets::transfer(Origin::signed(1), 0, 2, 50));
assert_ok!(Assets::transfer(Origin::signed(1), 0.into(), 2, 50.into()));
assert_eq!(Assets::balance(0, 1), 50);
assert_eq!(Assets::balance(0, 2), 50);
assert_ok!(Assets::transfer(Origin::signed(2), 0, 3, 31));
assert_ok!(Assets::transfer(Origin::signed(2), 0.into(), 3, 31.into()));
assert_eq!(Assets::balance(0, 1), 50);
assert_eq!(Assets::balance(0, 2), 19);
assert_eq!(Assets::balance(0, 3), 31);
assert_ok!(Assets::destroy(Origin::signed(3), 0));
assert_ok!(Assets::destroy(Origin::signed(3), 0.into()));
assert_eq!(Assets::total_supply(0), 69);
});
}
@@ -214,9 +229,9 @@ mod tests {
#[test]
fn transferring_amount_above_available_balance_should_work() {
with_externalities(&mut new_test_ext(), || {
assert_ok!(Assets::issue(Origin::signed(1), 100));
assert_ok!(Assets::issue(Origin::signed(1), 100.into()));
assert_eq!(Assets::balance(0, 1), 100);
assert_ok!(Assets::transfer(Origin::signed(1), 0, 2, 50));
assert_ok!(Assets::transfer(Origin::signed(1), 0.into(), 2, 50.into()));
assert_eq!(Assets::balance(0, 1), 50);
assert_eq!(Assets::balance(0, 2), 50);
});
@@ -225,50 +240,50 @@ mod tests {
#[test]
fn transferring_amount_less_than_available_balance_should_not_work() {
with_externalities(&mut new_test_ext(), || {
assert_ok!(Assets::issue(Origin::signed(1), 100));
assert_ok!(Assets::issue(Origin::signed(1), 100.into()));
assert_eq!(Assets::balance(0, 1), 100);
assert_ok!(Assets::transfer(Origin::signed(1), 0, 2, 50));
assert_ok!(Assets::transfer(Origin::signed(1), 0.into(), 2, 50.into()));
assert_eq!(Assets::balance(0, 1), 50);
assert_eq!(Assets::balance(0, 2), 50);
assert_ok!(Assets::destroy(Origin::signed(1), 0));
assert_ok!(Assets::destroy(Origin::signed(1), 0.into()));
assert_eq!(Assets::balance(0, 1), 0);
assert_noop!(Assets::transfer(Origin::signed(1), 0, 1, 50), "origin account balance must be greater than or equal to the transfer amount");
assert_noop!(Assets::transfer(Origin::signed(1), 0.into(), 1, 50.into()), "origin account balance must be greater than or equal to the transfer amount");
});
}
#[test]
fn transferring_less_than_one_unit_should_not_work() {
with_externalities(&mut new_test_ext(), || {
assert_ok!(Assets::issue(Origin::signed(1), 100));
assert_ok!(Assets::issue(Origin::signed(1), 100.into()));
assert_eq!(Assets::balance(0, 1), 100);
assert_noop!(Assets::transfer(Origin::signed(1), 0, 2, 0), "transfer amount should be non-zero");
assert_noop!(Assets::transfer(Origin::signed(1), 0.into(), 2, 0.into()), "transfer amount should be non-zero");
});
}
#[test]
fn transferring_more_units_than_total_supply_should_not_work() {
with_externalities(&mut new_test_ext(), || {
assert_ok!(Assets::issue(Origin::signed(1), 100));
assert_ok!(Assets::issue(Origin::signed(1), 100.into()));
assert_eq!(Assets::balance(0, 1), 100);
assert_noop!(Assets::transfer(Origin::signed(1), 0, 2, 101), "origin account balance must be greater than or equal to the transfer amount");
assert_noop!(Assets::transfer(Origin::signed(1), 0.into(), 2, 101.into()), "origin account balance must be greater than or equal to the transfer amount");
});
}
#[test]
fn destroying_asset_balance_with_positive_balance_should_work() {
with_externalities(&mut new_test_ext(), || {
assert_ok!(Assets::issue(Origin::signed(1), 100));
assert_ok!(Assets::issue(Origin::signed(1), 100.into()));
assert_eq!(Assets::balance(0, 1), 100);
assert_ok!(Assets::destroy(Origin::signed(1), 0));
assert_ok!(Assets::destroy(Origin::signed(1), 0.into()));
});
}
#[test]
fn destroying_asset_balance_with_zero_balance_should_not_work() {
with_externalities(&mut new_test_ext(), || {
assert_ok!(Assets::issue(Origin::signed(1), 100));
assert_ok!(Assets::issue(Origin::signed(1), 100.into()));
assert_eq!(Assets::balance(0, 2), 0);
assert_noop!(Assets::destroy(Origin::signed(2), 0), "origin balance should be non-zero");
assert_noop!(Assets::destroy(Origin::signed(2), 0.into()), "origin balance should be non-zero");
});
}
}