Skip to content
This repository has been archived by the owner on Sep 28, 2023. It is now read-only.

Commit

Permalink
Merge branch 'polkadot-v0.9.29' into feature/xvm-pallet-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
akru committed Oct 21, 2022
2 parents fc7f12d + ff57d9c commit 4d6aefe
Show file tree
Hide file tree
Showing 16 changed files with 424 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/checks-and-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ jobs:
hide_complexity: true
indicators: true
output: both
thresholds: '60 80'
thresholds: '50 80'

- name: Add Coverage PR Comment
uses: marocchino/sticky-pull-request-comment@v2
Expand Down
2 changes: 1 addition & 1 deletion chain-extensions/rmrk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ num-traits = { version = "0.2", default-features = false }
pallet-contracts = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.29", default-features = false, features = ["unstable-interface"] }
pallet-contracts-primitives = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.29", default-features = false }
pallet-contracts-rpc-runtime-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.29", default-features = false }
pallet-uniques = { git = "https://github.com/AstarNetwork/substrate.git", branch = "polkadot-v0.9.29", default-features = false }
pallet-uniques = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.29", default-features = false }
scale-info = { version = "2.1.0", default-features = false, features = ["derive"] }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.29", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.29", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion frame/dapps-staking/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pallet-dapps-staking"
version = "3.6.3"
version = "3.7.0"
authors = ["Stake Technologies <devops@stake.co.jp>"]
edition = "2021"
homepage = "https://astar.network/"
Expand Down
96 changes: 71 additions & 25 deletions frame/dapps-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
//!
#![cfg_attr(not(feature = "std"), no_std)]

use codec::{Decode, Encode, HasCompact};
use codec::{Decode, Encode, HasCompact, MaxEncodedLen};
use frame_support::traits::Currency;
use frame_system::{self as system};
use scale_info::TypeInfo;
Expand Down Expand Up @@ -80,8 +80,26 @@ pub type BalanceOf<T> =
/// Counter for the number of eras that have passed.
pub type EraIndex = u32;

// This represents the max assumed vector length that any storage item should have.
// In particular, this relates to `UnbondingInfo` and `StakerInfo`.
// In structs which are bound in size, `MaxEncodedLen` can just be derived but that's not the case for standard `vec`.
// To fix this 100% correctly, we'd need to do one of the following:
//
// - Use `BoundedVec` instead of `Vec` and do storage migration
// - Introduce a new type `S: Get<u32>` into the aforementioned structs and use it to inject max allowed size,
// thus allowing us to correctly calculate max encoded len
//
// The issue with first approach is that it requires storage migration which we want to avoid
// unless it's really necessary. The issue with second approach is that it makes code much more
// difficult to work with since all of it will be ridden with injections of the `S` type.
//
// Since dApps staking has been stable for long time and there are plans to redesign & refactor it,
// doing neither of the above makes sense, timewise. So we use an assumption that vec length
// won't go over the following constant.
const MAX_ASSUMED_VEC_LEN: u32 = 10;

