Bump Substrate (#816)

* Amalgamate pieces of balance module

* Fixes for vesting split

* Refactoring for vesting/balances split

* Build fixes

* Remove on_free_balance_zero and some docs.

* Indentation.

* Revert branch

* Fix.

* Update substrate: fixes after CLI refactoring

* Reverting removal of exit

* Removed too much again

* Update Cargo.lock

* Cargo.lock

* Update Substrate, ready for #4820

* Fixes

* Update to latest substrate master

* Fix network tests

* Update lock

* Fix tests

* Update futures to get bug fixes

* Fix tests for new balances/vesting logic

* Cargo fix

* Another fix

Co-authored-by: Cecile Tonglet <cecile.tonglet@cecton.com>
Co-authored-by: Robert Habermeier <rphmeier@gmail.com>
Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Gavin Wood
2020-02-09 17:50:17 +01:00
committed by GitHub
parent 9a2bba3ba1
commit 703ac8bbbc
27 changed files with 1449 additions and 1509 deletions
+59 -38
View File
@@ -18,15 +18,14 @@
use rstd::prelude::*;
use sp_io::{hashing::keccak_256, crypto::secp256k1_ecdsa_recover};
use frame_support::{decl_event, decl_storage, decl_module, decl_error};
use frame_support::weights::SimpleDispatchInfo;
use frame_support::traits::{Currency, Get, VestingCurrency};
use frame_support::{decl_event, decl_storage, decl_module, decl_error, ensure};
use frame_support::{dispatch::DispatchResult, weights::SimpleDispatchInfo};
use frame_support::traits::{Currency, Get, VestingSchedule};
use system::{ensure_root, ensure_none};
use codec::{Encode, Decode};
#[cfg(feature = "std")]
use serde::{self, Serialize, Deserialize, Serializer, Deserializer};
#[cfg(feature = "std")]
use sp_runtime::traits::Zero;
use sp_runtime::traits::{Zero, CheckedSub};
use sp_runtime::{
RuntimeDebug, transaction_validity::{
TransactionLongevity, TransactionValidity, ValidTransaction, InvalidTransaction
@@ -35,14 +34,14 @@ use sp_runtime::{
use primitives::ValidityError;
use system;
type BalanceOf<T> = <<T as Trait>::Currency as Currency<<T as system::Trait>::AccountId>>::Balance;
type CurrencyOf<T> = <<T as Trait>::VestingSchedule as VestingSchedule<<T as system::Trait>::AccountId>>::Currency;
type BalanceOf<T> = <CurrencyOf<T> as Currency<<T as system::Trait>::AccountId>>::Balance;
/// Configuration trait.
pub trait Trait: system::Trait {
/// The overarching event type.
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
type Currency: Currency<Self::AccountId>
+ VestingCurrency<Self::AccountId, Moment=Self::BlockNumber>;
type VestingSchedule: VestingSchedule<Self::AccountId, Moment=Self::BlockNumber>;
type Prefix: Get<&'static [u8]>;
}
@@ -108,6 +107,11 @@ decl_error! {
InvalidEthereumSignature,
/// Ethereum address has no claim.
SignerHasNoClaim,
/// The destination is already vesting and cannot be the target of a further claim.
DestinationVesting,
/// There's not enough in the pot to pay out some unvested amount. Generally implies a logic
/// error.
PotUnderflow,
}
}
@@ -157,24 +161,34 @@ decl_module! {
let balance_due = <Claims<T>>::get(&signer)
.ok_or(Error::<T>::SignerHasNoClaim)?;
let maybe_vested = <Vesting<T>>::get(&signer);
// If this fails, destination account already has a vesting schedule
// applied to it, and this claim should not be processed.
ensure!(
maybe_vested.is_none() || T::VestingSchedule::vesting_balance(&dest).is_zero(),
Error::<T>::DestinationVesting
);
<Total<T>>::mutate(|t| -> DispatchResult {
*t = t.checked_sub(&balance_due).ok_or(Error::<T>::PotUnderflow)?;
Ok(())
})?;
// This must happen before the add_vesting_schedule otherwise the schedule will be
// nullified.
CurrencyOf::<T>::deposit_creating(&dest, balance_due);
// Check if this claim should have a vesting schedule.
if let Some(vs) = <Vesting<T>>::get(&signer) {
// If this fails, destination account already has a vesting schedule
// applied to it, and this claim should not be processed.
T::Currency::add_vesting_schedule(&dest, vs.0, vs.1, vs.2)?;
if let Some(vs) = maybe_vested {
// Should never fail since we ensured that the destination is not already vesting.
// However, we already deposited, so can't error out here anyway.
let _ = T::VestingSchedule::add_vesting_schedule(&dest, vs.0, vs.1, vs.2);
}
<Claims<T>>::remove(&signer);
<Vesting<T>>::remove(&signer);
<Total<T>>::mutate(|t| if *t < balance_due {
panic!("Logic error: Pot less than the total of claims!")
} else {
*t -= balance_due
});
T::Currency::deposit_creating(&dest, balance_due);
// Let's deposit an event to let the outside world know this happened.
Self::deposit_event(RawEvent::Claimed(dest, signer, balance_due));
}
@@ -284,9 +298,11 @@ mod tests {
use codec::Encode;
// 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 sp_runtime::{Perbill, traits::{BlakeTwo256, IdentityLookup}, testing::Header};
use sp_runtime::{Perbill, traits::{BlakeTwo256, IdentityLookup, Identity}, testing::Header};
use frame_support::{
impl_outer_origin, assert_ok, assert_err, assert_noop, parameter_types
};
use balances;
use frame_support::{impl_outer_origin, assert_ok, assert_err, assert_noop, parameter_types};
impl_outer_origin! {
pub enum Origin for Test {}
@@ -315,8 +331,8 @@ mod tests {
type Event = ();
type BlockHashCount = BlockHashCount;
type MaximumBlockWeight = MaximumBlockWeight;
type AvailableBlockRatio = AvailableBlockRatio;
type MaximumBlockLength = MaximumBlockLength;
type AvailableBlockRatio = AvailableBlockRatio;
type Version = ();
type ModuleToIndex = ();
}
@@ -329,28 +345,33 @@ mod tests {
impl balances::Trait for Test {
type Balance = u64;
type OnFreeBalanceZero = ();
type OnReapAccount = System;
type OnNewAccount = ();
type Event = ();
type DustRemoval = ();
type TransferPayment = ();
type DustRemoval = ();
type Event = ();
type ExistentialDeposit = ExistentialDeposit;
type TransferFee = TransferFee;
type CreationFee = CreationFee;
}
impl vesting::Trait for Test {
type Event = ();
type Currency = Balances;
type BlockNumberToBalance = Identity;
}
parameter_types!{
pub const Prefix: &'static [u8] = b"Pay RUSTs to the TEST account:";
}
impl Trait for Test {
type Event = ();
type Currency = Balances;
type VestingSchedule = Vesting;
type Prefix = Prefix;
}
type System = system::Module<Test>;
type Balances = balances::Module<Test>;
type Vesting = vesting::Module<Test>;
type Claims = Module<Test>;
fn alice() -> secp256k1::SecretKey {
@@ -414,7 +435,7 @@ mod tests {
assert_eq!(Balances::free_balance(&42), 0);
assert_ok!(Claims::claim(Origin::NONE, 42, sig(&alice(), &42u64.encode())));
assert_eq!(Balances::free_balance(&42), 100);
assert_eq!(Balances::vesting_balance(&42), 50);
assert_eq!(Vesting::vesting_balance(&42), 50);
});
}
@@ -432,8 +453,8 @@ mod tests {
);
assert_ok!(Claims::mint_claim(Origin::ROOT, eth(&bob()), 200, None));
assert_ok!(Claims::claim(Origin::NONE, 69, sig(&bob(), &69u64.encode())));
assert_eq!(Balances::free_balance(&69), 200);
assert_eq!(Balances::vesting_balance(&69), 0);
assert_eq!(Balances::free_balance(69), 200);
assert_eq!(Vesting::vesting_balance(&69), 0);
});
}
@@ -444,22 +465,22 @@ mod tests {
Claims::mint_claim(Origin::signed(42), eth(&bob()), 200, Some((50, 10, 1))),
sp_runtime::traits::BadOrigin,
);
assert_eq!(Balances::free_balance(&42), 0);
assert_eq!(Balances::free_balance(42), 0);
assert_noop!(
Claims::claim(Origin::NONE, 69, sig(&bob(), &69u64.encode())),
Error::<Test>::SignerHasNoClaim
);
assert_ok!(Claims::mint_claim(Origin::ROOT, eth(&bob()), 200, Some((50, 10, 1))));
assert_ok!(Claims::claim(Origin::NONE, 69, sig(&bob(), &69u64.encode())));
assert_eq!(Balances::free_balance(&69), 200);
assert_eq!(Balances::vesting_balance(&69), 50);
assert_eq!(Balances::free_balance(69), 200);
assert_eq!(Vesting::vesting_balance(&69), 50);
});
}
#[test]
fn origin_signed_claiming_fail() {
new_test_ext().execute_with(|| {
assert_eq!(Balances::free_balance(&42), 0);
assert_eq!(Balances::free_balance(42), 0);
assert_err!(
Claims::claim(Origin::signed(42), 42, sig(&alice(), &42u64.encode())),
sp_runtime::traits::BadOrigin,
@@ -470,7 +491,7 @@ mod tests {
#[test]
fn double_claiming_doesnt_work() {
new_test_ext().execute_with(|| {
assert_eq!(Balances::free_balance(&42), 0);
assert_eq!(Balances::free_balance(42), 0);
assert_ok!(Claims::claim(Origin::NONE, 42, sig(&alice(), &42u64.encode())));
assert_noop!(
Claims::claim(Origin::NONE, 42, sig(&alice(), &42u64.encode())),
@@ -482,7 +503,7 @@ mod tests {
#[test]
fn non_sender_sig_doesnt_work() {
new_test_ext().execute_with(|| {
assert_eq!(Balances::free_balance(&42), 0);
assert_eq!(Balances::free_balance(42), 0);
assert_noop!(
Claims::claim(Origin::NONE, 42, sig(&alice(), &69u64.encode())),
Error::<Test>::SignerHasNoClaim
@@ -493,7 +514,7 @@ mod tests {
#[test]
fn non_claimant_doesnt_work() {
new_test_ext().execute_with(|| {
assert_eq!(Balances::free_balance(&42), 0);
assert_eq!(Balances::free_balance(42), 0);
assert_noop!(
Claims::claim(Origin::NONE, 42, sig(&bob(), &69u64.encode())),
Error::<Test>::SignerHasNoClaim
+12 -16
View File
@@ -284,7 +284,7 @@ decl_module! {
/// Contribute to a crowd sale. This will transfer some balance over to fund a parachain
/// slot. It will be withdrawable in two instances: the parachain becomes retired; or the
/// slot is
/// slot is unable to be purchased and the timeout expires.
fn contribute(origin, #[compact] index: FundIndex, #[compact] value: BalanceOf<T>) {
let who = ensure_signed(origin)?;
@@ -599,21 +599,16 @@ mod tests {
}
parameter_types! {
pub const ExistentialDeposit: u64 = 0;
// We want to make sure these fees are non zero, so we can check
// that our module correctly avoids these fees :)
pub const TransferFee: u64 = 10;
pub const CreationFee: u64 = 10;
pub const CreationFee: u64 = 0;
}
impl balances::Trait for Test {
type Balance = u64;
type OnFreeBalanceZero = ();
type OnReapAccount = System;
type OnNewAccount = ();
type Event = ();
type DustRemoval = ();
type TransferPayment = ();
type DustRemoval = ();
type Event = ();
type ExistentialDeposit = ExistentialDeposit;
type TransferFee = TransferFee;
type CreationFee = CreationFee;
}
@@ -730,7 +725,6 @@ mod tests {
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
balances::GenesisConfig::<Test>{
balances: vec![(1, 1000), (2, 2000), (3, 3000), (4, 4000)],
vesting: vec![],
}.assimilate_storage(&mut t).unwrap();
t.into()
}
@@ -830,7 +824,7 @@ mod tests {
// User 1 contributes to their own crowdfund
assert_ok!(Crowdfund::contribute(Origin::signed(1), 0, 49));
// User 1 has spent some funds to do this, transfer fees **are** taken
assert_eq!(Balances::free_balance(1), 940);
assert_eq!(Balances::free_balance(1), 950);
// Contributions are stored in the trie
assert_eq!(Crowdfund::contribution_get(0, &1), 49);
// Contributions appear in free balance of crowdfund
@@ -1108,13 +1102,13 @@ mod tests {
// User can withdraw their full balance without fees
assert_ok!(Crowdfund::withdraw(Origin::signed(1), 0));
assert_eq!(Balances::free_balance(1), 989);
assert_eq!(Balances::free_balance(1), 999);
assert_ok!(Crowdfund::withdraw(Origin::signed(2), 0));
assert_eq!(Balances::free_balance(2), 1990);
assert_eq!(Balances::free_balance(2), 2000);
assert_ok!(Crowdfund::withdraw(Origin::signed(3), 0));
assert_eq!(Balances::free_balance(3), 2990);
assert_eq!(Balances::free_balance(3), 3000);
});
}
@@ -1126,7 +1120,7 @@ mod tests {
assert_ok!(Crowdfund::create(Origin::signed(1), 1000, 1, 4, 9));
// Transfer fee is taken here
assert_ok!(Crowdfund::contribute(Origin::signed(1), 0, 49));
assert_eq!(Balances::free_balance(1), 940);
assert_eq!(Balances::free_balance(1), 950);
run_to_block(5);
@@ -1156,6 +1150,8 @@ mod tests {
// Skip all the way to the end
run_to_block(50);
// Check initiator's balance.
assert_eq!(Balances::free_balance(1), 899);
// Check current funds (contributions + deposit)
assert_eq!(Balances::free_balance(Crowdfund::fund_account_id(0)), 601);
@@ -1165,7 +1161,7 @@ mod tests {
// Fund account is emptied
assert_eq!(Balances::free_balance(Crowdfund::fund_account_id(0)), 0);
// Deposit is returned
assert_eq!(Balances::free_balance(1), 890);
assert_eq!(Balances::free_balance(1), 900);
// Treasury account is filled
assert_eq!(Balances::free_balance(Treasury::account_id()), 600);
+5 -9
View File
@@ -1010,13 +1010,13 @@ mod tests {
}
impl session::Trait for Test {
type SessionManager = ();
type Keys = UintAuthorityId;
type ShouldEndSession = session::PeriodicSessions<Period, Offset>;
type SessionHandler = session::TestSessionHandler;
type Event = ();
type ValidatorId = u64;
type ValidatorIdOf = staking::StashOf<Self>;
type ShouldEndSession = session::PeriodicSessions<Period, Offset>;
type SessionManager = ();
type SessionHandler = session::TestSessionHandler;
type Keys = UintAuthorityId;
type DisabledValidatorsThreshold = DisabledValidatorsThreshold;
}
@@ -1057,20 +1057,17 @@ mod tests {
parameter_types! {
pub const ExistentialDeposit: Balance = 0;
pub const TransferFee: Balance = 0;
pub const CreationFee: Balance = 0;
}
impl balances::Trait for Test {
type Balance = Balance;
type OnFreeBalanceZero = ();
type OnReapAccount = System;
type OnNewAccount = ();
type Event = ();
type DustRemoval = ();
type TransferPayment = ();
type ExistentialDeposit = ExistentialDeposit;
type TransferFee = TransferFee;
type TransferPayment = ();
type CreationFee = CreationFee;
}
@@ -1216,7 +1213,6 @@ mod tests {
balances::GenesisConfig::<Test> {
balances,
vesting: vec![],
}.assimilate_storage(&mut t).unwrap();
staking::GenesisConfig::<Test> {
+7 -11
View File
@@ -654,20 +654,17 @@ mod tests {
parameter_types! {
pub const ExistentialDeposit: Balance = 0;
pub const TransferFee: Balance = 0;
pub const CreationFee: Balance = 0;
}
impl balances::Trait for Test {
type Balance = Balance;
type OnFreeBalanceZero = ();
type OnReapAccount = System;
type OnNewAccount = ();
type OnReapAccount = System;
type Balance = Balance;
type Event = ();
type DustRemoval = ();
type TransferPayment = ();
type ExistentialDeposit = ExistentialDeposit;
type TransferFee = TransferFee;
type TransferPayment = ();
type CreationFee = CreationFee;
}
@@ -793,7 +790,6 @@ mod tests {
balances::GenesisConfig::<Test> {
balances,
vesting: vec![],
}.assimilate_storage(&mut t).unwrap();
t.into()
@@ -1060,8 +1056,8 @@ mod tests {
vec![3; 3],
));
// deposit should be taken (reserved)
assert_eq!(Balances::free_balance(&3u64) + ParathreadDeposit::get(), orig_bal);
assert_eq!(Balances::reserved_balance(&3u64), ParathreadDeposit::get());
assert_eq!(Balances::free_balance(3u64) + ParathreadDeposit::get(), orig_bal);
assert_eq!(Balances::reserved_balance(3u64), ParathreadDeposit::get());
run_to_block(3);
@@ -1083,8 +1079,8 @@ mod tests {
parachains::Origin::Parachain(user_id(0)).into()
));
// reserved balance should be returned.
assert_eq!(Balances::free_balance(&3u64), orig_bal);
assert_eq!(Balances::reserved_balance(&3u64), 0);
assert_eq!(Balances::free_balance(3u64), orig_bal);
assert_eq!(Balances::reserved_balance(3u64), 0);
run_to_block(4);
+22 -26
View File
@@ -898,21 +898,18 @@ mod tests {
parameter_types! {
pub const ExistentialDeposit: u64 = 0;
pub const TransferFee: u64 = 0;
pub const CreationFee: u64 = 0;
}
impl balances::Trait for Test {
type Balance = u64;
type OnFreeBalanceZero = ();
type OnReapAccount = System;
type OnNewAccount = ();
type Event = ();
type OnNewAccount = ();
type OnReapAccount = System;
type DustRemoval = ();
type TransferPayment = ();
type ExistentialDeposit = ExistentialDeposit;
type TransferFee = TransferFee;
type CreationFee = CreationFee;
type TransferPayment = ();
}
thread_local! {
@@ -987,7 +984,6 @@ mod tests {
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
balances::GenesisConfig::<Test>{
balances: vec![(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)],
vesting: vec![],
}.assimilate_storage(&mut t).unwrap();
t.into()
}
@@ -1086,8 +1082,8 @@ mod tests {
assert_ok!(Slots::new_auction(Origin::ROOT, 5, 1));
assert_ok!(Slots::bid(Origin::signed(1), 0, 1, 1, 4, 1));
assert_eq!(Balances::reserved_balance(&1), 1);
assert_eq!(Balances::free_balance(&1), 9);
assert_eq!(Balances::reserved_balance(1), 1);
assert_eq!(Balances::free_balance(1), 9);
run_to_block(9);
assert_eq!(Slots::onboard_queue(1), vec![0.into()]);
@@ -1095,8 +1091,8 @@ mod tests {
Some((1, IncomingParachain::Unset(NewBidder { who: 1, sub: 0 })))
);
assert_eq!(Slots::deposit_held(&0.into()), 1);
assert_eq!(Balances::reserved_balance(&1), 0);
assert_eq!(Balances::free_balance(&1), 9);
assert_eq!(Balances::reserved_balance(1), 0);
assert_eq!(Balances::free_balance(1), 9);
});
}
@@ -1106,7 +1102,7 @@ mod tests {
run_to_block(1);
assert_ok!(Slots::new_auction(Origin::ROOT, 5, 1));
assert_ok!(Slots::bid(Origin::signed(1), 0, 1, 1, 4, 1));
assert_eq!(Balances::free_balance(&1), 9);
assert_eq!(Balances::free_balance(1), 9);
run_to_block(9);
assert_eq!(Slots::deposit_held(&0.into()), 1);
@@ -1114,7 +1110,7 @@ mod tests {
run_to_block(50);
assert_eq!(Slots::deposit_held(&0.into()), 0);
assert_eq!(Balances::free_balance(&1), 10);
assert_eq!(Balances::free_balance(1), 10);
});
}
@@ -1135,7 +1131,7 @@ mod tests {
run_to_block(50);
assert_eq!(Slots::deposit_held(&0.into()), 0);
assert_eq!(Balances::free_balance(&10), 1);
assert_eq!(Balances::free_balance(10), 1);
});
}
@@ -1189,8 +1185,8 @@ mod tests {
assert_ok!(Slots::new_auction(Origin::ROOT, 5, 1));
assert_ok!(Slots::bid(Origin::signed(1), 0, 1, 1, 4, 5));
assert_ok!(Slots::bid(Origin::signed(2), 0, 1, 1, 4, 1));
assert_eq!(Balances::reserved_balance(&2), 0);
assert_eq!(Balances::free_balance(&2), 20);
assert_eq!(Balances::reserved_balance(2), 0);
assert_eq!(Balances::free_balance(2), 20);
assert_eq!(
Slots::winning(0).unwrap()[SlotRange::ZeroThree as u8 as usize],
Some((Bidder::New(NewBidder{who: 1, sub: 0}), 5))
@@ -1382,13 +1378,13 @@ mod tests {
assert_ok!(Slots::bid_renew(Origin::signed(ParaId::from(0).into_account()), 2, 2, 2, 3));
run_to_block(20);
assert_eq!(Balances::free_balance::<u64>(ParaId::from(0).into_account()), 2);
assert_eq!(Balances::free_balance(&ParaId::from(0u32).into_account()), 2);
assert_ok!(Slots::new_auction(Origin::ROOT, 5, 2));
assert_ok!(Slots::bid_renew(Origin::signed(ParaId::from(0).into_account()), 3, 3, 3, 4));
run_to_block(30);
assert_eq!(Balances::free_balance::<u64>(ParaId::from(0).into_account()), 1);
assert_eq!(Balances::free_balance(&ParaId::from(0u32).into_account()), 1);
});
}
@@ -1424,8 +1420,8 @@ mod tests {
run_to_block(i);
assert_ok!(Slots::bid(Origin::signed(i), 0, 1, 1, 4, i));
for j in 1..6 {
assert_eq!(Balances::reserved_balance(&j), if j == i { j } else { 0 });
assert_eq!(Balances::free_balance(&j), if j == i { j * 9 } else { j * 10 });
assert_eq!(Balances::reserved_balance(j), if j == i { j } else { 0 });
assert_eq!(Balances::free_balance(j), if j == i { j * 9 } else { j * 10 });
}
}
@@ -1436,8 +1432,8 @@ mod tests {
Some((1, IncomingParachain::Unset(NewBidder { who: 5, sub: 0 })))
);
assert_eq!(Slots::deposit_held(&0.into()), 5);
assert_eq!(Balances::reserved_balance(&5), 0);
assert_eq!(Balances::free_balance(&5), 45);
assert_eq!(Balances::reserved_balance(5), 0);
assert_eq!(Balances::free_balance(5), 45);
});
}
@@ -1452,8 +1448,8 @@ mod tests {
run_to_block(i + 3);
assert_ok!(Slots::bid(Origin::signed(i), 0, 1, 1, 4, i));
for j in 1..6 {
assert_eq!(Balances::reserved_balance(&j), if j == i { j } else { 0 });
assert_eq!(Balances::free_balance(&j), if j == i { j * 9 } else { j * 10 });
assert_eq!(Balances::reserved_balance(j), if j == i { j } else { 0 });
assert_eq!(Balances::free_balance(j), if j == i { j * 9 } else { j * 10 });
}
}
@@ -1464,8 +1460,8 @@ mod tests {
Some((1, IncomingParachain::Unset(NewBidder { who: 3, sub: 0 })))
);
assert_eq!(Slots::deposit_held(&0.into()), 3);
assert_eq!(Balances::reserved_balance(&3), 0);
assert_eq!(Balances::free_balance(&3), 27);
assert_eq!(Balances::reserved_balance(3), 0);
assert_eq!(Balances::free_balance(3), 27);
});
}