Skip to content

Commit

Permalink
Bench and restructure staking (#1067)
Browse files Browse the repository at this point in the history
* Restructure staking

* Allow death and format

Signed-off-by: Xavier Lau <xavier@inv.cafe>

* Bench staking

* Fix compile

* Fix tests

* Fix compile

* Format

* Log

* License

* Remove cfg

* Mint KTON through deposit

---------

Signed-off-by: Xavier Lau <xavier@inv.cafe>
  • Loading branch information
aurexav authored Mar 27, 2023
1 parent 94a0a2a commit 6aebcc4
Show file tree
Hide file tree
Showing 56 changed files with 1,672 additions and 241 deletions.
13 changes: 12 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ members = [
"core/*",
"node",
"pallet/*",
"pallet/*/traits",
"precompile/*",
"runtime/*",
]
Expand Down Expand Up @@ -62,6 +63,7 @@ darwinia-precompile-staking = { path = "precompile/staking", default-featu
darwinia-precompile-state-storage = { path = "precompile/state-storage", default-features = false }
darwinia-runtime = { path = "runtime/darwinia" }
darwinia-staking = { path = "pallet/staking", default-features = false }
darwinia-staking-traits = { path = "pallet/staking/traits", default-features = false }
dc-inflation = { path = "core/inflation", default-features = false }
dc-primitives = { path = "core/primitives", default-features = false }
dc-types = { path = "core/types" }
Expand Down
6 changes: 3 additions & 3 deletions pallet/account-migration/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ mod benchmarks {
let from = [0; 32].into();
let to = [0; 20].into();

// The worst case:
// Worst-case scenario:
//
// Migrate all kinds of data.
preset_data::<T>(&from);
Expand All @@ -167,7 +167,7 @@ mod benchmarks {
#[benchmark]
fn migrate_multisig(x: Linear<0, 99>, y: Linear<0, 99>) {
let from = AccountId32::from([0; 32]);
// The worst case:
// Worst-case scenario:
//
// 100 size multisig.
let other_members = other_multisig_members(x);
Expand All @@ -183,7 +183,7 @@ mod benchmarks {
#[benchmark]
fn complete_multisig_migration() {
let from = AccountId32::from([0; 32]);
// The worst case:
// Worst-case scenario:
//
// 100 size multisig.
let other_members = other_multisig_members(99);
Expand Down
8 changes: 4 additions & 4 deletions pallet/account-migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ use dc_primitives::{AccountId as AccountId20, AssetId, Balance, BlockNumber, Ind
use frame_support::{
migration,
pallet_prelude::*,
traits::{Currency, ExistenceRequirement::KeepAlive, LockableCurrency, WithdrawReasons},
traits::{Currency, ExistenceRequirement::AllowDeath, LockableCurrency, WithdrawReasons},
StorageHasher,
};
use frame_system::{pallet_prelude::*, AccountInfo, RawOrigin};
Expand Down Expand Up @@ -216,7 +216,7 @@ pub mod pallet {
///
/// The `_signature` should be provided by `who`.
#[pallet::call_index(1)]
#[pallet::weight(<T as Config>::WeightInfo::migrate_multisig(others.len() as u32, *threshold as u32))]
#[pallet::weight(<T as Config>::WeightInfo::migrate_multisig(others.len() as _, *threshold as _))]
pub fn migrate_multisig(
origin: OriginFor<T>,
submitter: AccountId32,
Expand Down Expand Up @@ -466,7 +466,7 @@ pub mod pallet {
&to,
&darwinia_deposit::account_id(),
ds.iter().map(|d| d.value).sum(),
KeepAlive,
AllowDeath,
)?;
<darwinia_deposit::Deposits<T>>::insert(
to,
Expand All @@ -490,7 +490,7 @@ pub mod pallet {
&to,
&staking_pot,
r,
KeepAlive,
AllowDeath,
)?;
}

Expand Down
3 changes: 3 additions & 0 deletions pallet/account-migration/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ impl darwinia_staking::Config for Runtime {
type RingCurrency = Balances;
type RuntimeEvent = RuntimeEvent;
type UnixTime = Dummy;
type WeightInfo = ();
}
#[cfg(not(feature = "runtime-benchmarks"))]
impl darwinia_staking::DepositConfig for Runtime {}

impl pallet_identity::Config for Runtime {
type BasicDeposit = ();
Expand Down
10 changes: 5 additions & 5 deletions pallet/deposit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ codec = { workspace = true, package = "parity-scale-codec" }
scale-info = { workspace = true }

# darwinia
darwinia-staking = { workspace = true }
dc-inflation = { workspace = true }
dc-types = { workspace = true }
darwinia-staking-traits = { workspace = true }
dc-inflation = { workspace = true }
dc-types = { workspace = true }

# subtrate
frame-support = { workspace = true }
Expand All @@ -29,8 +29,8 @@ frame-benchmarking = { workspace = true, optional = true }
# substrate
pallet-assets = { workspace = true, features = ["std"] }
pallet-balances = { workspace = true, features = ["std"] }
sp-io = { workspace = true, features = ["std"] }
sp-core = { workspace = true, features = ["std"] }
sp-io = { workspace = true, features = ["std"] }

[features]
default = ["std"]
Expand All @@ -40,7 +40,7 @@ std = [
"scale-info/std",

# darwinia
"darwinia-staking/std",
"darwinia-staking-traits/std",
"dc-inflation/std",

# subtrate
Expand Down
6 changes: 3 additions & 3 deletions pallet/deposit/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ mod benchmarks {

T::Ring::make_free_balance_be(&a, max_deposits as Balance * UNIT);

// The worst case:
// Worst-case scenario:
//
// Calculate the last deposit's id.
(0..max_deposits - 1).for_each(|_| {
Expand All @@ -61,7 +61,7 @@ mod benchmarks {
.unwrap()
});

// The worst case:
// Worst-case scenario:
//
// Let all locks be expired.
<pallet_timestamp::Pallet<T>>::set_timestamp(
Expand Down Expand Up @@ -90,7 +90,7 @@ mod benchmarks {
.unwrap()
});

// The worst case:
// Worst-case scenario:
//
// Remove the head item from a 'full-size' bounded vector.
{
Expand Down
94 changes: 45 additions & 49 deletions pallet/deposit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,57 +51,12 @@ use dc_types::{Balance, Moment};
// substrate
use frame_support::{
pallet_prelude::*,
traits::{
Currency,
ExistenceRequirement::{AllowDeath, KeepAlive},
UnixTime,
},
traits::{Currency, ExistenceRequirement::AllowDeath, UnixTime},
PalletId,
};
use frame_system::pallet_prelude::*;
use sp_runtime::traits::AccountIdConversion;

/// Milliseconds per month.
pub const MILLISECS_PER_MONTH: Moment = MILLISECS_PER_YEAR / 12;

/// The maximum locking period for a deposit.
pub const MAX_LOCKING_MONTHS: u8 = 36;

/// Simple asset APIs.
pub trait SimpleAsset {
/// Account type.
type AccountId;

/// Mint API.
fn mint(beneficiary: &Self::AccountId, amount: Balance) -> DispatchResult;

/// Burn API.
fn burn(who: &Self::AccountId, amount: Balance) -> DispatchResult;
}

/// Deposit identifier.
///
/// It's not a global-unique identifier.
/// It's only used for distinguishing the deposits under a specific account.
// https://github.com/polkadot-js/apps/issues/8591
// pub type DepositId = u8;
pub type DepositId = u16;

/// Deposit.
#[derive(Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen, TypeInfo, RuntimeDebug)]
pub struct Deposit {
/// Deposit ID.
pub id: DepositId,
/// Deposited RING.
pub value: Balance,
/// Start timestamp.
pub start_time: Moment,
/// Expired timestamp.
pub expired_time: Moment,
/// Deposit state.
pub in_use: bool,
}

#[frame_support::pallet]
pub mod pallet {
// darwinia
Expand Down Expand Up @@ -240,7 +195,7 @@ pub mod pallet {
<Result<_, DispatchError>>::Ok((id, start_time, expired_time))
})?;

T::Ring::transfer(&who, &account_id(), amount, KeepAlive)?;
T::Ring::transfer(&who, &account_id(), amount, AllowDeath)?;

let kton_reward = dc_inflation::deposit_interest(amount, months);

Expand Down Expand Up @@ -340,6 +295,47 @@ pub mod pallet {
}
pub use pallet::*;

/// Milliseconds per month.
pub const MILLISECS_PER_MONTH: Moment = MILLISECS_PER_YEAR / 12;

/// The maximum locking period for a deposit.
pub const MAX_LOCKING_MONTHS: u8 = 36;

/// Simple asset APIs.
pub trait SimpleAsset {
/// Account type.
type AccountId;

/// Mint API.
fn mint(beneficiary: &Self::AccountId, amount: Balance) -> DispatchResult;

/// Burn API.
fn burn(who: &Self::AccountId, amount: Balance) -> DispatchResult;
}

/// Deposit identifier.
///
/// It's not a global-unique identifier.
/// It's only used for distinguishing the deposits under a specific account.
// https://github.com/polkadot-js/apps/issues/8591
// pub type DepositId = u8;
pub type DepositId = u16;

/// Deposit.
#[derive(Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen, TypeInfo, RuntimeDebug)]
pub struct Deposit {
/// Deposit ID.
pub id: DepositId,
/// Deposited RING.
pub value: Balance,
/// Start timestamp.
pub start_time: Moment,
/// Expired timestamp.
pub expired_time: Moment,
/// Deposit state.
pub in_use: bool,
}

impl<T> Pallet<T>
where
T: Config,
Expand All @@ -348,7 +344,7 @@ where
<pallet_timestamp::Pallet<T> as UnixTime>::now().as_millis()
}
}
impl<T> darwinia_staking::Stake for Pallet<T>
impl<T> darwinia_staking_traits::Stake for Pallet<T>
where
T: Config,
{
Expand Down Expand Up @@ -385,7 +381,7 @@ where
})
}
}
impl<T> darwinia_staking::StakeExt for Pallet<T>
impl<T> darwinia_staking_traits::StakeExt for Pallet<T>
where
T: Config,
{
Expand Down
6 changes: 3 additions & 3 deletions pallet/deposit/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ impl pallet_assets::Config for Runtime {
type WeightInfo = ();
}

pub enum KtonAsset {}
impl darwinia_deposit::SimpleAsset for KtonAsset {
pub enum KtonMinting {}
impl darwinia_deposit::SimpleAsset for KtonMinting {
type AccountId = u32;

fn mint(beneficiary: &Self::AccountId, amount: Balance) -> sp_runtime::DispatchResult {
Expand All @@ -109,7 +109,7 @@ impl darwinia_deposit::SimpleAsset for KtonAsset {
}
}
impl darwinia_deposit::Config for Runtime {
type Kton = KtonAsset;
type Kton = KtonMinting;
type MaxDeposits = frame_support::traits::ConstU32<16>;
type MinLockingAmount = frame_support::traits::ConstU128<UNIT>;
type Ring = Balances;
Expand Down
4 changes: 2 additions & 2 deletions pallet/deposit/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
mock::{Deposit, *},
Deposit as DepositS, *,
};
use darwinia_staking::Stake;
use darwinia_staking_traits::Stake;
// substrate
use frame_support::{assert_noop, assert_ok};

Expand Down Expand Up @@ -317,7 +317,7 @@ fn claim_with_penalty_should_work() {
<pallet_assets::Error<Runtime>>::BalanceLow
);

assert_ok!(KtonAsset::mint(&1, UNIT));
assert_ok!(KtonMinting::mint(&1, UNIT));
assert_ok!(Deposit::claim_with_penalty(RuntimeOrigin::signed(1), 0));
assert_eq!(Assets::balance(0, 1), 999_984_771_573_604_062);
assert!(Deposit::deposit_of(&1).is_none());
Expand Down
Loading

0 comments on commit 6aebcc4

Please sign in to comment.