Stash/controller model for staking (#1782)

* First steps to stash/controller separation

* More drafting

* More drafting

* Finish draft.

* Optimisation

* Remove accidental commit

* Make it build.

* Fix linked map for traits.

* Fix Option<_> variant.

*  Improve naming a tad

* Rebuild runtime

* Builds!

* First test.

* Bump RT version

* Minor fix

* Update Mock

* adds the correct reward testcase (+staking eras which was already ok)

* fixes the basic staking testcase to work properly (along with a small fix in the module)

* New logic to avoid controller transferring stash.

* Fix some build issues.

* adding some comments to tests

* Fix impls.

* adds a few more lines to explain the test case

* More fixes.

* gets the basic test up and running again

* Fix rest of build

* Rebuild wasm

* Fix docs.

* fix staking test with new chnages

* updating some tests, pending questions

* More working tests

* adds double staking test

* Docs

* remove invalid slashing test

* Payee stuff.

* Fix build

* Docs

* Fix test

* Fix a couple of tests

* Layout plan for finishing tests before Pragmen

* Add some working tests

* re-build staking and reward tests

* Add more tests

* fix offline grace test

* Nominator should have payee checked for cleanup

* adds more nomination tets

* adds validator prefs tests

* Fix and clean up some TODOs

* Fix a couple of issues

* Fix tests

* noting warnings from tests

* final fix of local tests

* Fix slot_stake bug

* Half baked test

* Add logic to limit `unstake_threshold` set in storage

* Make sure to check before writing!

Almost forgot this one

* Move a couple of comments

* fix last broken slot_stake test

* Ignore broken test
This commit is contained in:
Gav Wood
2019-03-02 14:31:48 +01:00
committed by GitHub
parent ff331e7fdc
commit 828cd9580a
24 changed files with 1871 additions and 910 deletions
+116 -10
View File
@@ -18,7 +18,9 @@
use crate::rstd::result;
use crate::codec::Codec;
use crate::runtime_primitives::traits::{MaybeSerializeDebug, SimpleArithmetic, As};
use crate::runtime_primitives::traits::{
MaybeSerializeDebug, SimpleArithmetic, As
};
/// The account with the given id was killed.
pub trait OnFreeBalanceZero<AccountId> {
@@ -51,23 +53,58 @@ impl<Balance> OnDilution<Balance> for () {
fn on_dilution(_minted: Balance, _portion: Balance) {}
}
/// Determinator for whether a given account is able to transfer balance.
pub trait EnsureAccountLiquid<AccountId> {
/// Returns `Ok` iff the account is able to transfer funds normally. `Err(...)`
/// with the reason why not otherwise.
/// Determinator for whether a given account is able to use its **free** balance.
///
/// By convention, `ensure_account_liquid` overrules `ensure_account_can_withdraw`. If a
/// caller gets `Ok` from the former, then they do not need to call the latter.
///
/// This implies that if you define the latter away from its default of replicating the
/// former, then ensure you also redefine the former to return an `Err` in corresponding
/// situations, otherwise you'll end up giving inconsistent information.
// TODO: Remove in favour of explicit functionality in balances module: #1896
pub trait EnsureAccountLiquid<AccountId, Balance> {
/// Ensures that the account is completely unencumbered. If this is `Ok` then there's no need to
/// check any other items. If it's an `Err`, then you must use one pair of the other items.
fn ensure_account_liquid(who: &AccountId) -> result::Result<(), &'static str>;
/// Returns `Ok` iff the account is able to make a withdrawal of the given amount
/// for the given reason.
///
/// `Err(...)` with the reason why not otherwise.
///
/// By default this just reflects the results of `ensure_account_liquid`.
///
/// @warning If you redefine this away from the default, ensure that you define
/// `ensure_account_liquid` in accordance.
fn ensure_account_can_withdraw(
who: &AccountId,
_amount: Balance,
_reason: WithdrawReason
) -> result::Result<(), &'static str> {
Self::ensure_account_liquid(who)
}
}
impl<
AccountId,
X: EnsureAccountLiquid<AccountId>,
Y: EnsureAccountLiquid<AccountId>,
> EnsureAccountLiquid<AccountId> for (X, Y) {
Balance: Copy,
X: EnsureAccountLiquid<AccountId, Balance>,
Y: EnsureAccountLiquid<AccountId, Balance>,
> EnsureAccountLiquid<AccountId, Balance> for (X, Y) {
fn ensure_account_liquid(who: &AccountId) -> result::Result<(), &'static str> {
X::ensure_account_liquid(who)?;
Y::ensure_account_liquid(who)
}
fn ensure_account_can_withdraw(
who: &AccountId,
amount: Balance,
reason: WithdrawReason
) -> result::Result<(), &'static str> {
X::ensure_account_can_withdraw(who, amount, reason)?;
Y::ensure_account_can_withdraw(who, amount, reason)
}
}
impl<AccountId> EnsureAccountLiquid<AccountId> for () {
impl<AccountId, Balance> EnsureAccountLiquid<AccountId, Balance> for () {
fn ensure_account_liquid(_who: &AccountId) -> result::Result<(), &'static str> { Ok(()) }
}
@@ -102,7 +139,11 @@ pub trait Currency<AccountId> {
fn can_reserve(who: &AccountId, value: Self::Balance) -> bool;
/// The total amount of stake on the system.
fn total_issuance() -> Self:: Balance;
fn total_issuance() -> Self::Balance;
/// The minimum balance any single account may have. This is equivalent to Balances module's
/// Existential Deposit.
fn minimum_balance() -> Self::Balance;
/// The 'free' balance of a given account.
///
@@ -185,3 +226,68 @@ pub trait Currency<AccountId> {
value: Self::Balance
) -> result::Result<Option<Self::Balance>, &'static str>;
}
/// Charge bytes fee trait
pub trait ChargeBytesFee<AccountId> {
/// Charge fees from `transactor` for an extrinsic (transaction) of encoded length
/// `encoded_len` bytes. Return Ok iff the payment was successful.
fn charge_base_bytes_fee(transactor: &AccountId, encoded_len: usize) -> Result<(), &'static str>;
}
/// Charge fee trait
pub trait ChargeFee<AccountId>: ChargeBytesFee<AccountId> {
/// The type of fee amount.
type Amount;
/// Charge `amount` of fees from `transactor`. Return Ok iff the payment was successful.
fn charge_fee(transactor: &AccountId, amount: Self::Amount) -> Result<(), &'static str>;
/// Refund `amount` of previous charged fees from `transactor`. Return Ok iff the refund was successful.
fn refund_fee(transactor: &AccountId, amount: Self::Amount) -> Result<(), &'static str>;
}
/// Reason for moving funds out of an account.
#[derive(Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "std", derive(Debug))]
pub enum WithdrawReason {
/// In order to pay for (system) transaction costs.
TransactionPayment,
/// In order to transfer ownership.
Transfer,
/// In order to reserve some funds for a later return or repatriation
Reserve,
}
/// Transfer fungible asset trait
pub trait TransferAsset<AccountId> {
/// The type of asset amount.
type Amount;
/// Transfer asset from `from` account to `to` account with `amount` of asset.
fn transfer(from: &AccountId, to: &AccountId, amount: Self::Amount) -> Result<(), &'static str>;
/// Remove asset from `who` account by deducting `amount` in the account balances.
fn withdraw(who: &AccountId, amount: Self::Amount, reason: WithdrawReason) -> Result<(), &'static str>;
/// Add asset to `who` account by increasing `amount` in the account balances.
fn deposit(who: &AccountId, amount: Self::Amount) -> Result<(), &'static str>;
}
impl<T> ChargeBytesFee<T> for () {
fn charge_base_bytes_fee(_: &T, _: usize) -> Result<(), &'static str> { Ok(()) }
}
impl<T> ChargeFee<T> for () {
type Amount = ();
fn charge_fee(_: &T, _: Self::Amount) -> Result<(), &'static str> { Ok(()) }
fn refund_fee(_: &T, _: Self::Amount) -> Result<(), &'static str> { Ok(()) }
}
impl<T> TransferAsset<T> for () {
type Amount = ();
fn transfer(_: &T, _: &T, _: Self::Amount) -> Result<(), &'static str> { Ok(()) }
fn withdraw(_: &T, _: Self::Amount, _: WithdrawReason) -> Result<(), &'static str> { Ok(()) }
fn deposit(_: &T, _: Self::Amount) -> Result<(), &'static str> { Ok(()) }
}