/// DApp State descriptor
#[derive(Copy, Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug, TypeInfo)]
#[derive(Copy, Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug, TypeInfo, MaxEncodedLen)]
enum DAppState {
/// Contract is registered and active.
Registered,
Expand All @@ -90,7 +108,7 @@ enum DAppState {
Unregistered(EraIndex),
}

#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug, TypeInfo)]
#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct DAppInfo<AccountId> {
/// Developer (owner) account
developer: AccountId,
Expand All @@ -109,7 +127,7 @@ impl<AccountId> DAppInfo<AccountId> {
}

/// Mode of era-forcing.
#[derive(Copy, Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug, TypeInfo)]
#[derive(Copy, Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum Forcing {
/// Not forcing anything - just let whatever happen.
Expand All @@ -127,8 +145,8 @@ impl Default for Forcing {
}

/// A record of rewards allocated for stakers and dapps
#[derive(PartialEq, Eq, Clone, Default, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct RewardInfo<Balance: HasCompact> {
#[derive(PartialEq, Eq, Clone, Default, Encode, Decode, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct RewardInfo<Balance: HasCompact + MaxEncodedLen> {
/// Total amount of rewards for stakers in an era
#[codec(compact)]
pub stakers: Balance,
Expand All @@ -138,8 +156,8 @@ pub struct RewardInfo<Balance: HasCompact> {
}

/// A record for total rewards and total amount staked for an era
#[derive(PartialEq, Eq, Clone, Default, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct EraInfo<Balance: HasCompact> {
#[derive(PartialEq, Eq, Clone, Default, Encode, Decode, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct EraInfo<Balance: HasCompact + MaxEncodedLen> {
/// Total amount of earned rewards for an era
pub rewards: RewardInfo<Balance>,
/// Total staked amount in an era
Expand All @@ -153,8 +171,8 @@ pub struct EraInfo<Balance: HasCompact> {
/// Used to split total EraPayout among contracts.
/// Each tuple (contract, era) has this structure.
/// This will be used to reward contracts developer and his stakers.
#[derive(Clone, PartialEq, Encode, Decode, Default, RuntimeDebug, TypeInfo)]
pub struct ContractStakeInfo<Balance: HasCompact> {
#[derive(Clone, PartialEq, Encode, Decode, Default, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct ContractStakeInfo<Balance: HasCompact + MaxEncodedLen> {
/// Total staked amount.
#[codec(compact)]
pub total: Balance,
Expand All @@ -167,7 +185,7 @@ pub struct ContractStakeInfo<Balance: HasCompact> {

/// Storage value representing the current Dapps staking pallet storage version.
/// Used by `on_runtime_upgrade` to determine whether a storage migration is needed or not.
#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug, TypeInfo)]
#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub enum Version {
V1_0_0,
V2_0_0,
Expand All @@ -183,8 +201,8 @@ impl Default for Version {

/// Used to represent how much was staked in a particular era.
/// E.g. `{staked: 1000, era: 5}` means that in era `5`, staked amount was 1000.
#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug, TypeInfo)]
pub struct EraStake<Balance: AtLeast32BitUnsigned + Copy> {
#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct EraStake<Balance: AtLeast32BitUnsigned + Copy + MaxEncodedLen> {
/// Staked amount in era
#[codec(compact)]
staked: Balance,
Expand All @@ -193,7 +211,7 @@ pub struct EraStake<Balance: AtLeast32BitUnsigned + Copy> {
era: EraIndex,
}

impl<Balance: AtLeast32BitUnsigned + Copy> EraStake<Balance> {
impl<Balance: AtLeast32BitUnsigned + Copy + MaxEncodedLen> EraStake<Balance> {
/// Create a new instance of `EraStake` with given values
fn new(staked: Balance, era: EraIndex) -> Self {
Self { staked, era }
Expand Down Expand Up @@ -228,12 +246,24 @@ impl<Balance: AtLeast32BitUnsigned + Copy> EraStake<Balance> {
/// **NOTE:** It is important to understand that staker **DID NOT** claim any rewards during this period.
///
#[derive(Encode, Decode, Clone, Default, PartialEq, Eq, RuntimeDebug, TypeInfo)]
pub struct StakerInfo<Balance: AtLeast32BitUnsigned + Copy> {
pub struct StakerInfo<Balance: AtLeast32BitUnsigned + Copy + MaxEncodedLen> {
// Size of this list would be limited by a configurable constant
stakes: Vec<EraStake<Balance>>,
}

impl<Balance: AtLeast32BitUnsigned + Copy> StakerInfo<Balance> {
impl<Balance: AtLeast32BitUnsigned + Copy + MaxEncodedLen> MaxEncodedLen for StakerInfo<Balance> {
// This is just an assumption, will be calculated properly in the future. See the comment for `MAX_ASSUMED_VEC_LEN`.
fn max_encoded_len() -> usize {
codec::Compact(MAX_ASSUMED_VEC_LEN)
.encoded_size()
.saturating_add(
(MAX_ASSUMED_VEC_LEN as usize)
.saturating_mul(EraStake::<Balance>::max_encoded_len()),
)
}
}

impl<Balance: AtLeast32BitUnsigned + Copy + MaxEncodedLen> StakerInfo<Balance> {
/// `true` if no active stakes and unclaimed eras exist, `false` otherwise
fn is_empty(&self) -> bool {
self.stakes.is_empty()
Expand Down Expand Up @@ -380,8 +410,10 @@ impl<Balance: AtLeast32BitUnsigned + Copy> StakerInfo<Balance> {

/// Represents an balance amount undergoing the unbonding process.
/// Since unbonding takes time, it's important to keep track of when and how much was unbonded.
#[derive(Clone, Copy, PartialEq, Encode, Decode, Default, RuntimeDebug, TypeInfo)]
pub struct UnlockingChunk<Balance> {
#[derive(
Clone, Copy, PartialEq, Encode, Decode, Default, RuntimeDebug, TypeInfo, MaxEncodedLen,
)]
pub struct UnlockingChunk<Balance: MaxEncodedLen> {
/// Amount being unlocked
#[codec(compact)]
amount: Balance,
Expand All @@ -392,7 +424,7 @@ pub struct UnlockingChunk<Balance> {

impl<Balance> UnlockingChunk<Balance>
where
Balance: Add<Output = Balance> + Copy,
Balance: Add<Output = Balance> + Copy + MaxEncodedLen,
{
// Adds the specified amount to this chunk
fn add_amount(&mut self, amount: Balance) {
Expand All @@ -403,14 +435,28 @@ where
/// Contains unlocking chunks.
/// This is a convenience struct that provides various utility methods to help with unbonding handling.
#[derive(Clone, PartialEq, Encode, Decode, Default, RuntimeDebug, TypeInfo)]
pub struct UnbondingInfo<Balance: AtLeast32BitUnsigned + Default + Copy> {
pub struct UnbondingInfo<Balance: AtLeast32BitUnsigned + Default + Copy + MaxEncodedLen> {
// Vector of unlocking chunks. Sorted in ascending order in respect to unlock_era.
unlocking_chunks: Vec<UnlockingChunk<Balance>>,
}

impl<Balance: AtLeast32BitUnsigned + Default + Copy + MaxEncodedLen> MaxEncodedLen
for UnbondingInfo<Balance>
{
// This is just an assumption, will be calculated properly in the future. See the comment for `MAX_ASSUMED_VEC_LEN`.
fn max_encoded_len() -> usize {
codec::Compact(MAX_ASSUMED_VEC_LEN)
.encoded_size()
.saturating_add(
(MAX_ASSUMED_VEC_LEN as usize)
.saturating_mul(UnlockingChunk::<Balance>::max_encoded_len()),
)
}
}

impl<Balance> UnbondingInfo<Balance>
where
Balance: AtLeast32BitUnsigned + Default + Copy,
Balance: AtLeast32BitUnsigned + Default + Copy + MaxEncodedLen,
{
/// Returns total number of unlocking chunks.
fn len(&self) -> u32 {
Expand Down Expand Up @@ -480,7 +526,7 @@ where
/// Instruction on how to handle reward payout for stakers.
/// In order to make staking more competitive, majority of stakers will want to
/// automatically restake anything they earn.
#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug, TypeInfo)]
#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub enum RewardDestination {
/// Rewards are transferred to stakers free balance without any further action.
FreeBalance,
Expand All @@ -496,8 +542,8 @@ impl Default for RewardDestination {
}

/// Contains information about account's locked & unbonding balances.
#[derive(Clone, PartialEq, Encode, Decode, Default, RuntimeDebug, TypeInfo)]
pub struct AccountLedger<Balance: AtLeast32BitUnsigned + Default + Copy> {
#[derive(Clone, PartialEq, Encode, Decode, Default, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct AccountLedger<Balance: AtLeast32BitUnsigned + Default + Copy + MaxEncodedLen> {
/// Total balance locked.
#[codec(compact)]
pub locked: Balance,
Expand All @@ -507,7 +553,7 @@ pub struct AccountLedger<Balance: AtLeast32BitUnsigned + Default + Copy> {
reward_destination: RewardDestination,
}

impl<Balance: AtLeast32BitUnsigned + Default + Copy> AccountLedger<Balance> {
impl<Balance: AtLeast32BitUnsigned + Default + Copy + MaxEncodedLen> AccountLedger<Balance> {
/// `true` if ledger is empty (no locked funds, no unbonding chunks), `false` otherwise.
pub fn is_empty(&self) -> bool {
self.locked.is_zero() && self.unbonding_info.is_empty()
Expand Down
6 changes: 4 additions & 2 deletions frame/dapps-staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use frame_support::{
};
use sp_core::{H160, H256};

use codec::{Decode, Encode};
use codec::{Decode, Encode, MaxEncodedLen};
use sp_io::TestExternalities;
use sp_runtime::{
testing::Header,
Expand Down Expand Up @@ -144,7 +144,9 @@ impl pallet_dapps_staking::Config for TestRuntime {
type MaxEraStakeValues = MaxEraStakeValues;
}

#[derive(PartialEq, Eq, Copy, Clone, Encode, Decode, Debug, scale_info::TypeInfo)]
#[derive(
PartialEq, Eq, Copy, Clone, Encode, Decode, Debug, scale_info::TypeInfo, MaxEncodedLen,
)]
pub enum MockSmartContract<AccountId> {
Evm(sp_core::H160),
Wasm(AccountId),
Expand Down
3 changes: 1 addition & 2 deletions frame/dapps-staking/src/pallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ pub mod pallet {

#[pallet::pallet]
#[pallet::generate_store(pub(crate) trait Store)]
#[pallet::without_storage_info]
pub struct Pallet<T>(PhantomData<T>);

// Negative imbalance type of this pallet.
Expand All @@ -47,7 +46,7 @@ pub mod pallet {
+ ReservableCurrency<Self::AccountId>;

/// Describes smart contract in the context required by dapps staking.
type SmartContract: Default + Parameter + Member;
type SmartContract: Default + Parameter + Member + MaxEncodedLen;

/// Number of blocks per era.
#[pallet::constant]
Expand Down
15 changes: 15 additions & 0 deletions frame/dapps-staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2132,3 +2132,18 @@ pub fn set_contract_stake_info() {
);
})
}

#[test]
fn custom_max_encoded_len() {
let max_unbonding_info_len = 10 * (4 + 16) + 1;
assert_eq!(
UnbondingInfo::<u128>::max_encoded_len(),
max_unbonding_info_len as usize
);

let max_staker_info_len = 10 * (4 + 16) + 1;
assert_eq!(
StakerInfo::<u128>::max_encoded_len(),
max_staker_info_len as usize
);
}
2 changes: 1 addition & 1 deletion frame/xc-asset-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "pallet-xc-asset-config"
authors = ["Stake Technologies"]
edition = "2021"
version = "1.2.2"
version = "1.3.0"

[dependencies]
log = { version = "0.4", default-features = false }
Expand Down
17 changes: 11 additions & 6 deletions frame/xc-asset-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub use weights::WeightInfo;
pub mod pallet {

use crate::weights::WeightInfo;
use frame_support::pallet_prelude::*;
use frame_support::{pallet_prelude::*, traits::EnsureOrigin};
use frame_system::pallet_prelude::*;
use parity_scale_codec::HasCompact;
use sp_std::boxed::Box;
Expand Down Expand Up @@ -110,6 +110,11 @@ pub mod pallet {
/// Callback handling for cross-chain asset registration or unregistration.
type XcAssetChanged: XcAssetChanged<Self>;

/// The required origin for managing cross-chain asset configuration
///
/// Should most likely be root.
type ManagerOrigin: EnsureOrigin<<Self as frame_system::Config>::Origin>;

type WeightInfo: WeightInfo;
}

Expand Down Expand Up @@ -187,7 +192,7 @@ pub mod pallet {
asset_location: Box<VersionedMultiLocation>,
#[pallet::compact] asset_id: T::AssetId,
) -> DispatchResult {
ensure_root(origin)?;
T::ManagerOrigin::ensure_origin(origin)?;

// Ensure such an assetId does not exist
ensure!(
Expand Down Expand Up @@ -217,7 +222,7 @@ pub mod pallet {
asset_location: Box<VersionedMultiLocation>,
#[pallet::compact] units_per_second: u128,
) -> DispatchResult {
ensure_root(origin)?;
T::ManagerOrigin::ensure_origin(origin)?;

let asset_location = *asset_location;

Expand All @@ -243,7 +248,7 @@ pub mod pallet {
new_asset_location: Box<VersionedMultiLocation>,
#[pallet::compact] asset_id: T::AssetId,
) -> DispatchResult {
ensure_root(origin)?;
T::ManagerOrigin::ensure_origin(origin)?;

let new_asset_location = *new_asset_location;

Expand Down Expand Up @@ -278,7 +283,7 @@ pub mod pallet {
origin: OriginFor<T>,
asset_location: Box<VersionedMultiLocation>,
) -> DispatchResult {
ensure_root(origin)?;
T::ManagerOrigin::ensure_origin(origin)?;

let asset_location = *asset_location;

Expand All @@ -294,7 +299,7 @@ pub mod pallet {
origin: OriginFor<T>,
#[pallet::compact] asset_id: T::AssetId,
) -> DispatchResult {
ensure_root(origin)?;
T::ManagerOrigin::ensure_origin(origin)?;

let asset_location =
AssetIdToLocation::<T>::get(&asset_id).ok_or(Error::<T>::AssetDoesNotExist)?;
Expand Down
1 change: 1 addition & 0 deletions frame/xc-asset-config/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ impl pallet_xc_asset_config::Config for Test {
type Event = Event;
type AssetId = AssetId;
type XcAssetChanged = ();
type ManagerOrigin = frame_system::EnsureRoot<AccountId>;
type WeightInfo = ();
}

Expand Down
Loading

0 comments on commit 4d6aefe

Please sign in to comment.