mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 05:51:02 +00:00
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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user