feat: initialize Kurdistan SDK - independent fork of Polkadot SDK

This commit is contained in:
2025-12-13 15:44:15 +03:00
commit e4778b4576
6838 changed files with 1847450 additions and 0 deletions
@@ -0,0 +1,160 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Benchmarking for assigned_slots pallet
#![cfg(feature = "runtime-benchmarks")]
use super::*;
use frame_benchmarking::v2::*;
use frame_support::assert_ok;
use frame_system::{pallet_prelude::BlockNumberFor, RawOrigin};
use pezkuwi_primitives::Id as ParaId;
use sp_runtime::traits::Bounded;
type CurrencyOf<T> = <<T as Config>::Leaser as Leaser<BlockNumberFor<T>>>::Currency;
type BalanceOf<T> = <<<T as Config>::Leaser as Leaser<BlockNumberFor<T>>>::Currency as Currency<
<T as frame_system::Config>::AccountId,
>>::Balance;
#[benchmarks(where T: Config)]
mod benchmarks {
use super::*;
use crate::assigned_slots::Pallet as AssignedSlots;
fn register_teyrchain<T: Config>(para_id: ParaId) {
let who: T::AccountId = whitelisted_caller();
let worst_validation_code = T::Registrar::worst_validation_code();
let worst_head_data = T::Registrar::worst_head_data();
CurrencyOf::<T>::make_free_balance_be(&who, BalanceOf::<T>::max_value());
assert_ok!(T::Registrar::register(
who,
para_id,
worst_head_data,
worst_validation_code.clone()
));
assert_ok!(paras::Pallet::<T>::add_trusted_validation_code(
frame_system::Origin::<T>::Root.into(),
worst_validation_code,
));
T::Registrar::execute_pending_transitions();
}
#[benchmark]
fn assign_perm_teyrchain_slot() {
let para_id = ParaId::from(1_u32);
let caller = RawOrigin::Root;
let _ =
AssignedSlots::<T>::set_max_permanent_slots(frame_system::Origin::<T>::Root.into(), 10);
register_teyrchain::<T>(para_id);
let counter = PermanentSlotCount::<T>::get();
let current_lease_period: BlockNumberFor<T> =
T::Leaser::lease_period_index(frame_system::Pallet::<T>::block_number())
.and_then(|x| Some(x.0))
.unwrap();
#[extrinsic_call]
assign_perm_teyrchain_slot(caller, para_id);
assert_eq!(
PermanentSlots::<T>::get(para_id),
Some((
current_lease_period,
LeasePeriodOf::<T>::from(T::PermanentSlotLeasePeriodLength::get()),
))
);
assert_eq!(PermanentSlotCount::<T>::get(), counter + 1);
}
#[benchmark]
fn assign_temp_teyrchain_slot() {
let para_id = ParaId::from(2_u32);
let caller = RawOrigin::Root;
let _ =
AssignedSlots::<T>::set_max_temporary_slots(frame_system::Origin::<T>::Root.into(), 10);
register_teyrchain::<T>(para_id);
let current_lease_period: BlockNumberFor<T> =
T::Leaser::lease_period_index(frame_system::Pallet::<T>::block_number())
.and_then(|x| Some(x.0))
.unwrap();
let counter = TemporarySlotCount::<T>::get();
#[extrinsic_call]
assign_temp_teyrchain_slot(caller, para_id, SlotLeasePeriodStart::Current);
let tmp = TeyrchainTemporarySlot {
manager: whitelisted_caller(),
period_begin: current_lease_period,
period_count: LeasePeriodOf::<T>::from(T::TemporarySlotLeasePeriodLength::get()),
last_lease: Some(BlockNumberFor::<T>::zero()),
lease_count: 1,
};
assert_eq!(TemporarySlots::<T>::get(para_id), Some(tmp));
assert_eq!(TemporarySlotCount::<T>::get(), counter + 1);
}
#[benchmark]
fn unassign_teyrchain_slot() {
let para_id = ParaId::from(3_u32);
let caller = RawOrigin::Root;
let _ =
AssignedSlots::<T>::set_max_temporary_slots(frame_system::Origin::<T>::Root.into(), 10);
register_teyrchain::<T>(para_id);
let _ = AssignedSlots::<T>::assign_temp_teyrchain_slot(
caller.clone().into(),
para_id,
SlotLeasePeriodStart::Current,
);
let counter = TemporarySlotCount::<T>::get();
#[extrinsic_call]
unassign_teyrchain_slot(caller, para_id);
assert_eq!(TemporarySlots::<T>::get(para_id), None);
assert_eq!(TemporarySlotCount::<T>::get(), counter - 1);
}
#[benchmark]
fn set_max_permanent_slots() {
let caller = RawOrigin::Root;
#[extrinsic_call]
set_max_permanent_slots(caller, u32::MAX);
assert_eq!(MaxPermanentSlots::<T>::get(), u32::MAX);
}
#[benchmark]
fn set_max_temporary_slots() {
let caller = RawOrigin::Root;
#[extrinsic_call]
set_max_temporary_slots(caller, u32::MAX);
assert_eq!(MaxTemporarySlots::<T>::get(), u32::MAX);
}
impl_benchmark_test_suite!(
AssignedSlots,
crate::assigned_slots::tests::new_test_ext(),
crate::assigned_slots::tests::Test,
);
}
@@ -0,0 +1,72 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
use super::{Config, MaxPermanentSlots, MaxTemporarySlots, Pallet, LOG_TARGET};
use frame_support::traits::{Get, GetStorageVersion, UncheckedOnRuntimeUpgrade};
#[cfg(feature = "try-runtime")]
use alloc::vec::Vec;
#[cfg(feature = "try-runtime")]
use frame_support::ensure;
pub mod v1 {
use super::*;
pub struct VersionUncheckedMigrateToV1<T>(core::marker::PhantomData<T>);
impl<T: Config> UncheckedOnRuntimeUpgrade for VersionUncheckedMigrateToV1<T> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> {
let on_chain_version = Pallet::<T>::on_chain_storage_version();
ensure!(on_chain_version < 1, "assigned_slots::MigrateToV1 migration can be deleted");
Ok(Default::default())
}
fn on_runtime_upgrade() -> frame_support::weights::Weight {
let on_chain_version = Pallet::<T>::on_chain_storage_version();
if on_chain_version < 1 {
const MAX_PERMANENT_SLOTS: u32 = 100;
const MAX_TEMPORARY_SLOTS: u32 = 100;
MaxPermanentSlots::<T>::put(MAX_PERMANENT_SLOTS);
MaxTemporarySlots::<T>::put(MAX_TEMPORARY_SLOTS);
// Return the weight consumed by the migration.
T::DbWeight::get().reads_writes(1, 3)
} else {
log::info!(target: LOG_TARGET, "MigrateToV1 should be removed");
T::DbWeight::get().reads(1)
}
}
#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
let on_chain_version = Pallet::<T>::on_chain_storage_version();
ensure!(on_chain_version == 1, "assigned_slots::MigrateToV1 needs to be run");
assert_eq!(MaxPermanentSlots::<T>::get(), 100);
assert_eq!(MaxTemporarySlots::<T>::get(), 100);
Ok(())
}
}
/// [`VersionUncheckedMigrateToV1`] wrapped in a
/// [`VersionedMigration`](frame_support::migrations::VersionedMigration), ensuring the
/// migration is only performed when on-chain version is 0.
pub type MigrateToV1<T> = frame_support::migrations::VersionedMigration<
0,
1,
VersionUncheckedMigrateToV1<T>,
Pallet<T>,
<T as frame_system::Config>::DbWeight,
>;
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,282 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Benchmarking for auctions pallet
#![cfg(feature = "runtime-benchmarks")]
use super::{Pallet as Auctions, *};
use frame_support::{
assert_ok,
traits::{EnsureOrigin, OnInitialize},
};
use frame_system::RawOrigin;
use pezkuwi_runtime_teyrchains::paras;
use sp_runtime::{traits::Bounded, SaturatedConversion};
use frame_benchmarking::v2::*;
fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
let events = frame_system::Pallet::<T>::events();
let system_event: <T as frame_system::Config>::RuntimeEvent = generic_event.into();
// compare to the last event record
let frame_system::EventRecord { event, .. } = &events[events.len() - 1];
assert_eq!(event, &system_event);
}
fn fill_winners<T: Config + paras::Config>(lease_period_index: LeasePeriodOf<T>) {
let auction_index = AuctionCounter::<T>::get();
let minimum_balance = CurrencyOf::<T>::minimum_balance();
for n in 1..=SlotRange::SLOT_RANGE_COUNT as u32 {
let owner = account("owner", n, 0);
let worst_validation_code = T::Registrar::worst_validation_code();
let worst_head_data = T::Registrar::worst_head_data();
CurrencyOf::<T>::make_free_balance_be(&owner, BalanceOf::<T>::max_value());
assert!(T::Registrar::register(
owner,
ParaId::from(n),
worst_head_data,
worst_validation_code
)
.is_ok());
}
assert_ok!(paras::Pallet::<T>::add_trusted_validation_code(
frame_system::Origin::<T>::Root.into(),
T::Registrar::worst_validation_code(),
));
T::Registrar::execute_pending_transitions();
for n in 1..=SlotRange::SLOT_RANGE_COUNT as u32 {
let bidder = account("bidder", n, 0);
CurrencyOf::<T>::make_free_balance_be(&bidder, BalanceOf::<T>::max_value());
let slot_range = SlotRange::n((n - 1) as u8).unwrap();
let (start, end) = slot_range.as_pair();
assert!(Auctions::<T>::bid(
RawOrigin::Signed(bidder).into(),
ParaId::from(n),
auction_index,
lease_period_index + start.into(), // First Slot
lease_period_index + end.into(), // Last slot
minimum_balance.saturating_mul(n.into()), // Amount
)
.is_ok());
}
}
#[benchmarks(
where T: pallet_babe::Config + paras::Config,
)]
mod benchmarks {
use super::*;
#[benchmark]
fn new_auction() -> Result<(), BenchmarkError> {
let duration = BlockNumberFor::<T>::max_value();
let lease_period_index = LeasePeriodOf::<T>::max_value();
let origin =
T::InitiateOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
#[extrinsic_call]
_(origin as T::RuntimeOrigin, duration, lease_period_index);
assert_last_event::<T>(
Event::<T>::AuctionStarted {
auction_index: AuctionCounter::<T>::get(),
lease_period: LeasePeriodOf::<T>::max_value(),
ending: BlockNumberFor::<T>::max_value(),
}
.into(),
);
Ok(())
}
// Worst case scenario a new bid comes in which kicks out an existing bid for the same slot.
#[benchmark]
fn bid() -> Result<(), BenchmarkError> {
// If there is an offset, we need to be on that block to be able to do lease things.
let (_, offset) = T::Leaser::lease_period_length();
frame_system::Pallet::<T>::set_block_number(offset + One::one());
// Create a new auction
let duration = BlockNumberFor::<T>::max_value();
let lease_period_index = LeasePeriodOf::<T>::zero();
let origin = T::InitiateOrigin::try_successful_origin()
.expect("InitiateOrigin has no successful origin required for the benchmark");
Auctions::<T>::new_auction(origin, duration, lease_period_index)?;
let para = ParaId::from(0);
let new_para = ParaId::from(1_u32);
// Register the paras
let owner = account("owner", 0, 0);
CurrencyOf::<T>::make_free_balance_be(&owner, BalanceOf::<T>::max_value());
let worst_head_data = T::Registrar::worst_head_data();
let worst_validation_code = T::Registrar::worst_validation_code();
T::Registrar::register(
owner.clone(),
para,
worst_head_data.clone(),
worst_validation_code.clone(),
)?;
T::Registrar::register(owner, new_para, worst_head_data, worst_validation_code.clone())?;
assert_ok!(paras::Pallet::<T>::add_trusted_validation_code(
frame_system::Origin::<T>::Root.into(),
worst_validation_code,
));
T::Registrar::execute_pending_transitions();
// Make an existing bid
let auction_index = AuctionCounter::<T>::get();
let first_slot = AuctionInfo::<T>::get().unwrap().0;
let last_slot = first_slot + 3u32.into();
let first_amount = CurrencyOf::<T>::minimum_balance();
let first_bidder: T::AccountId = account("first_bidder", 0, 0);
CurrencyOf::<T>::make_free_balance_be(&first_bidder, BalanceOf::<T>::max_value());
Auctions::<T>::bid(
RawOrigin::Signed(first_bidder.clone()).into(),
para,
auction_index,
first_slot,
last_slot,
first_amount,
)?;
let caller: T::AccountId = whitelisted_caller();
CurrencyOf::<T>::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
let bigger_amount = CurrencyOf::<T>::minimum_balance().saturating_mul(10u32.into());
assert_eq!(CurrencyOf::<T>::reserved_balance(&first_bidder), first_amount);
#[extrinsic_call]
_(
RawOrigin::Signed(caller.clone()),
new_para,
auction_index,
first_slot,
last_slot,
bigger_amount,
);
// Confirms that we unreserved funds from a previous bidder, which is worst case
// scenario.
assert_eq!(CurrencyOf::<T>::reserved_balance(&caller), bigger_amount);
Ok(())
}
// Worst case: 10 bidders taking all wining spots, and we need to calculate the winner for
// auction end. Entire winner map should be full and removed at the end of the benchmark.
#[benchmark]
fn on_initialize() -> Result<(), BenchmarkError> {
// If there is an offset, we need to be on that block to be able to do lease things.
let (lease_length, offset) = T::Leaser::lease_period_length();
frame_system::Pallet::<T>::set_block_number(offset + One::one());
// Create a new auction
let duration: BlockNumberFor<T> = lease_length / 2u32.into();
let lease_period_index = LeasePeriodOf::<T>::zero();
let now = frame_system::Pallet::<T>::block_number();
let origin = T::InitiateOrigin::try_successful_origin()
.expect("InitiateOrigin has no successful origin required for the benchmark");
Auctions::<T>::new_auction(origin, duration, lease_period_index)?;
fill_winners::<T>(lease_period_index);
for winner in Winning::<T>::get(BlockNumberFor::<T>::from(0u32)).unwrap().iter() {
assert!(winner.is_some());
}
let winning_data = Winning::<T>::get(BlockNumberFor::<T>::from(0u32)).unwrap();
// Make winning map full
for i in 0u32..(T::EndingPeriod::get() / T::SampleLength::get()).saturated_into() {
Winning::<T>::insert(BlockNumberFor::<T>::from(i), winning_data.clone());
}
// Move ahead to the block we want to initialize
frame_system::Pallet::<T>::set_block_number(duration + now + T::EndingPeriod::get());
// Trigger epoch change for new random number value:
{
pallet_babe::EpochStart::<T>::set((Zero::zero(), u32::MAX.into()));
pallet_babe::Pallet::<T>::on_initialize(duration + now + T::EndingPeriod::get());
let authorities = pallet_babe::Pallet::<T>::authorities();
// Check for non empty authority set since it otherwise emits a No-OP warning.
if !authorities.is_empty() {
pallet_babe::Pallet::<T>::enact_epoch_change(
authorities.clone(),
authorities,
None,
);
}
}
#[block]
{
Auctions::<T>::on_initialize(duration + now + T::EndingPeriod::get());
}
let auction_index = AuctionCounter::<T>::get();
assert_last_event::<T>(Event::<T>::AuctionClosed { auction_index }.into());
assert!(Winning::<T>::iter().count().is_zero());
Ok(())
}
// Worst case: 10 bidders taking all wining spots, and winning data is full.
#[benchmark]
fn cancel_auction() -> Result<(), BenchmarkError> {
// If there is an offset, we need to be on that block to be able to do lease things.
let (lease_length, offset) = T::Leaser::lease_period_length();
frame_system::Pallet::<T>::set_block_number(offset + One::one());
// Create a new auction
let duration: BlockNumberFor<T> = lease_length / 2u32.into();
let lease_period_index = LeasePeriodOf::<T>::zero();
let origin = T::InitiateOrigin::try_successful_origin()
.expect("InitiateOrigin has no successful origin required for the benchmark");
Auctions::<T>::new_auction(origin, duration, lease_period_index)?;
fill_winners::<T>(lease_period_index);
let winning_data = Winning::<T>::get(BlockNumberFor::<T>::from(0u32)).unwrap();
for winner in winning_data.iter() {
assert!(winner.is_some());
}
// Make winning map full
for i in 0u32..(T::EndingPeriod::get() / T::SampleLength::get()).saturated_into() {
Winning::<T>::insert(BlockNumberFor::<T>::from(i), winning_data.clone());
}
assert!(AuctionInfo::<T>::get().is_some());
#[extrinsic_call]
_(RawOrigin::Root);
assert!(AuctionInfo::<T>::get().is_none());
Ok(())
}
impl_benchmark_test_suite!(
Auctions,
crate::integration_tests::new_test_ext(),
crate::integration_tests::Test,
);
}
+246
View File
@@ -0,0 +1,246 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Mocking utilities for testing in auctions pallet.
#[cfg(test)]
use super::*;
use crate::{auctions, mock::TestRegistrar};
use frame_support::{
assert_ok, derive_impl, ord_parameter_types, parameter_types, traits::EitherOfDiverse,
};
use frame_system::{EnsureRoot, EnsureSignedBy};
use pallet_balances;
use pezkuwi_primitives::{BlockNumber, Id as ParaId};
use pezkuwi_primitives_test_helpers::{dummy_head_data, dummy_validation_code};
use sp_core::H256;
use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup},
BuildStorage,
};
use std::{cell::RefCell, collections::BTreeMap};
type Block = frame_system::mocking::MockBlockU32<Test>;
frame_support::construct_runtime!(
pub enum Test
{
System: frame_system,
Balances: pallet_balances,
Auctions: auctions,
}
);
#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
impl frame_system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
type BlockWeights = ();
type BlockLength = ();
type DbWeight = ();
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
type Nonce = u64;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Block = Block;
type RuntimeEvent = RuntimeEvent;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
}
#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
impl pallet_balances::Config for Test {
type AccountStore = System;
}
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Debug)]
pub struct LeaseData {
pub leaser: u64,
pub amount: u64,
}
thread_local! {
pub static LEASES:
RefCell<BTreeMap<(ParaId, BlockNumber), LeaseData>> = RefCell::new(BTreeMap::new());
}
pub fn leases() -> Vec<((ParaId, BlockNumber), LeaseData)> {
LEASES.with(|p| (&*p.borrow()).clone().into_iter().collect::<Vec<_>>())
}
pub struct TestLeaser;
impl Leaser<BlockNumber> for TestLeaser {
type AccountId = u64;
type LeasePeriod = BlockNumber;
type Currency = Balances;
fn lease_out(
para: ParaId,
leaser: &Self::AccountId,
amount: <Self::Currency as Currency<Self::AccountId>>::Balance,
period_begin: Self::LeasePeriod,
period_count: Self::LeasePeriod,
) -> Result<(), LeaseError> {
LEASES.with(|l| {
let mut leases = l.borrow_mut();
let now = System::block_number();
let (current_lease_period, _) =
Self::lease_period_index(now).ok_or(LeaseError::NoLeasePeriod)?;
if period_begin < current_lease_period {
return Err(LeaseError::AlreadyEnded);
}
for period in period_begin..(period_begin + period_count) {
if leases.contains_key(&(para, period)) {
return Err(LeaseError::AlreadyLeased);
}
leases.insert((para, period), LeaseData { leaser: *leaser, amount });
}
Ok(())
})
}
fn deposit_held(
para: ParaId,
leaser: &Self::AccountId,
) -> <Self::Currency as Currency<Self::AccountId>>::Balance {
leases()
.iter()
.filter_map(|((id, _period), data)| {
if id == &para && &data.leaser == leaser {
Some(data.amount)
} else {
None
}
})
.max()
.unwrap_or_default()
}
fn lease_period_length() -> (BlockNumber, BlockNumber) {
(10, 0)
}
fn lease_period_index(b: BlockNumber) -> Option<(Self::LeasePeriod, bool)> {
let (lease_period_length, offset) = Self::lease_period_length();
let b = b.checked_sub(offset)?;
let lease_period = b / lease_period_length;
let first_block = (b % lease_period_length).is_zero();
Some((lease_period, first_block))
}
fn already_leased(
para_id: ParaId,
first_period: Self::LeasePeriod,
last_period: Self::LeasePeriod,
) -> bool {
leases().into_iter().any(|((para, period), _data)| {
para == para_id && first_period <= period && period <= last_period
})
}
}
ord_parameter_types! {
pub const Six: u64 = 6;
}
type RootOrSix = EitherOfDiverse<EnsureRoot<u64>, EnsureSignedBy<Six, u64>>;
thread_local! {
pub static LAST_RANDOM: RefCell<Option<(H256, u32)>> = RefCell::new(None);
}
pub fn set_last_random(output: H256, known_since: u32) {
LAST_RANDOM.with(|p| *p.borrow_mut() = Some((output, known_since)))
}
pub struct TestPastRandomness;
impl Randomness<H256, BlockNumber> for TestPastRandomness {
fn random(_subject: &[u8]) -> (H256, u32) {
LAST_RANDOM.with(|p| {
if let Some((output, known_since)) = &*p.borrow() {
(*output, *known_since)
} else {
(H256::zero(), frame_system::Pallet::<Test>::block_number())
}
})
}
}
parameter_types! {
pub static EndingPeriod: BlockNumber = 3;
pub static SampleLength: BlockNumber = 1;
}
impl Config for Test {
type RuntimeEvent = RuntimeEvent;
type Leaser = TestLeaser;
type Registrar = TestRegistrar<Self>;
type EndingPeriod = EndingPeriod;
type SampleLength = SampleLength;
type Randomness = TestPastRandomness;
type InitiateOrigin = RootOrSix;
type WeightInfo = crate::auctions::TestWeightInfo;
}
// This function basically just builds a genesis storage key/value store according to
// our desired mock up.
pub fn new_test_ext() -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
pallet_balances::GenesisConfig::<Test> {
balances: vec![(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)],
..Default::default()
}
.assimilate_storage(&mut t)
.unwrap();
let mut ext: sp_io::TestExternalities = t.into();
ext.execute_with(|| {
// Register para 0, 1, 2, and 3 for tests
assert_ok!(TestRegistrar::<Test>::register(
1,
0.into(),
dummy_head_data(),
dummy_validation_code()
));
assert_ok!(TestRegistrar::<Test>::register(
1,
1.into(),
dummy_head_data(),
dummy_validation_code()
));
assert_ok!(TestRegistrar::<Test>::register(
1,
2.into(),
dummy_head_data(),
dummy_validation_code()
));
assert_ok!(TestRegistrar::<Test>::register(
1,
3.into(),
dummy_head_data(),
dummy_validation_code()
));
});
ext
}
+678
View File
@@ -0,0 +1,678 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Auctioning system to determine the set of Teyrchains in operation. This includes logic for the
//! auctioning mechanism and for reserving balance as part of the "payment". Unreserving the balance
//! happens elsewhere.
use crate::{
slot_range::SlotRange,
traits::{AuctionStatus, Auctioneer, LeaseError, Leaser, Registrar},
};
use alloc::{vec, vec::Vec};
use codec::Decode;
use core::mem::swap;
use frame_support::{
dispatch::DispatchResult,
ensure,
traits::{Currency, Get, Randomness, ReservableCurrency},
weights::Weight,
};
use frame_system::pallet_prelude::BlockNumberFor;
pub use pallet::*;
use pezkuwi_primitives::Id as ParaId;
use sp_runtime::traits::{CheckedSub, One, Saturating, Zero};
type CurrencyOf<T> = <<T as Config>::Leaser as Leaser<BlockNumberFor<T>>>::Currency;
type BalanceOf<T> = <<<T as Config>::Leaser as Leaser<BlockNumberFor<T>>>::Currency as Currency<
<T as frame_system::Config>::AccountId,
>>::Balance;
pub trait WeightInfo {
fn new_auction() -> Weight;
fn bid() -> Weight;
fn cancel_auction() -> Weight;
fn on_initialize() -> Weight;
}
pub struct TestWeightInfo;
impl WeightInfo for TestWeightInfo {
fn new_auction() -> Weight {
Weight::zero()
}
fn bid() -> Weight {
Weight::zero()
}
fn cancel_auction() -> Weight {
Weight::zero()
}
fn on_initialize() -> Weight {
Weight::zero()
}
}
/// An auction index. We count auctions in this type.
pub type AuctionIndex = u32;
type LeasePeriodOf<T> = <<T as Config>::Leaser as Leaser<BlockNumberFor<T>>>::LeasePeriod;
// Winning data type. This encodes the top bidders of each range together with their bid.
type WinningData<T> = [Option<(<T as frame_system::Config>::AccountId, ParaId, BalanceOf<T>)>;
SlotRange::SLOT_RANGE_COUNT];
// Winners data type. This encodes each of the final winners of a teyrchain auction, the teyrchain
// index assigned to them, their winning bid and the range that they won.
type WinnersData<T> =
Vec<(<T as frame_system::Config>::AccountId, ParaId, BalanceOf<T>, SlotRange)>;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::{dispatch::DispatchClass, pallet_prelude::*, traits::EnsureOrigin};
use frame_system::{ensure_root, ensure_signed, pallet_prelude::*};
#[pallet::pallet]
pub struct Pallet<T>(_);
/// The module's configuration trait.
#[pallet::config]
pub trait Config: frame_system::Config {
/// The overarching event type.
#[allow(deprecated)]
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// The type representing the leasing system.
type Leaser: Leaser<
BlockNumberFor<Self>,
AccountId = Self::AccountId,
LeasePeriod = BlockNumberFor<Self>,
>;
/// The teyrchain registrar type.
type Registrar: Registrar<AccountId = Self::AccountId>;
/// The number of blocks over which an auction may be retroactively ended.
#[pallet::constant]
type EndingPeriod: Get<BlockNumberFor<Self>>;
/// The length of each sample to take during the ending period.
///
/// `EndingPeriod` / `SampleLength` = Total # of Samples
#[pallet::constant]
type SampleLength: Get<BlockNumberFor<Self>>;
/// Something that provides randomness in the runtime.
type Randomness: Randomness<Self::Hash, BlockNumberFor<Self>>;
/// The origin which may initiate auctions.
type InitiateOrigin: EnsureOrigin<Self::RuntimeOrigin>;
/// Weight Information for the Extrinsics in the Pallet
type WeightInfo: WeightInfo;
}
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// An auction started. Provides its index and the block number where it will begin to
/// close and the first lease period of the quadruplet that is auctioned.
AuctionStarted {
auction_index: AuctionIndex,
lease_period: LeasePeriodOf<T>,
ending: BlockNumberFor<T>,
},
/// An auction ended. All funds become unreserved.
AuctionClosed { auction_index: AuctionIndex },
/// Funds were reserved for a winning bid. First balance is the extra amount reserved.
/// Second is the total.
Reserved { bidder: T::AccountId, extra_reserved: BalanceOf<T>, total_amount: BalanceOf<T> },
/// Funds were unreserved since bidder is no longer active. `[bidder, amount]`
Unreserved { bidder: T::AccountId, amount: BalanceOf<T> },
/// Someone attempted to lease the same slot twice for a teyrchain. The amount is held in
/// reserve but no teyrchain slot has been leased.
ReserveConfiscated { para_id: ParaId, leaser: T::AccountId, amount: BalanceOf<T> },
/// A new bid has been accepted as the current winner.
BidAccepted {
bidder: T::AccountId,
para_id: ParaId,
amount: BalanceOf<T>,
first_slot: LeasePeriodOf<T>,
last_slot: LeasePeriodOf<T>,
},
/// The winning offset was chosen for an auction. This will map into the `Winning` storage
/// map.
WinningOffset { auction_index: AuctionIndex, block_number: BlockNumberFor<T> },
}
#[pallet::error]
pub enum Error<T> {
/// This auction is already in progress.
AuctionInProgress,
/// The lease period is in the past.
LeasePeriodInPast,
/// Para is not registered
ParaNotRegistered,
/// Not a current auction.
NotCurrentAuction,
/// Not an auction.
NotAuction,
/// Auction has already ended.
AuctionEnded,
/// The para is already leased out for part of this range.
AlreadyLeasedOut,
}
/// Number of auctions started so far.
#[pallet::storage]
pub type AuctionCounter<T> = StorageValue<_, AuctionIndex, ValueQuery>;
/// Information relating to the current auction, if there is one.
///
/// The first item in the tuple is the lease period index that the first of the four
/// contiguous lease periods on auction is for. The second is the block number when the
/// auction will "begin to end", i.e. the first block of the Ending Period of the auction.
#[pallet::storage]
pub type AuctionInfo<T: Config> = StorageValue<_, (LeasePeriodOf<T>, BlockNumberFor<T>)>;
/// Amounts currently reserved in the accounts of the bidders currently winning
/// (sub-)ranges.
#[pallet::storage]
pub type ReservedAmounts<T: Config> =
StorageMap<_, Twox64Concat, (T::AccountId, ParaId), BalanceOf<T>>;
/// The winning bids for each of the 10 ranges at each sample in the final Ending Period of
/// the current auction. The map's key is the 0-based index into the Sample Size. The
/// first sample of the ending period is 0; the last is `Sample Size - 1`.
#[pallet::storage]
pub type Winning<T: Config> = StorageMap<_, Twox64Concat, BlockNumberFor<T>, WinningData<T>>;
#[pallet::extra_constants]
impl<T: Config> Pallet<T> {
#[pallet::constant_name(SlotRangeCount)]
fn slot_range_count() -> u32 {
SlotRange::SLOT_RANGE_COUNT as u32
}
#[pallet::constant_name(LeasePeriodsPerSlot)]
fn lease_periods_per_slot() -> u32 {
SlotRange::LEASE_PERIODS_PER_SLOT as u32
}
}
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(n: BlockNumberFor<T>) -> Weight {
let mut weight = T::DbWeight::get().reads(1);
// If the current auction was in its ending period last block, then ensure that the
// (sub-)range winner information is duplicated from the previous block in case no bids
// happened in the last block.
if let AuctionStatus::EndingPeriod(offset, _sub_sample) = Self::auction_status(n) {
weight = weight.saturating_add(T::DbWeight::get().reads(1));
if !Winning::<T>::contains_key(&offset) {
weight = weight.saturating_add(T::DbWeight::get().writes(1));
let winning_data = offset
.checked_sub(&One::one())
.and_then(Winning::<T>::get)
.unwrap_or([Self::EMPTY; SlotRange::SLOT_RANGE_COUNT]);
Winning::<T>::insert(offset, winning_data);
}
}
// Check to see if an auction just ended.
if let Some((winning_ranges, auction_lease_period_index)) = Self::check_auction_end(n) {
// Auction is ended now. We have the winning ranges and the lease period index which
// acts as the offset. Handle it.
Self::manage_auction_end(auction_lease_period_index, winning_ranges);
weight = weight.saturating_add(T::WeightInfo::on_initialize());
}
weight
}
}
#[pallet::call]
impl<T: Config> Pallet<T> {
/// Create a new auction.
///
/// This can only happen when there isn't already an auction in progress and may only be
/// called by the root origin. Accepts the `duration` of this auction and the
/// `lease_period_index` of the initial lease period of the four that are to be auctioned.
#[pallet::call_index(0)]
#[pallet::weight((T::WeightInfo::new_auction(), DispatchClass::Operational))]
pub fn new_auction(
origin: OriginFor<T>,
#[pallet::compact] duration: BlockNumberFor<T>,
#[pallet::compact] lease_period_index: LeasePeriodOf<T>,
) -> DispatchResult {
T::InitiateOrigin::ensure_origin(origin)?;
Self::do_new_auction(duration, lease_period_index)
}
/// Make a new bid from an account (including a teyrchain account) for deploying a new
/// teyrchain.
///
/// Multiple simultaneous bids from the same bidder are allowed only as long as all active
/// bids overlap each other (i.e. are mutually exclusive). Bids cannot be redacted.
///
/// - `sub` is the sub-bidder ID, allowing for multiple competing bids to be made by (and
/// funded by) the same account.
/// - `auction_index` is the index of the auction to bid on. Should just be the present
/// value of `AuctionCounter`.
/// - `first_slot` is the first lease period index of the range to bid on. This is the
/// absolute lease period index value, not an auction-specific offset.
/// - `last_slot` is the last lease period index of the range to bid on. This is the
/// absolute lease period index value, not an auction-specific offset.
/// - `amount` is the amount to bid to be held as deposit for the teyrchain should the
/// bid win. This amount is held throughout the range.
#[pallet::call_index(1)]
#[pallet::weight(T::WeightInfo::bid())]
pub fn bid(
origin: OriginFor<T>,
#[pallet::compact] para: ParaId,
#[pallet::compact] auction_index: AuctionIndex,
#[pallet::compact] first_slot: LeasePeriodOf<T>,
#[pallet::compact] last_slot: LeasePeriodOf<T>,
#[pallet::compact] amount: BalanceOf<T>,
) -> DispatchResult {
let who = ensure_signed(origin)?;
Self::handle_bid(who, para, auction_index, first_slot, last_slot, amount)?;
Ok(())
}
/// Cancel an in-progress auction.
///
/// Can only be called by Root origin.
#[pallet::call_index(2)]
#[pallet::weight(T::WeightInfo::cancel_auction())]
pub fn cancel_auction(origin: OriginFor<T>) -> DispatchResult {
ensure_root(origin)?;
// Unreserve all bids.
for ((bidder, _), amount) in ReservedAmounts::<T>::drain() {
CurrencyOf::<T>::unreserve(&bidder, amount);
}
#[allow(deprecated)]
Winning::<T>::remove_all(None);
AuctionInfo::<T>::kill();
Ok(())
}
}
}
impl<T: Config> Auctioneer<BlockNumberFor<T>> for Pallet<T> {
type AccountId = T::AccountId;
type LeasePeriod = BlockNumberFor<T>;
type Currency = CurrencyOf<T>;
fn new_auction(
duration: BlockNumberFor<T>,
lease_period_index: LeasePeriodOf<T>,
) -> DispatchResult {
Self::do_new_auction(duration, lease_period_index)
}
// Returns the status of the auction given the current block number.
fn auction_status(now: BlockNumberFor<T>) -> AuctionStatus<BlockNumberFor<T>> {
let early_end = match AuctionInfo::<T>::get() {
Some((_, early_end)) => early_end,
None => return AuctionStatus::NotStarted,
};
let after_early_end = match now.checked_sub(&early_end) {
Some(after_early_end) => after_early_end,
None => return AuctionStatus::StartingPeriod,
};
let ending_period = T::EndingPeriod::get();
if after_early_end < ending_period {
let sample_length = T::SampleLength::get().max(One::one());
let sample = after_early_end / sample_length;
let sub_sample = after_early_end % sample_length;
return AuctionStatus::EndingPeriod(sample, sub_sample);
} else {
// This is safe because of the comparison operator above
return AuctionStatus::VrfDelay(after_early_end - ending_period);
}
}
fn place_bid(
bidder: T::AccountId,
para: ParaId,
first_slot: LeasePeriodOf<T>,
last_slot: LeasePeriodOf<T>,
amount: BalanceOf<T>,
) -> DispatchResult {
Self::handle_bid(bidder, para, AuctionCounter::<T>::get(), first_slot, last_slot, amount)
}
fn lease_period_index(b: BlockNumberFor<T>) -> Option<(Self::LeasePeriod, bool)> {
T::Leaser::lease_period_index(b)
}
#[cfg(any(feature = "runtime-benchmarks", test))]
fn lease_period_length() -> (BlockNumberFor<T>, BlockNumberFor<T>) {
T::Leaser::lease_period_length()
}
fn has_won_an_auction(para: ParaId, bidder: &T::AccountId) -> bool {
!T::Leaser::deposit_held(para, bidder).is_zero()
}
}
impl<T: Config> Pallet<T> {
// A trick to allow me to initialize large arrays with nothing in them.
const EMPTY: Option<(<T as frame_system::Config>::AccountId, ParaId, BalanceOf<T>)> = None;
/// Create a new auction.
///
/// This can only happen when there isn't already an auction in progress. Accepts the `duration`
/// of this auction and the `lease_period_index` of the initial lease period of the four that
/// are to be auctioned.
fn do_new_auction(
duration: BlockNumberFor<T>,
lease_period_index: LeasePeriodOf<T>,
) -> DispatchResult {
let maybe_auction = AuctionInfo::<T>::get();
ensure!(maybe_auction.is_none(), Error::<T>::AuctionInProgress);
let now = frame_system::Pallet::<T>::block_number();
if let Some((current_lease_period, _)) = T::Leaser::lease_period_index(now) {
// If there is no active lease period, then we don't need to make this check.
ensure!(lease_period_index >= current_lease_period, Error::<T>::LeasePeriodInPast);
}
// Bump the counter.
let n = AuctionCounter::<T>::mutate(|n| {
*n += 1;
*n
});
// Set the information.
let ending = frame_system::Pallet::<T>::block_number().saturating_add(duration);
AuctionInfo::<T>::put((lease_period_index, ending));
Self::deposit_event(Event::<T>::AuctionStarted {
auction_index: n,
lease_period: lease_period_index,
ending,
});
Ok(())
}
/// Actually place a bid in the current auction.
///
/// - `bidder`: The account that will be funding this bid.
/// - `auction_index`: The auction index of the bid. For this to succeed, must equal
/// the current value of `AuctionCounter`.
/// - `first_slot`: The first lease period index of the range to be bid on.
/// - `last_slot`: The last lease period index of the range to be bid on (inclusive).
/// - `amount`: The total amount to be the bid for deposit over the range.
pub fn handle_bid(
bidder: T::AccountId,
para: ParaId,
auction_index: u32,
first_slot: LeasePeriodOf<T>,
last_slot: LeasePeriodOf<T>,
amount: BalanceOf<T>,
) -> DispatchResult {
// Ensure para is registered before placing a bid on it.
ensure!(T::Registrar::is_registered(para), Error::<T>::ParaNotRegistered);
// Bidding on latest auction.
ensure!(auction_index == AuctionCounter::<T>::get(), Error::<T>::NotCurrentAuction);
// Assume it's actually an auction (this should never fail because of above).
let (first_lease_period, _) = AuctionInfo::<T>::get().ok_or(Error::<T>::NotAuction)?;
// Get the auction status and the current sample block. For the starting period, the sample
// block is zero.
let auction_status = Self::auction_status(frame_system::Pallet::<T>::block_number());
// The offset into the ending samples of the auction.
let offset = match auction_status {
AuctionStatus::NotStarted => return Err(Error::<T>::AuctionEnded.into()),
AuctionStatus::StartingPeriod => Zero::zero(),
AuctionStatus::EndingPeriod(o, _) => o,
AuctionStatus::VrfDelay(_) => return Err(Error::<T>::AuctionEnded.into()),
};
// We also make sure that the bid is not for any existing leases the para already has.
ensure!(
!T::Leaser::already_leased(para, first_slot, last_slot),
Error::<T>::AlreadyLeasedOut
);
// Our range.
let range = SlotRange::new_bounded(first_lease_period, first_slot, last_slot)?;
// Range as an array index.
let range_index = range as u8 as usize;
// The current winning ranges.
let mut current_winning = Winning::<T>::get(offset)
.or_else(|| offset.checked_sub(&One::one()).and_then(Winning::<T>::get))
.unwrap_or([Self::EMPTY; SlotRange::SLOT_RANGE_COUNT]);
// If this bid beat the previous winner of our range.
if current_winning[range_index].as_ref().map_or(true, |last| amount > last.2) {
// Ok; we are the new winner of this range - reserve the additional amount and record.
// Get the amount already held on deposit if this is a renewal bid (i.e. there's
// an existing lease on the same para by the same leaser).
let existing_lease_deposit = T::Leaser::deposit_held(para, &bidder);
let reserve_required = amount.saturating_sub(existing_lease_deposit);
// Get the amount already reserved in any prior and still active bids by us.
let bidder_para = (bidder.clone(), para);
let already_reserved = ReservedAmounts::<T>::get(&bidder_para).unwrap_or_default();
// If these don't already cover the bid...
if let Some(additional) = reserve_required.checked_sub(&already_reserved) {
// ...then reserve some more funds from their account, failing if there's not
// enough funds.
CurrencyOf::<T>::reserve(&bidder, additional)?;
// ...and record the amount reserved.
ReservedAmounts::<T>::insert(&bidder_para, reserve_required);
Self::deposit_event(Event::<T>::Reserved {
bidder: bidder.clone(),
extra_reserved: additional,
total_amount: reserve_required,
});
}
// Return any funds reserved for the previous winner if we are not in the ending period
// and they no longer have any active bids.
let mut outgoing_winner = Some((bidder.clone(), para, amount));
swap(&mut current_winning[range_index], &mut outgoing_winner);
if let Some((who, para, _amount)) = outgoing_winner {
if auction_status.is_starting() &&
current_winning
.iter()
.filter_map(Option::as_ref)
.all(|&(ref other, other_para, _)| other != &who || other_para != para)
{
// Previous bidder is no longer winning any ranges: unreserve their funds.
if let Some(amount) = ReservedAmounts::<T>::take(&(who.clone(), para)) {
// It really should be reserved; there's not much we can do here on fail.
let err_amt = CurrencyOf::<T>::unreserve(&who, amount);
debug_assert!(err_amt.is_zero());
Self::deposit_event(Event::<T>::Unreserved { bidder: who, amount });
}
}
}
// Update the range winner.
Winning::<T>::insert(offset, &current_winning);
Self::deposit_event(Event::<T>::BidAccepted {
bidder,
para_id: para,
amount,
first_slot,
last_slot,
});
}
Ok(())
}
/// Some when the auction's end is known (with the end block number). None if it is unknown.
/// If `Some` then the block number must be at most the previous block and at least the
/// previous block minus `T::EndingPeriod::get()`.
///
/// This mutates the state, cleaning up `AuctionInfo` and `Winning` in the case of an auction
/// ending. An immediately subsequent call with the same argument will always return `None`.
fn check_auction_end(now: BlockNumberFor<T>) -> Option<(WinningData<T>, LeasePeriodOf<T>)> {
if let Some((lease_period_index, early_end)) = AuctionInfo::<T>::get() {
let ending_period = T::EndingPeriod::get();
let late_end = early_end.saturating_add(ending_period);
let is_ended = now >= late_end;
if is_ended {
// auction definitely ended.
// check to see if we can determine the actual ending point.
let (raw_offset, known_since) = T::Randomness::random(&b"para_auction"[..]);
if late_end <= known_since {
// Our random seed was known only after the auction ended. Good to use.
let raw_offset_block_number = <BlockNumberFor<T>>::decode(
&mut raw_offset.as_ref(),
)
.expect("secure hashes should always be bigger than the block number; qed");
let offset = (raw_offset_block_number % ending_period) /
T::SampleLength::get().max(One::one());
let auction_counter = AuctionCounter::<T>::get();
Self::deposit_event(Event::<T>::WinningOffset {
auction_index: auction_counter,
block_number: offset,
});
let res = Winning::<T>::get(offset)
.unwrap_or([Self::EMPTY; SlotRange::SLOT_RANGE_COUNT]);
// This `remove_all` statement should remove at most `EndingPeriod` /
// `SampleLength` items, which should be bounded and sensibly configured in the
// runtime.
#[allow(deprecated)]
Winning::<T>::remove_all(None);
AuctionInfo::<T>::kill();
return Some((res, lease_period_index));
}
}
}
None
}
/// Auction just ended. We have the current lease period, the auction's lease period (which
/// is guaranteed to be at least the current period) and the bidders that were winning each
/// range at the time of the auction's close.
fn manage_auction_end(
auction_lease_period_index: LeasePeriodOf<T>,
winning_ranges: WinningData<T>,
) {
// First, unreserve all amounts that were reserved for the bids. We will later re-reserve
// the amounts from the bidders that ended up being assigned the slot so there's no need to
// special-case them here.
for ((bidder, _), amount) in ReservedAmounts::<T>::drain() {
CurrencyOf::<T>::unreserve(&bidder, amount);
}
// Next, calculate the winning combination of slots and thus the final winners of the
// auction.
let winners = Self::calculate_winners(winning_ranges);
// Go through those winners and re-reserve their bid, updating our table of deposits
// accordingly.
for (leaser, para, amount, range) in winners.into_iter() {
let begin_offset = LeasePeriodOf::<T>::from(range.as_pair().0 as u32);
let period_begin = auction_lease_period_index + begin_offset;
let period_count = LeasePeriodOf::<T>::from(range.len() as u32);
match T::Leaser::lease_out(para, &leaser, amount, period_begin, period_count) {
Err(LeaseError::ReserveFailed) |
Err(LeaseError::AlreadyEnded) |
Err(LeaseError::NoLeasePeriod) => {
// Should never happen since we just unreserved this amount (and our offset is
// from the present period). But if it does, there's not much we can do.
},
Err(LeaseError::AlreadyLeased) => {
// The leaser attempted to get a second lease on the same para ID, possibly
// griefing us. Let's keep the amount reserved and let governance sort it out.
if CurrencyOf::<T>::reserve(&leaser, amount).is_ok() {
Self::deposit_event(Event::<T>::ReserveConfiscated {
para_id: para,
leaser,
amount,
});
}
},
Ok(()) => {}, // Nothing to report.
}
}
Self::deposit_event(Event::<T>::AuctionClosed {
auction_index: AuctionCounter::<T>::get(),
});
}
/// Calculate the final winners from the winning slots.
///
/// This is a simple dynamic programming algorithm designed by Al, the original code is at:
/// `https://github.com/w3f/consensus/blob/master/NPoS/auctiondynamicthing.py`
fn calculate_winners(mut winning: WinningData<T>) -> WinnersData<T> {
let winning_ranges = {
let mut best_winners_ending_at: [(Vec<SlotRange>, BalanceOf<T>);
SlotRange::LEASE_PERIODS_PER_SLOT] = Default::default();
let best_bid = |range: SlotRange| {
winning[range as u8 as usize]
.as_ref()
.map(|(_, _, amount)| *amount * (range.len() as u32).into())
};
for i in 0..SlotRange::LEASE_PERIODS_PER_SLOT {
let r = SlotRange::new_bounded(0, 0, i as u32).expect("`i < LPPS`; qed");
if let Some(bid) = best_bid(r) {
best_winners_ending_at[i] = (vec![r], bid);
}
for j in 0..i {
let r = SlotRange::new_bounded(0, j as u32 + 1, i as u32)
.expect("`i < LPPS`; `j < i`; `j + 1 < LPPS`; qed");
if let Some(mut bid) = best_bid(r) {
bid += best_winners_ending_at[j].1;
if bid > best_winners_ending_at[i].1 {
let mut new_winners = best_winners_ending_at[j].0.clone();
new_winners.push(r);
best_winners_ending_at[i] = (new_winners, bid);
}
} else {
if best_winners_ending_at[j].1 > best_winners_ending_at[i].1 {
best_winners_ending_at[i] = best_winners_ending_at[j].clone();
}
}
}
}
best_winners_ending_at[SlotRange::LEASE_PERIODS_PER_SLOT - 1].0.clone()
};
winning_ranges
.into_iter()
.filter_map(|range| {
winning[range as u8 as usize]
.take()
.map(|(bidder, para, amount)| (bidder, para, amount, range))
})
.collect::<Vec<_>>()
}
}
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
@@ -0,0 +1,821 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Tests for the auctions pallet.
#[cfg(test)]
use super::*;
use crate::{auctions::mock::*, mock::TestRegistrar};
use frame_support::{assert_noop, assert_ok, assert_storage_noop};
use pallet_balances;
use pezkuwi_primitives::Id as ParaId;
use pezkuwi_primitives_test_helpers::{dummy_hash, dummy_head_data, dummy_validation_code};
use sp_core::H256;
use sp_runtime::DispatchError::BadOrigin;
#[test]
fn basic_setup_works() {
new_test_ext().execute_with(|| {
assert_eq!(AuctionCounter::<Test>::get(), 0);
assert_eq!(TestLeaser::deposit_held(0u32.into(), &1), 0);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::NotStarted
);
System::run_to_block::<AllPalletsWithSystem>(10);
assert_eq!(AuctionCounter::<Test>::get(), 0);
assert_eq!(TestLeaser::deposit_held(0u32.into(), &1), 0);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::NotStarted
);
});
}
#[test]
fn can_start_auction() {
new_test_ext().execute_with(|| {
System::run_to_block::<AllPalletsWithSystem>(1);
assert_noop!(Auctions::new_auction(RuntimeOrigin::signed(1), 5, 1), BadOrigin);
assert_ok!(Auctions::new_auction(RuntimeOrigin::signed(6), 5, 1));
assert_eq!(AuctionCounter::<Test>::get(), 1);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::StartingPeriod
);
});
}
#[test]
fn bidding_works() {
new_test_ext().execute_with(|| {
System::run_to_block::<AllPalletsWithSystem>(1);
assert_ok!(Auctions::new_auction(RuntimeOrigin::signed(6), 5, 1));
assert_ok!(Auctions::bid(RuntimeOrigin::signed(1), 0.into(), 1, 1, 4, 5));
assert_eq!(Balances::reserved_balance(1), 5);
assert_eq!(Balances::free_balance(1), 5);
assert_eq!(
Winning::<Test>::get(0).unwrap()[SlotRange::ZeroThree as u8 as usize],
Some((1, 0.into(), 5))
);
});
}
#[test]
fn under_bidding_works() {
new_test_ext().execute_with(|| {
System::run_to_block::<AllPalletsWithSystem>(1);
assert_ok!(Auctions::new_auction(RuntimeOrigin::signed(6), 5, 1));
assert_ok!(Auctions::bid(RuntimeOrigin::signed(1), 0.into(), 1, 1, 4, 5));
assert_storage_noop!({
assert_ok!(Auctions::bid(RuntimeOrigin::signed(2), 0.into(), 1, 1, 4, 1));
});
});
}
#[test]
fn over_bidding_works() {
new_test_ext().execute_with(|| {
System::run_to_block::<AllPalletsWithSystem>(1);
assert_ok!(Auctions::new_auction(RuntimeOrigin::signed(6), 5, 1));
assert_ok!(Auctions::bid(RuntimeOrigin::signed(1), 0.into(), 1, 1, 4, 5));
assert_ok!(Auctions::bid(RuntimeOrigin::signed(2), 0.into(), 1, 1, 4, 6));
assert_eq!(Balances::reserved_balance(1), 0);
assert_eq!(Balances::free_balance(1), 10);
assert_eq!(Balances::reserved_balance(2), 6);
assert_eq!(Balances::free_balance(2), 14);
assert_eq!(
Winning::<Test>::get(0).unwrap()[SlotRange::ZeroThree as u8 as usize],
Some((2, 0.into(), 6))
);
});
}
#[test]
fn auction_proceeds_correctly() {
new_test_ext().execute_with(|| {
System::run_to_block::<AllPalletsWithSystem>(1);
assert_ok!(Auctions::new_auction(RuntimeOrigin::signed(6), 5, 1));
assert_eq!(AuctionCounter::<Test>::get(), 1);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::StartingPeriod
);
System::run_to_block::<AllPalletsWithSystem>(2);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::StartingPeriod
);
System::run_to_block::<AllPalletsWithSystem>(3);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::StartingPeriod
);
System::run_to_block::<AllPalletsWithSystem>(4);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::StartingPeriod
);
System::run_to_block::<AllPalletsWithSystem>(5);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::StartingPeriod
);
System::run_to_block::<AllPalletsWithSystem>(6);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::EndingPeriod(0, 0)
);
System::run_to_block::<AllPalletsWithSystem>(7);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::EndingPeriod(1, 0)
);
System::run_to_block::<AllPalletsWithSystem>(8);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::EndingPeriod(2, 0)
);
System::run_to_block::<AllPalletsWithSystem>(9);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::NotStarted
);
});
}
#[test]
fn can_win_auction() {
new_test_ext().execute_with(|| {
System::run_to_block::<AllPalletsWithSystem>(1);
assert_ok!(Auctions::new_auction(RuntimeOrigin::signed(6), 5, 1));
assert_ok!(Auctions::bid(RuntimeOrigin::signed(1), 0.into(), 1, 1, 4, 1));
assert_eq!(Balances::reserved_balance(1), 1);
assert_eq!(Balances::free_balance(1), 9);
System::run_to_block::<AllPalletsWithSystem>(9);
assert_eq!(
leases(),
vec![
((0.into(), 1), LeaseData { leaser: 1, amount: 1 }),
((0.into(), 2), LeaseData { leaser: 1, amount: 1 }),
((0.into(), 3), LeaseData { leaser: 1, amount: 1 }),
((0.into(), 4), LeaseData { leaser: 1, amount: 1 }),
]
);
assert_eq!(TestLeaser::deposit_held(0.into(), &1), 1);
});
}
#[test]
fn can_win_auction_with_late_randomness() {
new_test_ext().execute_with(|| {
System::run_to_block::<AllPalletsWithSystem>(1);
assert_ok!(Auctions::new_auction(RuntimeOrigin::signed(6), 5, 1));
assert_ok!(Auctions::bid(RuntimeOrigin::signed(1), 0.into(), 1, 1, 4, 1));
assert_eq!(Balances::reserved_balance(1), 1);
assert_eq!(Balances::free_balance(1), 9);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::StartingPeriod
);
System::run_to_block::<AllPalletsWithSystem>(8);
// Auction has not yet ended.
assert_eq!(leases(), vec![]);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::EndingPeriod(2, 0)
);
// This will prevent the auction's winner from being decided in the next block, since
// the random seed was known before the final bids were made.
set_last_random(H256::zero(), 8);
// Auction definitely ended now, but we don't know exactly when in the last 3 blocks yet
// since no randomness available yet.
System::run_to_block::<AllPalletsWithSystem>(9);
// Auction has now ended... But auction winner still not yet decided, so no leases yet.
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::VrfDelay(0)
);
assert_eq!(leases(), vec![]);
// Random seed now updated to a value known at block 9, when the auction ended. This
// means that the winner can now be chosen.
set_last_random(H256::zero(), 9);
System::run_to_block::<AllPalletsWithSystem>(10);
// Auction ended and winner selected
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::NotStarted
);
assert_eq!(
leases(),
vec![
((0.into(), 1), LeaseData { leaser: 1, amount: 1 }),
((0.into(), 2), LeaseData { leaser: 1, amount: 1 }),
((0.into(), 3), LeaseData { leaser: 1, amount: 1 }),
((0.into(), 4), LeaseData { leaser: 1, amount: 1 }),
]
);
assert_eq!(TestLeaser::deposit_held(0.into(), &1), 1);
});
}
#[test]
fn can_win_incomplete_auction() {
new_test_ext().execute_with(|| {
System::run_to_block::<AllPalletsWithSystem>(1);
assert_ok!(Auctions::new_auction(RuntimeOrigin::signed(6), 5, 1));
assert_ok!(Auctions::bid(RuntimeOrigin::signed(1), 0.into(), 1, 4, 4, 5));
System::run_to_block::<AllPalletsWithSystem>(9);
assert_eq!(leases(), vec![((0.into(), 4), LeaseData { leaser: 1, amount: 5 }),]);
assert_eq!(TestLeaser::deposit_held(0.into(), &1), 5);
});
}
#[test]
fn should_choose_best_combination() {
new_test_ext().execute_with(|| {
System::run_to_block::<AllPalletsWithSystem>(1);
assert_ok!(Auctions::new_auction(RuntimeOrigin::signed(6), 5, 1));
assert_ok!(Auctions::bid(RuntimeOrigin::signed(1), 0.into(), 1, 1, 1, 1));
assert_ok!(Auctions::bid(RuntimeOrigin::signed(2), 0.into(), 1, 2, 3, 4));
assert_ok!(Auctions::bid(RuntimeOrigin::signed(3), 0.into(), 1, 4, 4, 2));
assert_ok!(Auctions::bid(RuntimeOrigin::signed(1), 1.into(), 1, 1, 4, 2));
System::run_to_block::<AllPalletsWithSystem>(9);
assert_eq!(
leases(),
vec![
((0.into(), 1), LeaseData { leaser: 1, amount: 1 }),
((0.into(), 2), LeaseData { leaser: 2, amount: 4 }),
((0.into(), 3), LeaseData { leaser: 2, amount: 4 }),
((0.into(), 4), LeaseData { leaser: 3, amount: 2 }),
]
);
assert_eq!(TestLeaser::deposit_held(0.into(), &1), 1);
assert_eq!(TestLeaser::deposit_held(1.into(), &1), 0);
assert_eq!(TestLeaser::deposit_held(0.into(), &2), 4);
assert_eq!(TestLeaser::deposit_held(0.into(), &3), 2);
});
}
#[test]
fn gap_bid_works() {
new_test_ext().execute_with(|| {
System::run_to_block::<AllPalletsWithSystem>(1);
assert_ok!(Auctions::new_auction(RuntimeOrigin::signed(6), 5, 1));
// User 1 will make a bid for period 1 and 4 for the same Para 0
assert_ok!(Auctions::bid(RuntimeOrigin::signed(1), 0.into(), 1, 1, 1, 1));
assert_ok!(Auctions::bid(RuntimeOrigin::signed(1), 0.into(), 1, 4, 4, 4));
// User 2 and 3 will make a bid for para 1 on period 2 and 3 respectively
assert_ok!(Auctions::bid(RuntimeOrigin::signed(2), 1.into(), 1, 2, 2, 2));
assert_ok!(Auctions::bid(RuntimeOrigin::signed(3), 1.into(), 1, 3, 3, 3));
// Total reserved should be the max of the two
assert_eq!(Balances::reserved_balance(1), 4);
// Other people are reserved correctly too
assert_eq!(Balances::reserved_balance(2), 2);
assert_eq!(Balances::reserved_balance(3), 3);
// End the auction.
System::run_to_block::<AllPalletsWithSystem>(9);
assert_eq!(
leases(),
vec![
((0.into(), 1), LeaseData { leaser: 1, amount: 1 }),
((0.into(), 4), LeaseData { leaser: 1, amount: 4 }),
((1.into(), 2), LeaseData { leaser: 2, amount: 2 }),
((1.into(), 3), LeaseData { leaser: 3, amount: 3 }),
]
);
assert_eq!(TestLeaser::deposit_held(0.into(), &1), 4);
assert_eq!(TestLeaser::deposit_held(1.into(), &2), 2);
assert_eq!(TestLeaser::deposit_held(1.into(), &3), 3);
});
}
#[test]
fn deposit_credit_should_work() {
new_test_ext().execute_with(|| {
System::run_to_block::<AllPalletsWithSystem>(1);
assert_ok!(Auctions::new_auction(RuntimeOrigin::signed(6), 5, 1));
assert_ok!(Auctions::bid(RuntimeOrigin::signed(1), 0.into(), 1, 1, 1, 5));
assert_eq!(Balances::reserved_balance(1), 5);
System::run_to_block::<AllPalletsWithSystem>(10);
assert_eq!(leases(), vec![((0.into(), 1), LeaseData { leaser: 1, amount: 5 }),]);
assert_eq!(TestLeaser::deposit_held(0.into(), &1), 5);
assert_ok!(Auctions::new_auction(RuntimeOrigin::signed(6), 5, 2));
assert_ok!(Auctions::bid(RuntimeOrigin::signed(1), 0.into(), 2, 2, 2, 6));
// Only 1 reserved since we have a deposit credit of 5.
assert_eq!(Balances::reserved_balance(1), 1);
System::run_to_block::<AllPalletsWithSystem>(20);
assert_eq!(
leases(),
vec![
((0.into(), 1), LeaseData { leaser: 1, amount: 5 }),
((0.into(), 2), LeaseData { leaser: 1, amount: 6 }),
]
);
assert_eq!(TestLeaser::deposit_held(0.into(), &1), 6);
});
}
#[test]
fn deposit_credit_on_alt_para_should_not_count() {
new_test_ext().execute_with(|| {
System::run_to_block::<AllPalletsWithSystem>(1);
assert_ok!(Auctions::new_auction(RuntimeOrigin::signed(6), 5, 1));
assert_ok!(Auctions::bid(RuntimeOrigin::signed(1), 0.into(), 1, 1, 1, 5));
assert_eq!(Balances::reserved_balance(1), 5);
System::run_to_block::<AllPalletsWithSystem>(10);
assert_eq!(leases(), vec![((0.into(), 1), LeaseData { leaser: 1, amount: 5 }),]);
assert_eq!(TestLeaser::deposit_held(0.into(), &1), 5);
assert_ok!(Auctions::new_auction(RuntimeOrigin::signed(6), 5, 2));
assert_ok!(Auctions::bid(RuntimeOrigin::signed(1), 1.into(), 2, 2, 2, 6));
// 6 reserved since we are bidding on a new para; only works because we don't
assert_eq!(Balances::reserved_balance(1), 6);
System::run_to_block::<AllPalletsWithSystem>(20);
assert_eq!(
leases(),
vec![
((0.into(), 1), LeaseData { leaser: 1, amount: 5 }),
((1.into(), 2), LeaseData { leaser: 1, amount: 6 }),
]
);
assert_eq!(TestLeaser::deposit_held(0.into(), &1), 5);
assert_eq!(TestLeaser::deposit_held(1.into(), &1), 6);
});
}
#[test]
fn multiple_bids_work_pre_ending() {
new_test_ext().execute_with(|| {
System::run_to_block::<AllPalletsWithSystem>(1);
assert_ok!(Auctions::new_auction(RuntimeOrigin::signed(6), 5, 1));
for i in 1..6u64 {
System::run_to_block::<AllPalletsWithSystem>(i as _);
assert_ok!(Auctions::bid(RuntimeOrigin::signed(i), 0.into(), 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 });
}
}
System::run_to_block::<AllPalletsWithSystem>(9);
assert_eq!(
leases(),
vec![
((0.into(), 1), LeaseData { leaser: 5, amount: 5 }),
((0.into(), 2), LeaseData { leaser: 5, amount: 5 }),
((0.into(), 3), LeaseData { leaser: 5, amount: 5 }),
((0.into(), 4), LeaseData { leaser: 5, amount: 5 }),
]
);
});
}
#[test]
fn multiple_bids_work_post_ending() {
new_test_ext().execute_with(|| {
System::run_to_block::<AllPalletsWithSystem>(1);
assert_ok!(Auctions::new_auction(RuntimeOrigin::signed(6), 0, 1));
for i in 1..6u64 {
System::run_to_block::<AllPalletsWithSystem>(((i - 1) / 2 + 1) as _);
assert_ok!(Auctions::bid(RuntimeOrigin::signed(i), 0.into(), 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 });
}
}
for i in 1..6u64 {
assert_eq!(ReservedAmounts::<Test>::get((i, ParaId::from(0))).unwrap(), i);
}
System::run_to_block::<AllPalletsWithSystem>(5);
assert_eq!(
leases(),
(1..=4)
.map(|i| ((0.into(), i), LeaseData { leaser: 2, amount: 2 }))
.collect::<Vec<_>>()
);
});
}
#[test]
fn incomplete_calculate_winners_works() {
let mut winning = [None; SlotRange::SLOT_RANGE_COUNT];
winning[SlotRange::ThreeThree as u8 as usize] = Some((1, 0.into(), 1));
let winners = vec![(1, 0.into(), 1, SlotRange::ThreeThree)];
assert_eq!(Auctions::calculate_winners(winning), winners);
}
#[test]
fn first_incomplete_calculate_winners_works() {
let mut winning = [None; SlotRange::SLOT_RANGE_COUNT];
winning[0] = Some((1, 0.into(), 1));
let winners = vec![(1, 0.into(), 1, SlotRange::ZeroZero)];
assert_eq!(Auctions::calculate_winners(winning), winners);
}
#[test]
fn calculate_winners_works() {
let mut winning = [None; SlotRange::SLOT_RANGE_COUNT];
winning[SlotRange::ZeroZero as u8 as usize] = Some((2, 0.into(), 2));
winning[SlotRange::ZeroThree as u8 as usize] = Some((1, 100.into(), 1));
winning[SlotRange::OneOne as u8 as usize] = Some((3, 1.into(), 1));
winning[SlotRange::TwoTwo as u8 as usize] = Some((1, 2.into(), 53));
winning[SlotRange::ThreeThree as u8 as usize] = Some((5, 3.into(), 1));
let winners = vec![
(2, 0.into(), 2, SlotRange::ZeroZero),
(3, 1.into(), 1, SlotRange::OneOne),
(1, 2.into(), 53, SlotRange::TwoTwo),
(5, 3.into(), 1, SlotRange::ThreeThree),
];
assert_eq!(Auctions::calculate_winners(winning), winners);
winning[SlotRange::ZeroOne as u8 as usize] = Some((4, 10.into(), 3));
let winners = vec![
(4, 10.into(), 3, SlotRange::ZeroOne),
(1, 2.into(), 53, SlotRange::TwoTwo),
(5, 3.into(), 1, SlotRange::ThreeThree),
];
assert_eq!(Auctions::calculate_winners(winning), winners);
winning[SlotRange::ZeroThree as u8 as usize] = Some((1, 100.into(), 100));
let winners = vec![(1, 100.into(), 100, SlotRange::ZeroThree)];
assert_eq!(Auctions::calculate_winners(winning), winners);
}
#[test]
fn lower_bids_are_correctly_refunded() {
new_test_ext().execute_with(|| {
System::run_to_block::<AllPalletsWithSystem>(1);
assert_ok!(Auctions::new_auction(RuntimeOrigin::signed(6), 1, 1));
let para_1 = ParaId::from(1_u32);
let para_2 = ParaId::from(2_u32);
// Make a bid and reserve a balance
assert_ok!(Auctions::bid(RuntimeOrigin::signed(1), para_1, 1, 1, 4, 9));
assert_eq!(Balances::reserved_balance(1), 9);
assert_eq!(ReservedAmounts::<Test>::get((1, para_1)), Some(9));
assert_eq!(Balances::reserved_balance(2), 0);
assert_eq!(ReservedAmounts::<Test>::get((2, para_2)), None);
// Bigger bid, reserves new balance and returns funds
assert_ok!(Auctions::bid(RuntimeOrigin::signed(2), para_2, 1, 1, 4, 19));
assert_eq!(Balances::reserved_balance(1), 0);
assert_eq!(ReservedAmounts::<Test>::get((1, para_1)), None);
assert_eq!(Balances::reserved_balance(2), 19);
assert_eq!(ReservedAmounts::<Test>::get((2, para_2)), Some(19));
});
}
#[test]
fn initialize_winners_in_ending_period_works() {
new_test_ext().execute_with(|| {
let ed: u64 = <Test as pallet_balances::Config>::ExistentialDeposit::get();
assert_eq!(ed, 1);
System::run_to_block::<AllPalletsWithSystem>(1);
assert_ok!(Auctions::new_auction(RuntimeOrigin::signed(6), 9, 1));
let para_1 = ParaId::from(1_u32);
let para_2 = ParaId::from(2_u32);
let para_3 = ParaId::from(3_u32);
// Make bids
assert_ok!(Auctions::bid(RuntimeOrigin::signed(1), para_1, 1, 1, 4, 9));
assert_ok!(Auctions::bid(RuntimeOrigin::signed(2), para_2, 1, 3, 4, 19));
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::StartingPeriod
);
let mut winning = [None; SlotRange::SLOT_RANGE_COUNT];
winning[SlotRange::ZeroThree as u8 as usize] = Some((1, para_1, 9));
winning[SlotRange::TwoThree as u8 as usize] = Some((2, para_2, 19));
assert_eq!(Winning::<Test>::get(0), Some(winning));
System::run_to_block::<AllPalletsWithSystem>(9);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::StartingPeriod
);
System::run_to_block::<AllPalletsWithSystem>(10);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::EndingPeriod(0, 0)
);
assert_eq!(Winning::<Test>::get(0), Some(winning));
System::run_to_block::<AllPalletsWithSystem>(11);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::EndingPeriod(1, 0)
);
assert_eq!(Winning::<Test>::get(1), Some(winning));
assert_ok!(Auctions::bid(RuntimeOrigin::signed(3), para_3, 1, 3, 4, 29));
System::run_to_block::<AllPalletsWithSystem>(12);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::EndingPeriod(2, 0)
);
winning[SlotRange::TwoThree as u8 as usize] = Some((3, para_3, 29));
assert_eq!(Winning::<Test>::get(2), Some(winning));
});
}
#[test]
fn handle_bid_requires_registered_para() {
new_test_ext().execute_with(|| {
System::run_to_block::<AllPalletsWithSystem>(1);
assert_ok!(Auctions::new_auction(RuntimeOrigin::signed(6), 5, 1));
assert_noop!(
Auctions::bid(RuntimeOrigin::signed(1), 1337.into(), 1, 1, 4, 1),
Error::<Test>::ParaNotRegistered
);
assert_ok!(TestRegistrar::<Test>::register(
1,
1337.into(),
dummy_head_data(),
dummy_validation_code()
));
assert_ok!(Auctions::bid(RuntimeOrigin::signed(1), 1337.into(), 1, 1, 4, 1));
});
}
#[test]
fn handle_bid_checks_existing_lease_periods() {
new_test_ext().execute_with(|| {
System::run_to_block::<AllPalletsWithSystem>(1);
assert_ok!(Auctions::new_auction(RuntimeOrigin::signed(6), 5, 1));
assert_ok!(Auctions::bid(RuntimeOrigin::signed(1), 0.into(), 1, 2, 3, 1));
assert_eq!(Balances::reserved_balance(1), 1);
assert_eq!(Balances::free_balance(1), 9);
System::run_to_block::<AllPalletsWithSystem>(9);
assert_eq!(
leases(),
vec![
((0.into(), 2), LeaseData { leaser: 1, amount: 1 }),
((0.into(), 3), LeaseData { leaser: 1, amount: 1 }),
]
);
assert_eq!(TestLeaser::deposit_held(0.into(), &1), 1);
// Para 1 just won an auction above and won some lease periods.
// No bids can work which overlap these periods.
assert_ok!(Auctions::new_auction(RuntimeOrigin::signed(6), 5, 1));
assert_noop!(
Auctions::bid(RuntimeOrigin::signed(1), 0.into(), 2, 1, 4, 1),
Error::<Test>::AlreadyLeasedOut,
);
assert_noop!(
Auctions::bid(RuntimeOrigin::signed(1), 0.into(), 2, 1, 2, 1),
Error::<Test>::AlreadyLeasedOut,
);
assert_noop!(
Auctions::bid(RuntimeOrigin::signed(1), 0.into(), 2, 3, 4, 1),
Error::<Test>::AlreadyLeasedOut,
);
// This is okay, not an overlapping bid.
assert_ok!(Auctions::bid(RuntimeOrigin::signed(1), 0.into(), 2, 1, 1, 1));
});
}
// Here we will test that taking only 10 samples during the ending period works as expected.
#[test]
fn less_winning_samples_work() {
new_test_ext().execute_with(|| {
let ed: u64 = <Test as pallet_balances::Config>::ExistentialDeposit::get();
assert_eq!(ed, 1);
EndingPeriod::set(30);
SampleLength::set(10);
System::run_to_block::<AllPalletsWithSystem>(1);
assert_ok!(Auctions::new_auction(RuntimeOrigin::signed(6), 9, 11));
let para_1 = ParaId::from(1_u32);
let para_2 = ParaId::from(2_u32);
let para_3 = ParaId::from(3_u32);
// Make bids
assert_ok!(Auctions::bid(RuntimeOrigin::signed(1), para_1, 1, 11, 14, 9));
assert_ok!(Auctions::bid(RuntimeOrigin::signed(2), para_2, 1, 13, 14, 19));
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::StartingPeriod
);
let mut winning = [None; SlotRange::SLOT_RANGE_COUNT];
winning[SlotRange::ZeroThree as u8 as usize] = Some((1, para_1, 9));
winning[SlotRange::TwoThree as u8 as usize] = Some((2, para_2, 19));
assert_eq!(Winning::<Test>::get(0), Some(winning));
System::run_to_block::<AllPalletsWithSystem>(9);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::StartingPeriod
);
System::run_to_block::<AllPalletsWithSystem>(10);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::EndingPeriod(0, 0)
);
assert_eq!(Winning::<Test>::get(0), Some(winning));
// New bids update the current winning
assert_ok!(Auctions::bid(RuntimeOrigin::signed(3), para_3, 1, 14, 14, 29));
winning[SlotRange::ThreeThree as u8 as usize] = Some((3, para_3, 29));
assert_eq!(Winning::<Test>::get(0), Some(winning));
System::run_to_block::<AllPalletsWithSystem>(20);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::EndingPeriod(1, 0)
);
assert_eq!(Winning::<Test>::get(1), Some(winning));
System::run_to_block::<AllPalletsWithSystem>(25);
// Overbid mid sample
assert_ok!(Auctions::bid(RuntimeOrigin::signed(3), para_3, 1, 13, 14, 29));
winning[SlotRange::TwoThree as u8 as usize] = Some((3, para_3, 29));
assert_eq!(Winning::<Test>::get(1), Some(winning));
System::run_to_block::<AllPalletsWithSystem>(30);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::EndingPeriod(2, 0)
);
assert_eq!(Winning::<Test>::get(2), Some(winning));
set_last_random(H256::from([254; 32]), 40);
System::run_to_block::<AllPalletsWithSystem>(40);
// Auction ended and winner selected
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::NotStarted
);
assert_eq!(
leases(),
vec![
((3.into(), 13), LeaseData { leaser: 3, amount: 29 }),
((3.into(), 14), LeaseData { leaser: 3, amount: 29 }),
]
);
});
}
#[test]
fn auction_status_works() {
new_test_ext().execute_with(|| {
EndingPeriod::set(30);
SampleLength::set(10);
set_last_random(dummy_hash(), 0);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::NotStarted
);
System::run_to_block::<AllPalletsWithSystem>(1);
assert_ok!(Auctions::new_auction(RuntimeOrigin::signed(6), 9, 11));
System::run_to_block::<AllPalletsWithSystem>(9);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::StartingPeriod
);
System::run_to_block::<AllPalletsWithSystem>(10);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::EndingPeriod(0, 0)
);
System::run_to_block::<AllPalletsWithSystem>(11);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::EndingPeriod(0, 1)
);
System::run_to_block::<AllPalletsWithSystem>(19);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::EndingPeriod(0, 9)
);
System::run_to_block::<AllPalletsWithSystem>(20);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::EndingPeriod(1, 0)
);
System::run_to_block::<AllPalletsWithSystem>(25);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::EndingPeriod(1, 5)
);
System::run_to_block::<AllPalletsWithSystem>(30);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::EndingPeriod(2, 0)
);
System::run_to_block::<AllPalletsWithSystem>(39);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::EndingPeriod(2, 9)
);
System::run_to_block::<AllPalletsWithSystem>(40);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::VrfDelay(0)
);
System::run_to_block::<AllPalletsWithSystem>(44);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::VrfDelay(4)
);
set_last_random(dummy_hash(), 45);
System::run_to_block::<AllPalletsWithSystem>(45);
assert_eq!(
Auctions::auction_status(System::block_number()),
AuctionStatus::<u32>::NotStarted
);
});
}
#[test]
fn can_cancel_auction() {
new_test_ext().execute_with(|| {
System::run_to_block::<AllPalletsWithSystem>(1);
assert_ok!(Auctions::new_auction(RuntimeOrigin::signed(6), 5, 1));
assert_ok!(Auctions::bid(RuntimeOrigin::signed(1), 0.into(), 1, 1, 4, 1));
assert_eq!(Balances::reserved_balance(1), 1);
assert_eq!(Balances::free_balance(1), 9);
assert_noop!(Auctions::cancel_auction(RuntimeOrigin::signed(6)), BadOrigin);
assert_ok!(Auctions::cancel_auction(RuntimeOrigin::root()));
assert!(AuctionInfo::<Test>::get().is_none());
assert_eq!(Balances::reserved_balance(1), 0);
assert_eq!(ReservedAmounts::<Test>::iter().count(), 0);
assert_eq!(Winning::<Test>::iter().count(), 0);
});
}
@@ -0,0 +1,318 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Benchmarking for claims pallet
#[cfg(feature = "runtime-benchmarks")]
use super::*;
use crate::claims::Call;
use frame_benchmarking::v2::*;
use frame_support::{
dispatch::{DispatchInfo, GetDispatchInfo},
traits::UnfilteredDispatchable,
};
use frame_system::RawOrigin;
use secp_utils::*;
use sp_runtime::{
traits::{DispatchTransaction, ValidateUnsigned},
DispatchResult,
};
const SEED: u32 = 0;
const MAX_CLAIMS: u32 = 10_000;
const VALUE: u32 = 1_000_000;
fn create_claim<T: Config>(input: u32) -> DispatchResult {
let secret_key = libsecp256k1::SecretKey::parse(&keccak_256(&input.encode())).unwrap();
let eth_address = eth(&secret_key);
let vesting = Some((100_000u32.into(), 1_000u32.into(), 100u32.into()));
super::Pallet::<T>::mint_claim(
RawOrigin::Root.into(),
eth_address,
VALUE.into(),
vesting,
None,
)?;
Ok(())
}
fn create_claim_attest<T: Config>(input: u32) -> DispatchResult {
let secret_key = libsecp256k1::SecretKey::parse(&keccak_256(&input.encode())).unwrap();
let eth_address = eth(&secret_key);
let vesting = Some((100_000u32.into(), 1_000u32.into(), 100u32.into()));
super::Pallet::<T>::mint_claim(
RawOrigin::Root.into(),
eth_address,
VALUE.into(),
vesting,
Some(Default::default()),
)?;
Ok(())
}
#[benchmarks(
where
<T as frame_system::Config>::RuntimeCall: IsSubType<Call<T>> + From<Call<T>>,
<T as frame_system::Config>::RuntimeCall: Dispatchable<Info = DispatchInfo> + GetDispatchInfo,
<<T as frame_system::Config>::RuntimeCall as Dispatchable>::RuntimeOrigin: AsSystemOriginSigner<T::AccountId> + AsTransactionAuthorizedOrigin + Clone,
<<T as frame_system::Config>::RuntimeCall as Dispatchable>::PostInfo: Default,
)]
mod benchmarks {
use super::*;
// Benchmark `claim` including `validate_unsigned` logic.
#[benchmark]
fn claim() -> Result<(), BenchmarkError> {
let c = MAX_CLAIMS;
for _ in 0..c / 2 {
create_claim::<T>(c)?;
create_claim_attest::<T>(u32::MAX - c)?;
}
let secret_key = libsecp256k1::SecretKey::parse(&keccak_256(&c.encode())).unwrap();
let eth_address = eth(&secret_key);
let account: T::AccountId = account("user", c, SEED);
let vesting = Some((100_000u32.into(), 1_000u32.into(), 100u32.into()));
let signature = sig::<T>(&secret_key, &account.encode(), &[][..]);
super::Pallet::<T>::mint_claim(
RawOrigin::Root.into(),
eth_address,
VALUE.into(),
vesting,
None,
)?;
assert_eq!(Claims::<T>::get(eth_address), Some(VALUE.into()));
let source = sp_runtime::transaction_validity::TransactionSource::External;
let call_enc =
Call::<T>::claim { dest: account.clone(), ethereum_signature: signature.clone() }
.encode();
#[block]
{
let call = <Call<T> as Decode>::decode(&mut &*call_enc)
.expect("call is encoded above, encoding must be correct");
super::Pallet::<T>::validate_unsigned(source, &call)
.map_err(|e| -> &'static str { e.into() })?;
call.dispatch_bypass_filter(RawOrigin::None.into())?;
}
assert_eq!(Claims::<T>::get(eth_address), None);
Ok(())
}
// Benchmark `mint_claim` when there already exists `c` claims in storage.
#[benchmark]
fn mint_claim() -> Result<(), BenchmarkError> {
let c = MAX_CLAIMS;
for _ in 0..c / 2 {
create_claim::<T>(c)?;
create_claim_attest::<T>(u32::MAX - c)?;
}
let eth_address = account("eth_address", 0, SEED);
let vesting = Some((100_000u32.into(), 1_000u32.into(), 100u32.into()));
let statement = StatementKind::Regular;
#[extrinsic_call]
_(RawOrigin::Root, eth_address, VALUE.into(), vesting, Some(statement));
assert_eq!(Claims::<T>::get(eth_address), Some(VALUE.into()));
Ok(())
}
// Benchmark `claim_attest` including `validate_unsigned` logic.
#[benchmark]
fn claim_attest() -> Result<(), BenchmarkError> {
let c = MAX_CLAIMS;
for _ in 0..c / 2 {
create_claim::<T>(c)?;
create_claim_attest::<T>(u32::MAX - c)?;
}
// Crate signature
let attest_c = u32::MAX - c;
let secret_key = libsecp256k1::SecretKey::parse(&keccak_256(&attest_c.encode())).unwrap();
let eth_address = eth(&secret_key);
let account: T::AccountId = account("user", c, SEED);
let vesting = Some((100_000u32.into(), 1_000u32.into(), 100u32.into()));
let statement = StatementKind::Regular;
let signature = sig::<T>(&secret_key, &account.encode(), statement.to_text());
super::Pallet::<T>::mint_claim(
RawOrigin::Root.into(),
eth_address,
VALUE.into(),
vesting,
Some(statement),
)?;
assert_eq!(Claims::<T>::get(eth_address), Some(VALUE.into()));
let call_enc = Call::<T>::claim_attest {
dest: account.clone(),
ethereum_signature: signature.clone(),
statement: StatementKind::Regular.to_text().to_vec(),
}
.encode();
let source = sp_runtime::transaction_validity::TransactionSource::External;
#[block]
{
let call = <Call<T> as Decode>::decode(&mut &*call_enc)
.expect("call is encoded above, encoding must be correct");
super::Pallet::<T>::validate_unsigned(source, &call)
.map_err(|e| -> &'static str { e.into() })?;
call.dispatch_bypass_filter(RawOrigin::None.into())?;
}
assert_eq!(Claims::<T>::get(eth_address), None);
Ok(())
}
// Benchmark `attest` including prevalidate logic.
#[benchmark]
fn attest() -> Result<(), BenchmarkError> {
let c = MAX_CLAIMS;
for _ in 0..c / 2 {
create_claim::<T>(c)?;
create_claim_attest::<T>(u32::MAX - c)?;
}
let attest_c = u32::MAX - c;
let secret_key = libsecp256k1::SecretKey::parse(&keccak_256(&attest_c.encode())).unwrap();
let eth_address = eth(&secret_key);
let account: T::AccountId = account("user", c, SEED);
let vesting = Some((100_000u32.into(), 1_000u32.into(), 100u32.into()));
let statement = StatementKind::Regular;
super::Pallet::<T>::mint_claim(
RawOrigin::Root.into(),
eth_address,
VALUE.into(),
vesting,
Some(statement),
)?;
Preclaims::<T>::insert(&account, eth_address);
assert_eq!(Claims::<T>::get(eth_address), Some(VALUE.into()));
let stmt = StatementKind::Regular.to_text().to_vec();
#[extrinsic_call]
_(RawOrigin::Signed(account), stmt);
assert_eq!(Claims::<T>::get(eth_address), None);
Ok(())
}
#[benchmark]
fn move_claim() -> Result<(), BenchmarkError> {
let c = MAX_CLAIMS;
for _ in 0..c / 2 {
create_claim::<T>(c)?;
create_claim_attest::<T>(u32::MAX - c)?;
}
let attest_c = u32::MAX - c;
let secret_key = libsecp256k1::SecretKey::parse(&keccak_256(&attest_c.encode())).unwrap();
let eth_address = eth(&secret_key);
let new_secret_key =
libsecp256k1::SecretKey::parse(&keccak_256(&(u32::MAX / 2).encode())).unwrap();
let new_eth_address = eth(&new_secret_key);
let account: T::AccountId = account("user", c, SEED);
Preclaims::<T>::insert(&account, eth_address);
assert!(Claims::<T>::contains_key(eth_address));
assert!(!Claims::<T>::contains_key(new_eth_address));
#[extrinsic_call]
_(RawOrigin::Root, eth_address, new_eth_address, Some(account));
assert!(!Claims::<T>::contains_key(eth_address));
assert!(Claims::<T>::contains_key(new_eth_address));
Ok(())
}
// Benchmark the time it takes to do `repeat` number of keccak256 hashes
#[benchmark(extra)]
fn keccak256(i: Linear<0, 10_000>) {
let bytes = (i).encode();
#[block]
{
for _ in 0..i {
let _hash = keccak_256(&bytes);
}
}
}
// Benchmark the time it takes to do `repeat` number of `eth_recover`
#[benchmark(extra)]
fn eth_recover(i: Linear<0, 1_000>) {
// Crate signature
let secret_key = libsecp256k1::SecretKey::parse(&keccak_256(&i.encode())).unwrap();
let account: T::AccountId = account("user", i, SEED);
let signature = sig::<T>(&secret_key, &account.encode(), &[][..]);
let data = account.using_encoded(to_ascii_hex);
let extra = StatementKind::default().to_text();
#[block]
{
for _ in 0..i {
assert!(super::Pallet::<T>::eth_recover(&signature, &data, extra).is_some());
}
}
}
#[benchmark]
fn prevalidate_attests() -> Result<(), BenchmarkError> {
let c = MAX_CLAIMS;
for _ in 0..c / 2 {
create_claim::<T>(c)?;
create_claim_attest::<T>(u32::MAX - c)?;
}
let ext = PrevalidateAttests::<T>::new();
let call = super::Call::attest { statement: StatementKind::Regular.to_text().to_vec() };
let call: <T as frame_system::Config>::RuntimeCall = call.into();
let info = call.get_dispatch_info();
let attest_c = u32::MAX - c;
let secret_key = libsecp256k1::SecretKey::parse(&keccak_256(&attest_c.encode())).unwrap();
let eth_address = eth(&secret_key);
let account: T::AccountId = account("user", c, SEED);
let vesting = Some((100_000u32.into(), 1_000u32.into(), 100u32.into()));
let statement = StatementKind::Regular;
super::Pallet::<T>::mint_claim(
RawOrigin::Root.into(),
eth_address,
VALUE.into(),
vesting,
Some(statement),
)?;
Preclaims::<T>::insert(&account, eth_address);
assert_eq!(Claims::<T>::get(eth_address), Some(VALUE.into()));
#[block]
{
assert!(ext
.test_run(RawOrigin::Signed(account).into(), &call, &info, 0, 0, |_| {
Ok(Default::default())
})
.unwrap()
.is_ok());
}
Ok(())
}
impl_benchmark_test_suite!(
Pallet,
crate::claims::mock::new_test_ext(),
crate::claims::mock::Test,
);
}
+129
View File
@@ -0,0 +1,129 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Mocking utilities for testing in claims pallet.
#[cfg(test)]
use super::*;
use secp_utils::*;
// 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 crate::claims;
use frame_support::{derive_impl, ord_parameter_types, parameter_types, traits::WithdrawReasons};
use pallet_balances;
use sp_runtime::{traits::Identity, BuildStorage};
type Block = frame_system::mocking::MockBlock<Test>;
frame_support::construct_runtime!(
pub enum Test
{
System: frame_system,
Balances: pallet_balances,
Vesting: pallet_vesting,
Claims: claims,
}
);
#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
impl frame_system::Config for Test {
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
type Block = Block;
type RuntimeEvent = RuntimeEvent;
type AccountData = pallet_balances::AccountData<u64>;
type MaxConsumers = frame_support::traits::ConstU32<16>;
}
#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
impl pallet_balances::Config for Test {
type AccountStore = System;
}
parameter_types! {
pub const MinVestedTransfer: u64 = 1;
pub UnvestedFundsAllowedWithdrawReasons: WithdrawReasons =
WithdrawReasons::except(WithdrawReasons::TRANSFER | WithdrawReasons::RESERVE);
}
impl pallet_vesting::Config for Test {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type BlockNumberToBalance = Identity;
type MinVestedTransfer = MinVestedTransfer;
type WeightInfo = ();
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
type BlockNumberProvider = System;
const MAX_VESTING_SCHEDULES: u32 = 28;
}
parameter_types! {
pub Prefix: &'static [u8] = b"Pay RUSTs to the TEST account:";
}
ord_parameter_types! {
pub const Six: u64 = 6;
}
impl Config for Test {
type RuntimeEvent = RuntimeEvent;
type VestingSchedule = Vesting;
type Prefix = Prefix;
type MoveClaimOrigin = frame_system::EnsureSignedBy<Six, u64>;
type WeightInfo = TestWeightInfo;
}
pub fn alice() -> libsecp256k1::SecretKey {
libsecp256k1::SecretKey::parse(&keccak_256(b"Alice")).unwrap()
}
pub fn bob() -> libsecp256k1::SecretKey {
libsecp256k1::SecretKey::parse(&keccak_256(b"Bob")).unwrap()
}
pub fn dave() -> libsecp256k1::SecretKey {
libsecp256k1::SecretKey::parse(&keccak_256(b"Dave")).unwrap()
}
pub fn eve() -> libsecp256k1::SecretKey {
libsecp256k1::SecretKey::parse(&keccak_256(b"Eve")).unwrap()
}
pub fn frank() -> libsecp256k1::SecretKey {
libsecp256k1::SecretKey::parse(&keccak_256(b"Frank")).unwrap()
}
// This function basically just builds a genesis storage key/value store according to
// our desired mockup.
pub fn new_test_ext() -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
// We use default for brevity, but you can configure as desired if needed.
pallet_balances::GenesisConfig::<Test>::default()
.assimilate_storage(&mut t)
.unwrap();
claims::GenesisConfig::<Test> {
claims: vec![
(eth(&alice()), 100, None, None),
(eth(&dave()), 200, None, Some(StatementKind::Regular)),
(eth(&eve()), 300, Some(42), Some(StatementKind::Saft)),
(eth(&frank()), 400, Some(43), None),
],
vesting: vec![(eth(&alice()), (50, 10, 1))],
}
.assimilate_storage(&mut t)
.unwrap();
t.into()
}
pub fn total_claims() -> u64 {
100 + 200 + 300 + 400
}
+741
View File
@@ -0,0 +1,741 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Pallet to process claims from Ethereum addresses.
#[cfg(not(feature = "std"))]
use alloc::{format, string::String};
use alloc::{vec, vec::Vec};
use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
use core::fmt::Debug;
use frame_support::{
ensure,
traits::{Currency, Get, IsSubType, VestingSchedule},
weights::Weight,
DefaultNoBound,
};
pub use pallet::*;
use pezkuwi_primitives::ValidityError;
use scale_info::TypeInfo;
use serde::{self, Deserialize, Deserializer, Serialize, Serializer};
use sp_io::{crypto::secp256k1_ecdsa_recover, hashing::keccak_256};
use sp_runtime::{
impl_tx_ext_default,
traits::{
AsSystemOriginSigner, AsTransactionAuthorizedOrigin, CheckedSub, DispatchInfoOf,
Dispatchable, TransactionExtension, Zero,
},
transaction_validity::{
InvalidTransaction, TransactionSource, TransactionValidity, TransactionValidityError,
ValidTransaction,
},
RuntimeDebug,
};
type CurrencyOf<T> = <<T as Config>::VestingSchedule as VestingSchedule<
<T as frame_system::Config>::AccountId,
>>::Currency;
type BalanceOf<T> = <CurrencyOf<T> as Currency<<T as frame_system::Config>::AccountId>>::Balance;
pub trait WeightInfo {
fn claim() -> Weight;
fn mint_claim() -> Weight;
fn claim_attest() -> Weight;
fn attest() -> Weight;
fn move_claim() -> Weight;
fn prevalidate_attests() -> Weight;
}
pub struct TestWeightInfo;
impl WeightInfo for TestWeightInfo {
fn claim() -> Weight {
Weight::zero()
}
fn mint_claim() -> Weight {
Weight::zero()
}
fn claim_attest() -> Weight {
Weight::zero()
}
fn attest() -> Weight {
Weight::zero()
}
fn move_claim() -> Weight {
Weight::zero()
}
fn prevalidate_attests() -> Weight {
Weight::zero()
}
}
/// The kind of statement an account needs to make for a claim to be valid.
#[derive(
Encode,
Decode,
DecodeWithMemTracking,
Clone,
Copy,
Eq,
PartialEq,
RuntimeDebug,
TypeInfo,
Serialize,
Deserialize,
MaxEncodedLen,
)]
pub enum StatementKind {
/// Statement required to be made by non-SAFT holders.
Regular,
/// Statement required to be made by SAFT holders.
Saft,
}
impl StatementKind {
/// Convert this to the (English) statement it represents.
fn to_text(self) -> &'static [u8] {
match self {
StatementKind::Regular =>
&b"I hereby agree to the terms of the statement whose SHA-256 multihash is \
Qmc1XYqT6S39WNp2UeiRUrZichUWUPpGEThDE6dAb3f6Ny. (This may be found at the URL: \
https://statement.polkadot.network/regular.html)"[..],
StatementKind::Saft =>
&b"I hereby agree to the terms of the statement whose SHA-256 multihash is \
QmXEkMahfhHJPzT3RjkXiZVFi77ZeVeuxtAjhojGRNYckz. (This may be found at the URL: \
https://statement.polkadot.network/saft.html)"[..],
}
}
}
impl Default for StatementKind {
fn default() -> Self {
StatementKind::Regular
}
}
/// An Ethereum address (i.e. 20 bytes, used to represent an Ethereum account).
///
/// This gets serialized to the 0x-prefixed hex representation.
#[derive(
Clone,
Copy,
PartialEq,
Eq,
Encode,
Decode,
DecodeWithMemTracking,
Default,
RuntimeDebug,
TypeInfo,
MaxEncodedLen,
)]
pub struct EthereumAddress(pub [u8; 20]);
impl Serialize for EthereumAddress {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let hex: String = rustc_hex::ToHex::to_hex(&self.0[..]);
serializer.serialize_str(&format!("0x{}", hex))
}
}
impl<'de> Deserialize<'de> for EthereumAddress {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let base_string = String::deserialize(deserializer)?;
let offset = if base_string.starts_with("0x") { 2 } else { 0 };
let s = &base_string[offset..];
if s.len() != 40 {
Err(serde::de::Error::custom(
"Bad length of Ethereum address (should be 42 including '0x')",
))?;
}
let raw: Vec<u8> = rustc_hex::FromHex::from_hex(s)
.map_err(|e| serde::de::Error::custom(format!("{:?}", e)))?;
let mut r = Self::default();
r.0.copy_from_slice(&raw);
Ok(r)
}
}
impl AsRef<[u8]> for EthereumAddress {
fn as_ref(&self) -> &[u8] {
&self.0[..]
}
}
#[derive(Encode, Decode, DecodeWithMemTracking, Clone, TypeInfo, MaxEncodedLen)]
pub struct EcdsaSignature(pub [u8; 65]);
impl PartialEq for EcdsaSignature {
fn eq(&self, other: &Self) -> bool {
&self.0[..] == &other.0[..]
}
}
impl core::fmt::Debug for EcdsaSignature {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "EcdsaSignature({:?})", &self.0[..])
}
}
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
#[pallet::pallet]
pub struct Pallet<T>(_);
/// Configuration trait.
#[pallet::config]
pub trait Config: frame_system::Config {
/// The overarching event type.
#[allow(deprecated)]
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
type VestingSchedule: VestingSchedule<Self::AccountId, Moment = BlockNumberFor<Self>>;
#[pallet::constant]
type Prefix: Get<&'static [u8]>;
type MoveClaimOrigin: EnsureOrigin<Self::RuntimeOrigin>;
type WeightInfo: WeightInfo;
}
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// Someone claimed some DOTs.
Claimed { who: T::AccountId, ethereum_address: EthereumAddress, amount: BalanceOf<T> },
}
#[pallet::error]
pub enum Error<T> {
/// Invalid Ethereum signature.
InvalidEthereumSignature,
/// Ethereum address has no claim.
SignerHasNoClaim,
/// Account ID sending transaction has no claim.
SenderHasNoClaim,
/// There's not enough in the pot to pay out some unvested amount. Generally implies a
/// logic error.
PotUnderflow,
/// A needed statement was not included.
InvalidStatement,
/// The account already has a vested balance.
VestedBalanceExists,
}
#[pallet::storage]
pub type Claims<T: Config> = StorageMap<_, Identity, EthereumAddress, BalanceOf<T>>;
#[pallet::storage]
pub type Total<T: Config> = StorageValue<_, BalanceOf<T>, ValueQuery>;
/// Vesting schedule for a claim.
/// First balance is the total amount that should be held for vesting.
/// Second balance is how much should be unlocked per block.
/// The block number is when the vesting should start.
#[pallet::storage]
pub type Vesting<T: Config> =
StorageMap<_, Identity, EthereumAddress, (BalanceOf<T>, BalanceOf<T>, BlockNumberFor<T>)>;
/// The statement kind that must be signed, if any.
#[pallet::storage]
pub type Signing<T> = StorageMap<_, Identity, EthereumAddress, StatementKind>;
/// Pre-claimed Ethereum accounts, by the Account ID that they are claimed to.
#[pallet::storage]
pub type Preclaims<T: Config> = StorageMap<_, Identity, T::AccountId, EthereumAddress>;
#[pallet::genesis_config]
#[derive(DefaultNoBound)]
pub struct GenesisConfig<T: Config> {
pub claims:
Vec<(EthereumAddress, BalanceOf<T>, Option<T::AccountId>, Option<StatementKind>)>,
pub vesting: Vec<(EthereumAddress, (BalanceOf<T>, BalanceOf<T>, BlockNumberFor<T>))>,
}
#[pallet::genesis_build]
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
fn build(&self) {
// build `Claims`
self.claims.iter().map(|(a, b, _, _)| (*a, *b)).for_each(|(a, b)| {
Claims::<T>::insert(a, b);
});
// build `Total`
Total::<T>::put(
self.claims
.iter()
.fold(Zero::zero(), |acc: BalanceOf<T>, &(_, b, _, _)| acc + b),
);
// build `Vesting`
self.vesting.iter().for_each(|(k, v)| {
Vesting::<T>::insert(k, v);
});
// build `Signing`
self.claims
.iter()
.filter_map(|(a, _, _, s)| Some((*a, (*s)?)))
.for_each(|(a, s)| {
Signing::<T>::insert(a, s);
});
// build `Preclaims`
self.claims.iter().filter_map(|(a, _, i, _)| Some((i.clone()?, *a))).for_each(
|(i, a)| {
Preclaims::<T>::insert(i, a);
},
);
}
}
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
#[pallet::call]
impl<T: Config> Pallet<T> {
/// Make a claim to collect your DOTs.
///
/// The dispatch origin for this call must be _None_.
///
/// Unsigned Validation:
/// A call to claim is deemed valid if the signature provided matches
/// the expected signed message of:
///
/// > Ethereum Signed Message:
/// > (configured prefix string)(address)
///
/// and `address` matches the `dest` account.
///
/// Parameters:
/// - `dest`: The destination account to payout the claim.
/// - `ethereum_signature`: The signature of an ethereum signed message matching the format
/// described above.
///
/// <weight>
/// The weight of this call is invariant over the input parameters.
/// Weight includes logic to validate unsigned `claim` call.
///
/// Total Complexity: O(1)
/// </weight>
#[pallet::call_index(0)]
#[pallet::weight(T::WeightInfo::claim())]
pub fn claim(
origin: OriginFor<T>,
dest: T::AccountId,
ethereum_signature: EcdsaSignature,
) -> DispatchResult {
ensure_none(origin)?;
let data = dest.using_encoded(to_ascii_hex);
let signer = Self::eth_recover(&ethereum_signature, &data, &[][..])
.ok_or(Error::<T>::InvalidEthereumSignature)?;
ensure!(Signing::<T>::get(&signer).is_none(), Error::<T>::InvalidStatement);
Self::process_claim(signer, dest)?;
Ok(())
}
/// Mint a new claim to collect DOTs.
///
/// The dispatch origin for this call must be _Root_.
///
/// Parameters:
/// - `who`: The Ethereum address allowed to collect this claim.
/// - `value`: The number of DOTs that will be claimed.
/// - `vesting_schedule`: An optional vesting schedule for these DOTs.
///
/// <weight>
/// The weight of this call is invariant over the input parameters.
/// We assume worst case that both vesting and statement is being inserted.
///
/// Total Complexity: O(1)
/// </weight>
#[pallet::call_index(1)]
#[pallet::weight(T::WeightInfo::mint_claim())]
pub fn mint_claim(
origin: OriginFor<T>,
who: EthereumAddress,
value: BalanceOf<T>,
vesting_schedule: Option<(BalanceOf<T>, BalanceOf<T>, BlockNumberFor<T>)>,
statement: Option<StatementKind>,
) -> DispatchResult {
ensure_root(origin)?;
Total::<T>::mutate(|t| *t += value);
Claims::<T>::insert(who, value);
if let Some(vs) = vesting_schedule {
Vesting::<T>::insert(who, vs);
}
if let Some(s) = statement {
Signing::<T>::insert(who, s);
}
Ok(())
}
/// Make a claim to collect your DOTs by signing a statement.
///
/// The dispatch origin for this call must be _None_.
///
/// Unsigned Validation:
/// A call to `claim_attest` is deemed valid if the signature provided matches
/// the expected signed message of:
///
/// > Ethereum Signed Message:
/// > (configured prefix string)(address)(statement)
///
/// and `address` matches the `dest` account; the `statement` must match that which is
/// expected according to your purchase arrangement.
///
/// Parameters:
/// - `dest`: The destination account to payout the claim.
/// - `ethereum_signature`: The signature of an ethereum signed message matching the format
/// described above.
/// - `statement`: The identity of the statement which is being attested to in the
/// signature.
///
/// <weight>
/// The weight of this call is invariant over the input parameters.
/// Weight includes logic to validate unsigned `claim_attest` call.
///
/// Total Complexity: O(1)
/// </weight>
#[pallet::call_index(2)]
#[pallet::weight(T::WeightInfo::claim_attest())]
pub fn claim_attest(
origin: OriginFor<T>,
dest: T::AccountId,
ethereum_signature: EcdsaSignature,
statement: Vec<u8>,
) -> DispatchResult {
ensure_none(origin)?;
let data = dest.using_encoded(to_ascii_hex);
let signer = Self::eth_recover(&ethereum_signature, &data, &statement)
.ok_or(Error::<T>::InvalidEthereumSignature)?;
if let Some(s) = Signing::<T>::get(signer) {
ensure!(s.to_text() == &statement[..], Error::<T>::InvalidStatement);
}
Self::process_claim(signer, dest)?;
Ok(())
}
/// Attest to a statement, needed to finalize the claims process.
///
/// WARNING: Insecure unless your chain includes `PrevalidateAttests` as a
/// `TransactionExtension`.
///
/// Unsigned Validation:
/// A call to attest is deemed valid if the sender has a `Preclaim` registered
/// and provides a `statement` which is expected for the account.
///
/// Parameters:
/// - `statement`: The identity of the statement which is being attested to in the
/// signature.
///
/// <weight>
/// The weight of this call is invariant over the input parameters.
/// Weight includes logic to do pre-validation on `attest` call.
///
/// Total Complexity: O(1)
/// </weight>
#[pallet::call_index(3)]
#[pallet::weight((
T::WeightInfo::attest(),
DispatchClass::Normal,
Pays::No
))]
pub fn attest(origin: OriginFor<T>, statement: Vec<u8>) -> DispatchResult {
let who = ensure_signed(origin)?;
let signer = Preclaims::<T>::get(&who).ok_or(Error::<T>::SenderHasNoClaim)?;
if let Some(s) = Signing::<T>::get(signer) {
ensure!(s.to_text() == &statement[..], Error::<T>::InvalidStatement);
}
Self::process_claim(signer, who.clone())?;
Preclaims::<T>::remove(&who);
Ok(())
}
#[pallet::call_index(4)]
#[pallet::weight(T::WeightInfo::move_claim())]
pub fn move_claim(
origin: OriginFor<T>,
old: EthereumAddress,
new: EthereumAddress,
maybe_preclaim: Option<T::AccountId>,
) -> DispatchResultWithPostInfo {
T::MoveClaimOrigin::try_origin(origin).map(|_| ()).or_else(ensure_root)?;
Claims::<T>::take(&old).map(|c| Claims::<T>::insert(&new, c));
Vesting::<T>::take(&old).map(|c| Vesting::<T>::insert(&new, c));
Signing::<T>::take(&old).map(|c| Signing::<T>::insert(&new, c));
maybe_preclaim.map(|preclaim| {
Preclaims::<T>::mutate(&preclaim, |maybe_o| {
if maybe_o.as_ref().map_or(false, |o| o == &old) {
*maybe_o = Some(new)
}
})
});
Ok(Pays::No.into())
}
}
#[pallet::validate_unsigned]
impl<T: Config> ValidateUnsigned for Pallet<T> {
type Call = Call<T>;
fn validate_unsigned(_source: TransactionSource, call: &Self::Call) -> TransactionValidity {
const PRIORITY: u64 = 100;
let (maybe_signer, maybe_statement) = match call {
// <weight>
// The weight of this logic is included in the `claim` dispatchable.
// </weight>
Call::claim { dest: account, ethereum_signature } => {
let data = account.using_encoded(to_ascii_hex);
(Self::eth_recover(&ethereum_signature, &data, &[][..]), None)
},
// <weight>
// The weight of this logic is included in the `claim_attest` dispatchable.
// </weight>
Call::claim_attest { dest: account, ethereum_signature, statement } => {
let data = account.using_encoded(to_ascii_hex);
(
Self::eth_recover(&ethereum_signature, &data, &statement),
Some(statement.as_slice()),
)
},
_ => return Err(InvalidTransaction::Call.into()),
};
let signer = maybe_signer.ok_or(InvalidTransaction::Custom(
ValidityError::InvalidEthereumSignature.into(),
))?;
let e = InvalidTransaction::Custom(ValidityError::SignerHasNoClaim.into());
ensure!(Claims::<T>::contains_key(&signer), e);
let e = InvalidTransaction::Custom(ValidityError::InvalidStatement.into());
match Signing::<T>::get(signer) {
None => ensure!(maybe_statement.is_none(), e),
Some(s) => ensure!(Some(s.to_text()) == maybe_statement, e),
}
Ok(ValidTransaction {
priority: PRIORITY,
requires: vec![],
provides: vec![("claims", signer).encode()],
longevity: TransactionLongevity::max_value(),
propagate: true,
})
}
}
}
/// Converts the given binary data into ASCII-encoded hex. It will be twice the length.
fn to_ascii_hex(data: &[u8]) -> Vec<u8> {
let mut r = Vec::with_capacity(data.len() * 2);
let mut push_nibble = |n| r.push(if n < 10 { b'0' + n } else { b'a' - 10 + n });
for &b in data.iter() {
push_nibble(b / 16);
push_nibble(b % 16);
}
r
}
impl<T: Config> Pallet<T> {
// Constructs the message that Ethereum RPC's `personal_sign` and `eth_sign` would sign.
fn ethereum_signable_message(what: &[u8], extra: &[u8]) -> Vec<u8> {
let prefix = T::Prefix::get();
let mut l = prefix.len() + what.len() + extra.len();
let mut rev = Vec::new();
while l > 0 {
rev.push(b'0' + (l % 10) as u8);
l /= 10;
}
let mut v = b"\x19Ethereum Signed Message:\n".to_vec();
v.extend(rev.into_iter().rev());
v.extend_from_slice(prefix);
v.extend_from_slice(what);
v.extend_from_slice(extra);
v
}
// Attempts to recover the Ethereum address from a message signature signed by using
// the Ethereum RPC's `personal_sign` and `eth_sign`.
fn eth_recover(s: &EcdsaSignature, what: &[u8], extra: &[u8]) -> Option<EthereumAddress> {
let msg = keccak_256(&Self::ethereum_signable_message(what, extra));
let mut res = EthereumAddress::default();
res.0
.copy_from_slice(&keccak_256(&secp256k1_ecdsa_recover(&s.0, &msg).ok()?[..])[12..]);
Some(res)
}
fn process_claim(signer: EthereumAddress, dest: T::AccountId) -> sp_runtime::DispatchResult {
let balance_due = Claims::<T>::get(&signer).ok_or(Error::<T>::SignerHasNoClaim)?;
let new_total =
Total::<T>::get().checked_sub(&balance_due).ok_or(Error::<T>::PotUnderflow)?;
let vesting = Vesting::<T>::get(&signer);
if vesting.is_some() && T::VestingSchedule::vesting_balance(&dest).is_some() {
return Err(Error::<T>::VestedBalanceExists.into());
}
// We first need to deposit the balance to ensure that the account exists.
let _ = CurrencyOf::<T>::deposit_creating(&dest, balance_due);
// Check if this claim should have a vesting schedule.
if let Some(vs) = vesting {
// This can only fail if the account already has a vesting schedule,
// but this is checked above.
T::VestingSchedule::add_vesting_schedule(&dest, vs.0, vs.1, vs.2)
.expect("No other vesting schedule exists, as checked above; qed");
}
Total::<T>::put(new_total);
Claims::<T>::remove(&signer);
Vesting::<T>::remove(&signer);
Signing::<T>::remove(&signer);
// Let's deposit an event to let the outside world know this happened.
Self::deposit_event(Event::<T>::Claimed {
who: dest,
ethereum_address: signer,
amount: balance_due,
});
Ok(())
}
}
/// Validate `attest` calls prior to execution. Needed to avoid a DoS attack since they are
/// otherwise free to place on chain.
#[derive(Encode, Decode, DecodeWithMemTracking, Clone, Eq, PartialEq, TypeInfo)]
#[scale_info(skip_type_params(T))]
pub struct PrevalidateAttests<T>(core::marker::PhantomData<fn(T)>);
impl<T: Config> Debug for PrevalidateAttests<T>
where
<T as frame_system::Config>::RuntimeCall: IsSubType<Call<T>>,
{
#[cfg(feature = "std")]
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "PrevalidateAttests")
}
#[cfg(not(feature = "std"))]
fn fmt(&self, _: &mut core::fmt::Formatter) -> core::fmt::Result {
Ok(())
}
}
impl<T: Config> PrevalidateAttests<T>
where
<T as frame_system::Config>::RuntimeCall: IsSubType<Call<T>>,
{
/// Create new `TransactionExtension` to check runtime version.
pub fn new() -> Self {
Self(core::marker::PhantomData)
}
}
impl<T: Config> TransactionExtension<T::RuntimeCall> for PrevalidateAttests<T>
where
<T as frame_system::Config>::RuntimeCall: IsSubType<Call<T>>,
<<T as frame_system::Config>::RuntimeCall as Dispatchable>::RuntimeOrigin:
AsSystemOriginSigner<T::AccountId> + AsTransactionAuthorizedOrigin + Clone,
{
const IDENTIFIER: &'static str = "PrevalidateAttests";
type Implicit = ();
type Pre = ();
type Val = ();
fn weight(&self, call: &T::RuntimeCall) -> Weight {
if let Some(Call::attest { .. }) = call.is_sub_type() {
T::WeightInfo::prevalidate_attests()
} else {
Weight::zero()
}
}
fn validate(
&self,
origin: <T::RuntimeCall as Dispatchable>::RuntimeOrigin,
call: &T::RuntimeCall,
_info: &DispatchInfoOf<T::RuntimeCall>,
_len: usize,
_self_implicit: Self::Implicit,
_inherited_implication: &impl Encode,
_source: TransactionSource,
) -> Result<
(ValidTransaction, Self::Val, <T::RuntimeCall as Dispatchable>::RuntimeOrigin),
TransactionValidityError,
> {
if let Some(Call::attest { statement: attested_statement }) = call.is_sub_type() {
let who = origin.as_system_origin_signer().ok_or(InvalidTransaction::BadSigner)?;
let signer = Preclaims::<T>::get(who)
.ok_or(InvalidTransaction::Custom(ValidityError::SignerHasNoClaim.into()))?;
if let Some(s) = Signing::<T>::get(signer) {
let e = InvalidTransaction::Custom(ValidityError::InvalidStatement.into());
ensure!(&attested_statement[..] == s.to_text(), e);
}
}
Ok((ValidTransaction::default(), (), origin))
}
impl_tx_ext_default!(T::RuntimeCall; prepare);
}
#[cfg(any(test, feature = "runtime-benchmarks"))]
mod secp_utils {
use super::*;
pub fn public(secret: &libsecp256k1::SecretKey) -> libsecp256k1::PublicKey {
libsecp256k1::PublicKey::from_secret_key(secret)
}
pub fn eth(secret: &libsecp256k1::SecretKey) -> EthereumAddress {
let mut res = EthereumAddress::default();
res.0.copy_from_slice(&keccak_256(&public(secret).serialize()[1..65])[12..]);
res
}
pub fn sig<T: Config>(
secret: &libsecp256k1::SecretKey,
what: &[u8],
extra: &[u8],
) -> EcdsaSignature {
let msg = keccak_256(&super::Pallet::<T>::ethereum_signable_message(
&to_ascii_hex(what)[..],
extra,
));
let (sig, recovery_id) = libsecp256k1::sign(&libsecp256k1::Message::parse(&msg), secret);
let mut r = [0u8; 65];
r[0..64].copy_from_slice(&sig.serialize()[..]);
r[64] = recovery_id.serialize();
EcdsaSignature(r)
}
}
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
+666
View File
@@ -0,0 +1,666 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Tests for the claims pallet.
#[cfg(test)]
use super::*;
use crate::{claims, claims::mock::*};
use claims::Call as ClaimsCall;
use hex_literal::hex;
use secp_utils::*;
use sp_runtime::transaction_validity::TransactionSource::External;
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 frame_support::{
assert_err, assert_noop, assert_ok,
dispatch::{GetDispatchInfo, Pays},
traits::ExistenceRequirement,
};
use sp_runtime::{
traits::DispatchTransaction, transaction_validity::TransactionLongevity,
DispatchError::BadOrigin, TokenError,
};
#[test]
fn basic_setup_works() {
new_test_ext().execute_with(|| {
assert_eq!(claims::Total::<Test>::get(), total_claims());
assert_eq!(claims::Claims::<Test>::get(&eth(&alice())), Some(100));
assert_eq!(claims::Claims::<Test>::get(&eth(&dave())), Some(200));
assert_eq!(claims::Claims::<Test>::get(&eth(&eve())), Some(300));
assert_eq!(claims::Claims::<Test>::get(&eth(&frank())), Some(400));
assert_eq!(claims::Claims::<Test>::get(&EthereumAddress::default()), None);
assert_eq!(claims::Vesting::<Test>::get(&eth(&alice())), Some((50, 10, 1)));
});
}
#[test]
fn serde_works() {
let x = EthereumAddress(hex!["0123456789abcdef0123456789abcdef01234567"]);
let y = serde_json::to_string(&x).unwrap();
assert_eq!(y, "\"0x0123456789abcdef0123456789abcdef01234567\"");
let z: EthereumAddress = serde_json::from_str(&y).unwrap();
assert_eq!(x, z);
}
#[test]
fn claiming_works() {
new_test_ext().execute_with(|| {
assert_eq!(Balances::free_balance(42), 0);
assert_ok!(claims::mock::Claims::claim(
RuntimeOrigin::none(),
42,
sig::<Test>(&alice(), &42u64.encode(), &[][..])
));
assert_eq!(Balances::free_balance(&42), 100);
assert_eq!(claims::mock::Vesting::vesting_balance(&42), Some(50));
assert_eq!(claims::Total::<Test>::get(), total_claims() - 100);
});
}
#[test]
fn basic_claim_moving_works() {
new_test_ext().execute_with(|| {
assert_eq!(Balances::free_balance(42), 0);
assert_noop!(
claims::mock::Claims::move_claim(
RuntimeOrigin::signed(1),
eth(&alice()),
eth(&bob()),
None
),
BadOrigin
);
assert_ok!(claims::mock::Claims::move_claim(
RuntimeOrigin::signed(6),
eth(&alice()),
eth(&bob()),
None
));
assert_noop!(
claims::mock::Claims::claim(
RuntimeOrigin::none(),
42,
sig::<Test>(&alice(), &42u64.encode(), &[][..])
),
Error::<Test>::SignerHasNoClaim
);
assert_ok!(claims::mock::Claims::claim(
RuntimeOrigin::none(),
42,
sig::<Test>(&bob(), &42u64.encode(), &[][..])
));
assert_eq!(Balances::free_balance(&42), 100);
assert_eq!(claims::mock::Vesting::vesting_balance(&42), Some(50));
assert_eq!(claims::Total::<Test>::get(), total_claims() - 100);
});
}
#[test]
fn claim_attest_moving_works() {
new_test_ext().execute_with(|| {
assert_ok!(claims::mock::Claims::move_claim(
RuntimeOrigin::signed(6),
eth(&dave()),
eth(&bob()),
None
));
let s = sig::<Test>(&bob(), &42u64.encode(), StatementKind::Regular.to_text());
assert_ok!(claims::mock::Claims::claim_attest(
RuntimeOrigin::none(),
42,
s,
StatementKind::Regular.to_text().to_vec()
));
assert_eq!(Balances::free_balance(&42), 200);
});
}
#[test]
fn attest_moving_works() {
new_test_ext().execute_with(|| {
assert_ok!(claims::mock::Claims::move_claim(
RuntimeOrigin::signed(6),
eth(&eve()),
eth(&bob()),
Some(42)
));
assert_ok!(claims::mock::Claims::attest(
RuntimeOrigin::signed(42),
StatementKind::Saft.to_text().to_vec()
));
assert_eq!(Balances::free_balance(&42), 300);
});
}
#[test]
fn claiming_does_not_bypass_signing() {
new_test_ext().execute_with(|| {
assert_ok!(claims::mock::Claims::claim(
RuntimeOrigin::none(),
42,
sig::<Test>(&alice(), &42u64.encode(), &[][..])
));
assert_noop!(
claims::mock::Claims::claim(
RuntimeOrigin::none(),
42,
sig::<Test>(&dave(), &42u64.encode(), &[][..])
),
Error::<Test>::InvalidStatement,
);
assert_noop!(
claims::mock::Claims::claim(
RuntimeOrigin::none(),
42,
sig::<Test>(&eve(), &42u64.encode(), &[][..])
),
Error::<Test>::InvalidStatement,
);
assert_ok!(claims::mock::Claims::claim(
RuntimeOrigin::none(),
42,
sig::<Test>(&frank(), &42u64.encode(), &[][..])
));
});
}
#[test]
fn attest_claiming_works() {
new_test_ext().execute_with(|| {
assert_eq!(Balances::free_balance(42), 0);
let s = sig::<Test>(&dave(), &42u64.encode(), StatementKind::Saft.to_text());
let r = claims::mock::Claims::claim_attest(
RuntimeOrigin::none(),
42,
s.clone(),
StatementKind::Saft.to_text().to_vec(),
);
assert_noop!(r, Error::<Test>::InvalidStatement);
let r = claims::mock::Claims::claim_attest(
RuntimeOrigin::none(),
42,
s,
StatementKind::Regular.to_text().to_vec(),
);
assert_noop!(r, Error::<Test>::SignerHasNoClaim);
// ^^^ we use ecdsa_recover, so an invalid signature just results in a random signer id
// being recovered, which realistically will never have a claim.
let s = sig::<Test>(&dave(), &42u64.encode(), StatementKind::Regular.to_text());
assert_ok!(claims::mock::Claims::claim_attest(
RuntimeOrigin::none(),
42,
s,
StatementKind::Regular.to_text().to_vec()
));
assert_eq!(Balances::free_balance(&42), 200);
assert_eq!(claims::Total::<Test>::get(), total_claims() - 200);
let s = sig::<Test>(&dave(), &42u64.encode(), StatementKind::Regular.to_text());
let r = claims::mock::Claims::claim_attest(
RuntimeOrigin::none(),
42,
s,
StatementKind::Regular.to_text().to_vec(),
);
assert_noop!(r, Error::<Test>::SignerHasNoClaim);
});
}
#[test]
fn attesting_works() {
new_test_ext().execute_with(|| {
assert_eq!(Balances::free_balance(42), 0);
assert_noop!(
claims::mock::Claims::attest(
RuntimeOrigin::signed(69),
StatementKind::Saft.to_text().to_vec()
),
Error::<Test>::SenderHasNoClaim
);
assert_noop!(
claims::mock::Claims::attest(
RuntimeOrigin::signed(42),
StatementKind::Regular.to_text().to_vec()
),
Error::<Test>::InvalidStatement
);
assert_ok!(claims::mock::Claims::attest(
RuntimeOrigin::signed(42),
StatementKind::Saft.to_text().to_vec()
));
assert_eq!(Balances::free_balance(&42), 300);
assert_eq!(claims::Total::<Test>::get(), total_claims() - 300);
});
}
#[test]
fn claim_cannot_clobber_preclaim() {
new_test_ext().execute_with(|| {
assert_eq!(Balances::free_balance(42), 0);
// Alice's claim is 100
assert_ok!(claims::mock::Claims::claim(
RuntimeOrigin::none(),
42,
sig::<Test>(&alice(), &42u64.encode(), &[][..])
));
assert_eq!(Balances::free_balance(&42), 100);
// Eve's claim is 300 through Account 42
assert_ok!(claims::mock::Claims::attest(
RuntimeOrigin::signed(42),
StatementKind::Saft.to_text().to_vec()
));
assert_eq!(Balances::free_balance(&42), 100 + 300);
assert_eq!(claims::Total::<Test>::get(), total_claims() - 400);
});
}
#[test]
fn valid_attest_transactions_are_free() {
new_test_ext().execute_with(|| {
let p = PrevalidateAttests::<Test>::new();
let c = claims::mock::RuntimeCall::Claims(ClaimsCall::attest {
statement: StatementKind::Saft.to_text().to_vec(),
});
let di = c.get_dispatch_info();
assert_eq!(di.pays_fee, Pays::No);
let r = p.validate_only(Some(42).into(), &c, &di, 20, External, 0);
assert_eq!(r.unwrap().0, ValidTransaction::default());
});
}
#[test]
fn invalid_attest_transactions_are_recognized() {
new_test_ext().execute_with(|| {
let p = PrevalidateAttests::<Test>::new();
let c = claims::mock::RuntimeCall::Claims(ClaimsCall::attest {
statement: StatementKind::Regular.to_text().to_vec(),
});
let di = c.get_dispatch_info();
let r = p.validate_only(Some(42).into(), &c, &di, 20, External, 0);
assert!(r.is_err());
let c = claims::mock::RuntimeCall::Claims(ClaimsCall::attest {
statement: StatementKind::Saft.to_text().to_vec(),
});
let di = c.get_dispatch_info();
let r = p.validate_only(Some(69).into(), &c, &di, 20, External, 0);
assert!(r.is_err());
});
}
#[test]
fn cannot_bypass_attest_claiming() {
new_test_ext().execute_with(|| {
assert_eq!(Balances::free_balance(42), 0);
let s = sig::<Test>(&dave(), &42u64.encode(), &[]);
let r = claims::mock::Claims::claim(RuntimeOrigin::none(), 42, s.clone());
assert_noop!(r, Error::<Test>::InvalidStatement);
});
}
#[test]
fn add_claim_works() {
new_test_ext().execute_with(|| {
assert_noop!(
claims::mock::Claims::mint_claim(
RuntimeOrigin::signed(42),
eth(&bob()),
200,
None,
None
),
sp_runtime::traits::BadOrigin,
);
assert_eq!(Balances::free_balance(42), 0);
assert_noop!(
claims::mock::Claims::claim(
RuntimeOrigin::none(),
69,
sig::<Test>(&bob(), &69u64.encode(), &[][..])
),
Error::<Test>::SignerHasNoClaim,
);
assert_ok!(claims::mock::Claims::mint_claim(
RuntimeOrigin::root(),
eth(&bob()),
200,
None,
None
));
assert_eq!(claims::Total::<Test>::get(), total_claims() + 200);
assert_ok!(claims::mock::Claims::claim(
RuntimeOrigin::none(),
69,
sig::<Test>(&bob(), &69u64.encode(), &[][..])
));
assert_eq!(Balances::free_balance(&69), 200);
assert_eq!(claims::mock::Vesting::vesting_balance(&69), None);
assert_eq!(claims::Total::<Test>::get(), total_claims());
});
}
#[test]
fn add_claim_with_vesting_works() {
new_test_ext().execute_with(|| {
assert_noop!(
claims::mock::Claims::mint_claim(
RuntimeOrigin::signed(42),
eth(&bob()),
200,
Some((50, 10, 1)),
None
),
sp_runtime::traits::BadOrigin,
);
assert_eq!(Balances::free_balance(42), 0);
assert_noop!(
claims::mock::Claims::claim(
RuntimeOrigin::none(),
69,
sig::<Test>(&bob(), &69u64.encode(), &[][..])
),
Error::<Test>::SignerHasNoClaim,
);
assert_ok!(claims::mock::Claims::mint_claim(
RuntimeOrigin::root(),
eth(&bob()),
200,
Some((50, 10, 1)),
None
));
assert_ok!(claims::mock::Claims::claim(
RuntimeOrigin::none(),
69,
sig::<Test>(&bob(), &69u64.encode(), &[][..])
));
assert_eq!(Balances::free_balance(&69), 200);
assert_eq!(claims::mock::Vesting::vesting_balance(&69), Some(50));
// Make sure we can not transfer the vested balance.
assert_err!(
<Balances as Currency<_>>::transfer(&69, &80, 180, ExistenceRequirement::AllowDeath),
TokenError::Frozen,
);
});
}
#[test]
fn add_claim_with_statement_works() {
new_test_ext().execute_with(|| {
assert_noop!(
claims::mock::Claims::mint_claim(
RuntimeOrigin::signed(42),
eth(&bob()),
200,
None,
Some(StatementKind::Regular)
),
sp_runtime::traits::BadOrigin,
);
assert_eq!(Balances::free_balance(42), 0);
let signature = sig::<Test>(&bob(), &69u64.encode(), StatementKind::Regular.to_text());
assert_noop!(
claims::mock::Claims::claim_attest(
RuntimeOrigin::none(),
69,
signature.clone(),
StatementKind::Regular.to_text().to_vec()
),
Error::<Test>::SignerHasNoClaim
);
assert_ok!(claims::mock::Claims::mint_claim(
RuntimeOrigin::root(),
eth(&bob()),
200,
None,
Some(StatementKind::Regular)
));
assert_noop!(
claims::mock::Claims::claim_attest(
RuntimeOrigin::none(),
69,
signature.clone(),
vec![],
),
Error::<Test>::SignerHasNoClaim
);
assert_ok!(claims::mock::Claims::claim_attest(
RuntimeOrigin::none(),
69,
signature.clone(),
StatementKind::Regular.to_text().to_vec()
));
assert_eq!(Balances::free_balance(&69), 200);
});
}
#[test]
fn origin_signed_claiming_fail() {
new_test_ext().execute_with(|| {
assert_eq!(Balances::free_balance(42), 0);
assert_err!(
claims::mock::Claims::claim(
RuntimeOrigin::signed(42),
42,
sig::<Test>(&alice(), &42u64.encode(), &[][..])
),
sp_runtime::traits::BadOrigin,
);
});
}
#[test]
fn double_claiming_doesnt_work() {
new_test_ext().execute_with(|| {
assert_eq!(Balances::free_balance(42), 0);
assert_ok!(claims::mock::Claims::claim(
RuntimeOrigin::none(),
42,
sig::<Test>(&alice(), &42u64.encode(), &[][..])
));
assert_noop!(
claims::mock::Claims::claim(
RuntimeOrigin::none(),
42,
sig::<Test>(&alice(), &42u64.encode(), &[][..])
),
Error::<Test>::SignerHasNoClaim
);
});
}
#[test]
fn claiming_while_vested_doesnt_work() {
new_test_ext().execute_with(|| {
CurrencyOf::<Test>::make_free_balance_be(&69, total_claims());
assert_eq!(Balances::free_balance(69), total_claims());
// A user is already vested
assert_ok!(<Test as Config>::VestingSchedule::add_vesting_schedule(
&69,
total_claims(),
100,
10
));
assert_ok!(claims::mock::Claims::mint_claim(
RuntimeOrigin::root(),
eth(&bob()),
200,
Some((50, 10, 1)),
None
));
// New total
assert_eq!(claims::Total::<Test>::get(), total_claims() + 200);
// They should not be able to claim
assert_noop!(
claims::mock::Claims::claim(
RuntimeOrigin::none(),
69,
sig::<Test>(&bob(), &69u64.encode(), &[][..])
),
Error::<Test>::VestedBalanceExists,
);
});
}
#[test]
fn non_sender_sig_doesnt_work() {
new_test_ext().execute_with(|| {
assert_eq!(Balances::free_balance(42), 0);
assert_noop!(
claims::mock::Claims::claim(
RuntimeOrigin::none(),
42,
sig::<Test>(&alice(), &69u64.encode(), &[][..])
),
Error::<Test>::SignerHasNoClaim
);
});
}
#[test]
fn non_claimant_doesnt_work() {
new_test_ext().execute_with(|| {
assert_eq!(Balances::free_balance(42), 0);
assert_noop!(
claims::mock::Claims::claim(
RuntimeOrigin::none(),
42,
sig::<Test>(&bob(), &69u64.encode(), &[][..])
),
Error::<Test>::SignerHasNoClaim
);
});
}
#[test]
fn real_eth_sig_works() {
new_test_ext().execute_with(|| {
// "Pay RUSTs to the TEST account:2a00000000000000"
let sig = hex!["444023e89b67e67c0562ed0305d252a5dd12b2af5ac51d6d3cb69a0b486bc4b3191401802dc29d26d586221f7256cd3329fe82174bdf659baea149a40e1c495d1c"];
let sig = EcdsaSignature(sig);
let who = 42u64.using_encoded(to_ascii_hex);
let signer = claims::mock::Claims::eth_recover(&sig, &who, &[][..]).unwrap();
assert_eq!(signer.0, hex!["6d31165d5d932d571f3b44695653b46dcc327e84"]);
});
}
#[test]
fn validate_unsigned_works() {
use sp_runtime::traits::ValidateUnsigned;
let source = sp_runtime::transaction_validity::TransactionSource::External;
new_test_ext().execute_with(|| {
assert_eq!(
Pallet::<Test>::validate_unsigned(
source,
&ClaimsCall::claim {
dest: 1,
ethereum_signature: sig::<Test>(&alice(), &1u64.encode(), &[][..])
}
),
Ok(ValidTransaction {
priority: 100,
requires: vec![],
provides: vec![("claims", eth(&alice())).encode()],
longevity: TransactionLongevity::max_value(),
propagate: true,
})
);
assert_eq!(
Pallet::<Test>::validate_unsigned(
source,
&ClaimsCall::claim { dest: 0, ethereum_signature: EcdsaSignature([0; 65]) }
),
InvalidTransaction::Custom(ValidityError::InvalidEthereumSignature.into()).into(),
);
assert_eq!(
Pallet::<Test>::validate_unsigned(
source,
&ClaimsCall::claim {
dest: 1,
ethereum_signature: sig::<Test>(&bob(), &1u64.encode(), &[][..])
}
),
InvalidTransaction::Custom(ValidityError::SignerHasNoClaim.into()).into(),
);
let s = sig::<Test>(&dave(), &1u64.encode(), StatementKind::Regular.to_text());
let call = ClaimsCall::claim_attest {
dest: 1,
ethereum_signature: s,
statement: StatementKind::Regular.to_text().to_vec(),
};
assert_eq!(
Pallet::<Test>::validate_unsigned(source, &call),
Ok(ValidTransaction {
priority: 100,
requires: vec![],
provides: vec![("claims", eth(&dave())).encode()],
longevity: TransactionLongevity::max_value(),
propagate: true,
})
);
assert_eq!(
Pallet::<Test>::validate_unsigned(
source,
&ClaimsCall::claim_attest {
dest: 1,
ethereum_signature: EcdsaSignature([0; 65]),
statement: StatementKind::Regular.to_text().to_vec()
}
),
InvalidTransaction::Custom(ValidityError::InvalidEthereumSignature.into()).into(),
);
let s = sig::<Test>(&bob(), &1u64.encode(), StatementKind::Regular.to_text());
let call = ClaimsCall::claim_attest {
dest: 1,
ethereum_signature: s,
statement: StatementKind::Regular.to_text().to_vec(),
};
assert_eq!(
Pallet::<Test>::validate_unsigned(source, &call),
InvalidTransaction::Custom(ValidityError::SignerHasNoClaim.into()).into(),
);
let s = sig::<Test>(&dave(), &1u64.encode(), StatementKind::Saft.to_text());
let call = ClaimsCall::claim_attest {
dest: 1,
ethereum_signature: s,
statement: StatementKind::Regular.to_text().to_vec(),
};
assert_eq!(
Pallet::<Test>::validate_unsigned(source, &call),
InvalidTransaction::Custom(ValidityError::SignerHasNoClaim.into()).into(),
);
let s = sig::<Test>(&dave(), &1u64.encode(), StatementKind::Saft.to_text());
let call = ClaimsCall::claim_attest {
dest: 1,
ethereum_signature: s,
statement: StatementKind::Saft.to_text().to_vec(),
};
assert_eq!(
Pallet::<Test>::validate_unsigned(source, &call),
InvalidTransaction::Custom(ValidityError::InvalidStatement.into()).into(),
);
});
}
@@ -0,0 +1,227 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
use super::*;
use frame_support::{
storage_alias,
traits::{GetStorageVersion, OnRuntimeUpgrade, StorageVersion},
Twox64Concat,
};
pub struct MigrateToTrackInactiveV2<T>(core::marker::PhantomData<T>);
impl<T: Config> OnRuntimeUpgrade for MigrateToTrackInactiveV2<T> {
fn on_runtime_upgrade() -> Weight {
let on_chain_version = Pallet::<T>::on_chain_storage_version();
if on_chain_version == 1 {
let mut translated = 0u64;
for item in Funds::<T>::iter_values() {
let b =
CurrencyOf::<T>::total_balance(&Pallet::<T>::fund_account_id(item.fund_index));
CurrencyOf::<T>::deactivate(b);
translated.saturating_inc();
}
StorageVersion::new(2).put::<Pallet<T>>();
log::info!(target: "runtime::crowdloan", "Summed {} funds, storage to version 1", translated);
T::DbWeight::get().reads_writes(translated * 2 + 1, translated * 2 + 1)
} else {
log::info!(target: "runtime::crowdloan", "Migration did not execute. This probably should be removed");
T::DbWeight::get().reads(1)
}
}
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> {
let total = Funds::<T>::iter_values()
.map(|item| {
CurrencyOf::<T>::total_balance(&Pallet::<T>::fund_account_id(item.fund_index))
})
.fold(BalanceOf::<T>::zero(), |a, i| a.saturating_add(i));
Ok((total, CurrencyOf::<T>::active_issuance()).encode())
}
#[cfg(feature = "try-runtime")]
fn post_upgrade(total: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
if let Ok((total, active)) = <(BalanceOf<T>, BalanceOf<T>)>::decode(&mut total.as_slice()) {
ensure!(active - total == CurrencyOf::<T>::active_issuance(), "the total be correct");
Ok(())
} else {
Err("the state parameter should be something that was generated by pre_upgrade".into())
}
}
}
/// Migrations for using fund index to create fund accounts instead of para ID.
pub mod crowdloan_index_migration {
use super::*;
#[storage_alias]
type NextTrieIndex<T: Config> = StorageValue<Pallet<T>, FundIndex>;
#[storage_alias]
type Leases<T: Config> = StorageMap<
Slots,
Twox64Concat,
ParaId,
Vec<Option<(<T as frame_system::Config>::AccountId, BalanceOf<T>)>>,
>;
// The old way we generated fund accounts.
fn old_fund_account_id<T: Config>(index: ParaId) -> T::AccountId {
T::PalletId::get().into_sub_account_truncating(index)
}
pub fn pre_migrate<T: Config>() -> Result<(), &'static str> {
// `NextTrieIndex` should have a value.
let next_index = NextTrieIndex::<T>::get().unwrap_or_default();
ensure!(next_index > 0, "Next index is zero, which implies no migration is needed.");
log::info!(
target: "runtime",
"next trie index: {:?}",
next_index,
);
for (para_id, fund) in Funds::<T>::iter() {
let old_fund_account = old_fund_account_id::<T>(para_id);
let total_balance = CurrencyOf::<T>::total_balance(&old_fund_account);
log::info!(
target: "runtime",
"para_id={:?}, old_fund_account={:?}, total_balance={:?}, fund.raised={:?}",
para_id, old_fund_account, total_balance, fund.raised
);
// Each fund should have some non-zero balance.
ensure!(
total_balance >= fund.raised,
"Total balance is not equal to the funds raised."
);
let leases = Leases::<T>::get(para_id).unwrap_or_default();
let mut found_lease_deposit = false;
for (who, _amount) in leases.iter().flatten() {
if *who == old_fund_account {
found_lease_deposit = true;
break;
}
}
if found_lease_deposit {
log::info!(
target: "runtime",
"para_id={:?}, old_fund_account={:?}, leases={:?}",
para_id, old_fund_account, leases,
);
}
}
Ok(())
}
/// This migration converts crowdloans to use a crowdloan index rather than the teyrchain id as
/// a unique identifier. This makes it easier to swap two crowdloans between teyrchains.
pub fn migrate<T: Config>() -> frame_support::weights::Weight {
let mut weight = Weight::zero();
// First migrate `NextTrieIndex` counter to `NextFundIndex`.
let next_index = NextTrieIndex::<T>::take().unwrap_or_default();
NextFundIndex::<T>::set(next_index);
weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 2));
// Migrate all accounts from `old_fund_account` to `fund_account` using `fund_index`.
for (para_id, fund) in Funds::<T>::iter() {
let old_fund_account = old_fund_account_id::<T>(para_id);
let new_fund_account = Pallet::<T>::fund_account_id(fund.fund_index);
// Funds should only have a free balance and a reserve balance. Both of these are in the
// `Account` storage item, so we just swap them.
let account_info = frame_system::Account::<T>::take(&old_fund_account);
frame_system::Account::<T>::insert(&new_fund_account, account_info);
weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 2));
let mut leases = Leases::<T>::get(para_id).unwrap_or_default();
for (who, _amount) in leases.iter_mut().flatten() {
if *who == old_fund_account {
*who = new_fund_account.clone();
}
}
Leases::<T>::insert(para_id, leases);
}
weight
}
pub fn post_migrate<T: Config>() -> Result<(), &'static str> {
// `NextTrieIndex` should not have a value, and `NextFundIndex` should.
ensure!(NextTrieIndex::<T>::get().is_none(), "NextTrieIndex still has a value.");
let next_index = NextFundIndex::<T>::get();
log::info!(
target: "runtime",
"next fund index: {:?}",
next_index,
);
ensure!(
next_index > 0,
"NextFundIndex was not migrated or is zero. We assume it cannot be zero else no migration is needed."
);
// Each fund should have balance migrated correctly.
for (para_id, fund) in Funds::<T>::iter() {
// Old fund account is deleted.
let old_fund_account = old_fund_account_id::<T>(para_id);
ensure!(
frame_system::Account::<T>::get(&old_fund_account) == Default::default(),
"Old account wasn't reset to default value."
);
// New fund account has the correct balance.
let new_fund_account = Pallet::<T>::fund_account_id(fund.fund_index);
let total_balance = CurrencyOf::<T>::total_balance(&new_fund_account);
ensure!(
total_balance >= fund.raised,
"Total balance in new account is different than the funds raised."
);
let leases = Leases::<T>::get(para_id).unwrap_or_default();
let mut new_account_found = false;
for (who, _amount) in leases.iter().flatten() {
if *who == old_fund_account {
panic!("Old fund account found after migration!");
} else if *who == new_fund_account {
new_account_found = true;
}
}
if new_account_found {
log::info!(
target: "runtime::crowdloan",
"para_id={:?}, new_fund_account={:?}, leases={:?}",
para_id, new_fund_account, leases,
);
}
}
Ok(())
}
}
File diff suppressed because it is too large Load Diff
+63
View File
@@ -0,0 +1,63 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Code for elections.
/// Implements the weight types for the elections module and a specific
/// runtime.
/// This macro should not be called directly; use
/// [`impl_runtime_weights`](crate::impl_runtime_weights!) instead.
#[macro_export]
macro_rules! impl_elections_weights {
($runtime:ident) => {
parameter_types! {
/// A limit for off-chain phragmen unsigned solution submission.
///
/// We want to keep it as high as possible, but can't risk having it reject,
/// so we always subtract the base block execution weight.
pub OffchainSolutionWeightLimit: Weight = BlockWeights::get()
.get(DispatchClass::Normal)
.max_extrinsic
.expect("Normal extrinsics have weight limit configured by default; qed")
.saturating_sub($runtime::weights::BlockExecutionWeight::get());
/// A limit for off-chain phragmen unsigned solution length.
///
/// We allow up to 90% of the block's size to be consumed by the solution.
pub OffchainSolutionLengthLimit: u32 = Perbill::from_rational(90_u32, 100) *
*BlockLength::get()
.max
.get(DispatchClass::Normal);
}
};
}
/// The numbers configured here could always be more than the the maximum limits of staking pallet
/// to ensure election snapshot will not run out of memory. For now, we set them to smaller values
/// since the staking is bounded and the weight pipeline takes hours for this single pallet.
pub struct BenchmarkConfig;
impl pallet_election_provider_multi_phase::BenchmarkingConfig for BenchmarkConfig {
const VOTERS: [u32; 2] = [1000, 2000];
const TARGETS: [u32; 2] = [500, 1000];
const ACTIVE_VOTERS: [u32; 2] = [500, 800];
const DESIRED_TARGETS: [u32; 2] = [200, 400];
const SNAPSHOT_MAXIMUM_VOTERS: u32 = 1000;
const MINER_MAXIMUM_VOTERS: u32 = 1000;
const MAXIMUM_TARGETS: u32 = 300;
}
/// The accuracy type used for genesis election provider;
pub type OnChainAccuracy = sp_runtime::Perbill;
@@ -0,0 +1,323 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! This pallet is designed to go into a source chain and destination chain to migrate data. The
//! design motivations are:
//!
//! - Call some function on the source chain that executes some migration (clearing state,
//! forwarding an XCM program).
//! - Call some function (probably from an XCM program) on the destination chain.
//! - Avoid cluttering the source pallet with new dispatchables that are unrelated to its
//! functionality and only used for migration.
//!
//! After the migration is complete, the pallet may be removed from both chains' runtimes as well as
//! the `pezkuwi-runtime-common` crate.
use frame_support::{dispatch::DispatchResult, traits::Currency, weights::Weight};
pub use pallet::*;
use pallet_identity;
use sp_core::Get;
#[cfg(feature = "runtime-benchmarks")]
use frame_benchmarking::{account, v2::*, BenchmarkError};
pub trait WeightInfo {
fn reap_identity(r: u32, s: u32) -> Weight;
fn poke_deposit() -> Weight;
}
impl WeightInfo for () {
fn reap_identity(_r: u32, _s: u32) -> Weight {
Weight::MAX
}
fn poke_deposit() -> Weight {
Weight::MAX
}
}
pub struct TestWeightInfo;
impl WeightInfo for TestWeightInfo {
fn reap_identity(_r: u32, _s: u32) -> Weight {
Weight::zero()
}
fn poke_deposit() -> Weight {
Weight::zero()
}
}
// Must use the same `Balance` as `T`'s Identity pallet to handle deposits.
type BalanceOf<T> = <<T as pallet_identity::Config>::Currency as Currency<
<T as frame_system::Config>::AccountId,
>>::Balance;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::{
dispatch::{DispatchResultWithPostInfo, PostDispatchInfo},
pallet_prelude::*,
traits::EnsureOrigin,
};
use frame_system::pallet_prelude::*;
#[pallet::pallet]
pub struct Pallet<T>(_);
#[pallet::config]
pub trait Config: frame_system::Config + pallet_identity::Config {
/// Overarching event type.
#[allow(deprecated)]
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// The origin that can reap identities. Expected to be `EnsureSigned<AccountId>` on the
/// source chain such that anyone can all this function.
type Reaper: EnsureOrigin<Self::RuntimeOrigin>;
/// A handler for what to do when an identity is reaped.
type ReapIdentityHandler: OnReapIdentity<Self::AccountId>;
/// Weight information for the extrinsics in the pallet.
type WeightInfo: WeightInfo;
}
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// The identity and all sub accounts were reaped for `who`.
IdentityReaped { who: T::AccountId },
/// The deposits held for `who` were updated. `identity` is the new deposit held for
/// identity info, and `subs` is the new deposit held for the sub-accounts.
DepositUpdated { who: T::AccountId, identity: BalanceOf<T>, subs: BalanceOf<T> },
}
#[pallet::call]
impl<T: Config> Pallet<T> {
/// Reap the `IdentityInfo` of `who` from the Identity pallet of `T`, unreserving any
/// deposits held and removing storage items associated with `who`.
#[pallet::call_index(0)]
#[pallet::weight(<T as pallet::Config>::WeightInfo::reap_identity(
T::MaxRegistrars::get(),
T::MaxSubAccounts::get()
))]
pub fn reap_identity(
origin: OriginFor<T>,
who: T::AccountId,
) -> DispatchResultWithPostInfo {
T::Reaper::ensure_origin(origin)?;
// - number of registrars (required to calculate weight)
// - byte size of `IdentityInfo` (required to calculate remote deposit)
// - number of sub accounts (required to calculate both weight and remote deposit)
let (registrars, bytes, subs) = pallet_identity::Pallet::<T>::reap_identity(&who)?;
T::ReapIdentityHandler::on_reap_identity(&who, bytes, subs)?;
Self::deposit_event(Event::IdentityReaped { who });
let post = PostDispatchInfo {
actual_weight: Some(<T as pallet::Config>::WeightInfo::reap_identity(
registrars, subs,
)),
pays_fee: Pays::No,
};
Ok(post)
}
/// Update the deposit of `who`. Meant to be called by the system with an XCM `Transact`
/// Instruction.
#[pallet::call_index(1)]
#[pallet::weight(<T as pallet::Config>::WeightInfo::poke_deposit())]
pub fn poke_deposit(origin: OriginFor<T>, who: T::AccountId) -> DispatchResultWithPostInfo {
ensure_root(origin)?;
let (id_deposit, subs_deposit) = pallet_identity::Pallet::<T>::poke_deposit(&who)?;
Self::deposit_event(Event::DepositUpdated {
who,
identity: id_deposit,
subs: subs_deposit,
});
Ok(Pays::No.into())
}
}
}
/// Trait to handle reaping identity from state.
pub trait OnReapIdentity<AccountId> {
/// What to do when an identity is reaped. For example, the implementation could send an XCM
/// program to another chain. Concretely, a type implementing this trait in the Pezkuwi
/// runtime would teleport enough HEZ to the People Chain to cover the Identity deposit there.
///
/// This could also directly include `Transact { poke_deposit(..), ..}`.
///
/// Inputs
/// - `who`: Whose identity was reaped.
/// - `bytes`: The byte size of `IdentityInfo`.
/// - `subs`: The number of sub-accounts they had.
fn on_reap_identity(who: &AccountId, bytes: u32, subs: u32) -> DispatchResult;
/// Ensure that identity reaping will be succesful in benchmarking.
///
/// Should setup the state in a way that the same call ot `[Self::on_reap_identity]` will be
/// successful.
#[cfg(feature = "runtime-benchmarks")]
fn ensure_successful_identity_reaping(who: &AccountId, bytes: u32, subs: u32);
}
impl<AccountId> OnReapIdentity<AccountId> for () {
fn on_reap_identity(_who: &AccountId, _bytes: u32, _subs: u32) -> DispatchResult {
Ok(())
}
#[cfg(feature = "runtime-benchmarks")]
fn ensure_successful_identity_reaping(_: &AccountId, _: u32, _: u32) {}
}
#[cfg(feature = "runtime-benchmarks")]
#[benchmarks]
mod benchmarks {
use super::*;
use alloc::{boxed::Box, vec, vec::Vec};
use codec::Encode;
use frame_support::traits::EnsureOrigin;
use frame_system::RawOrigin;
use pallet_identity::{Data, IdentityInformationProvider, Judgement, Pallet as Identity};
use sp_runtime::{
traits::{Bounded, Hash, StaticLookup},
Saturating,
};
const SEED: u32 = 0;
fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
let events = frame_system::Pallet::<T>::events();
let system_event: <T as frame_system::Config>::RuntimeEvent = generic_event.into();
let frame_system::EventRecord { event, .. } = &events[events.len() - 1];
assert_eq!(event, &system_event);
}
#[benchmark]
fn reap_identity(
r: Linear<0, { T::MaxRegistrars::get() }>,
s: Linear<0, { T::MaxSubAccounts::get() }>,
) -> Result<(), BenchmarkError> {
// set up target
let target: T::AccountId = account("target", 0, SEED);
let target_origin =
<T as frame_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(target.clone()));
let target_lookup = T::Lookup::unlookup(target.clone());
let _ = T::Currency::make_free_balance_be(&target, BalanceOf::<T>::max_value());
// set identity
let info = <T as pallet_identity::Config>::IdentityInformation::create_identity_info();
Identity::<T>::set_identity(
RawOrigin::Signed(target.clone()).into(),
Box::new(info.clone()),
)?;
// create and set subs
let mut subs = Vec::new();
let data = Data::Raw(vec![0; 32].try_into().unwrap());
for ii in 0..s {
let sub_account = account("sub", ii, SEED);
subs.push((sub_account, data.clone()));
}
Identity::<T>::set_subs(target_origin.clone(), subs.clone())?;
T::ReapIdentityHandler::ensure_successful_identity_reaping(
&target,
info.encoded_size() as u32,
subs.len() as u32,
);
// add registrars and provide judgements
let registrar_origin = T::RegistrarOrigin::try_successful_origin()
.expect("RegistrarOrigin has no successful origin required for the benchmark");
for ii in 0..r {
// registrar account
let registrar: T::AccountId = account("registrar", ii, SEED);
let registrar_lookup = T::Lookup::unlookup(registrar.clone());
let _ = <T as pallet_identity::Config>::Currency::make_free_balance_be(
&registrar,
<T as pallet_identity::Config>::Currency::minimum_balance(),
);
// add registrar
Identity::<T>::add_registrar(registrar_origin.clone(), registrar_lookup)?;
Identity::<T>::set_fee(RawOrigin::Signed(registrar.clone()).into(), ii, 10u32.into())?;
let fields = <T as pallet_identity::Config>::IdentityInformation::all_fields();
Identity::<T>::set_fields(RawOrigin::Signed(registrar.clone()).into(), ii, fields)?;
// request and provide judgement
Identity::<T>::request_judgement(target_origin.clone(), ii, 10u32.into())?;
Identity::<T>::provide_judgement(
RawOrigin::Signed(registrar).into(),
ii,
target_lookup.clone(),
Judgement::Reasonable,
<T as frame_system::Config>::Hashing::hash_of(&info),
)?;
}
let origin = T::Reaper::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
#[extrinsic_call]
_(origin as T::RuntimeOrigin, target.clone());
assert_last_event::<T>(Event::<T>::IdentityReaped { who: target.clone() }.into());
let fields = <T as pallet_identity::Config>::IdentityInformation::all_fields();
assert!(!Identity::<T>::has_identity(&target, fields));
assert_eq!(Identity::<T>::subs(&target).len(), 0);
Ok(())
}
#[benchmark]
fn poke_deposit() -> Result<(), BenchmarkError> {
let target: T::AccountId = account("target", 0, SEED);
let _ = T::Currency::make_free_balance_be(&target, BalanceOf::<T>::max_value());
let info = <T as pallet_identity::Config>::IdentityInformation::create_identity_info();
let _ = Identity::<T>::set_identity_no_deposit(&target, info.clone());
let sub_account: T::AccountId = account("sub", 0, SEED);
let name = Data::Raw(b"benchsub".to_vec().try_into().unwrap());
let _ = Identity::<T>::set_subs_no_deposit(&target, vec![(sub_account.clone(), name)]);
// expected deposits
let expected_id_deposit = <T as pallet_identity::Config>::BasicDeposit::get()
.saturating_add(
<T as pallet_identity::Config>::ByteDeposit::get()
.saturating_mul(<BalanceOf<T>>::from(info.encoded_size() as u32)),
);
// only 1 sub
let expected_sub_deposit = <T as pallet_identity::Config>::SubAccountDeposit::get();
#[extrinsic_call]
_(RawOrigin::Root, target.clone());
assert_last_event::<T>(
Event::<T>::DepositUpdated {
who: target,
identity: expected_id_deposit,
subs: expected_sub_deposit,
}
.into(),
);
Ok(())
}
impl_benchmark_test_suite!(
Pallet,
crate::integration_tests::new_test_ext(),
crate::integration_tests::Test,
);
}
+615
View File
@@ -0,0 +1,615 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Auxiliary `struct`/`enum`s for pezkuwi runtime.
use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
use frame_support::traits::{
fungible::{Balanced, Credit},
tokens::imbalance::ResolveTo,
Contains, ContainsPair, Imbalance, OnUnbalanced,
};
use pallet_treasury::TreasuryAccountId;
use pezkuwi_primitives::Balance;
use sp_runtime::{traits::TryConvert, Perquintill, RuntimeDebug};
use xcm::VersionedLocation;
/// Logic for the author to get a portion of fees.
pub struct ToAuthor<R>(core::marker::PhantomData<R>);
impl<R> OnUnbalanced<Credit<R::AccountId, pallet_balances::Pallet<R>>> for ToAuthor<R>
where
R: pallet_balances::Config + pallet_authorship::Config,
<R as frame_system::Config>::AccountId: From<pezkuwi_primitives::AccountId>,
<R as frame_system::Config>::AccountId: Into<pezkuwi_primitives::AccountId>,
{
fn on_nonzero_unbalanced(
amount: Credit<<R as frame_system::Config>::AccountId, pallet_balances::Pallet<R>>,
) {
if let Some(author) = <pallet_authorship::Pallet<R>>::author() {
let _ = <pallet_balances::Pallet<R>>::resolve(&author, amount);
}
}
}
pub struct DealWithFees<R>(core::marker::PhantomData<R>);
impl<R> OnUnbalanced<Credit<R::AccountId, pallet_balances::Pallet<R>>> for DealWithFees<R>
where
R: pallet_balances::Config + pallet_authorship::Config + pallet_treasury::Config,
<R as frame_system::Config>::AccountId: From<pezkuwi_primitives::AccountId>,
<R as frame_system::Config>::AccountId: Into<pezkuwi_primitives::AccountId>,
{
fn on_unbalanceds(
mut fees_then_tips: impl Iterator<Item = Credit<R::AccountId, pallet_balances::Pallet<R>>>,
) {
if let Some(fees) = fees_then_tips.next() {
// for fees, 80% to treasury, 20% to author
let mut split = fees.ration(80, 20);
if let Some(tips) = fees_then_tips.next() {
// for tips, if any, 100% to author
tips.merge_into(&mut split.1);
}
ResolveTo::<TreasuryAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(split.0);
<ToAuthor<R> as OnUnbalanced<_>>::on_unbalanced(split.1);
}
}
}
/// Parameters passed into [`relay_era_payout`] function.
pub struct EraPayoutParams {
/// Total staked amount.
pub total_staked: Balance,
/// Total stakable amount.
///
/// Usually, this is equal to the total issuance, except if a large part of the issuance is
/// locked in another sub-system.
pub total_stakable: Balance,
/// Ideal stake ratio, which is deducted by `legacy_auction_proportion` if not `None`.
pub ideal_stake: Perquintill,
/// Maximum inflation rate.
pub max_annual_inflation: Perquintill,
/// Minimum inflation rate.
pub min_annual_inflation: Perquintill,
/// Falloff used to calculate era payouts.
pub falloff: Perquintill,
/// Fraction of the era period used to calculate era payouts.
pub period_fraction: Perquintill,
/// Legacy auction proportion, which substracts from `ideal_stake` if not `None`.
pub legacy_auction_proportion: Option<Perquintill>,
}
/// A specialized function to compute the inflation of the staking system, tailored for pezkuwi
/// relay chains, such as Pezkuwi, Kusama and Zagros.
pub fn relay_era_payout(params: EraPayoutParams) -> (Balance, Balance) {
use sp_runtime::traits::Saturating;
let EraPayoutParams {
total_staked,
total_stakable,
ideal_stake,
max_annual_inflation,
min_annual_inflation,
falloff,
period_fraction,
legacy_auction_proportion,
} = params;
let delta_annual_inflation = max_annual_inflation.saturating_sub(min_annual_inflation);
let ideal_stake = ideal_stake.saturating_sub(legacy_auction_proportion.unwrap_or_default());
let stake = Perquintill::from_rational(total_staked, total_stakable);
let adjustment = pallet_staking_reward_fn::compute_inflation(stake, ideal_stake, falloff);
let staking_inflation =
min_annual_inflation.saturating_add(delta_annual_inflation * adjustment);
let max_payout = period_fraction * max_annual_inflation * total_stakable;
let staking_payout = (period_fraction * staking_inflation) * total_stakable;
let rest = max_payout.saturating_sub(staking_payout);
let other_issuance = total_stakable.saturating_sub(total_staked);
if total_staked > other_issuance {
let _cap_rest = Perquintill::from_rational(other_issuance, total_staked) * staking_payout;
// We don't do anything with this, but if we wanted to, we could introduce a cap on the
// treasury amount with: `rest = rest.min(cap_rest);`
}
(staking_payout, rest)
}
/// Versioned locatable asset type which contains both an XCM `location` and `asset_id` to identify
/// an asset which exists on some chain.
#[derive(
Encode,
Decode,
DecodeWithMemTracking,
Eq,
PartialEq,
Clone,
RuntimeDebug,
scale_info::TypeInfo,
MaxEncodedLen,
)]
pub enum VersionedLocatableAsset {
#[codec(index = 3)]
V3 { location: xcm::v3::Location, asset_id: xcm::v3::AssetId },
#[codec(index = 4)]
V4 { location: xcm::v4::Location, asset_id: xcm::v4::AssetId },
#[codec(index = 5)]
V5 { location: xcm::v5::Location, asset_id: xcm::v5::AssetId },
}
/// A conversion from latest xcm to `VersionedLocatableAsset`.
impl From<(xcm::latest::Location, xcm::latest::AssetId)> for VersionedLocatableAsset {
fn from(value: (xcm::latest::Location, xcm::latest::AssetId)) -> Self {
VersionedLocatableAsset::V5 { location: value.0, asset_id: value.1 }
}
}
/// Converts the [`VersionedLocatableAsset`] to the [`xcm_builder::LocatableAssetId`].
pub struct LocatableAssetConverter;
impl TryConvert<VersionedLocatableAsset, xcm_builder::LocatableAssetId>
for LocatableAssetConverter
{
fn try_convert(
asset: VersionedLocatableAsset,
) -> Result<xcm_builder::LocatableAssetId, VersionedLocatableAsset> {
match asset {
VersionedLocatableAsset::V3 { location, asset_id } => {
let v4_location: xcm::v4::Location =
location.try_into().map_err(|_| asset.clone())?;
let v4_asset_id: xcm::v4::AssetId =
asset_id.try_into().map_err(|_| asset.clone())?;
Ok(xcm_builder::LocatableAssetId {
location: v4_location.try_into().map_err(|_| asset.clone())?,
asset_id: v4_asset_id.try_into().map_err(|_| asset.clone())?,
})
},
VersionedLocatableAsset::V4 { ref location, ref asset_id } =>
Ok(xcm_builder::LocatableAssetId {
location: location.clone().try_into().map_err(|_| asset.clone())?,
asset_id: asset_id.clone().try_into().map_err(|_| asset.clone())?,
}),
VersionedLocatableAsset::V5 { location, asset_id } =>
Ok(xcm_builder::LocatableAssetId { location, asset_id }),
}
}
}
/// Converts the [`VersionedLocation`] to the [`xcm::latest::Location`].
pub struct VersionedLocationConverter;
impl TryConvert<&VersionedLocation, xcm::latest::Location> for VersionedLocationConverter {
fn try_convert(
location: &VersionedLocation,
) -> Result<xcm::latest::Location, &VersionedLocation> {
let latest = match location.clone() {
VersionedLocation::V3(l) => {
let v4_location: xcm::v4::Location = l.try_into().map_err(|_| location)?;
v4_location.try_into().map_err(|_| location)?
},
VersionedLocation::V4(l) => l.try_into().map_err(|_| location)?,
VersionedLocation::V5(l) => l,
};
Ok(latest)
}
}
/// Adapter for [`Contains`] trait to match [`VersionedLocatableAsset`] type converted to the latest
/// version of itself where it's location matched by `L` and it's asset id by `A` parameter types.
pub struct ContainsParts<C>(core::marker::PhantomData<C>);
impl<C> Contains<VersionedLocatableAsset> for ContainsParts<C>
where
C: ContainsPair<xcm::latest::Location, xcm::latest::Location>,
{
fn contains(asset: &VersionedLocatableAsset) -> bool {
use VersionedLocatableAsset::*;
let (location, asset_id) = match asset.clone() {
V3 { location, asset_id } => {
let v4_location: xcm::v4::Location = match location.try_into() {
Ok(l) => l,
Err(_) => return false,
};
let v4_asset_id: xcm::v4::AssetId = match asset_id.try_into() {
Ok(a) => a,
Err(_) => return false,
};
match (v4_location.try_into(), v4_asset_id.try_into()) {
(Ok(l), Ok(a)) => (l, a),
_ => return false,
}
},
V4 { location, asset_id } => match (location.try_into(), asset_id.try_into()) {
(Ok(l), Ok(a)) => (l, a),
_ => return false,
},
V5 { location, asset_id } => (location, asset_id),
};
C::contains(&location, &asset_id.0)
}
}
#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarks {
use super::VersionedLocatableAsset;
use core::marker::PhantomData;
use frame_support::traits::Get;
use pallet_asset_rate::AssetKindFactory;
use pallet_treasury::ArgumentsFactory as TreasuryArgumentsFactory;
use sp_core::{ConstU32, ConstU8};
use xcm::prelude::*;
/// Provides a factory method for the [`VersionedLocatableAsset`].
/// The location of the asset is determined as a Teyrchain with an ID equal to the passed seed.
pub struct AssetRateArguments;
impl AssetKindFactory<VersionedLocatableAsset> for AssetRateArguments {
fn create_asset_kind(seed: u32) -> VersionedLocatableAsset {
(
Location::new(0, [Teyrchain(seed)]),
AssetId(Location::new(
0,
[PalletInstance(seed.try_into().unwrap()), GeneralIndex(seed.into())],
)),
)
.into()
}
}
/// Provide factory methods for the [`VersionedLocatableAsset`] and the `Beneficiary` of the
/// [`VersionedLocation`]. The location of the asset is determined as a Teyrchain with an
/// ID equal to the passed seed.
pub struct TreasuryArguments<Parents = ConstU8<0>, ParaId = ConstU32<0>>(
PhantomData<(Parents, ParaId)>,
);
impl<Parents: Get<u8>, ParaId: Get<u32>>
TreasuryArgumentsFactory<VersionedLocatableAsset, VersionedLocation>
for TreasuryArguments<Parents, ParaId>
{
fn create_asset_kind(seed: u32) -> VersionedLocatableAsset {
(
Location::new(Parents::get(), [Junction::Teyrchain(ParaId::get())]),
AssetId(Location::new(
0,
[PalletInstance(seed.try_into().unwrap()), GeneralIndex(seed.into())],
)),
)
.into()
}
fn create_beneficiary(seed: [u8; 32]) -> VersionedLocation {
VersionedLocation::from(Location::new(0, [AccountId32 { network: None, id: seed }]))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use frame_support::{
derive_impl,
dispatch::DispatchClass,
parameter_types,
traits::{
tokens::{PayFromAccount, UnityAssetBalanceConversion},
FindAuthor,
},
weights::Weight,
PalletId,
};
use frame_system::limits;
use pezkuwi_primitives::AccountId;
use sp_core::{ConstU64, H256};
use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup},
BuildStorage, Perbill,
};
type Block = frame_system::mocking::MockBlock<Test>;
const TEST_ACCOUNT: AccountId = AccountId::new([1; 32]);
frame_support::construct_runtime!(
pub enum Test
{
System: frame_system,
Authorship: pallet_authorship,
Balances: pallet_balances,
Treasury: pallet_treasury,
}
);
parameter_types! {
pub BlockWeights: limits::BlockWeights = limits::BlockWeights::builder()
.base_block(Weight::from_parts(10, 0))
.for_class(DispatchClass::all(), |weight| {
weight.base_extrinsic = Weight::from_parts(100, 0);
})
.for_class(DispatchClass::non_mandatory(), |weight| {
weight.max_total = Some(Weight::from_parts(1024, u64::MAX));
})
.build_or_panic();
pub BlockLength: limits::BlockLength = limits::BlockLength::max(2 * 1024);
pub const AvailableBlockRatio: Perbill = Perbill::one();
}
#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
impl frame_system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
type RuntimeOrigin = RuntimeOrigin;
type Nonce = u64;
type RuntimeCall = RuntimeCall;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = AccountId;
type Lookup = IdentityLookup<Self::AccountId>;
type Block = Block;
type RuntimeEvent = RuntimeEvent;
type BlockLength = BlockLength;
type BlockWeights = BlockWeights;
type DbWeight = ();
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
}
#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
impl pallet_balances::Config for Test {
type AccountStore = System;
}
parameter_types! {
pub const TreasuryPalletId: PalletId = PalletId(*b"py/trsry");
pub const MaxApprovals: u32 = 100;
pub TreasuryAccount: AccountId = Treasury::account_id();
}
impl pallet_treasury::Config for Test {
type Currency = pallet_balances::Pallet<Test>;
type RejectOrigin = frame_system::EnsureRoot<AccountId>;
type RuntimeEvent = RuntimeEvent;
type SpendPeriod = ();
type Burn = ();
type BurnDestination = ();
type PalletId = TreasuryPalletId;
type SpendFunds = ();
type MaxApprovals = MaxApprovals;
type WeightInfo = ();
type SpendOrigin = frame_support::traits::NeverEnsureOrigin<u64>;
type AssetKind = ();
type Beneficiary = Self::AccountId;
type BeneficiaryLookup = IdentityLookup<Self::AccountId>;
type Paymaster = PayFromAccount<Balances, TreasuryAccount>;
type BalanceConverter = UnityAssetBalanceConversion;
type PayoutPeriod = ConstU64<0>;
type BlockNumberProvider = System;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}
pub struct OneAuthor;
impl FindAuthor<AccountId> for OneAuthor {
fn find_author<'a, I>(_: I) -> Option<AccountId>
where
I: 'a,
{
Some(TEST_ACCOUNT)
}
}
impl pallet_authorship::Config for Test {
type FindAuthor = OneAuthor;
type EventHandler = ();
}
pub fn new_test_ext() -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
// We use default for brevity, but you can configure as desired if needed.
pallet_balances::GenesisConfig::<Test>::default()
.assimilate_storage(&mut t)
.unwrap();
t.into()
}
pub fn deprecated_era_payout(
total_staked: Balance,
total_stakable: Balance,
max_annual_inflation: Perquintill,
period_fraction: Perquintill,
auctioned_slots: u64,
) -> (Balance, Balance) {
use pallet_staking_reward_fn::compute_inflation;
use sp_runtime::traits::Saturating;
let min_annual_inflation = Perquintill::from_rational(25u64, 1000u64);
let delta_annual_inflation = max_annual_inflation.saturating_sub(min_annual_inflation);
// 30% reserved for up to 60 slots.
let auction_proportion = Perquintill::from_rational(auctioned_slots.min(60), 200u64);
// Therefore the ideal amount at stake (as a percentage of total issuance) is 75% less the
// amount that we expect to be taken up with auctions.
let ideal_stake = Perquintill::from_percent(75).saturating_sub(auction_proportion);
let stake = Perquintill::from_rational(total_staked, total_stakable);
let falloff = Perquintill::from_percent(5);
let adjustment = compute_inflation(stake, ideal_stake, falloff);
let staking_inflation =
min_annual_inflation.saturating_add(delta_annual_inflation * adjustment);
let max_payout = period_fraction * max_annual_inflation * total_stakable;
let staking_payout = (period_fraction * staking_inflation) * total_stakable;
let rest = max_payout.saturating_sub(staking_payout);
let other_issuance = total_stakable.saturating_sub(total_staked);
if total_staked > other_issuance {
let _cap_rest =
Perquintill::from_rational(other_issuance, total_staked) * staking_payout;
// We don't do anything with this, but if we wanted to, we could introduce a cap on the
// treasury amount with: `rest = rest.min(cap_rest);`
}
(staking_payout, rest)
}
#[test]
fn test_fees_and_tip_split() {
new_test_ext().execute_with(|| {
let fee =
<pallet_balances::Pallet<Test> as frame_support::traits::fungible::Balanced<
AccountId,
>>::issue(10);
let tip =
<pallet_balances::Pallet<Test> as frame_support::traits::fungible::Balanced<
AccountId,
>>::issue(20);
assert_eq!(Balances::free_balance(Treasury::account_id()), 0);
assert_eq!(Balances::free_balance(TEST_ACCOUNT), 0);
DealWithFees::on_unbalanceds(vec![fee, tip].into_iter());
// Author gets 100% of tip and 20% of fee = 22
assert_eq!(Balances::free_balance(TEST_ACCOUNT), 22);
// Treasury gets 80% of fee
assert_eq!(Balances::free_balance(Treasury::account_id()), 8);
});
}
#[test]
fn compute_inflation_should_give_sensible_results() {
assert_eq!(
pallet_staking_reward_fn::compute_inflation(
Perquintill::from_percent(75),
Perquintill::from_percent(75),
Perquintill::from_percent(5),
),
Perquintill::one()
);
assert_eq!(
pallet_staking_reward_fn::compute_inflation(
Perquintill::from_percent(50),
Perquintill::from_percent(75),
Perquintill::from_percent(5),
),
Perquintill::from_rational(2u64, 3u64)
);
assert_eq!(
pallet_staking_reward_fn::compute_inflation(
Perquintill::from_percent(80),
Perquintill::from_percent(75),
Perquintill::from_percent(5),
),
Perquintill::from_rational(1u64, 2u64)
);
}
#[test]
fn era_payout_should_give_sensible_results() {
let payout =
deprecated_era_payout(75, 100, Perquintill::from_percent(10), Perquintill::one(), 0);
assert_eq!(payout, (10, 0));
let payout =
deprecated_era_payout(80, 100, Perquintill::from_percent(10), Perquintill::one(), 0);
assert_eq!(payout, (6, 4));
}
#[test]
fn relay_era_payout_should_give_sensible_results() {
let params = EraPayoutParams {
total_staked: 75,
total_stakable: 100,
ideal_stake: Perquintill::from_percent(75),
max_annual_inflation: Perquintill::from_percent(10),
min_annual_inflation: Perquintill::from_rational(25u64, 1000u64),
falloff: Perquintill::from_percent(5),
period_fraction: Perquintill::one(),
legacy_auction_proportion: None,
};
assert_eq!(relay_era_payout(params), (10, 0));
let params = EraPayoutParams {
total_staked: 80,
total_stakable: 100,
ideal_stake: Perquintill::from_percent(75),
max_annual_inflation: Perquintill::from_percent(10),
min_annual_inflation: Perquintill::from_rational(25u64, 1000u64),
falloff: Perquintill::from_percent(5),
period_fraction: Perquintill::one(),
legacy_auction_proportion: None,
};
assert_eq!(relay_era_payout(params), (6, 4));
}
#[test]
fn relay_era_payout_should_give_same_results_as_era_payout() {
let total_staked = 1_000_000;
let total_stakable = 2_000_000;
let max_annual_inflation = Perquintill::from_percent(10);
let period_fraction = Perquintill::from_percent(25);
let auctioned_slots = 30;
let params = EraPayoutParams {
total_staked,
total_stakable,
ideal_stake: Perquintill::from_percent(75),
max_annual_inflation,
min_annual_inflation: Perquintill::from_rational(25u64, 1000u64),
falloff: Perquintill::from_percent(5),
period_fraction,
legacy_auction_proportion: Some(Perquintill::from_rational(
auctioned_slots.min(60),
200u64,
)),
};
let payout = deprecated_era_payout(
total_staked,
total_stakable,
max_annual_inflation,
period_fraction,
auctioned_slots,
);
assert_eq!(relay_era_payout(params), payout);
let total_staked = 1_900_000;
let total_stakable = 2_000_000;
let auctioned_slots = 60;
let params = EraPayoutParams {
total_staked,
total_stakable,
ideal_stake: Perquintill::from_percent(75),
max_annual_inflation,
min_annual_inflation: Perquintill::from_rational(25u64, 1000u64),
falloff: Perquintill::from_percent(5),
period_fraction,
legacy_auction_proportion: Some(Perquintill::from_rational(
auctioned_slots.min(60),
200u64,
)),
};
let payout = deprecated_era_payout(
total_staked,
total_stakable,
max_annual_inflation,
period_fraction,
auctioned_slots,
);
assert_eq!(relay_era_payout(params), payout);
}
}
File diff suppressed because it is too large Load Diff
+285
View File
@@ -0,0 +1,285 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Common runtime code for the Relay Chain, e.g. Pezkuwichain, Zagros, Pezkuwi, Kusama ...
#![cfg_attr(not(feature = "std"), no_std)]
pub mod assigned_slots;
pub mod auctions;
pub mod claims;
pub mod crowdloan;
pub mod elections;
pub mod identity_migrator;
pub mod impls;
pub mod paras_registrar;
pub mod paras_sudo_wrapper;
pub mod purchase;
pub mod slot_range;
pub mod slots;
pub mod traits;
#[cfg(feature = "try-runtime")]
pub mod try_runtime;
pub mod xcm_sender;
#[cfg(test)]
mod integration_tests;
#[cfg(test)]
mod mock;
extern crate alloc;
use frame_support::{
parameter_types,
traits::{ConstU32, Currency, OneSessionHandler},
weights::{constants::WEIGHT_REF_TIME_PER_SECOND, Weight},
};
use frame_system::limits;
use pezkuwi_primitives::{AssignmentId, Balance, BlockNumber, ValidatorId};
use sp_runtime::{FixedPointNumber, Perbill, Perquintill};
use static_assertions::const_assert;
pub use pallet_balances::Call as BalancesCall;
#[cfg(feature = "std")]
pub use pallet_staking::StakerStatus;
pub use pallet_timestamp::Call as TimestampCall;
use pallet_transaction_payment::{Multiplier, TargetedFeeAdjustment};
pub use sp_runtime::traits::Bounded;
#[cfg(any(feature = "std", test))]
pub use sp_runtime::BuildStorage;
/// Implementations of some helper traits passed into runtime modules as associated types.
pub use impls::ToAuthor;
#[deprecated(
note = "Please use fungible::Credit instead. This type will be removed some time after March 2024."
)]
pub type NegativeImbalance<T> = <pallet_balances::Pallet<T> as Currency<
<T as frame_system::Config>::AccountId,
>>::NegativeImbalance;
/// We assume that an on-initialize consumes 1% of the weight on average, hence a single extrinsic
/// will not be allowed to consume more than `AvailableBlockRatio - 1%`.
pub const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(1);
/// We allow `Normal` extrinsics to fill up the block up to 75%, the rest can be used
/// by Operational extrinsics.
pub const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
/// We allow for 2 seconds of compute with a 6 second average block time.
/// The storage proof size is not limited so far.
pub const MAXIMUM_BLOCK_WEIGHT: Weight =
Weight::from_parts(WEIGHT_REF_TIME_PER_SECOND.saturating_mul(2), u64::MAX);
const_assert!(NORMAL_DISPATCH_RATIO.deconstruct() >= AVERAGE_ON_INITIALIZE_RATIO.deconstruct());
// Common constants used in all runtimes.
parameter_types! {
pub const BlockHashCount: BlockNumber = 4096;
/// The portion of the `NORMAL_DISPATCH_RATIO` that we adjust the fees with. Blocks filled less
/// than this will decrease the weight and more will increase.
pub const TargetBlockFullness: Perquintill = Perquintill::from_percent(25);
/// The adjustment variable of the runtime. Higher values will cause `TargetBlockFullness` to
/// change the fees more rapidly.
pub AdjustmentVariable: Multiplier = Multiplier::saturating_from_rational(75, 1000_000);
/// Minimum amount of the multiplier. This value cannot be too low. A test case should ensure
/// that combined with `AdjustmentVariable`, we can recover from the minimum.
/// See `multiplier_can_grow_from_zero`.
pub MinimumMultiplier: Multiplier = Multiplier::saturating_from_rational(1, 10u128);
/// The maximum amount of the multiplier.
pub MaximumMultiplier: Multiplier = Bounded::max_value();
/// Maximum length of block. Up to 5MB.
pub BlockLength: limits::BlockLength =
limits::BlockLength::max_with_normal_ratio(5 * 1024 * 1024, NORMAL_DISPATCH_RATIO);
}
/// Parameterized slow adjusting fee updated based on
/// <https://research.web3.foundation/Polkadot/overview/token-economics#2-slow-adjusting-mechanism>
pub type SlowAdjustingFeeUpdate<R> = TargetedFeeAdjustment<
R,
TargetBlockFullness,
AdjustmentVariable,
MinimumMultiplier,
MaximumMultiplier,
>;
/// Implements the weight types for a runtime.
/// It expects the passed runtime constants to contain a `weights` module.
/// The generated weight types were formerly part of the common
/// runtime but are now runtime dependant.
#[macro_export]
macro_rules! impl_runtime_weights {
($runtime:ident) => {
use frame_support::{dispatch::DispatchClass, weights::Weight};
use frame_system::limits;
use pallet_transaction_payment::{Multiplier, TargetedFeeAdjustment};
pub use pezkuwi_runtime_common::{
impl_elections_weights, AVERAGE_ON_INITIALIZE_RATIO, MAXIMUM_BLOCK_WEIGHT,
NORMAL_DISPATCH_RATIO,
};
use sp_runtime::{FixedPointNumber, Perquintill};
// Implement the weight types of the elections module.
impl_elections_weights!($runtime);
// Expose the weight from the runtime constants module.
pub use $runtime::weights::{
BlockExecutionWeight, ExtrinsicBaseWeight, ParityDbWeight, RocksDbWeight,
};
parameter_types! {
/// Block weights base values and limits.
pub BlockWeights: limits::BlockWeights = limits::BlockWeights::builder()
.base_block($runtime::weights::BlockExecutionWeight::get())
.for_class(DispatchClass::all(), |weights| {
weights.base_extrinsic = $runtime::weights::ExtrinsicBaseWeight::get();
})
.for_class(DispatchClass::Normal, |weights| {
weights.max_total = Some(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT);
})
.for_class(DispatchClass::Operational, |weights| {
weights.max_total = Some(MAXIMUM_BLOCK_WEIGHT);
// Operational transactions have an extra reserved space, so that they
// are included even if block reached `MAXIMUM_BLOCK_WEIGHT`.
weights.reserved = Some(
MAXIMUM_BLOCK_WEIGHT - NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT,
);
})
.avg_block_initialization(AVERAGE_ON_INITIALIZE_RATIO)
.build_or_panic();
}
};
}
/// The type used for currency conversion.
///
/// This must only be used as long as the balance type is `u128`.
pub type CurrencyToVote = sp_staking::currency_to_vote::U128CurrencyToVote;
static_assertions::assert_eq_size!(pezkuwi_primitives::Balance, u128);
/// A placeholder since there is currently no provided session key handler for teyrchain validator
/// keys.
pub struct TeyrchainSessionKeyPlaceholder<T>(core::marker::PhantomData<T>);
impl<T> sp_runtime::BoundToRuntimeAppPublic for TeyrchainSessionKeyPlaceholder<T> {
type Public = ValidatorId;
}
impl<T: pallet_session::Config> OneSessionHandler<T::AccountId>
for TeyrchainSessionKeyPlaceholder<T>
{
type Key = ValidatorId;
fn on_genesis_session<'a, I: 'a>(_validators: I)
where
I: Iterator<Item = (&'a T::AccountId, ValidatorId)>,
T::AccountId: 'a,
{
}
fn on_new_session<'a, I: 'a>(_changed: bool, _v: I, _q: I)
where
I: Iterator<Item = (&'a T::AccountId, ValidatorId)>,
T::AccountId: 'a,
{
}
fn on_disabled(_: u32) {}
}
/// A placeholder since there is currently no provided session key handler for teyrchain validator
/// keys.
pub struct AssignmentSessionKeyPlaceholder<T>(core::marker::PhantomData<T>);
impl<T> sp_runtime::BoundToRuntimeAppPublic for AssignmentSessionKeyPlaceholder<T> {
type Public = AssignmentId;
}
impl<T: pallet_session::Config> OneSessionHandler<T::AccountId>
for AssignmentSessionKeyPlaceholder<T>
{
type Key = AssignmentId;
fn on_genesis_session<'a, I: 'a>(_validators: I)
where
I: Iterator<Item = (&'a T::AccountId, AssignmentId)>,
T::AccountId: 'a,
{
}
fn on_new_session<'a, I: 'a>(_changed: bool, _v: I, _q: I)
where
I: Iterator<Item = (&'a T::AccountId, AssignmentId)>,
T::AccountId: 'a,
{
}
fn on_disabled(_: u32) {}
}
/// A reasonable benchmarking config for staking pallet.
pub struct StakingBenchmarkingConfig;
impl pallet_staking::BenchmarkingConfig for StakingBenchmarkingConfig {
type MaxValidators = ConstU32<1000>;
type MaxNominators = ConstU32<1000>;
}
/// Convert a balance to an unsigned 256-bit number, use in nomination pools.
pub struct BalanceToU256;
impl sp_runtime::traits::Convert<Balance, sp_core::U256> for BalanceToU256 {
fn convert(n: Balance) -> sp_core::U256 {
n.into()
}
}
/// Convert an unsigned 256-bit number to balance, use in nomination pools.
pub struct U256ToBalance;
impl sp_runtime::traits::Convert<sp_core::U256, Balance> for U256ToBalance {
fn convert(n: sp_core::U256) -> Balance {
use frame_support::traits::Defensive;
n.try_into().defensive_unwrap_or(Balance::MAX)
}
}
/// Macro to set a value (e.g. when using the `parameter_types` macro) to either a production value
/// or to an environment variable or testing value (in case the `fast-runtime` feature is selected)
/// or one of two testing values depending on feature.
/// Note that the environment variable is evaluated _at compile time_.
///
/// Usage:
/// ```Rust
/// parameter_types! {
/// // Note that the env variable version parameter cannot be const.
/// pub LaunchPeriod: BlockNumber = prod_or_fast!(7 * DAYS, 1, "HEZ_LAUNCH_PERIOD");
/// pub const VotingPeriod: BlockNumber = prod_or_fast!(7 * DAYS, 1 * MINUTES);
/// pub const EpochDuration: BlockNumber =
/// prod_or_fast!(1 * HOURS, "fast-runtime", 1 * MINUTES, "fast-runtime-10m", 10 * MINUTES);
/// }
/// ```
#[macro_export]
macro_rules! prod_or_fast {
($prod:expr, $test:expr) => {
if cfg!(feature = "fast-runtime") {
$test
} else {
$prod
}
};
($prod:expr, $test:expr, $env:expr) => {
if cfg!(feature = "fast-runtime") {
core::option_env!($env).map(|s| s.parse().ok()).flatten().unwrap_or($test)
} else {
$prod
}
};
}
+269
View File
@@ -0,0 +1,269 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Mocking utilities for testing.
use crate::traits::Registrar;
use codec::{Decode, Encode};
use frame_support::{dispatch::DispatchResult, weights::Weight};
use frame_system::pallet_prelude::BlockNumberFor;
use pezkuwi_primitives::{HeadData, Id as ParaId, PvfCheckStatement, SessionIndex, ValidationCode};
use pezkuwi_runtime_teyrchains::paras;
use sp_keyring::Sr25519Keyring;
use sp_runtime::{traits::SaturatedConversion, DispatchError, Permill};
use std::{cell::RefCell, collections::HashMap};
thread_local! {
static OPERATIONS: RefCell<Vec<(ParaId, u32, bool)>> = RefCell::new(Vec::new());
static TEYRCHAINS: RefCell<Vec<ParaId>> = RefCell::new(Vec::new());
// On-demand teyrchains
static PARATHREADS: RefCell<Vec<ParaId>> = RefCell::new(Vec::new());
static LOCKS: RefCell<HashMap<ParaId, bool>> = RefCell::new(HashMap::new());
static MANAGERS: RefCell<HashMap<ParaId, Vec<u8>>> = RefCell::new(HashMap::new());
}
pub struct TestRegistrar<T>(core::marker::PhantomData<T>);
impl<T: frame_system::Config> Registrar for TestRegistrar<T> {
type AccountId = T::AccountId;
fn manager_of(id: ParaId) -> Option<Self::AccountId> {
MANAGERS.with(|x| x.borrow().get(&id).and_then(|v| T::AccountId::decode(&mut &v[..]).ok()))
}
fn teyrchains() -> Vec<ParaId> {
TEYRCHAINS.with(|x| x.borrow().clone())
}
// Is on-demand teyrchain
fn is_parathread(id: ParaId) -> bool {
PARATHREADS.with(|x| x.borrow().binary_search(&id).is_ok())
}
fn apply_lock(id: ParaId) {
LOCKS.with(|x| x.borrow_mut().insert(id, true));
}
fn remove_lock(id: ParaId) {
LOCKS.with(|x| x.borrow_mut().insert(id, false));
}
fn register(
manager: Self::AccountId,
id: ParaId,
_genesis_head: HeadData,
_validation_code: ValidationCode,
) -> DispatchResult {
// Should not be teyrchain.
TEYRCHAINS.with(|x| {
let teyrchains = x.borrow_mut();
match teyrchains.binary_search(&id) {
Ok(_) => Err(DispatchError::Other("Already Teyrchain")),
Err(_) => Ok(()),
}
})?;
// Should not be parathread (on-demand teyrchain), then make it.
PARATHREADS.with(|x| {
let mut parathreads = x.borrow_mut();
match parathreads.binary_search(&id) {
Ok(_) => Err(DispatchError::Other("Already Parathread")),
Err(i) => {
parathreads.insert(i, id);
Ok(())
},
}
})?;
MANAGERS.with(|x| x.borrow_mut().insert(id, manager.encode()));
Ok(())
}
fn deregister(id: ParaId) -> DispatchResult {
// Should not be teyrchain.
TEYRCHAINS.with(|x| {
let teyrchains = x.borrow_mut();
match teyrchains.binary_search(&id) {
Ok(_) => Err(DispatchError::Other("cannot deregister teyrchain")),
Err(_) => Ok(()),
}
})?;
// Remove from parathreads (on-demand teyrchains).
PARATHREADS.with(|x| {
let mut parathreads = x.borrow_mut();
match parathreads.binary_search(&id) {
Ok(i) => {
parathreads.remove(i);
Ok(())
},
Err(_) => Err(DispatchError::Other("not parathread, so cannot `deregister`")),
}
})?;
MANAGERS.with(|x| x.borrow_mut().remove(&id));
Ok(())
}
/// If the ParaId corresponds to a parathread (on-demand teyrchain),
/// then upgrade it to a lease holding teyrchain
fn make_teyrchain(id: ParaId) -> DispatchResult {
PARATHREADS.with(|x| {
let mut parathreads = x.borrow_mut();
match parathreads.binary_search(&id) {
Ok(i) => {
parathreads.remove(i);
Ok(())
},
Err(_) => Err(DispatchError::Other("not parathread, so cannot `make_teyrchain`")),
}
})?;
TEYRCHAINS.with(|x| {
let mut teyrchains = x.borrow_mut();
match teyrchains.binary_search(&id) {
Ok(_) => Err(DispatchError::Other("already teyrchain, so cannot `make_teyrchain`")),
Err(i) => {
teyrchains.insert(i, id);
Ok(())
},
}
})?;
OPERATIONS.with(|x| {
x.borrow_mut().push((
id,
frame_system::Pallet::<T>::block_number().saturated_into(),
true,
))
});
Ok(())
}
/// If the ParaId corresponds to a lease holding teyrchain, then downgrade it to a
/// parathread (on-demand teyrchain)
fn make_parathread(id: ParaId) -> DispatchResult {
TEYRCHAINS.with(|x| {
let mut teyrchains = x.borrow_mut();
match teyrchains.binary_search(&id) {
Ok(i) => {
teyrchains.remove(i);
Ok(())
},
Err(_) => Err(DispatchError::Other("not teyrchain, so cannot `make_parathread`")),
}
})?;
PARATHREADS.with(|x| {
let mut parathreads = x.borrow_mut();
match parathreads.binary_search(&id) {
Ok(_) =>
Err(DispatchError::Other("already parathread, so cannot `make_parathread`")),
Err(i) => {
parathreads.insert(i, id);
Ok(())
},
}
})?;
OPERATIONS.with(|x| {
x.borrow_mut().push((
id,
frame_system::Pallet::<T>::block_number().saturated_into(),
false,
))
});
Ok(())
}
#[cfg(test)]
fn worst_head_data() -> HeadData {
vec![0u8; 1000].into()
}
#[cfg(test)]
fn worst_validation_code() -> ValidationCode {
let validation_code = vec![0u8; 1000];
validation_code.into()
}
#[cfg(test)]
fn execute_pending_transitions() {}
}
impl<T: frame_system::Config> TestRegistrar<T> {
pub fn operations() -> Vec<(ParaId, BlockNumberFor<T>, bool)> {
OPERATIONS
.with(|x| x.borrow().iter().map(|(p, b, c)| (*p, (*b).into(), *c)).collect::<Vec<_>>())
}
#[allow(dead_code)]
pub fn teyrchains() -> Vec<ParaId> {
TEYRCHAINS.with(|x| x.borrow().clone())
}
#[allow(dead_code)]
pub fn parathreads() -> Vec<ParaId> {
PARATHREADS.with(|x| x.borrow().clone())
}
#[allow(dead_code)]
pub fn clear_storage() {
OPERATIONS.with(|x| x.borrow_mut().clear());
TEYRCHAINS.with(|x| x.borrow_mut().clear());
PARATHREADS.with(|x| x.borrow_mut().clear());
MANAGERS.with(|x| x.borrow_mut().clear());
}
}
/// A very dumb implementation of `EstimateNextSessionRotation`. At the moment of writing, this
/// is more to satisfy type requirements rather than to test anything.
pub struct TestNextSessionRotation;
impl frame_support::traits::EstimateNextSessionRotation<u32> for TestNextSessionRotation {
fn average_session_length() -> u32 {
10
}
fn estimate_current_session_progress(_now: u32) -> (Option<Permill>, Weight) {
(None, Weight::zero())
}
fn estimate_next_session_rotation(_now: u32) -> (Option<u32>, Weight) {
(None, Weight::zero())
}
}
pub fn validators_public_keys(
validators: &[Sr25519Keyring],
) -> Vec<pezkuwi_primitives::ValidatorId> {
validators.iter().map(|v| v.public().into()).collect()
}
pub fn conclude_pvf_checking<T: paras::Config>(
validation_code: &ValidationCode,
validators: &[Sr25519Keyring],
session_index: SessionIndex,
) {
let num_required = pezkuwi_primitives::supermajority_threshold(validators.len());
validators.iter().enumerate().take(num_required).for_each(|(idx, key)| {
let validator_index = idx as u32;
let statement = PvfCheckStatement {
accept: true,
subject: validation_code.hash(),
session_index,
validator_index: validator_index.into(),
};
let signature = key.sign(&statement.signing_payload());
let _ = paras::Pallet::<T>::include_pvf_check_statement(
frame_system::Origin::<T>::None.into(),
statement,
signature.into(),
);
});
}
@@ -0,0 +1,213 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Benchmarking for paras_registrar pallet
#[cfg(feature = "runtime-benchmarks")]
use super::{Pallet as Registrar, *};
use crate::traits::Registrar as RegistrarT;
use frame_support::assert_ok;
use frame_system::RawOrigin;
use pezkuwi_primitives::{MAX_CODE_SIZE, MAX_HEAD_DATA_SIZE, MIN_CODE_SIZE};
use pezkuwi_runtime_teyrchains::{paras, shared, Origin as ParaOrigin};
use sp_runtime::traits::Bounded;
use frame_benchmarking::v2::*;
fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
let events = frame_system::Pallet::<T>::events();
let system_event: <T as frame_system::Config>::RuntimeEvent = generic_event.into();
// compare to the last event record
let frame_system::EventRecord { event, .. } = &events[events.len() - 1];
assert_eq!(event, &system_event);
}
fn register_para<T: Config>(id: u32) -> ParaId {
let para = ParaId::from(id);
let genesis_head = Registrar::<T>::worst_head_data();
let validation_code = Registrar::<T>::worst_validation_code();
let caller: T::AccountId = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
assert_ok!(Registrar::<T>::reserve(RawOrigin::Signed(caller.clone()).into()));
assert_ok!(Registrar::<T>::register(
RawOrigin::Signed(caller).into(),
para,
genesis_head,
validation_code.clone()
));
assert_ok!(pezkuwi_runtime_teyrchains::paras::Pallet::<T>::add_trusted_validation_code(
frame_system::Origin::<T>::Root.into(),
validation_code,
));
return para;
}
fn para_origin(id: u32) -> ParaOrigin {
ParaOrigin::Teyrchain(id.into())
}
// This function moves forward to the next scheduled session for teyrchain lifecycle upgrades.
fn next_scheduled_session<T: Config>() {
shared::Pallet::<T>::set_session_index(shared::Pallet::<T>::scheduled_session());
paras::Pallet::<T>::test_on_new_session();
}
#[benchmarks(
where ParaOrigin: Into<<T as frame_system::Config>::RuntimeOrigin>,
)]
mod benchmarks {
use super::*;
#[benchmark]
fn reserve() -> Result<(), BenchmarkError> {
let caller: T::AccountId = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()));
assert_last_event::<T>(
Event::<T>::Reserved { para_id: LOWEST_PUBLIC_ID, who: caller }.into(),
);
assert!(Paras::<T>::get(LOWEST_PUBLIC_ID).is_some());
assert_eq!(paras::Pallet::<T>::lifecycle(LOWEST_PUBLIC_ID), None);
Ok(())
}
#[benchmark]
fn register() -> Result<(), BenchmarkError> {
let para = LOWEST_PUBLIC_ID;
let genesis_head = Registrar::<T>::worst_head_data();
let validation_code = Registrar::<T>::worst_validation_code();
let caller: T::AccountId = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
assert_ok!(Registrar::<T>::reserve(RawOrigin::Signed(caller.clone()).into()));
#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), para, genesis_head, validation_code.clone());
assert_last_event::<T>(Event::<T>::Registered { para_id: para, manager: caller }.into());
assert_eq!(paras::Pallet::<T>::lifecycle(para), Some(ParaLifecycle::Onboarding));
assert_ok!(pezkuwi_runtime_teyrchains::paras::Pallet::<T>::add_trusted_validation_code(
frame_system::Origin::<T>::Root.into(),
validation_code,
));
next_scheduled_session::<T>();
assert_eq!(paras::Pallet::<T>::lifecycle(para), Some(ParaLifecycle::Parathread));
Ok(())
}
#[benchmark]
fn force_register() -> Result<(), BenchmarkError> {
let manager: T::AccountId = account("manager", 0, 0);
let deposit = 0u32.into();
let para = ParaId::from(69);
let genesis_head = Registrar::<T>::worst_head_data();
let validation_code = Registrar::<T>::worst_validation_code();
#[extrinsic_call]
_(RawOrigin::Root, manager.clone(), deposit, para, genesis_head, validation_code.clone());
assert_last_event::<T>(Event::<T>::Registered { para_id: para, manager }.into());
assert_eq!(paras::Pallet::<T>::lifecycle(para), Some(ParaLifecycle::Onboarding));
assert_ok!(pezkuwi_runtime_teyrchains::paras::Pallet::<T>::add_trusted_validation_code(
frame_system::Origin::<T>::Root.into(),
validation_code,
));
next_scheduled_session::<T>();
assert_eq!(paras::Pallet::<T>::lifecycle(para), Some(ParaLifecycle::Parathread));
Ok(())
}
#[benchmark]
fn deregister() -> Result<(), BenchmarkError> {
let para = register_para::<T>(LOWEST_PUBLIC_ID.into());
next_scheduled_session::<T>();
let caller: T::AccountId = whitelisted_caller();
#[extrinsic_call]
_(RawOrigin::Signed(caller), para);
assert_last_event::<T>(Event::<T>::Deregistered { para_id: para }.into());
Ok(())
}
#[benchmark]
fn swap() -> Result<(), BenchmarkError> {
// On demand teyrchain
let parathread = register_para::<T>(LOWEST_PUBLIC_ID.into());
let teyrchain = register_para::<T>((LOWEST_PUBLIC_ID + 1).into());
let teyrchain_origin = para_origin(teyrchain.into());
// Actually finish registration process
next_scheduled_session::<T>();
// Upgrade the teyrchain
Registrar::<T>::make_teyrchain(teyrchain)?;
next_scheduled_session::<T>();
assert_eq!(paras::Pallet::<T>::lifecycle(teyrchain), Some(ParaLifecycle::Teyrchain));
assert_eq!(paras::Pallet::<T>::lifecycle(parathread), Some(ParaLifecycle::Parathread));
let caller: T::AccountId = whitelisted_caller();
Registrar::<T>::swap(teyrchain_origin.into(), teyrchain, parathread)?;
#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), parathread, teyrchain);
next_scheduled_session::<T>();
// Swapped!
assert_eq!(paras::Pallet::<T>::lifecycle(teyrchain), Some(ParaLifecycle::Parathread));
assert_eq!(paras::Pallet::<T>::lifecycle(parathread), Some(ParaLifecycle::Teyrchain));
Ok(())
}
#[benchmark]
fn schedule_code_upgrade(
b: Linear<MIN_CODE_SIZE, MAX_CODE_SIZE>,
) -> Result<(), BenchmarkError> {
let new_code = ValidationCode(vec![0; b as usize]);
let para_id = ParaId::from(1000);
#[extrinsic_call]
_(RawOrigin::Root, para_id, new_code);
Ok(())
}
#[benchmark]
fn set_current_head(b: Linear<1, MAX_HEAD_DATA_SIZE>) -> Result<(), BenchmarkError> {
let new_head = HeadData(vec![0; b as usize]);
let para_id = ParaId::from(1000);
#[extrinsic_call]
_(RawOrigin::Root, para_id, new_head);
Ok(())
}
impl_benchmark_test_suite!(
Registrar,
crate::integration_tests::new_test_ext(),
crate::integration_tests::Test,
);
}
@@ -0,0 +1,69 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
use super::*;
use frame_support::traits::{Contains, UncheckedOnRuntimeUpgrade};
#[derive(Encode, Decode)]
pub struct ParaInfoV1<Account, Balance> {
manager: Account,
deposit: Balance,
locked: bool,
}
pub struct VersionUncheckedMigrateToV1<T, UnlockParaIds>(
core::marker::PhantomData<(T, UnlockParaIds)>,
);
impl<T: Config, UnlockParaIds: Contains<ParaId>> UncheckedOnRuntimeUpgrade
for VersionUncheckedMigrateToV1<T, UnlockParaIds>
{
fn on_runtime_upgrade() -> Weight {
let mut count = 0u64;
Paras::<T>::translate::<ParaInfoV1<T::AccountId, BalanceOf<T>>, _>(|key, v1| {
count.saturating_inc();
Some(ParaInfo {
manager: v1.manager,
deposit: v1.deposit,
locked: if UnlockParaIds::contains(&key) { None } else { Some(v1.locked) },
})
});
log::info!(target: "runtime::registrar", "Upgraded {} storages to version 1", count);
T::DbWeight::get().reads_writes(count, count)
}
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> {
Ok((Paras::<T>::iter_keys().count() as u32).encode())
}
#[cfg(feature = "try-runtime")]
fn post_upgrade(state: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
let old_count = u32::decode(&mut &state[..]).expect("Known good");
let new_count = Paras::<T>::iter_values().count() as u32;
ensure!(old_count == new_count, "Paras count should not change");
Ok(())
}
}
pub type MigrateToV1<T, UnlockParaIds> = frame_support::migrations::VersionedMigration<
0,
1,
VersionUncheckedMigrateToV1<T, UnlockParaIds>,
super::Pallet<T>,
<T as frame_system::Config>::DbWeight,
>;
@@ -0,0 +1,250 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Mocking utilities for testing in paras_registrar pallet.
#[cfg(test)]
use super::*;
use crate::paras_registrar;
use alloc::collections::btree_map::BTreeMap;
use frame_support::{derive_impl, parameter_types};
use frame_system::limits;
use pezkuwi_primitives::{Balance, BlockNumber, MAX_CODE_SIZE};
use pezkuwi_runtime_teyrchains::{configuration, origin, shared};
use sp_core::{ConstUint, H256};
use sp_io::TestExternalities;
use sp_keyring::Sr25519Keyring;
use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup},
transaction_validity::TransactionPriority,
BuildStorage, Perbill,
};
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlockU32<Test>;
frame_support::construct_runtime!(
pub enum Test
{
System: frame_system,
Balances: pallet_balances,
Configuration: configuration,
Teyrchains: paras,
ParasShared: shared,
Registrar: paras_registrar,
TeyrchainsOrigin: origin,
}
);
impl<C> frame_system::offchain::CreateTransactionBase<C> for Test
where
RuntimeCall: From<C>,
{
type Extrinsic = UncheckedExtrinsic;
type RuntimeCall = RuntimeCall;
}
impl<C> frame_system::offchain::CreateBare<C> for Test
where
RuntimeCall: From<C>,
{
fn create_bare(call: Self::RuntimeCall) -> Self::Extrinsic {
UncheckedExtrinsic::new_bare(call)
}
}
const NORMAL_RATIO: Perbill = Perbill::from_percent(75);
parameter_types! {
pub BlockWeights: limits::BlockWeights =
frame_system::limits::BlockWeights::simple_max(Weight::from_parts(1024, u64::MAX));
pub BlockLength: limits::BlockLength =
limits::BlockLength::max_with_normal_ratio(4 * 1024 * 1024, NORMAL_RATIO);
}
#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
impl frame_system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
type Nonce = u64;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<u64>;
type Block = Block;
type RuntimeEvent = RuntimeEvent;
type DbWeight = ();
type BlockWeights = BlockWeights;
type BlockLength = BlockLength;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<u128>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
}
parameter_types! {
pub const ExistentialDeposit: Balance = 1;
}
#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
impl pallet_balances::Config for Test {
type Balance = Balance;
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
}
impl shared::Config for Test {
type DisabledValidators = ();
}
impl origin::Config for Test {}
parameter_types! {
pub const ParasUnsignedPriority: TransactionPriority = TransactionPriority::max_value();
}
impl paras::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = paras::TestWeightInfo;
type UnsignedPriority = ParasUnsignedPriority;
type QueueFootprinter = ();
type NextSessionRotation = crate::mock::TestNextSessionRotation;
type OnNewHead = ();
type AssignCoretime = ();
type Fungible = Balances;
type CooldownRemovalMultiplier = ConstUint<1>;
type AuthorizeCurrentCodeOrigin = frame_system::EnsureRoot<u64>;
}
impl configuration::Config for Test {
type WeightInfo = configuration::TestWeightInfo;
}
parameter_types! {
pub const ParaDeposit: Balance = 10;
pub const DataDepositPerByte: Balance = 1;
pub const MaxRetries: u32 = 3;
}
impl Config for Test {
type RuntimeOrigin = RuntimeOrigin;
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type OnSwap = MockSwap;
type ParaDeposit = ParaDeposit;
type DataDepositPerByte = DataDepositPerByte;
type WeightInfo = TestWeightInfo;
}
pub fn new_test_ext() -> TestExternalities {
let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
configuration::GenesisConfig::<Test> {
config: configuration::HostConfiguration {
max_code_size: MAX_CODE_SIZE,
max_head_data_size: 1 * 1024 * 1024, // 1 MB
..Default::default()
},
}
.assimilate_storage(&mut t)
.unwrap();
pallet_balances::GenesisConfig::<Test> {
balances: vec![(1, 10_000_000), (2, 10_000_000), (3, 10_000_000)],
..Default::default()
}
.assimilate_storage(&mut t)
.unwrap();
t.into()
}
parameter_types! {
pub static SwapData: BTreeMap<ParaId, u64> = BTreeMap::new();
}
pub struct MockSwap;
impl OnSwap for MockSwap {
fn on_swap(one: ParaId, other: ParaId) {
let mut swap_data = SwapData::get();
let one_data = swap_data.remove(&one).unwrap_or_default();
let other_data = swap_data.remove(&other).unwrap_or_default();
swap_data.insert(one, other_data);
swap_data.insert(other, one_data);
SwapData::set(swap_data);
}
}
pub const BLOCKS_PER_SESSION: u32 = 3;
pub const VALIDATORS: &[Sr25519Keyring] = &[
Sr25519Keyring::Alice,
Sr25519Keyring::Bob,
Sr25519Keyring::Charlie,
Sr25519Keyring::Dave,
Sr25519Keyring::Ferdie,
];
pub fn run_to_block(n: BlockNumber) {
// NOTE that this function only simulates modules of interest. Depending on new pallet may
// require adding it here.
System::run_to_block_with::<AllPalletsWithSystem>(
n,
frame_system::RunToBlockHooks::default().before_finalize(|bn| {
// Session change every 3 blocks.
if (bn + 1) % BLOCKS_PER_SESSION == 0 {
let session_index = shared::CurrentSessionIndex::<Test>::get() + 1;
let validators_pub_keys = VALIDATORS.iter().map(|v| v.public().into()).collect();
shared::Pallet::<Test>::set_session_index(session_index);
shared::Pallet::<Test>::set_active_validators_ascending(validators_pub_keys);
Teyrchains::test_on_new_session();
}
}),
);
}
pub fn run_to_session(n: BlockNumber) {
let block_number = n * BLOCKS_PER_SESSION;
run_to_block(block_number);
}
pub fn test_genesis_head(size: usize) -> HeadData {
HeadData(vec![0u8; size])
}
pub fn test_validation_code(size: usize) -> ValidationCode {
let validation_code = vec![0u8; size as usize];
ValidationCode(validation_code)
}
pub fn para_origin(id: ParaId) -> RuntimeOrigin {
pezkuwi_runtime_teyrchains::Origin::Teyrchain(id).into()
}
pub fn max_code_size() -> u32 {
configuration::ActiveConfig::<Test>::get().max_code_size
}
pub fn max_head_size() -> u32 {
configuration::ActiveConfig::<Test>::get().max_head_data_size
}
@@ -0,0 +1,733 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Pallet to handle teyrchain registration and related fund management.
//! In essence this is a simple wrapper around `paras`.
pub mod migration;
use alloc::{vec, vec::Vec};
use core::result;
use frame_support::{
dispatch::DispatchResult,
ensure,
pallet_prelude::Weight,
traits::{Currency, Get, ReservableCurrency},
};
use frame_system::{self, ensure_root, ensure_signed};
use pezkuwi_primitives::{HeadData, Id as ParaId, ValidationCode, LOWEST_PUBLIC_ID, MIN_CODE_SIZE};
use pezkuwi_runtime_teyrchains::{
configuration, ensure_teyrchain,
paras::{self, ParaGenesisArgs, UpgradeStrategy},
Origin, ParaLifecycle,
};
use crate::traits::{OnSwap, Registrar};
use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
pub use pallet::*;
use pezkuwi_runtime_teyrchains::paras::{OnNewHead, ParaKind};
use scale_info::TypeInfo;
use sp_runtime::{
traits::{CheckedSub, Saturating},
RuntimeDebug,
};
#[derive(
Encode,
Decode,
Clone,
PartialEq,
Eq,
Default,
RuntimeDebug,
TypeInfo,
MaxEncodedLen,
DecodeWithMemTracking,
)]
pub struct ParaInfo<Account, Balance> {
/// The account that has placed a deposit for registering this para.
pub manager: Account,
/// The amount reserved by the `manager` account for the registration.
pub deposit: Balance,
/// Whether the para registration should be locked from being controlled by the manager.
/// None means the lock had not been explicitly set, and should be treated as false.
pub locked: Option<bool>,
}
impl<Account, Balance> ParaInfo<Account, Balance> {
/// Returns if the para is locked.
pub fn is_locked(&self) -> bool {
self.locked.unwrap_or(false)
}
}
type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
pub trait WeightInfo {
fn reserve() -> Weight;
fn register() -> Weight;
fn force_register() -> Weight;
fn deregister() -> Weight;
fn swap() -> Weight;
fn schedule_code_upgrade(b: u32) -> Weight;
fn set_current_head(b: u32) -> Weight;
}
pub struct TestWeightInfo;
impl WeightInfo for TestWeightInfo {
fn reserve() -> Weight {
Weight::zero()
}
fn register() -> Weight {
Weight::zero()
}
fn force_register() -> Weight {
Weight::zero()
}
fn deregister() -> Weight {
Weight::zero()
}
fn swap() -> Weight {
Weight::zero()
}
fn schedule_code_upgrade(_b: u32) -> Weight {
Weight::zero()
}
fn set_current_head(_b: u32) -> Weight {
Weight::zero()
}
}
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
/// The in-code storage version.
const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
#[pallet::pallet]
#[pallet::without_storage_info]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);
#[pallet::config]
#[pallet::disable_frame_system_supertrait_check]
pub trait Config: configuration::Config + paras::Config {
/// The overarching event type.
#[allow(deprecated)]
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// The aggregated origin type must support the `teyrchains` origin. We require that we can
/// infallibly convert between this origin and the system origin, but in reality, they're
/// the same type, we just can't express that to the Rust type system without writing a
/// `where` clause everywhere.
type RuntimeOrigin: From<<Self as frame_system::Config>::RuntimeOrigin>
+ Into<result::Result<Origin, <Self as Config>::RuntimeOrigin>>;
/// The system's currency for on-demand teyrchain payment.
type Currency: ReservableCurrency<Self::AccountId>;
/// Runtime hook for when a lease holding teyrchain and on-demand teyrchain swap.
type OnSwap: crate::traits::OnSwap;
/// The deposit to be paid to run a on-demand teyrchain.
/// This should include the cost for storing the genesis head and validation code.
#[pallet::constant]
type ParaDeposit: Get<BalanceOf<Self>>;
/// The deposit to be paid per byte stored on chain.
#[pallet::constant]
type DataDepositPerByte: Get<BalanceOf<Self>>;
/// Weight Information for the Extrinsics in the Pallet
type WeightInfo: WeightInfo;
}
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
Registered { para_id: ParaId, manager: T::AccountId },
Deregistered { para_id: ParaId },
Reserved { para_id: ParaId, who: T::AccountId },
Swapped { para_id: ParaId, other_id: ParaId },
}
#[pallet::error]
pub enum Error<T> {
/// The ID is not registered.
NotRegistered,
/// The ID is already registered.
AlreadyRegistered,
/// The caller is not the owner of this Id.
NotOwner,
/// Invalid para code size.
CodeTooLarge,
/// Invalid para head data size.
HeadDataTooLarge,
/// Para is not a Teyrchain.
NotTeyrchain,
/// Para is not a Parathread (on-demand teyrchain).
NotParathread,
/// Cannot deregister para
CannotDeregister,
/// Cannot schedule downgrade of lease holding teyrchain to on-demand teyrchain
CannotDowngrade,
/// Cannot schedule upgrade of on-demand teyrchain to lease holding teyrchain
CannotUpgrade,
/// Para is locked from manipulation by the manager. Must use teyrchain or relay chain
/// governance.
ParaLocked,
/// The ID given for registration has not been reserved.
NotReserved,
/// The validation code is invalid.
InvalidCode,
/// Cannot perform a teyrchain slot / lifecycle swap. Check that the state of both paras
/// are correct for the swap to work.
CannotSwap,
}
/// Pending swap operations.
#[pallet::storage]
pub(super) type PendingSwap<T> = StorageMap<_, Twox64Concat, ParaId, ParaId>;
/// Amount held on deposit for each para and the original depositor.
///
/// The given account ID is responsible for registering the code and initial head data, but may
/// only do so if it isn't yet registered. (After that, it's up to governance to do so.)
#[pallet::storage]
pub type Paras<T: Config> =
StorageMap<_, Twox64Concat, ParaId, ParaInfo<T::AccountId, BalanceOf<T>>>;
/// The next free `ParaId`.
#[pallet::storage]
pub type NextFreeParaId<T> = StorageValue<_, ParaId, ValueQuery>;
#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
#[serde(skip)]
pub _config: core::marker::PhantomData<T>,
pub next_free_para_id: ParaId,
}
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
GenesisConfig { next_free_para_id: LOWEST_PUBLIC_ID, _config: Default::default() }
}
}
#[pallet::genesis_build]
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
fn build(&self) {
NextFreeParaId::<T>::put(self.next_free_para_id);
}
}
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
#[pallet::call]
impl<T: Config> Pallet<T> {
/// Register head data and validation code for a reserved Para Id.
///
/// ## Arguments
/// - `origin`: Must be called by a `Signed` origin.
/// - `id`: The para ID. Must be owned/managed by the `origin` signing account.
/// - `genesis_head`: The genesis head data of the teyrchain/thread.
/// - `validation_code`: The initial validation code of the teyrchain/thread.
///
/// ## Deposits/Fees
/// The account with the originating signature must reserve a deposit.
///
/// The deposit is required to cover the costs associated with storing the genesis head
/// data and the validation code.
/// This accounts for the potential to store validation code of a size up to the
/// `max_code_size`, as defined in the configuration pallet
///
/// Anything already reserved previously for this para ID is accounted for.
///
/// ## Events
/// The `Registered` event is emitted in case of success.
#[pallet::call_index(0)]
#[pallet::weight(<T as Config>::WeightInfo::register())]
pub fn register(
origin: OriginFor<T>,
id: ParaId,
genesis_head: HeadData,
validation_code: ValidationCode,
) -> DispatchResult {
let who = ensure_signed(origin)?;
Self::do_register(who, None, id, genesis_head, validation_code, true)?;
Ok(())
}
/// Force the registration of a Para Id on the relay chain.
///
/// This function must be called by a Root origin.
///
/// The deposit taken can be specified for this registration. Any `ParaId`
/// can be registered, including sub-1000 IDs which are System Teyrchains.
#[pallet::call_index(1)]
#[pallet::weight(<T as Config>::WeightInfo::force_register())]
pub fn force_register(
origin: OriginFor<T>,
who: T::AccountId,
deposit: BalanceOf<T>,
id: ParaId,
genesis_head: HeadData,
validation_code: ValidationCode,
) -> DispatchResult {
ensure_root(origin)?;
Self::do_register(who, Some(deposit), id, genesis_head, validation_code, false)
}
/// Deregister a Para Id, freeing all data and returning any deposit.
///
/// The caller must be Root, the `para` owner, or the `para` itself. The para must be an
/// on-demand teyrchain.
#[pallet::call_index(2)]
#[pallet::weight(<T as Config>::WeightInfo::deregister())]
pub fn deregister(origin: OriginFor<T>, id: ParaId) -> DispatchResult {
Self::ensure_root_para_or_owner(origin, id)?;
Self::do_deregister(id)
}
/// Swap a lease holding teyrchain with another teyrchain, either on-demand or lease
/// holding.
///
/// The origin must be Root, the `para` owner, or the `para` itself.
///
/// The swap will happen only if there is already an opposite swap pending. If there is not,
/// the swap will be stored in the pending swaps map, ready for a later confirmatory swap.
///
/// The `ParaId`s remain mapped to the same head data and code so external code can rely on
/// `ParaId` to be a long-term identifier of a notional "teyrchain". However, their
/// scheduling info (i.e. whether they're an on-demand teyrchain or lease holding
/// teyrchain), auction information and the auction deposit are switched.
#[pallet::call_index(3)]
#[pallet::weight(<T as Config>::WeightInfo::swap())]
pub fn swap(origin: OriginFor<T>, id: ParaId, other: ParaId) -> DispatchResult {
Self::ensure_root_para_or_owner(origin, id)?;
// If `id` and `other` is the same id, we treat this as a "clear" function, and exit
// early, since swapping the same id would otherwise be a noop.
if id == other {
PendingSwap::<T>::remove(id);
return Ok(());
}
// Sanity check that `id` is even a para.
let id_lifecycle =
paras::Pallet::<T>::lifecycle(id).ok_or(Error::<T>::NotRegistered)?;
if PendingSwap::<T>::get(other) == Some(id) {
let other_lifecycle =
paras::Pallet::<T>::lifecycle(other).ok_or(Error::<T>::NotRegistered)?;
// identify which is a lease holding teyrchain and which is a parathread (on-demand
// teyrchain)
if id_lifecycle == ParaLifecycle::Teyrchain &&
other_lifecycle == ParaLifecycle::Parathread
{
Self::do_thread_and_chain_swap(id, other);
} else if id_lifecycle == ParaLifecycle::Parathread &&
other_lifecycle == ParaLifecycle::Teyrchain
{
Self::do_thread_and_chain_swap(other, id);
} else if id_lifecycle == ParaLifecycle::Teyrchain &&
other_lifecycle == ParaLifecycle::Teyrchain
{
// If both chains are currently teyrchains, there is nothing funny we
// need to do for their lifecycle management, just swap the underlying
// data.
T::OnSwap::on_swap(id, other);
} else {
return Err(Error::<T>::CannotSwap.into());
}
Self::deposit_event(Event::<T>::Swapped { para_id: id, other_id: other });
PendingSwap::<T>::remove(other);
} else {
PendingSwap::<T>::insert(id, other);
}
Ok(())
}
/// Remove a manager lock from a para. This will allow the manager of a
/// previously locked para to deregister or swap a para without using governance.
///
/// Can only be called by the Root origin or the teyrchain.
#[pallet::call_index(4)]
#[pallet::weight(T::DbWeight::get().reads_writes(1, 1))]
pub fn remove_lock(origin: OriginFor<T>, para: ParaId) -> DispatchResult {
Self::ensure_root_or_para(origin, para)?;
<Self as Registrar>::remove_lock(para);
Ok(())
}
/// Reserve a Para Id on the relay chain.
///
/// This function will reserve a new Para Id to be owned/managed by the origin account.
/// The origin account is able to register head data and validation code using `register` to
/// create an on-demand teyrchain. Using the Slots pallet, an on-demand teyrchain can then
/// be upgraded to a lease holding teyrchain.
///
/// ## Arguments
/// - `origin`: Must be called by a `Signed` origin. Becomes the manager/owner of the new
/// para ID.
///
/// ## Deposits/Fees
/// The origin must reserve a deposit of `ParaDeposit` for the registration.
///
/// ## Events
/// The `Reserved` event is emitted in case of success, which provides the ID reserved for
/// use.
#[pallet::call_index(5)]
#[pallet::weight(<T as Config>::WeightInfo::reserve())]
pub fn reserve(origin: OriginFor<T>) -> DispatchResult {
let who = ensure_signed(origin)?;
let id = NextFreeParaId::<T>::get().max(LOWEST_PUBLIC_ID);
Self::do_reserve(who, None, id)?;
NextFreeParaId::<T>::set(id + 1);
Ok(())
}
/// Add a manager lock from a para. This will prevent the manager of a
/// para to deregister or swap a para.
///
/// Can be called by Root, the teyrchain, or the teyrchain manager if the teyrchain is
/// unlocked.
#[pallet::call_index(6)]
#[pallet::weight(T::DbWeight::get().reads_writes(1, 1))]
pub fn add_lock(origin: OriginFor<T>, para: ParaId) -> DispatchResult {
Self::ensure_root_para_or_owner(origin, para)?;
<Self as Registrar>::apply_lock(para);
Ok(())
}
/// Schedule a teyrchain upgrade.
///
/// This will kick off a check of `new_code` by all validators. After the majority of the
/// validators have reported on the validity of the code, the code will either be enacted
/// or the upgrade will be rejected. If the code will be enacted, the current code of the
/// teyrchain will be overwritten directly. This means that any PoV will be checked by this
/// new code. The teyrchain itself will not be informed explicitly that the validation code
/// has changed.
///
/// Can be called by Root, the teyrchain, or the teyrchain manager if the teyrchain is
/// unlocked.
#[pallet::call_index(7)]
#[pallet::weight(<T as Config>::WeightInfo::schedule_code_upgrade(new_code.0.len() as u32))]
pub fn schedule_code_upgrade(
origin: OriginFor<T>,
para: ParaId,
new_code: ValidationCode,
) -> DispatchResult {
Self::ensure_root_para_or_owner(origin, para)?;
pezkuwi_runtime_teyrchains::schedule_code_upgrade::<T>(
para,
new_code,
UpgradeStrategy::ApplyAtExpectedBlock,
)?;
Ok(())
}
/// Set the teyrchain's current head.
///
/// Can be called by Root, the teyrchain, or the teyrchain manager if the teyrchain is
/// unlocked.
#[pallet::call_index(8)]
#[pallet::weight(<T as Config>::WeightInfo::set_current_head(new_head.0.len() as u32))]
pub fn set_current_head(
origin: OriginFor<T>,
para: ParaId,
new_head: HeadData,
) -> DispatchResult {
Self::ensure_root_para_or_owner(origin, para)?;
pezkuwi_runtime_teyrchains::set_current_head::<T>(para, new_head);
Ok(())
}
}
}
impl<T: Config> Registrar for Pallet<T> {
type AccountId = T::AccountId;
/// Return the manager `AccountId` of a para if one exists.
fn manager_of(id: ParaId) -> Option<T::AccountId> {
Some(Paras::<T>::get(id)?.manager)
}
// All lease holding teyrchains. Ordered ascending by ParaId. On-demand teyrchains are not
// included.
fn teyrchains() -> Vec<ParaId> {
paras::Teyrchains::<T>::get()
}
// Return if a para is a parathread (on-demand teyrchain)
fn is_parathread(id: ParaId) -> bool {
paras::Pallet::<T>::is_parathread(id)
}
// Return if a para is a lease holding teyrchain
fn is_teyrchain(id: ParaId) -> bool {
paras::Pallet::<T>::is_teyrchain(id)
}
// Apply a lock to the teyrchain.
fn apply_lock(id: ParaId) {
Paras::<T>::mutate(id, |x| x.as_mut().map(|info| info.locked = Some(true)));
}
// Remove a lock from the teyrchain.
fn remove_lock(id: ParaId) {
Paras::<T>::mutate(id, |x| x.as_mut().map(|info| info.locked = Some(false)));
}
// Register a Para ID under control of `manager`.
//
// Note this is a backend registration API, so verification of ParaId
// is not done here to prevent.
fn register(
manager: T::AccountId,
id: ParaId,
genesis_head: HeadData,
validation_code: ValidationCode,
) -> DispatchResult {
Self::do_register(manager, None, id, genesis_head, validation_code, false)
}
// Deregister a Para ID, free any data, and return any deposits.
fn deregister(id: ParaId) -> DispatchResult {
Self::do_deregister(id)
}
// Upgrade a registered on-demand teyrchain into a lease holding teyrchain.
fn make_teyrchain(id: ParaId) -> DispatchResult {
// Para backend should think this is an on-demand teyrchain...
ensure!(
paras::Pallet::<T>::lifecycle(id) == Some(ParaLifecycle::Parathread),
Error::<T>::NotParathread
);
pezkuwi_runtime_teyrchains::schedule_parathread_upgrade::<T>(id)
.map_err(|_| Error::<T>::CannotUpgrade)?;
Ok(())
}
// Downgrade a registered para into a parathread (on-demand teyrchain).
fn make_parathread(id: ParaId) -> DispatchResult {
// Para backend should think this is a teyrchain...
ensure!(
paras::Pallet::<T>::lifecycle(id) == Some(ParaLifecycle::Teyrchain),
Error::<T>::NotTeyrchain
);
pezkuwi_runtime_teyrchains::schedule_teyrchain_downgrade::<T>(id)
.map_err(|_| Error::<T>::CannotDowngrade)?;
Ok(())
}
#[cfg(any(feature = "runtime-benchmarks", test))]
fn worst_head_data() -> HeadData {
let max_head_size = configuration::ActiveConfig::<T>::get().max_head_data_size;
assert!(max_head_size > 0, "max_head_data can't be zero for generating worst head data.");
vec![0u8; max_head_size as usize].into()
}
#[cfg(any(feature = "runtime-benchmarks", test))]
fn worst_validation_code() -> ValidationCode {
let max_code_size = configuration::ActiveConfig::<T>::get().max_code_size;
assert!(max_code_size > 0, "max_code_size can't be zero for generating worst code data.");
let validation_code = vec![0u8; max_code_size as usize];
validation_code.into()
}
#[cfg(any(feature = "runtime-benchmarks", test))]
fn execute_pending_transitions() {
use pezkuwi_runtime_teyrchains::shared;
shared::Pallet::<T>::set_session_index(shared::Pallet::<T>::scheduled_session());
paras::Pallet::<T>::test_on_new_session();
}
}
impl<T: Config> Pallet<T> {
/// Ensure the origin is one of Root, the `para` owner, or the `para` itself.
/// If the origin is the `para` owner, the `para` must be unlocked.
fn ensure_root_para_or_owner(
origin: <T as frame_system::Config>::RuntimeOrigin,
id: ParaId,
) -> DispatchResult {
if let Ok(who) = ensure_signed(origin.clone()) {
let para_info = Paras::<T>::get(id).ok_or(Error::<T>::NotRegistered)?;
if para_info.manager == who {
ensure!(!para_info.is_locked(), Error::<T>::ParaLocked);
return Ok(());
}
}
Self::ensure_root_or_para(origin, id)
}
/// Ensure the origin is one of Root or the `para` itself.
fn ensure_root_or_para(
origin: <T as frame_system::Config>::RuntimeOrigin,
id: ParaId,
) -> DispatchResult {
if ensure_root(origin.clone()).is_ok() {
return Ok(());
}
let caller_id = ensure_teyrchain(<T as Config>::RuntimeOrigin::from(origin))?;
// Check if matching para id...
ensure!(caller_id == id, Error::<T>::NotOwner);
Ok(())
}
fn do_reserve(
who: T::AccountId,
deposit_override: Option<BalanceOf<T>>,
id: ParaId,
) -> DispatchResult {
ensure!(!Paras::<T>::contains_key(id), Error::<T>::AlreadyRegistered);
ensure!(paras::Pallet::<T>::lifecycle(id).is_none(), Error::<T>::AlreadyRegistered);
let deposit = deposit_override.unwrap_or_else(T::ParaDeposit::get);
<T as Config>::Currency::reserve(&who, deposit)?;
let info = ParaInfo { manager: who.clone(), deposit, locked: None };
Paras::<T>::insert(id, info);
Self::deposit_event(Event::<T>::Reserved { para_id: id, who });
Ok(())
}
/// Attempt to register a new Para Id under management of `who` in the
/// system with the given information.
fn do_register(
who: T::AccountId,
deposit_override: Option<BalanceOf<T>>,
id: ParaId,
genesis_head: HeadData,
validation_code: ValidationCode,
ensure_reserved: bool,
) -> DispatchResult {
let deposited = if let Some(para_data) = Paras::<T>::get(id) {
ensure!(para_data.manager == who, Error::<T>::NotOwner);
ensure!(!para_data.is_locked(), Error::<T>::ParaLocked);
para_data.deposit
} else {
ensure!(!ensure_reserved, Error::<T>::NotReserved);
Default::default()
};
ensure!(paras::Pallet::<T>::lifecycle(id).is_none(), Error::<T>::AlreadyRegistered);
let (genesis, deposit) =
Self::validate_onboarding_data(genesis_head, validation_code, ParaKind::Parathread)?;
let deposit = deposit_override.unwrap_or(deposit);
if let Some(additional) = deposit.checked_sub(&deposited) {
<T as Config>::Currency::reserve(&who, additional)?;
} else if let Some(rebate) = deposited.checked_sub(&deposit) {
<T as Config>::Currency::unreserve(&who, rebate);
};
let info = ParaInfo { manager: who.clone(), deposit, locked: None };
Paras::<T>::insert(id, info);
// We check above that para has no lifecycle, so this should not fail.
let res = pezkuwi_runtime_teyrchains::schedule_para_initialize::<T>(id, genesis);
debug_assert!(res.is_ok());
Self::deposit_event(Event::<T>::Registered { para_id: id, manager: who });
Ok(())
}
/// Deregister a Para Id, freeing all data returning any deposit.
fn do_deregister(id: ParaId) -> DispatchResult {
match paras::Pallet::<T>::lifecycle(id) {
// Para must be a parathread (on-demand teyrchain), or not exist at all.
Some(ParaLifecycle::Parathread) | None => {},
_ => return Err(Error::<T>::NotParathread.into()),
}
pezkuwi_runtime_teyrchains::schedule_para_cleanup::<T>(id)
.map_err(|_| Error::<T>::CannotDeregister)?;
if let Some(info) = Paras::<T>::take(&id) {
<T as Config>::Currency::unreserve(&info.manager, info.deposit);
}
PendingSwap::<T>::remove(id);
Self::deposit_event(Event::<T>::Deregistered { para_id: id });
Ok(())
}
/// Verifies the onboarding data is valid for a para.
///
/// Returns `ParaGenesisArgs` and the deposit needed for the data.
fn validate_onboarding_data(
genesis_head: HeadData,
validation_code: ValidationCode,
para_kind: ParaKind,
) -> Result<(ParaGenesisArgs, BalanceOf<T>), sp_runtime::DispatchError> {
let config = configuration::ActiveConfig::<T>::get();
ensure!(validation_code.0.len() >= MIN_CODE_SIZE as usize, Error::<T>::InvalidCode);
ensure!(validation_code.0.len() <= config.max_code_size as usize, Error::<T>::CodeTooLarge);
ensure!(
genesis_head.0.len() <= config.max_head_data_size as usize,
Error::<T>::HeadDataTooLarge
);
let per_byte_fee = T::DataDepositPerByte::get();
let deposit = T::ParaDeposit::get()
.saturating_add(per_byte_fee.saturating_mul((genesis_head.0.len() as u32).into()))
.saturating_add(per_byte_fee.saturating_mul(config.max_code_size.into()));
Ok((ParaGenesisArgs { genesis_head, validation_code, para_kind }, deposit))
}
/// Swap a lease holding teyrchain and parathread (on-demand teyrchain), which involves
/// scheduling an appropriate lifecycle update.
fn do_thread_and_chain_swap(to_downgrade: ParaId, to_upgrade: ParaId) {
let res1 = pezkuwi_runtime_teyrchains::schedule_teyrchain_downgrade::<T>(to_downgrade);
debug_assert!(res1.is_ok());
let res2 = pezkuwi_runtime_teyrchains::schedule_parathread_upgrade::<T>(to_upgrade);
debug_assert!(res2.is_ok());
T::OnSwap::on_swap(to_upgrade, to_downgrade);
}
}
impl<T: Config> OnNewHead for Pallet<T> {
fn on_new_head(id: ParaId, _head: &HeadData) -> Weight {
// mark the teyrchain locked if the locked value is not already set
let mut writes = 0;
if let Some(mut info) = Paras::<T>::get(id) {
if info.locked.is_none() {
info.locked = Some(true);
Paras::<T>::insert(id, info);
writes += 1;
}
}
T::DbWeight::get().reads_writes(1, writes)
}
}
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
@@ -0,0 +1,588 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Tests for the paras_registrar pallet.
#[cfg(test)]
use super::*;
use crate::{
mock::conclude_pvf_checking, paras_registrar, paras_registrar::mock::*,
traits::Registrar as RegistrarTrait,
};
use frame_support::{assert_noop, assert_ok};
use pallet_balances::Error as BalancesError;
use pezkuwi_primitives::SessionIndex;
use sp_runtime::traits::BadOrigin;
#[test]
fn end_to_end_scenario_works() {
new_test_ext().execute_with(|| {
let para_id = LOWEST_PUBLIC_ID;
const START_SESSION_INDEX: SessionIndex = 1;
run_to_session(START_SESSION_INDEX);
// first para is not yet registered
assert!(!Teyrchains::is_parathread(para_id));
// We register the Para ID
let validation_code = test_validation_code(32);
assert_ok!(mock::Registrar::reserve(RuntimeOrigin::signed(1)));
assert_ok!(mock::Registrar::register(
RuntimeOrigin::signed(1),
para_id,
test_genesis_head(32),
validation_code.clone(),
));
conclude_pvf_checking::<Test>(&validation_code, VALIDATORS, START_SESSION_INDEX);
run_to_session(START_SESSION_INDEX + 2);
// It is now a parathread (on-demand teyrchain).
assert!(Teyrchains::is_parathread(para_id));
assert!(!Teyrchains::is_teyrchain(para_id));
// Some other external process will elevate on-demand to lease holding teyrchain
assert_ok!(mock::Registrar::make_teyrchain(para_id));
run_to_session(START_SESSION_INDEX + 4);
// It is now a lease holding teyrchain.
assert!(!Teyrchains::is_parathread(para_id));
assert!(Teyrchains::is_teyrchain(para_id));
// Turn it back into a parathread (on-demand teyrchain)
assert_ok!(mock::Registrar::make_parathread(para_id));
run_to_session(START_SESSION_INDEX + 6);
assert!(Teyrchains::is_parathread(para_id));
assert!(!Teyrchains::is_teyrchain(para_id));
// Deregister it
assert_ok!(mock::Registrar::deregister(RuntimeOrigin::root(), para_id,));
run_to_session(START_SESSION_INDEX + 8);
// It is nothing
assert!(!Teyrchains::is_parathread(para_id));
assert!(!Teyrchains::is_teyrchain(para_id));
});
}
#[test]
fn register_works() {
new_test_ext().execute_with(|| {
const START_SESSION_INDEX: SessionIndex = 1;
run_to_session(START_SESSION_INDEX);
let para_id = LOWEST_PUBLIC_ID;
assert!(!Teyrchains::is_parathread(para_id));
let validation_code = test_validation_code(32);
assert_ok!(mock::Registrar::reserve(RuntimeOrigin::signed(1)));
assert_eq!(Balances::reserved_balance(&1), <Test as Config>::ParaDeposit::get());
assert_ok!(mock::Registrar::register(
RuntimeOrigin::signed(1),
para_id,
test_genesis_head(32),
validation_code.clone(),
));
conclude_pvf_checking::<Test>(&validation_code, VALIDATORS, START_SESSION_INDEX);
run_to_session(START_SESSION_INDEX + 2);
assert!(Teyrchains::is_parathread(para_id));
// Even though the registered validation code has a smaller size than the maximum the
// para manager's deposit is reserved as though they registered the maximum-sized code.
// Consequently, they can upgrade their code to the maximum size at any point without
// additional cost.
let validation_code_deposit =
max_code_size() as BalanceOf<Test> * <Test as Config>::DataDepositPerByte::get();
let head_deposit = 32 * <Test as Config>::DataDepositPerByte::get();
assert_eq!(
Balances::reserved_balance(&1),
<Test as Config>::ParaDeposit::get() + head_deposit + validation_code_deposit
);
});
}
#[test]
fn schedule_code_upgrade_validates_code() {
new_test_ext().execute_with(|| {
const START_SESSION_INDEX: SessionIndex = 1;
run_to_session(START_SESSION_INDEX);
let para_id = LOWEST_PUBLIC_ID;
assert!(!Teyrchains::is_parathread(para_id));
let validation_code = test_validation_code(32);
assert_ok!(mock::Registrar::reserve(RuntimeOrigin::signed(1)));
assert_eq!(Balances::reserved_balance(&1), <Test as Config>::ParaDeposit::get());
assert_ok!(mock::Registrar::register(
RuntimeOrigin::signed(1),
para_id,
test_genesis_head(32),
validation_code.clone(),
));
conclude_pvf_checking::<Test>(&validation_code, VALIDATORS, START_SESSION_INDEX);
run_to_session(START_SESSION_INDEX + 2);
assert!(Teyrchains::is_parathread(para_id));
let new_code = test_validation_code(0);
assert_noop!(
mock::Registrar::schedule_code_upgrade(
RuntimeOrigin::signed(1),
para_id,
new_code.clone(),
),
paras::Error::<Test>::InvalidCode
);
let new_code = test_validation_code(max_code_size() as usize + 1);
assert_noop!(
mock::Registrar::schedule_code_upgrade(
RuntimeOrigin::signed(1),
para_id,
new_code.clone(),
),
paras::Error::<Test>::InvalidCode
);
});
}
#[test]
fn register_handles_basic_errors() {
new_test_ext().execute_with(|| {
let para_id = LOWEST_PUBLIC_ID;
assert_noop!(
mock::Registrar::register(
RuntimeOrigin::signed(1),
para_id,
test_genesis_head(max_head_size() as usize),
test_validation_code(max_code_size() as usize),
),
Error::<Test>::NotReserved
);
// Successfully register para
assert_ok!(mock::Registrar::reserve(RuntimeOrigin::signed(1)));
assert_noop!(
mock::Registrar::register(
RuntimeOrigin::signed(2),
para_id,
test_genesis_head(max_head_size() as usize),
test_validation_code(max_code_size() as usize),
),
Error::<Test>::NotOwner
);
assert_ok!(mock::Registrar::register(
RuntimeOrigin::signed(1),
para_id,
test_genesis_head(max_head_size() as usize),
test_validation_code(max_code_size() as usize),
));
// Can skip pre-check and deregister para which's still onboarding.
run_to_session(2);
assert_ok!(mock::Registrar::deregister(RuntimeOrigin::root(), para_id));
// Can't do it again
assert_noop!(
mock::Registrar::register(
RuntimeOrigin::signed(1),
para_id,
test_genesis_head(max_head_size() as usize),
test_validation_code(max_code_size() as usize),
),
Error::<Test>::NotReserved
);
// Head Size Check
assert_ok!(mock::Registrar::reserve(RuntimeOrigin::signed(2)));
assert_noop!(
mock::Registrar::register(
RuntimeOrigin::signed(2),
para_id + 1,
test_genesis_head((max_head_size() + 1) as usize),
test_validation_code(max_code_size() as usize),
),
Error::<Test>::HeadDataTooLarge
);
// Code Size Check
assert_noop!(
mock::Registrar::register(
RuntimeOrigin::signed(2),
para_id + 1,
test_genesis_head(max_head_size() as usize),
test_validation_code((max_code_size() + 1) as usize),
),
Error::<Test>::CodeTooLarge
);
// Needs enough funds for deposit
assert_noop!(
mock::Registrar::reserve(RuntimeOrigin::signed(1337)),
BalancesError::<Test, _>::InsufficientBalance
);
});
}
#[test]
fn deregister_works() {
new_test_ext().execute_with(|| {
const START_SESSION_INDEX: SessionIndex = 1;
run_to_session(START_SESSION_INDEX);
let para_id = LOWEST_PUBLIC_ID;
assert!(!Teyrchains::is_parathread(para_id));
let validation_code = test_validation_code(32);
assert_ok!(mock::Registrar::reserve(RuntimeOrigin::signed(1)));
assert_ok!(mock::Registrar::register(
RuntimeOrigin::signed(1),
para_id,
test_genesis_head(32),
validation_code.clone(),
));
conclude_pvf_checking::<Test>(&validation_code, VALIDATORS, START_SESSION_INDEX);
run_to_session(START_SESSION_INDEX + 2);
assert!(Teyrchains::is_parathread(para_id));
assert_ok!(mock::Registrar::deregister(RuntimeOrigin::root(), para_id,));
run_to_session(START_SESSION_INDEX + 4);
assert!(paras::Pallet::<Test>::lifecycle(para_id).is_none());
assert_eq!(Balances::reserved_balance(&1), 0);
});
}
#[test]
fn deregister_handles_basic_errors() {
new_test_ext().execute_with(|| {
const START_SESSION_INDEX: SessionIndex = 1;
run_to_session(START_SESSION_INDEX);
let para_id = LOWEST_PUBLIC_ID;
assert!(!Teyrchains::is_parathread(para_id));
let validation_code = test_validation_code(32);
assert_ok!(mock::Registrar::reserve(RuntimeOrigin::signed(1)));
assert_ok!(mock::Registrar::register(
RuntimeOrigin::signed(1),
para_id,
test_genesis_head(32),
validation_code.clone(),
));
conclude_pvf_checking::<Test>(&validation_code, VALIDATORS, START_SESSION_INDEX);
run_to_session(START_SESSION_INDEX + 2);
assert!(Teyrchains::is_parathread(para_id));
// Owner check
assert_noop!(mock::Registrar::deregister(RuntimeOrigin::signed(2), para_id,), BadOrigin);
assert_ok!(mock::Registrar::make_teyrchain(para_id));
run_to_session(START_SESSION_INDEX + 4);
// Cant directly deregister teyrchain
assert_noop!(
mock::Registrar::deregister(RuntimeOrigin::root(), para_id,),
Error::<Test>::NotParathread
);
});
}
#[test]
fn swap_works() {
new_test_ext().execute_with(|| {
const START_SESSION_INDEX: SessionIndex = 1;
run_to_session(START_SESSION_INDEX);
// Successfully register first two teyrchains
let para_1 = LOWEST_PUBLIC_ID;
let para_2 = LOWEST_PUBLIC_ID + 1;
let validation_code = test_validation_code(max_code_size() as usize);
assert_ok!(mock::Registrar::reserve(RuntimeOrigin::signed(1)));
assert_ok!(mock::Registrar::register(
RuntimeOrigin::signed(1),
para_1,
test_genesis_head(max_head_size() as usize),
validation_code.clone(),
));
assert_ok!(mock::Registrar::reserve(RuntimeOrigin::signed(2)));
assert_ok!(mock::Registrar::register(
RuntimeOrigin::signed(2),
para_2,
test_genesis_head(max_head_size() as usize),
validation_code.clone(),
));
conclude_pvf_checking::<Test>(&validation_code, VALIDATORS, START_SESSION_INDEX);
run_to_session(START_SESSION_INDEX + 2);
// Upgrade para 1 into a teyrchain
assert_ok!(mock::Registrar::make_teyrchain(para_1));
// Set some mock swap data.
let mut swap_data = SwapData::get();
swap_data.insert(para_1, 69);
swap_data.insert(para_2, 1337);
SwapData::set(swap_data);
run_to_session(START_SESSION_INDEX + 4);
// Roles are as we expect
assert!(Teyrchains::is_teyrchain(para_1));
assert!(!Teyrchains::is_parathread(para_1));
assert!(!Teyrchains::is_teyrchain(para_2));
assert!(Teyrchains::is_parathread(para_2));
// Both paras initiate a swap
// Swap between teyrchain and parathread
assert_ok!(mock::Registrar::swap(para_origin(para_1), para_1, para_2,));
assert_ok!(mock::Registrar::swap(para_origin(para_2), para_2, para_1,));
System::assert_last_event(RuntimeEvent::Registrar(paras_registrar::Event::Swapped {
para_id: para_2,
other_id: para_1,
}));
run_to_session(START_SESSION_INDEX + 6);
// Roles are swapped
assert!(!Teyrchains::is_teyrchain(para_1));
assert!(Teyrchains::is_parathread(para_1));
assert!(Teyrchains::is_teyrchain(para_2));
assert!(!Teyrchains::is_parathread(para_2));
// Data is swapped
assert_eq!(SwapData::get().get(&para_1).unwrap(), &1337);
assert_eq!(SwapData::get().get(&para_2).unwrap(), &69);
// Both paras initiate a swap
// Swap between parathread and teyrchain
assert_ok!(mock::Registrar::swap(para_origin(para_1), para_1, para_2,));
assert_ok!(mock::Registrar::swap(para_origin(para_2), para_2, para_1,));
System::assert_last_event(RuntimeEvent::Registrar(paras_registrar::Event::Swapped {
para_id: para_2,
other_id: para_1,
}));
// Data is swapped
assert_eq!(SwapData::get().get(&para_1).unwrap(), &69);
assert_eq!(SwapData::get().get(&para_2).unwrap(), &1337);
// Teyrchain to teyrchain swap
let para_3 = LOWEST_PUBLIC_ID + 2;
let validation_code = test_validation_code(max_code_size() as usize);
assert_ok!(mock::Registrar::reserve(RuntimeOrigin::signed(3)));
assert_ok!(mock::Registrar::register(
RuntimeOrigin::signed(3),
para_3,
test_genesis_head(max_head_size() as usize),
validation_code.clone(),
));
conclude_pvf_checking::<Test>(&validation_code, VALIDATORS, START_SESSION_INDEX + 6);
run_to_session(START_SESSION_INDEX + 8);
// Upgrade para 3 into a teyrchain
assert_ok!(mock::Registrar::make_teyrchain(para_3));
// Set some mock swap data.
let mut swap_data = SwapData::get();
swap_data.insert(para_3, 777);
SwapData::set(swap_data);
run_to_session(START_SESSION_INDEX + 10);
// Both are teyrchains
assert!(Teyrchains::is_teyrchain(para_3));
assert!(!Teyrchains::is_parathread(para_3));
assert!(Teyrchains::is_teyrchain(para_1));
assert!(!Teyrchains::is_parathread(para_1));
// Both paras initiate a swap
// Swap between teyrchain and teyrchain
assert_ok!(mock::Registrar::swap(para_origin(para_1), para_1, para_3,));
assert_ok!(mock::Registrar::swap(para_origin(para_3), para_3, para_1,));
System::assert_last_event(RuntimeEvent::Registrar(paras_registrar::Event::Swapped {
para_id: para_3,
other_id: para_1,
}));
// Data is swapped
assert_eq!(SwapData::get().get(&para_3).unwrap(), &69);
assert_eq!(SwapData::get().get(&para_1).unwrap(), &777);
});
}
#[test]
fn para_lock_works() {
new_test_ext().execute_with(|| {
run_to_block(1);
assert_ok!(mock::Registrar::reserve(RuntimeOrigin::signed(1)));
let para_id = LOWEST_PUBLIC_ID;
assert_ok!(mock::Registrar::register(
RuntimeOrigin::signed(1),
para_id,
vec![1; 3].into(),
test_validation_code(32)
));
assert_noop!(mock::Registrar::add_lock(RuntimeOrigin::signed(2), para_id), BadOrigin);
// Once they produces new block, we lock them in.
mock::Registrar::on_new_head(para_id, &Default::default());
// Owner cannot pass origin check when checking lock
assert_noop!(
mock::Registrar::ensure_root_para_or_owner(RuntimeOrigin::signed(1), para_id),
Error::<Test>::ParaLocked,
);
// Owner cannot remove lock.
assert_noop!(mock::Registrar::remove_lock(RuntimeOrigin::signed(1), para_id), BadOrigin);
// Para can.
assert_ok!(mock::Registrar::remove_lock(para_origin(para_id), para_id));
// Owner can pass origin check again
assert_ok!(mock::Registrar::ensure_root_para_or_owner(RuntimeOrigin::signed(1), para_id));
// Won't lock again after it is unlocked
mock::Registrar::on_new_head(para_id, &Default::default());
assert_ok!(mock::Registrar::ensure_root_para_or_owner(RuntimeOrigin::signed(1), para_id));
});
}
#[test]
fn swap_handles_bad_states() {
new_test_ext().execute_with(|| {
const START_SESSION_INDEX: SessionIndex = 1;
run_to_session(START_SESSION_INDEX);
let para_1 = LOWEST_PUBLIC_ID;
let para_2 = LOWEST_PUBLIC_ID + 1;
// paras are not yet registered
assert!(!Teyrchains::is_parathread(para_1));
assert!(!Teyrchains::is_parathread(para_2));
// Cannot even start a swap
assert_noop!(
mock::Registrar::swap(RuntimeOrigin::root(), para_1, para_2),
Error::<Test>::NotRegistered
);
// We register Paras 1 and 2
let validation_code = test_validation_code(32);
assert_ok!(mock::Registrar::reserve(RuntimeOrigin::signed(1)));
assert_ok!(mock::Registrar::reserve(RuntimeOrigin::signed(2)));
assert_ok!(mock::Registrar::register(
RuntimeOrigin::signed(1),
para_1,
test_genesis_head(32),
validation_code.clone(),
));
assert_ok!(mock::Registrar::register(
RuntimeOrigin::signed(2),
para_2,
test_genesis_head(32),
validation_code.clone(),
));
conclude_pvf_checking::<Test>(&validation_code, VALIDATORS, START_SESSION_INDEX);
// Cannot swap
assert_ok!(mock::Registrar::swap(RuntimeOrigin::root(), para_1, para_2));
assert_noop!(
mock::Registrar::swap(RuntimeOrigin::root(), para_2, para_1),
Error::<Test>::CannotSwap
);
run_to_session(START_SESSION_INDEX + 2);
// They are now parathreads (on-demand teyrchains).
assert!(Teyrchains::is_parathread(para_1));
assert!(Teyrchains::is_parathread(para_2));
// Cannot swap
assert_ok!(mock::Registrar::swap(RuntimeOrigin::root(), para_1, para_2));
assert_noop!(
mock::Registrar::swap(RuntimeOrigin::root(), para_2, para_1),
Error::<Test>::CannotSwap
);
// Some other external process will elevate one on-demand
// teyrchain to a lease holding teyrchain
assert_ok!(mock::Registrar::make_teyrchain(para_1));
// Cannot swap
assert_ok!(mock::Registrar::swap(RuntimeOrigin::root(), para_1, para_2));
assert_noop!(
mock::Registrar::swap(RuntimeOrigin::root(), para_2, para_1),
Error::<Test>::CannotSwap
);
run_to_session(START_SESSION_INDEX + 3);
// Cannot swap
assert_ok!(mock::Registrar::swap(RuntimeOrigin::root(), para_1, para_2));
assert_noop!(
mock::Registrar::swap(RuntimeOrigin::root(), para_2, para_1),
Error::<Test>::CannotSwap
);
run_to_session(START_SESSION_INDEX + 4);
// It is now a lease holding teyrchain.
assert!(Teyrchains::is_teyrchain(para_1));
assert!(Teyrchains::is_parathread(para_2));
// Swap works here.
assert_ok!(mock::Registrar::swap(RuntimeOrigin::root(), para_1, para_2));
assert_ok!(mock::Registrar::swap(RuntimeOrigin::root(), para_2, para_1));
assert!(System::events().iter().any(|r| matches!(
r.event,
RuntimeEvent::Registrar(paras_registrar::Event::Swapped { .. })
)));
run_to_session(START_SESSION_INDEX + 5);
// Cannot swap
assert_ok!(mock::Registrar::swap(RuntimeOrigin::root(), para_1, para_2));
assert_noop!(
mock::Registrar::swap(RuntimeOrigin::root(), para_2, para_1),
Error::<Test>::CannotSwap
);
run_to_session(START_SESSION_INDEX + 6);
// Swap worked!
assert!(Teyrchains::is_teyrchain(para_2));
assert!(Teyrchains::is_parathread(para_1));
assert!(System::events().iter().any(|r| matches!(
r.event,
RuntimeEvent::Registrar(paras_registrar::Event::Swapped { .. })
)));
// Something starts to downgrade a para
assert_ok!(mock::Registrar::make_parathread(para_2));
run_to_session(START_SESSION_INDEX + 7);
// Cannot swap
assert_ok!(mock::Registrar::swap(RuntimeOrigin::root(), para_1, para_2));
assert_noop!(
mock::Registrar::swap(RuntimeOrigin::root(), para_2, para_1),
Error::<Test>::CannotSwap
);
run_to_session(START_SESSION_INDEX + 8);
assert!(Teyrchains::is_parathread(para_1));
assert!(Teyrchains::is_parathread(para_2));
});
}
@@ -0,0 +1,191 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! A simple wrapper allowing `Sudo` to call into `paras` routines.
use alloc::boxed::Box;
use codec::Encode;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
pub use pallet::*;
use pezkuwi_primitives::Id as ParaId;
use pezkuwi_runtime_teyrchains::{
configuration, dmp, hrmp,
paras::{self, AssignCoretime, ParaGenesisArgs, ParaKind},
ParaLifecycle,
};
#[frame_support::pallet]
pub mod pallet {
use super::*;
#[pallet::pallet]
pub struct Pallet<T>(_);
#[pallet::config]
#[pallet::disable_frame_system_supertrait_check]
pub trait Config: configuration::Config + paras::Config + dmp::Config + hrmp::Config {}
#[pallet::error]
pub enum Error<T> {
/// The specified teyrchain is not registered.
ParaDoesntExist,
/// The specified teyrchain is already registered.
ParaAlreadyExists,
/// A DMP message couldn't be sent because it exceeds the maximum size allowed for a
/// downward message.
ExceedsMaxMessageSize,
/// A DMP message couldn't be sent because the destination is unreachable.
Unroutable,
/// Could not schedule para cleanup.
CouldntCleanup,
/// Not a parathread (on-demand teyrchain).
NotParathread,
/// Not a lease holding teyrchain.
NotTeyrchain,
/// Cannot upgrade on-demand teyrchain to lease holding teyrchain.
CannotUpgrade,
/// Cannot downgrade lease holding teyrchain to on-demand.
CannotDowngrade,
/// There are more cores than supported by the runtime.
TooManyCores,
}
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
#[pallet::call]
impl<T: Config> Pallet<T> {
/// Schedule a para to be initialized at the start of the next session.
///
/// This should only be used for TESTING and not on PRODUCTION chains. It automatically
/// assigns Coretime to the chain and increases the number of cores. Thus, there is no
/// running coretime chain required.
#[pallet::call_index(0)]
#[pallet::weight((1_000, DispatchClass::Operational))]
pub fn sudo_schedule_para_initialize(
origin: OriginFor<T>,
id: ParaId,
genesis: ParaGenesisArgs,
) -> DispatchResult {
ensure_root(origin)?;
let assign_coretime = genesis.para_kind == ParaKind::Teyrchain;
pezkuwi_runtime_teyrchains::schedule_para_initialize::<T>(id, genesis)
.map_err(|_| Error::<T>::ParaAlreadyExists)?;
if assign_coretime {
T::AssignCoretime::assign_coretime(id)?;
}
Ok(())
}
/// Schedule a para to be cleaned up at the start of the next session.
#[pallet::call_index(1)]
#[pallet::weight((1_000, DispatchClass::Operational))]
pub fn sudo_schedule_para_cleanup(origin: OriginFor<T>, id: ParaId) -> DispatchResult {
ensure_root(origin)?;
pezkuwi_runtime_teyrchains::schedule_para_cleanup::<T>(id)
.map_err(|_| Error::<T>::CouldntCleanup)?;
Ok(())
}
/// Upgrade a parathread (on-demand teyrchain) to a lease holding teyrchain
#[pallet::call_index(2)]
#[pallet::weight((1_000, DispatchClass::Operational))]
pub fn sudo_schedule_parathread_upgrade(
origin: OriginFor<T>,
id: ParaId,
) -> DispatchResult {
ensure_root(origin)?;
// Para backend should think this is a parathread (on-demand teyrchain)...
ensure!(
paras::Pallet::<T>::lifecycle(id) == Some(ParaLifecycle::Parathread),
Error::<T>::NotParathread,
);
pezkuwi_runtime_teyrchains::schedule_parathread_upgrade::<T>(id)
.map_err(|_| Error::<T>::CannotUpgrade)?;
Ok(())
}
/// Downgrade a lease holding teyrchain to an on-demand teyrchain
#[pallet::call_index(3)]
#[pallet::weight((1_000, DispatchClass::Operational))]
pub fn sudo_schedule_teyrchain_downgrade(
origin: OriginFor<T>,
id: ParaId,
) -> DispatchResult {
ensure_root(origin)?;
// Para backend should think this is a teyrchain...
ensure!(
paras::Pallet::<T>::lifecycle(id) == Some(ParaLifecycle::Teyrchain),
Error::<T>::NotTeyrchain,
);
pezkuwi_runtime_teyrchains::schedule_teyrchain_downgrade::<T>(id)
.map_err(|_| Error::<T>::CannotDowngrade)?;
Ok(())
}
/// Send a downward XCM to the given para.
///
/// The given teyrchain should exist and the payload should not exceed the preconfigured
/// size `config.max_downward_message_size`.
#[pallet::call_index(4)]
#[pallet::weight((1_000, DispatchClass::Operational))]
pub fn sudo_queue_downward_xcm(
origin: OriginFor<T>,
id: ParaId,
xcm: Box<xcm::opaque::VersionedXcm>,
) -> DispatchResult {
ensure_root(origin)?;
ensure!(paras::Pallet::<T>::is_valid_para(id), Error::<T>::ParaDoesntExist);
let config = configuration::ActiveConfig::<T>::get();
dmp::Pallet::<T>::queue_downward_message(&config, id, xcm.encode()).map_err(|e| match e
{
dmp::QueueDownwardMessageError::ExceedsMaxMessageSize =>
Error::<T>::ExceedsMaxMessageSize.into(),
dmp::QueueDownwardMessageError::Unroutable => Error::<T>::Unroutable.into(),
})
}
/// Forcefully establish a channel from the sender to the recipient.
///
/// This is equivalent to sending an `Hrmp::hrmp_init_open_channel` extrinsic followed by
/// `Hrmp::hrmp_accept_open_channel`.
#[pallet::call_index(5)]
#[pallet::weight((1_000, DispatchClass::Operational))]
pub fn sudo_establish_hrmp_channel(
origin: OriginFor<T>,
sender: ParaId,
recipient: ParaId,
max_capacity: u32,
max_message_size: u32,
) -> DispatchResult {
ensure_root(origin)?;
hrmp::Pallet::<T>::init_open_channel(
sender,
recipient,
max_capacity,
max_message_size,
)?;
hrmp::Pallet::<T>::accept_open_channel(recipient, sender)?;
Ok(())
}
}
}
+181
View File
@@ -0,0 +1,181 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Mocking utilities for testing in purchase pallet.
#[cfg(test)]
use super::*;
use sp_core::{crypto::AccountId32, H256};
use sp_keyring::{Ed25519Keyring, Sr25519Keyring};
// 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 crate::purchase;
use frame_support::{
derive_impl, ord_parameter_types, parameter_types,
traits::{Currency, WithdrawReasons},
};
use sp_runtime::{
traits::{BlakeTwo256, Identity, IdentityLookup},
BuildStorage,
};
type Block = frame_system::mocking::MockBlock<Test>;
frame_support::construct_runtime!(
pub enum Test
{
System: frame_system,
Balances: pallet_balances,
Vesting: pallet_vesting,
Purchase: purchase,
}
);
type AccountId = AccountId32;
#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
impl frame_system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
type BlockWeights = ();
type BlockLength = ();
type DbWeight = ();
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
type Nonce = u64;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = AccountId;
type Lookup = IdentityLookup<AccountId>;
type Block = Block;
type RuntimeEvent = RuntimeEvent;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
}
#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
impl pallet_balances::Config for Test {
type AccountStore = System;
}
parameter_types! {
pub const MinVestedTransfer: u64 = 1;
pub UnvestedFundsAllowedWithdrawReasons: WithdrawReasons =
WithdrawReasons::except(WithdrawReasons::TRANSFER | WithdrawReasons::RESERVE);
}
impl pallet_vesting::Config for Test {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type BlockNumberToBalance = Identity;
type MinVestedTransfer = MinVestedTransfer;
type WeightInfo = ();
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
type BlockNumberProvider = System;
const MAX_VESTING_SCHEDULES: u32 = 28;
}
parameter_types! {
pub const MaxStatementLength: u32 = 1_000;
pub const UnlockedProportion: Permill = Permill::from_percent(10);
pub const MaxUnlocked: u64 = 10;
}
ord_parameter_types! {
pub const ValidityOrigin: AccountId = AccountId32::from([0u8; 32]);
pub const PaymentOrigin: AccountId = AccountId32::from([1u8; 32]);
pub const ConfigurationOrigin: AccountId = AccountId32::from([2u8; 32]);
}
impl Config for Test {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type VestingSchedule = Vesting;
type ValidityOrigin = frame_system::EnsureSignedBy<ValidityOrigin, AccountId>;
type ConfigurationOrigin = frame_system::EnsureSignedBy<ConfigurationOrigin, AccountId>;
type MaxStatementLength = MaxStatementLength;
type UnlockedProportion = UnlockedProportion;
type MaxUnlocked = MaxUnlocked;
}
// This function basically just builds a genesis storage key/value store according to
// our desired mockup. It also executes our `setup` function which sets up this pallet for use.
pub fn new_test_ext() -> sp_io::TestExternalities {
let t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| setup());
ext
}
pub fn setup() {
let statement = b"Hello, World".to_vec();
let unlock_block = 100;
Purchase::set_statement(RuntimeOrigin::signed(configuration_origin()), statement).unwrap();
Purchase::set_unlock_block(RuntimeOrigin::signed(configuration_origin()), unlock_block)
.unwrap();
Purchase::set_payment_account(RuntimeOrigin::signed(configuration_origin()), payment_account())
.unwrap();
Balances::make_free_balance_be(&payment_account(), 100_000);
}
pub fn alice() -> AccountId {
Sr25519Keyring::Alice.to_account_id()
}
pub fn alice_ed25519() -> AccountId {
Ed25519Keyring::Alice.to_account_id()
}
pub fn bob() -> AccountId {
Sr25519Keyring::Bob.to_account_id()
}
pub fn alice_signature() -> [u8; 64] {
// echo -n "Hello, World" | subkey -s sign "bottom drive obey lake curtain smoke basket hold
// race lonely fit walk//Alice"
hex_literal::hex!("20e0faffdf4dfe939f2faa560f73b1d01cde8472e2b690b7b40606a374244c3a2e9eb9c8107c10b605138374003af8819bd4387d7c24a66ee9253c2e688ab881")
}
pub fn bob_signature() -> [u8; 64] {
// echo -n "Hello, World" | subkey -s sign "bottom drive obey lake curtain smoke basket hold
// race lonely fit walk//Bob"
hex_literal::hex!("d6d460187ecf530f3ec2d6e3ac91b9d083c8fbd8f1112d92a82e4d84df552d18d338e6da8944eba6e84afaacf8a9850f54e7b53a84530d649be2e0119c7ce889")
}
pub fn alice_signature_ed25519() -> [u8; 64] {
// echo -n "Hello, World" | subkey -e sign "bottom drive obey lake curtain smoke basket hold
// race lonely fit walk//Alice"
hex_literal::hex!("ee3f5a6cbfc12a8f00c18b811dc921b550ddf272354cda4b9a57b1d06213fcd8509f5af18425d39a279d13622f14806c3e978e2163981f2ec1c06e9628460b0e")
}
pub fn validity_origin() -> AccountId {
ValidityOrigin::get()
}
pub fn configuration_origin() -> AccountId {
ConfigurationOrigin::get()
}
pub fn payment_account() -> AccountId {
[42u8; 32].into()
}
+485
View File
@@ -0,0 +1,485 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Pallet to process purchase of DOTs.
use alloc::vec::Vec;
use codec::{Decode, Encode};
use frame_support::{
pallet_prelude::*,
traits::{Currency, EnsureOrigin, ExistenceRequirement, Get, VestingSchedule},
};
use frame_system::pallet_prelude::*;
pub use pallet::*;
use scale_info::TypeInfo;
use sp_core::sr25519;
use sp_runtime::{
traits::{CheckedAdd, Saturating, Verify, Zero},
AnySignature, DispatchError, DispatchResult, Permill, RuntimeDebug,
};
type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
/// The kind of statement an account needs to make for a claim to be valid.
#[derive(
Encode, Decode, DecodeWithMemTracking, Clone, Copy, Eq, PartialEq, RuntimeDebug, TypeInfo,
)]
pub enum AccountValidity {
/// Account is not valid.
Invalid,
/// Account has initiated the account creation process.
Initiated,
/// Account is pending validation.
Pending,
/// Account is valid with a low contribution amount.
ValidLow,
/// Account is valid with a high contribution amount.
ValidHigh,
/// Account has completed the purchase process.
Completed,
}
impl Default for AccountValidity {
fn default() -> Self {
AccountValidity::Invalid
}
}
impl AccountValidity {
fn is_valid(&self) -> bool {
match self {
Self::Invalid => false,
Self::Initiated => false,
Self::Pending => false,
Self::ValidLow => true,
Self::ValidHigh => true,
Self::Completed => false,
}
}
}
/// All information about an account regarding the purchase of DOTs.
#[derive(Encode, Decode, Default, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo)]
pub struct AccountStatus<Balance> {
/// The current validity status of the user. Will denote if the user has passed KYC,
/// how much they are able to purchase, and when their purchase process has completed.
validity: AccountValidity,
/// The amount of free DOTs they have purchased.
free_balance: Balance,
/// The amount of locked DOTs they have purchased.
locked_balance: Balance,
/// Their sr25519/ed25519 signature verifying they have signed our required statement.
signature: Vec<u8>,
/// The percentage of VAT the purchaser is responsible for. This is already factored into
/// account balance.
vat: Permill,
}
#[frame_support::pallet]
pub mod pallet {
use super::*;
#[pallet::pallet]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);
#[pallet::config]
pub trait Config: frame_system::Config {
/// The overarching event type.
#[allow(deprecated)]
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// Balances Pallet
type Currency: Currency<Self::AccountId>;
/// Vesting Pallet
type VestingSchedule: VestingSchedule<
Self::AccountId,
Moment = BlockNumberFor<Self>,
Currency = Self::Currency,
>;
/// The origin allowed to set account status.
type ValidityOrigin: EnsureOrigin<Self::RuntimeOrigin>;
/// The origin allowed to make configurations to the pallet.
type ConfigurationOrigin: EnsureOrigin<Self::RuntimeOrigin>;
/// The maximum statement length for the statement users to sign when creating an account.
#[pallet::constant]
type MaxStatementLength: Get<u32>;
/// The amount of purchased locked DOTs that we will unlock for basic actions on the chain.
#[pallet::constant]
type UnlockedProportion: Get<Permill>;
/// The maximum amount of locked DOTs that we will unlock.
#[pallet::constant]
type MaxUnlocked: Get<BalanceOf<Self>>;
}
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// A new account was created.
AccountCreated { who: T::AccountId },
/// Someone's account validity was updated.
ValidityUpdated { who: T::AccountId, validity: AccountValidity },
/// Someone's purchase balance was updated.
BalanceUpdated { who: T::AccountId, free: BalanceOf<T>, locked: BalanceOf<T> },
/// A payout was made to a purchaser.
PaymentComplete { who: T::AccountId, free: BalanceOf<T>, locked: BalanceOf<T> },
/// A new payment account was set.
PaymentAccountSet { who: T::AccountId },
/// A new statement was set.
StatementUpdated,
/// A new statement was set. `[block_number]`
UnlockBlockUpdated { block_number: BlockNumberFor<T> },
}
#[pallet::error]
pub enum Error<T> {
/// Account is not currently valid to use.
InvalidAccount,
/// Account used in the purchase already exists.
ExistingAccount,
/// Provided signature is invalid
InvalidSignature,
/// Account has already completed the purchase process.
AlreadyCompleted,
/// An overflow occurred when doing calculations.
Overflow,
/// The statement is too long to be stored on chain.
InvalidStatement,
/// The unlock block is in the past!
InvalidUnlockBlock,
/// Vesting schedule already exists for this account.
VestingScheduleExists,
}
// A map of all participants in the HEZ purchase process.
#[pallet::storage]
pub(super) type Accounts<T: Config> =
StorageMap<_, Blake2_128Concat, T::AccountId, AccountStatus<BalanceOf<T>>, ValueQuery>;
// The account that will be used to payout participants of the HEZ purchase process.
#[pallet::storage]
pub(super) type PaymentAccount<T: Config> = StorageValue<_, T::AccountId, OptionQuery>;
// The statement purchasers will need to sign to participate.
#[pallet::storage]
pub(super) type Statement<T> = StorageValue<_, Vec<u8>, ValueQuery>;
// The block where all locked dots will unlock.
#[pallet::storage]
pub(super) type UnlockBlock<T: Config> = StorageValue<_, BlockNumberFor<T>, ValueQuery>;
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
#[pallet::call]
impl<T: Config> Pallet<T> {
/// Create a new account. Proof of existence through a valid signed message.
///
/// We check that the account does not exist at this stage.
///
/// Origin must match the `ValidityOrigin`.
#[pallet::call_index(0)]
#[pallet::weight(Weight::from_parts(200_000_000, 0) + T::DbWeight::get().reads_writes(4, 1))]
pub fn create_account(
origin: OriginFor<T>,
who: T::AccountId,
signature: Vec<u8>,
) -> DispatchResult {
T::ValidityOrigin::ensure_origin(origin)?;
// Account is already being tracked by the pallet.
ensure!(!Accounts::<T>::contains_key(&who), Error::<T>::ExistingAccount);
// Account should not have a vesting schedule.
ensure!(
T::VestingSchedule::vesting_balance(&who).is_none(),
Error::<T>::VestingScheduleExists
);
// Verify the signature provided is valid for the statement.
Self::verify_signature(&who, &signature)?;
// Create a new pending account.
let status = AccountStatus {
validity: AccountValidity::Initiated,
signature,
free_balance: Zero::zero(),
locked_balance: Zero::zero(),
vat: Permill::zero(),
};
Accounts::<T>::insert(&who, status);
Self::deposit_event(Event::<T>::AccountCreated { who });
Ok(())
}
/// Update the validity status of an existing account. If set to completed, the account
/// will no longer be able to continue through the crowdfund process.
///
/// We check that the account exists at this stage, but has not completed the process.
///
/// Origin must match the `ValidityOrigin`.
#[pallet::call_index(1)]
#[pallet::weight(T::DbWeight::get().reads_writes(1, 1))]
pub fn update_validity_status(
origin: OriginFor<T>,
who: T::AccountId,
validity: AccountValidity,
) -> DispatchResult {
T::ValidityOrigin::ensure_origin(origin)?;
ensure!(Accounts::<T>::contains_key(&who), Error::<T>::InvalidAccount);
Accounts::<T>::try_mutate(
&who,
|status: &mut AccountStatus<BalanceOf<T>>| -> DispatchResult {
ensure!(
status.validity != AccountValidity::Completed,
Error::<T>::AlreadyCompleted
);
status.validity = validity;
Ok(())
},
)?;
Self::deposit_event(Event::<T>::ValidityUpdated { who, validity });
Ok(())
}
/// Update the balance of a valid account.
///
/// We check that the account is valid for a balance transfer at this point.
///
/// Origin must match the `ValidityOrigin`.
#[pallet::call_index(2)]
#[pallet::weight(T::DbWeight::get().reads_writes(2, 1))]
pub fn update_balance(
origin: OriginFor<T>,
who: T::AccountId,
free_balance: BalanceOf<T>,
locked_balance: BalanceOf<T>,
vat: Permill,
) -> DispatchResult {
T::ValidityOrigin::ensure_origin(origin)?;
Accounts::<T>::try_mutate(
&who,
|status: &mut AccountStatus<BalanceOf<T>>| -> DispatchResult {
// Account has a valid status (not Invalid, Pending, or Completed)...
ensure!(status.validity.is_valid(), Error::<T>::InvalidAccount);
free_balance.checked_add(&locked_balance).ok_or(Error::<T>::Overflow)?;
status.free_balance = free_balance;
status.locked_balance = locked_balance;
status.vat = vat;
Ok(())
},
)?;
Self::deposit_event(Event::<T>::BalanceUpdated {
who,
free: free_balance,
locked: locked_balance,
});
Ok(())
}
/// Pay the user and complete the purchase process.
///
/// We reverify all assumptions about the state of an account, and complete the process.
///
/// Origin must match the configured `PaymentAccount` (if it is not configured then this
/// will always fail with `BadOrigin`).
#[pallet::call_index(3)]
#[pallet::weight(T::DbWeight::get().reads_writes(4, 2))]
pub fn payout(origin: OriginFor<T>, who: T::AccountId) -> DispatchResult {
// Payments must be made directly by the `PaymentAccount`.
let payment_account = ensure_signed(origin)?;
let test_against = PaymentAccount::<T>::get().ok_or(DispatchError::BadOrigin)?;
ensure!(payment_account == test_against, DispatchError::BadOrigin);
// Account should not have a vesting schedule.
ensure!(
T::VestingSchedule::vesting_balance(&who).is_none(),
Error::<T>::VestingScheduleExists
);
Accounts::<T>::try_mutate(
&who,
|status: &mut AccountStatus<BalanceOf<T>>| -> DispatchResult {
// Account has a valid status (not Invalid, Pending, or Completed)...
ensure!(status.validity.is_valid(), Error::<T>::InvalidAccount);
// Transfer funds from the payment account into the purchasing user.
let total_balance = status
.free_balance
.checked_add(&status.locked_balance)
.ok_or(Error::<T>::Overflow)?;
T::Currency::transfer(
&payment_account,
&who,
total_balance,
ExistenceRequirement::AllowDeath,
)?;
if !status.locked_balance.is_zero() {
let unlock_block = UnlockBlock::<T>::get();
// We allow some configurable portion of the purchased locked DOTs to be
// unlocked for basic usage.
let unlocked = (T::UnlockedProportion::get() * status.locked_balance)
.min(T::MaxUnlocked::get());
let locked = status.locked_balance.saturating_sub(unlocked);
// We checked that this account has no existing vesting schedule. So this
// function should never fail, however if it does, not much we can do about
// it at this point.
let _ = T::VestingSchedule::add_vesting_schedule(
// Apply vesting schedule to this user
&who,
// For this much amount
locked,
// Unlocking the full amount after one block
locked,
// When everything unlocks
unlock_block,
);
}
// Setting the user account to `Completed` ends the purchase process for this
// user.
status.validity = AccountValidity::Completed;
Self::deposit_event(Event::<T>::PaymentComplete {
who: who.clone(),
free: status.free_balance,
locked: status.locked_balance,
});
Ok(())
},
)?;
Ok(())
}
/* Configuration Operations */
/// Set the account that will be used to payout users in the HEZ purchase process.
///
/// Origin must match the `ConfigurationOrigin`
#[pallet::call_index(4)]
#[pallet::weight(T::DbWeight::get().writes(1))]
pub fn set_payment_account(origin: OriginFor<T>, who: T::AccountId) -> DispatchResult {
T::ConfigurationOrigin::ensure_origin(origin)?;
// Possibly this is worse than having the caller account be the payment account?
PaymentAccount::<T>::put(who.clone());
Self::deposit_event(Event::<T>::PaymentAccountSet { who });
Ok(())
}
/// Set the statement that must be signed for a user to participate on the HEZ sale.
///
/// Origin must match the `ConfigurationOrigin`
#[pallet::call_index(5)]
#[pallet::weight(T::DbWeight::get().writes(1))]
pub fn set_statement(origin: OriginFor<T>, statement: Vec<u8>) -> DispatchResult {
T::ConfigurationOrigin::ensure_origin(origin)?;
ensure!(
(statement.len() as u32) < T::MaxStatementLength::get(),
Error::<T>::InvalidStatement
);
// Possibly this is worse than having the caller account be the payment account?
Statement::<T>::set(statement);
Self::deposit_event(Event::<T>::StatementUpdated);
Ok(())
}
/// Set the block where locked DOTs will become unlocked.
///
/// Origin must match the `ConfigurationOrigin`
#[pallet::call_index(6)]
#[pallet::weight(T::DbWeight::get().writes(1))]
pub fn set_unlock_block(
origin: OriginFor<T>,
unlock_block: BlockNumberFor<T>,
) -> DispatchResult {
T::ConfigurationOrigin::ensure_origin(origin)?;
ensure!(
unlock_block > frame_system::Pallet::<T>::block_number(),
Error::<T>::InvalidUnlockBlock
);
// Possibly this is worse than having the caller account be the payment account?
UnlockBlock::<T>::set(unlock_block);
Self::deposit_event(Event::<T>::UnlockBlockUpdated { block_number: unlock_block });
Ok(())
}
}
}
impl<T: Config> Pallet<T> {
fn verify_signature(who: &T::AccountId, signature: &[u8]) -> Result<(), DispatchError> {
// sr25519 always expects a 64 byte signature.
let signature: AnySignature = sr25519::Signature::try_from(signature)
.map_err(|_| Error::<T>::InvalidSignature)?
.into();
// In Pezkuwi, the AccountId is always the same as the 32 byte public key.
let account_bytes: [u8; 32] = account_to_bytes(who)?;
let public_key = sr25519::Public::from_raw(account_bytes);
let message = Statement::<T>::get();
// Check if everything is good or not.
match signature.verify(message.as_slice(), &public_key) {
true => Ok(()),
false => Err(Error::<T>::InvalidSignature)?,
}
}
}
// This function converts a 32 byte AccountId to its byte-array equivalent form.
fn account_to_bytes<AccountId>(account: &AccountId) -> Result<[u8; 32], DispatchError>
where
AccountId: Encode,
{
let account_vec = account.encode();
ensure!(account_vec.len() == 32, "AccountId must be 32 bytes.");
let mut bytes = [0u8; 32];
bytes.copy_from_slice(&account_vec);
Ok(bytes)
}
/// WARNING: Executing this function will clear all storage used by this pallet.
/// Be sure this is what you want...
pub fn remove_pallet<T>() -> frame_support::weights::Weight
where
T: frame_system::Config,
{
#[allow(deprecated)]
use frame_support::migration::remove_storage_prefix;
#[allow(deprecated)]
remove_storage_prefix(b"Purchase", b"Accounts", b"");
#[allow(deprecated)]
remove_storage_prefix(b"Purchase", b"PaymentAccount", b"");
#[allow(deprecated)]
remove_storage_prefix(b"Purchase", b"Statement", b"");
#[allow(deprecated)]
remove_storage_prefix(b"Purchase", b"UnlockBlock", b"");
<T as frame_system::Config>::BlockWeights::get().max_block
}
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
@@ -0,0 +1,547 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Tests for the purchase pallet.
#[cfg(test)]
use super::*;
use sp_core::crypto::AccountId32;
// 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 frame_support::{assert_noop, assert_ok, traits::Currency};
use sp_runtime::{traits::Dispatchable, ArithmeticError, DispatchError::BadOrigin};
use crate::purchase::mock::*;
#[test]
fn set_statement_works_and_handles_basic_errors() {
new_test_ext().execute_with(|| {
let statement = b"Test Set Statement".to_vec();
// Invalid origin
assert_noop!(
Purchase::set_statement(RuntimeOrigin::signed(alice()), statement.clone()),
BadOrigin,
);
// Too Long
let long_statement = [0u8; 10_000].to_vec();
assert_noop!(
Purchase::set_statement(RuntimeOrigin::signed(configuration_origin()), long_statement),
Error::<Test>::InvalidStatement,
);
// Just right...
assert_ok!(Purchase::set_statement(
RuntimeOrigin::signed(configuration_origin()),
statement.clone()
));
assert_eq!(Statement::<Test>::get(), statement);
});
}
#[test]
fn set_unlock_block_works_and_handles_basic_errors() {
new_test_ext().execute_with(|| {
let unlock_block = 69;
// Invalid origin
assert_noop!(
Purchase::set_unlock_block(RuntimeOrigin::signed(alice()), unlock_block),
BadOrigin,
);
// Block Number in Past
let bad_unlock_block = 50;
System::set_block_number(bad_unlock_block);
assert_noop!(
Purchase::set_unlock_block(
RuntimeOrigin::signed(configuration_origin()),
bad_unlock_block
),
Error::<Test>::InvalidUnlockBlock,
);
// Just right...
assert_ok!(Purchase::set_unlock_block(
RuntimeOrigin::signed(configuration_origin()),
unlock_block
));
assert_eq!(UnlockBlock::<Test>::get(), unlock_block);
});
}
#[test]
fn set_payment_account_works_and_handles_basic_errors() {
new_test_ext().execute_with(|| {
let payment_account: AccountId32 = [69u8; 32].into();
// Invalid Origin
assert_noop!(
Purchase::set_payment_account(RuntimeOrigin::signed(alice()), payment_account.clone()),
BadOrigin,
);
// Just right...
assert_ok!(Purchase::set_payment_account(
RuntimeOrigin::signed(configuration_origin()),
payment_account.clone()
));
assert_eq!(PaymentAccount::<Test>::get(), Some(payment_account));
});
}
#[test]
fn signature_verification_works() {
new_test_ext().execute_with(|| {
assert_ok!(Purchase::verify_signature(&alice(), &alice_signature()));
assert_ok!(Purchase::verify_signature(&alice_ed25519(), &alice_signature_ed25519()));
assert_ok!(Purchase::verify_signature(&bob(), &bob_signature()));
// Mixing and matching fails
assert_noop!(
Purchase::verify_signature(&alice(), &bob_signature()),
Error::<Test>::InvalidSignature
);
assert_noop!(
Purchase::verify_signature(&bob(), &alice_signature()),
Error::<Test>::InvalidSignature
);
});
}
#[test]
fn account_creation_works() {
new_test_ext().execute_with(|| {
assert!(!Accounts::<Test>::contains_key(alice()));
assert_ok!(Purchase::create_account(
RuntimeOrigin::signed(validity_origin()),
alice(),
alice_signature().to_vec(),
));
assert_eq!(
Accounts::<Test>::get(alice()),
AccountStatus {
validity: AccountValidity::Initiated,
free_balance: Zero::zero(),
locked_balance: Zero::zero(),
signature: alice_signature().to_vec(),
vat: Permill::zero(),
}
);
});
}
#[test]
fn account_creation_handles_basic_errors() {
new_test_ext().execute_with(|| {
// Wrong Origin
assert_noop!(
Purchase::create_account(
RuntimeOrigin::signed(alice()),
alice(),
alice_signature().to_vec()
),
BadOrigin,
);
// Wrong Account/Signature
assert_noop!(
Purchase::create_account(
RuntimeOrigin::signed(validity_origin()),
alice(),
bob_signature().to_vec()
),
Error::<Test>::InvalidSignature,
);
// Account with vesting
Balances::make_free_balance_be(&alice(), 100);
assert_ok!(<Test as Config>::VestingSchedule::add_vesting_schedule(&alice(), 100, 1, 50));
assert_noop!(
Purchase::create_account(
RuntimeOrigin::signed(validity_origin()),
alice(),
alice_signature().to_vec()
),
Error::<Test>::VestingScheduleExists,
);
// Duplicate Purchasing Account
assert_ok!(Purchase::create_account(
RuntimeOrigin::signed(validity_origin()),
bob(),
bob_signature().to_vec()
));
assert_noop!(
Purchase::create_account(
RuntimeOrigin::signed(validity_origin()),
bob(),
bob_signature().to_vec()
),
Error::<Test>::ExistingAccount,
);
});
}
#[test]
fn update_validity_status_works() {
new_test_ext().execute_with(|| {
// Alice account is created.
assert_ok!(Purchase::create_account(
RuntimeOrigin::signed(validity_origin()),
alice(),
alice_signature().to_vec(),
));
// She submits KYC, and we update the status to `Pending`.
assert_ok!(Purchase::update_validity_status(
RuntimeOrigin::signed(validity_origin()),
alice(),
AccountValidity::Pending,
));
// KYC comes back negative, so we mark the account invalid.
assert_ok!(Purchase::update_validity_status(
RuntimeOrigin::signed(validity_origin()),
alice(),
AccountValidity::Invalid,
));
assert_eq!(
Accounts::<Test>::get(alice()),
AccountStatus {
validity: AccountValidity::Invalid,
free_balance: Zero::zero(),
locked_balance: Zero::zero(),
signature: alice_signature().to_vec(),
vat: Permill::zero(),
}
);
// She fixes it, we mark her account valid.
assert_ok!(Purchase::update_validity_status(
RuntimeOrigin::signed(validity_origin()),
alice(),
AccountValidity::ValidLow,
));
assert_eq!(
Accounts::<Test>::get(alice()),
AccountStatus {
validity: AccountValidity::ValidLow,
free_balance: Zero::zero(),
locked_balance: Zero::zero(),
signature: alice_signature().to_vec(),
vat: Permill::zero(),
}
);
});
}
#[test]
fn update_validity_status_handles_basic_errors() {
new_test_ext().execute_with(|| {
// Wrong Origin
assert_noop!(
Purchase::update_validity_status(
RuntimeOrigin::signed(alice()),
alice(),
AccountValidity::Pending,
),
BadOrigin
);
// Inactive Account
assert_noop!(
Purchase::update_validity_status(
RuntimeOrigin::signed(validity_origin()),
alice(),
AccountValidity::Pending,
),
Error::<Test>::InvalidAccount
);
// Already Completed
assert_ok!(Purchase::create_account(
RuntimeOrigin::signed(validity_origin()),
alice(),
alice_signature().to_vec(),
));
assert_ok!(Purchase::update_validity_status(
RuntimeOrigin::signed(validity_origin()),
alice(),
AccountValidity::Completed,
));
assert_noop!(
Purchase::update_validity_status(
RuntimeOrigin::signed(validity_origin()),
alice(),
AccountValidity::Pending,
),
Error::<Test>::AlreadyCompleted
);
});
}
#[test]
fn update_balance_works() {
new_test_ext().execute_with(|| {
// Alice account is created
assert_ok!(Purchase::create_account(
RuntimeOrigin::signed(validity_origin()),
alice(),
alice_signature().to_vec()
));
// And approved for basic contribution
assert_ok!(Purchase::update_validity_status(
RuntimeOrigin::signed(validity_origin()),
alice(),
AccountValidity::ValidLow,
));
// We set a balance on the user based on the payment they made. 50 locked, 50 free.
assert_ok!(Purchase::update_balance(
RuntimeOrigin::signed(validity_origin()),
alice(),
50,
50,
Permill::from_rational(77u32, 1000u32),
));
assert_eq!(
Accounts::<Test>::get(alice()),
AccountStatus {
validity: AccountValidity::ValidLow,
free_balance: 50,
locked_balance: 50,
signature: alice_signature().to_vec(),
vat: Permill::from_parts(77000),
}
);
// We can update the balance based on new information.
assert_ok!(Purchase::update_balance(
RuntimeOrigin::signed(validity_origin()),
alice(),
25,
50,
Permill::zero(),
));
assert_eq!(
Accounts::<Test>::get(alice()),
AccountStatus {
validity: AccountValidity::ValidLow,
free_balance: 25,
locked_balance: 50,
signature: alice_signature().to_vec(),
vat: Permill::zero(),
}
);
});
}
#[test]
fn update_balance_handles_basic_errors() {
new_test_ext().execute_with(|| {
// Wrong Origin
assert_noop!(
Purchase::update_balance(
RuntimeOrigin::signed(alice()),
alice(),
50,
50,
Permill::zero(),
),
BadOrigin
);
// Inactive Account
assert_noop!(
Purchase::update_balance(
RuntimeOrigin::signed(validity_origin()),
alice(),
50,
50,
Permill::zero(),
),
Error::<Test>::InvalidAccount
);
// Overflow
assert_noop!(
Purchase::update_balance(
RuntimeOrigin::signed(validity_origin()),
alice(),
u64::MAX,
u64::MAX,
Permill::zero(),
),
Error::<Test>::InvalidAccount
);
});
}
#[test]
fn payout_works() {
new_test_ext().execute_with(|| {
// Alice and Bob accounts are created
assert_ok!(Purchase::create_account(
RuntimeOrigin::signed(validity_origin()),
alice(),
alice_signature().to_vec()
));
assert_ok!(Purchase::create_account(
RuntimeOrigin::signed(validity_origin()),
bob(),
bob_signature().to_vec()
));
// Alice is approved for basic contribution
assert_ok!(Purchase::update_validity_status(
RuntimeOrigin::signed(validity_origin()),
alice(),
AccountValidity::ValidLow,
));
// Bob is approved for high contribution
assert_ok!(Purchase::update_validity_status(
RuntimeOrigin::signed(validity_origin()),
bob(),
AccountValidity::ValidHigh,
));
// We set a balance on the users based on the payment they made. 50 locked, 50 free.
assert_ok!(Purchase::update_balance(
RuntimeOrigin::signed(validity_origin()),
alice(),
50,
50,
Permill::zero(),
));
assert_ok!(Purchase::update_balance(
RuntimeOrigin::signed(validity_origin()),
bob(),
100,
150,
Permill::zero(),
));
// Now we call payout for Alice and Bob.
assert_ok!(Purchase::payout(RuntimeOrigin::signed(payment_account()), alice(),));
assert_ok!(Purchase::payout(RuntimeOrigin::signed(payment_account()), bob(),));
// Payment is made.
assert_eq!(<Test as Config>::Currency::free_balance(&payment_account()), 99_650);
assert_eq!(<Test as Config>::Currency::free_balance(&alice()), 100);
// 10% of the 50 units is unlocked automatically for Alice
assert_eq!(<Test as Config>::VestingSchedule::vesting_balance(&alice()), Some(45));
assert_eq!(<Test as Config>::Currency::free_balance(&bob()), 250);
// A max of 10 units is unlocked automatically for Bob
assert_eq!(<Test as Config>::VestingSchedule::vesting_balance(&bob()), Some(140));
// Status is completed.
assert_eq!(
Accounts::<Test>::get(alice()),
AccountStatus {
validity: AccountValidity::Completed,
free_balance: 50,
locked_balance: 50,
signature: alice_signature().to_vec(),
vat: Permill::zero(),
}
);
assert_eq!(
Accounts::<Test>::get(bob()),
AccountStatus {
validity: AccountValidity::Completed,
free_balance: 100,
locked_balance: 150,
signature: bob_signature().to_vec(),
vat: Permill::zero(),
}
);
// Vesting lock is removed in whole on block 101 (100 blocks after block 1)
System::set_block_number(100);
let vest_call = RuntimeCall::Vesting(pallet_vesting::Call::<Test>::vest {});
assert_ok!(vest_call.clone().dispatch(RuntimeOrigin::signed(alice())));
assert_ok!(vest_call.clone().dispatch(RuntimeOrigin::signed(bob())));
assert_eq!(<Test as Config>::VestingSchedule::vesting_balance(&alice()), Some(45));
assert_eq!(<Test as Config>::VestingSchedule::vesting_balance(&bob()), Some(140));
System::set_block_number(101);
assert_ok!(vest_call.clone().dispatch(RuntimeOrigin::signed(alice())));
assert_ok!(vest_call.clone().dispatch(RuntimeOrigin::signed(bob())));
assert_eq!(<Test as Config>::VestingSchedule::vesting_balance(&alice()), None);
assert_eq!(<Test as Config>::VestingSchedule::vesting_balance(&bob()), None);
});
}
#[test]
fn payout_handles_basic_errors() {
new_test_ext().execute_with(|| {
// Wrong Origin
assert_noop!(Purchase::payout(RuntimeOrigin::signed(alice()), alice(),), BadOrigin);
// Account with Existing Vesting Schedule
Balances::make_free_balance_be(&bob(), 100);
assert_ok!(<Test as Config>::VestingSchedule::add_vesting_schedule(&bob(), 100, 1, 50,));
assert_noop!(
Purchase::payout(RuntimeOrigin::signed(payment_account()), bob(),),
Error::<Test>::VestingScheduleExists
);
// Invalid Account (never created)
assert_noop!(
Purchase::payout(RuntimeOrigin::signed(payment_account()), alice(),),
Error::<Test>::InvalidAccount
);
// Invalid Account (created, but not valid)
assert_ok!(Purchase::create_account(
RuntimeOrigin::signed(validity_origin()),
alice(),
alice_signature().to_vec()
));
assert_noop!(
Purchase::payout(RuntimeOrigin::signed(payment_account()), alice(),),
Error::<Test>::InvalidAccount
);
// Not enough funds in payment account
assert_ok!(Purchase::update_validity_status(
RuntimeOrigin::signed(validity_origin()),
alice(),
AccountValidity::ValidHigh,
));
assert_ok!(Purchase::update_balance(
RuntimeOrigin::signed(validity_origin()),
alice(),
100_000,
100_000,
Permill::zero(),
));
assert_noop!(
Purchase::payout(RuntimeOrigin::signed(payment_account()), alice()),
ArithmeticError::Underflow
);
});
}
#[test]
fn remove_pallet_works() {
new_test_ext().execute_with(|| {
let account_status = AccountStatus {
validity: AccountValidity::Completed,
free_balance: 1234,
locked_balance: 4321,
signature: b"my signature".to_vec(),
vat: Permill::from_percent(50),
};
// Add some storage.
Accounts::<Test>::insert(alice(), account_status.clone());
Accounts::<Test>::insert(bob(), account_status);
PaymentAccount::<Test>::put(alice());
Statement::<Test>::put(b"hello, world!".to_vec());
UnlockBlock::<Test>::put(4);
// Verify storage exists.
assert_eq!(Accounts::<Test>::iter().count(), 2);
assert!(PaymentAccount::<Test>::exists());
assert!(Statement::<Test>::exists());
assert!(UnlockBlock::<Test>::exists());
// Remove storage.
remove_pallet::<Test>();
// Verify storage is gone.
assert_eq!(Accounts::<Test>::iter().count(), 0);
assert!(!PaymentAccount::<Test>::exists());
assert!(!Statement::<Test>::exists());
assert!(!UnlockBlock::<Test>::exists());
});
}
+69
View File
@@ -0,0 +1,69 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! The `SlotRange` struct which succinctly handles the 36 values that
//! represent all sub ranges between 0 and 7 inclusive.
slot_range_helper::generate_slot_range!(
Zero(0),
One(1),
Two(2),
Three(3),
Four(4),
Five(5),
Six(6),
Seven(7)
);
// Will generate:
// pub enum SlotRange {
// ZeroZero, 0
// ZeroOne, 1
// ZeroTwo, 2
// ZeroThree, 3
// ZeroFour, 4
// ZeroFive, 5
// ZeroSix, 6
// ZeroSeven, 7
// OneOne, 8
// OneTwo, 9
// OneThree, 10
// OneFour, 11
// OneFive, 12
// OneSix, 13
// OneSeven, 14
// TwoTwo, 15
// TwoThree, 16
// TwoFour, 17
// TwoFive, 18
// TwoSix, 19
// TwoSeven, 20
// ThreeThree, 21
// ThreeFour, 22
// ThreeFive, 23
// ThreeSix, 24
// ThreeSeven, 25
// FourFour, 26
// FourFive, 27
// FourSix, 28
// FourSeven, 29
// FiveFive, 30
// FiveSix, 31
// FiveSeven, 32
// SixSix, 33
// SixSeven, 34
// SevenSeven, 35
// }
@@ -0,0 +1,90 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
use super::*;
use crate::crowdloan;
use sp_runtime::traits::AccountIdConversion;
/// Migrations for using fund index to create fund accounts instead of para ID.
pub mod slots_crowdloan_index_migration {
use super::*;
// The old way we generated fund accounts.
fn old_fund_account_id<T: Config + crowdloan::Config>(index: ParaId) -> T::AccountId {
<T as crowdloan::Config>::PalletId::get().into_sub_account_truncating(index)
}
pub fn pre_migrate<T: Config + crowdloan::Config>() -> Result<(), &'static str> {
for (para_id, leases) in Leases::<T>::iter() {
let old_fund_account = old_fund_account_id::<T>(para_id);
for (who, _amount) in leases.iter().flatten() {
if *who == old_fund_account {
let crowdloan =
crowdloan::Funds::<T>::get(para_id).ok_or("no crowdloan found")?;
log::info!(
target: "runtime",
"para_id={:?}, old_fund_account={:?}, fund_id={:?}, leases={:?}",
para_id, old_fund_account, crowdloan.fund_index, leases,
);
break;
}
}
}
Ok(())
}
pub fn migrate<T: Config + crowdloan::Config>() -> frame_support::weights::Weight {
let mut weight = Weight::zero();
for (para_id, mut leases) in Leases::<T>::iter() {
weight = weight.saturating_add(T::DbWeight::get().reads(2));
// the para id must have a crowdloan
if let Some(fund) = crowdloan::Funds::<T>::get(para_id) {
let old_fund_account = old_fund_account_id::<T>(para_id);
let new_fund_account = crowdloan::Pallet::<T>::fund_account_id(fund.fund_index);
// look for places the old account is used, and replace with the new account.
for (who, _amount) in leases.iter_mut().flatten() {
if *who == old_fund_account {
*who = new_fund_account.clone();
}
}
// insert the changes.
weight = weight.saturating_add(T::DbWeight::get().writes(1));
Leases::<T>::insert(para_id, leases);
}
}
weight
}
pub fn post_migrate<T: Config + crowdloan::Config>() -> Result<(), &'static str> {
for (para_id, leases) in Leases::<T>::iter() {
let old_fund_account = old_fund_account_id::<T>(para_id);
log::info!(target: "runtime", "checking para_id: {:?}", para_id);
// check the old fund account doesn't exist anywhere.
for (who, _amount) in leases.iter().flatten() {
if *who == old_fund_account {
panic!("old fund account found after migration!");
}
}
}
Ok(())
}
}
File diff suppressed because it is too large Load Diff
+265
View File
@@ -0,0 +1,265 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Traits used across pallets for Pezkuwi.
use alloc::vec::*;
use frame_support::{
dispatch::DispatchResult,
traits::{Currency, ReservableCurrency},
};
use pezkuwi_primitives::{HeadData, Id as ParaId, ValidationCode};
/// Teyrchain registration API.
pub trait Registrar {
/// The account ID type that encodes a teyrchain manager ID.
type AccountId;
/// Report the manager (permissioned owner) of a teyrchain, if there is one.
fn manager_of(id: ParaId) -> Option<Self::AccountId>;
/// All lease holding teyrchains. Ordered ascending by `ParaId`. On-demand
/// teyrchains are not included.
fn teyrchains() -> Vec<ParaId>;
/// Return if a `ParaId` is a lease holding Teyrchain.
fn is_teyrchain(id: ParaId) -> bool {
Self::teyrchains().binary_search(&id).is_ok()
}
/// Return if a `ParaId` is a Parathread (on-demand teyrchain).
fn is_parathread(id: ParaId) -> bool;
/// Return if a `ParaId` is registered in the system.
fn is_registered(id: ParaId) -> bool {
Self::is_parathread(id) || Self::is_teyrchain(id)
}
/// Apply a lock to the para registration so that it cannot be modified by
/// the manager directly. Instead the para must use its sovereign governance
/// or the governance of the relay chain.
fn apply_lock(id: ParaId);
/// Remove any lock on the para registration.
fn remove_lock(id: ParaId);
/// Register a Para ID under control of `who`. Registration may be
/// delayed by session rotation.
fn register(
who: Self::AccountId,
id: ParaId,
genesis_head: HeadData,
validation_code: ValidationCode,
) -> DispatchResult;
/// Deregister a Para ID, free any data, and return any deposits.
fn deregister(id: ParaId) -> DispatchResult;
/// Elevate a para to teyrchain status.
fn make_teyrchain(id: ParaId) -> DispatchResult;
/// Downgrade lease holding teyrchain into parathread (on-demand teyrchain)
fn make_parathread(id: ParaId) -> DispatchResult;
#[cfg(any(feature = "runtime-benchmarks", test))]
fn worst_head_data() -> HeadData;
#[cfg(any(feature = "runtime-benchmarks", test))]
fn worst_validation_code() -> ValidationCode;
/// Execute any pending state transitions for paras.
/// For example onboarding to on-demand teyrchain, or upgrading on-demand to
/// lease holding teyrchain.
#[cfg(any(feature = "runtime-benchmarks", test))]
fn execute_pending_transitions();
}
/// Error type for something that went wrong with leasing.
#[derive(Debug)]
pub enum LeaseError {
/// Unable to reserve the funds in the leaser's account.
ReserveFailed,
/// There is already a lease on at least one period for the given para.
AlreadyLeased,
/// The period to be leased has already ended.
AlreadyEnded,
/// A lease period has not started yet, due to an offset in the starting block.
NoLeasePeriod,
}
/// Lease manager. Used by the auction module to handle teyrchain slot leases.
pub trait Leaser<BlockNumber> {
/// An account identifier for a leaser.
type AccountId;
/// The measurement type for counting lease periods (generally just a `BlockNumber`).
type LeasePeriod;
/// The currency type in which the lease is taken.
type Currency: ReservableCurrency<Self::AccountId>;
/// Lease a new teyrchain slot for `para`.
///
/// `leaser` shall have a total of `amount` balance reserved by the implementer of this trait.
///
/// Note: The implementer of the trait (the leasing system) is expected to do all
/// reserve/unreserve calls. The caller of this trait *SHOULD NOT* pre-reserve the deposit
/// (though should ensure that it is reservable).
///
/// The lease will last from `period_begin` for `period_count` lease periods. It is undefined if
/// the `para` already has a slot leased during those periods.
///
/// Returns `Err` in the case of an error, and in which case nothing is changed.
fn lease_out(
para: ParaId,
leaser: &Self::AccountId,
amount: <Self::Currency as Currency<Self::AccountId>>::Balance,
period_begin: Self::LeasePeriod,
period_count: Self::LeasePeriod,
) -> Result<(), LeaseError>;
/// Return the amount of balance currently held in reserve on `leaser`'s account for leasing
/// `para`. This won't go down outside a lease period.
fn deposit_held(
para: ParaId,
leaser: &Self::AccountId,
) -> <Self::Currency as Currency<Self::AccountId>>::Balance;
/// The length of a lease period, and any offset which may be introduced.
/// This is only used in benchmarking to automate certain calls.
#[cfg(any(feature = "runtime-benchmarks", test))]
fn lease_period_length() -> (BlockNumber, BlockNumber);
/// Returns the lease period at `block`, and if this is the first block of a new lease period.
///
/// Will return `None` if the first lease period has not started yet, for example when an offset
/// is placed.
fn lease_period_index(block: BlockNumber) -> Option<(Self::LeasePeriod, bool)>;
/// Returns true if the teyrchain already has a lease in any of lease periods in the inclusive
/// range `[first_period, last_period]`, intersected with the unbounded range
/// `[current_lease_period..]`.
fn already_leased(
para_id: ParaId,
first_period: Self::LeasePeriod,
last_period: Self::LeasePeriod,
) -> bool;
}
/// An enum which tracks the status of the auction system, and which phase it is in.
#[derive(PartialEq, Debug)]
pub enum AuctionStatus<BlockNumber> {
/// An auction has not started yet.
NotStarted,
/// We are in the starting period of the auction, collecting initial bids.
StartingPeriod,
/// We are in the ending period of the auction, where we are taking snapshots of the winning
/// bids. This state supports "sampling", where we may only take a snapshot every N blocks.
/// In this case, the first number is the current sample number, and the second number
/// is the sub-sample. i.e. for sampling every 20 blocks, the 25th block in the ending period
/// will be `EndingPeriod(1, 5)`.
EndingPeriod(BlockNumber, BlockNumber),
/// We have completed the bidding process and are waiting for the VRF to return some acceptable
/// randomness to select the winner. The number represents how many blocks we have been
/// waiting.
VrfDelay(BlockNumber),
}
impl<BlockNumber> AuctionStatus<BlockNumber> {
/// Returns true if the auction is in any state other than `NotStarted`.
pub fn is_in_progress(&self) -> bool {
!matches!(self, Self::NotStarted)
}
/// Return true if the auction is in the starting period.
pub fn is_starting(&self) -> bool {
matches!(self, Self::StartingPeriod)
}
/// Returns `Some(sample, sub_sample)` if the auction is in the `EndingPeriod`,
/// otherwise returns `None`.
pub fn is_ending(self) -> Option<(BlockNumber, BlockNumber)> {
match self {
Self::EndingPeriod(sample, sub_sample) => Some((sample, sub_sample)),
_ => None,
}
}
/// Returns true if the auction is in the `VrfDelay` period.
pub fn is_vrf(&self) -> bool {
matches!(self, Self::VrfDelay(_))
}
}
pub trait Auctioneer<BlockNumber> {
/// An account identifier for a leaser.
type AccountId;
/// The measurement type for counting lease periods (generally the same as `BlockNumber`).
type LeasePeriod;
/// The currency type in which the lease is taken.
type Currency: ReservableCurrency<Self::AccountId>;
/// Create a new auction.
///
/// This can only happen when there isn't already an auction in progress. Accepts the `duration`
/// of this auction and the `lease_period_index` of the initial lease period of the four that
/// are to be auctioned.
fn new_auction(duration: BlockNumber, lease_period_index: Self::LeasePeriod) -> DispatchResult;
/// Given the current block number, return the current auction status.
fn auction_status(now: BlockNumber) -> AuctionStatus<BlockNumber>;
/// Place a bid in the current auction.
///
/// - `bidder`: The account that will be funding this bid.
/// - `para`: The para to bid for.
/// - `first_slot`: The first lease period index of the range to be bid on.
/// - `last_slot`: The last lease period index of the range to be bid on (inclusive).
/// - `amount`: The total amount to be the bid for deposit over the range.
///
/// The account `Bidder` must have at least `amount` available as a free balance in `Currency`.
/// The implementation *MUST* remove or reserve `amount` funds from `bidder` and those funds
/// should be returned or freed once the bid is rejected or lease has ended.
fn place_bid(
bidder: Self::AccountId,
para: ParaId,
first_slot: Self::LeasePeriod,
last_slot: Self::LeasePeriod,
amount: <Self::Currency as Currency<Self::AccountId>>::Balance,
) -> DispatchResult;
/// The length of a lease period, and any offset which may be introduced.
/// This is only used in benchmarking to automate certain calls.
#[cfg(any(feature = "runtime-benchmarks", test))]
fn lease_period_length() -> (BlockNumber, BlockNumber);
/// Returns the lease period at `block`, and if this is the first block of a new lease period.
///
/// Will return `None` if the first lease period has not started yet, for example when an offset
/// is placed.
fn lease_period_index(block: BlockNumber) -> Option<(Self::LeasePeriod, bool)>;
/// Check if the para and user combination has won an auction in the past.
fn has_won_an_auction(para: ParaId, bidder: &Self::AccountId) -> bool;
}
/// Runtime hook for when we swap a lease holding teyrchain and an on-demand teyrchain.
#[impl_trait_for_tuples::impl_for_tuples(30)]
pub trait OnSwap {
/// Updates any needed state/references to enact a logical swap of two teyrchains. Identity,
/// code and `head_data` remain equivalent for all teyrchains/threads, however other properties
/// such as leases, deposits held and thread/chain nature are swapped.
fn on_swap(one: ParaId, other: ParaId);
}
+107
View File
@@ -0,0 +1,107 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Common try-runtime only tests for runtimes.
use alloc::{collections::btree_set::BTreeSet, vec::Vec};
use frame_support::{
dispatch::RawOrigin,
traits::{Get, Hooks},
};
use pallet_fast_unstake::{Pallet as FastUnstake, *};
use pallet_staking::*;
/// register all inactive nominators for fast-unstake, and progress until they have all been
/// processed.
pub fn migrate_all_inactive_nominators<T: pallet_fast_unstake::Config + pallet_staking::Config>()
where
<T as frame_system::Config>::RuntimeEvent: TryInto<pallet_fast_unstake::Event<T>>,
{
let mut unstaked_ok = 0;
let mut unstaked_err = 0;
let mut unstaked_slashed = 0;
let all_stakers = Ledger::<T>::iter().map(|(ctrl, l)| (ctrl, l.stash)).collect::<BTreeSet<_>>();
let mut all_exposed = BTreeSet::new();
ErasStakersPaged::<T>::iter().for_each(|((_era, val, _page), expo)| {
all_exposed.insert(val);
all_exposed.extend(expo.others.iter().map(|ie| ie.who.clone()))
});
let eligible = all_stakers
.iter()
.filter_map(|(ctrl, stash)| all_exposed.contains(stash).then_some(ctrl))
.collect::<Vec<_>>();
log::info!(
target: "runtime::test",
"registering {} out of {} stakers for fast-unstake",
eligible.len(),
all_stakers.len()
);
for ctrl in eligible {
if let Err(why) =
FastUnstake::<T>::register_fast_unstake(RawOrigin::Signed(ctrl.clone()).into())
{
log::warn!(target: "runtime::test", "failed to register {:?} due to {:?}", ctrl, why);
}
}
log::info!(
target: "runtime::test",
"registered {} successfully, starting at {:?}.",
Queue::<T>::count(),
frame_system::Pallet::<T>::block_number(),
);
while Queue::<T>::count() != 0 || Head::<T>::get().is_some() {
let now = frame_system::Pallet::<T>::block_number();
let weight = <T as frame_system::Config>::BlockWeights::get().max_block;
let consumed = FastUnstake::<T>::on_idle(now, weight);
log::debug!(target: "runtime::test", "consumed {:?} ({})", consumed, consumed.ref_time() as f32 / weight.ref_time() as f32);
frame_system::Pallet::<T>::read_events_no_consensus()
.into_iter()
.map(|r| r.event)
.filter_map(|e| {
let maybe_fast_unstake_event: Option<pallet_fast_unstake::Event<T>> =
e.try_into().ok();
maybe_fast_unstake_event
})
.for_each(|e: pallet_fast_unstake::Event<T>| match e {
pallet_fast_unstake::Event::<T>::Unstaked { result, .. } =>
if result.is_ok() {
unstaked_ok += 1;
} else {
unstaked_err += 1
},
pallet_fast_unstake::Event::<T>::Slashed { .. } => unstaked_slashed += 1,
pallet_fast_unstake::Event::<T>::InternalError => unreachable!(),
_ => {},
});
if now % 100u32.into() == sp_runtime::traits::Zero::zero() {
log::info!(
target: "runtime::test",
"status: ok {}, err {}, slash {}",
unstaked_ok,
unstaked_err,
unstaked_slashed,
);
}
frame_system::Pallet::<T>::reset_events();
}
}
+386
View File
@@ -0,0 +1,386 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! XCM sender for relay chain.
use alloc::vec::Vec;
use codec::{DecodeLimit, Encode};
use core::marker::PhantomData;
use frame_support::traits::Get;
use frame_system::pallet_prelude::BlockNumberFor;
use pezkuwi_primitives::Id as ParaId;
use pezkuwi_runtime_teyrchains::{
configuration::{self, HostConfiguration},
dmp, FeeTracker,
};
use sp_runtime::FixedPointNumber;
use xcm::{prelude::*, MAX_XCM_DECODE_DEPTH};
use xcm_builder::InspectMessageQueues;
use SendError::*;
/// Simple value-bearing trait for determining/expressing the assets required to be paid for a
/// messages to be delivered to a teyrchain.
pub trait PriceForMessageDelivery {
/// Type used for charging different prices to different destinations
type Id;
/// Return the assets required to deliver `message` to the given `para` destination.
fn price_for_delivery(id: Self::Id, message: &Xcm<()>) -> Assets;
}
impl PriceForMessageDelivery for () {
type Id = ();
fn price_for_delivery(_: Self::Id, _: &Xcm<()>) -> Assets {
Assets::new()
}
}
pub struct NoPriceForMessageDelivery<Id>(PhantomData<Id>);
impl<Id> PriceForMessageDelivery for NoPriceForMessageDelivery<Id> {
type Id = Id;
fn price_for_delivery(_: Self::Id, _: &Xcm<()>) -> Assets {
Assets::new()
}
}
/// Implementation of [`PriceForMessageDelivery`] which returns a fixed price.
pub struct ConstantPrice<T>(core::marker::PhantomData<T>);
impl<T: Get<Assets>> PriceForMessageDelivery for ConstantPrice<T> {
type Id = ();
fn price_for_delivery(_: Self::Id, _: &Xcm<()>) -> Assets {
T::get()
}
}
/// Implementation of [`PriceForMessageDelivery`] which returns an exponentially increasing price.
/// The formula for the fee is based on the sum of a base fee plus a message length fee, multiplied
/// by a specified factor. In mathematical form:
///
/// `F * (B + encoded_msg_len * M)`
///
/// Thus, if F = 1 and M = 0, this type is equivalent to [`ConstantPrice<B>`].
///
/// The type parameters are understood as follows:
///
/// - `A`: Used to denote the asset ID that will be used for paying the delivery fee.
/// - `B`: The base fee to pay for message delivery.
/// - `M`: The fee to pay for each and every byte of the message after encoding it.
/// - `F`: A fee factor multiplier. It can be understood as the exponent term in the formula.
pub struct ExponentialPrice<A, B, M, F>(core::marker::PhantomData<(A, B, M, F)>);
impl<A: Get<AssetId>, B: Get<u128>, M: Get<u128>, F: FeeTracker> PriceForMessageDelivery
for ExponentialPrice<A, B, M, F>
{
type Id = F::Id;
fn price_for_delivery(id: Self::Id, msg: &Xcm<()>) -> Assets {
let msg_fee = (msg.encoded_size() as u128).saturating_mul(M::get());
let fee_sum = B::get().saturating_add(msg_fee);
let amount = F::get_fee_factor(id).saturating_mul_int(fee_sum);
(A::get(), amount).into()
}
}
/// XCM sender for relay chain. It only sends downward message.
pub struct ChildTeyrchainRouter<T, W, P>(PhantomData<(T, W, P)>);
impl<T: configuration::Config + dmp::Config, W: xcm::WrapVersion, P> SendXcm
for ChildTeyrchainRouter<T, W, P>
where
P: PriceForMessageDelivery<Id = ParaId>,
{
type Ticket = (HostConfiguration<BlockNumberFor<T>>, ParaId, Vec<u8>);
fn validate(
dest: &mut Option<Location>,
msg: &mut Option<Xcm<()>>,
) -> SendResult<(HostConfiguration<BlockNumberFor<T>>, ParaId, Vec<u8>)> {
let d = dest.take().ok_or(MissingArgument)?;
let id = if let (0, [Teyrchain(id)]) = d.unpack() {
*id
} else {
*dest = Some(d);
return Err(NotApplicable);
};
// Downward message passing.
let xcm = msg.take().ok_or(MissingArgument)?;
let config = configuration::ActiveConfig::<T>::get();
let para = id.into();
let price = P::price_for_delivery(para, &xcm);
let versioned_xcm = W::wrap_version(&d, xcm).map_err(|()| DestinationUnsupported)?;
versioned_xcm.check_is_decodable().map_err(|()| ExceedsMaxMessageSize)?;
let blob = versioned_xcm.encode();
dmp::Pallet::<T>::can_queue_downward_message(&config, &para, &blob)
.map_err(Into::<SendError>::into)?;
Ok(((config, para, blob), price))
}
fn deliver(
(config, para, blob): (HostConfiguration<BlockNumberFor<T>>, ParaId, Vec<u8>),
) -> Result<XcmHash, SendError> {
let hash = sp_io::hashing::blake2_256(&blob[..]);
dmp::Pallet::<T>::queue_downward_message(&config, para, blob)
.map(|()| hash)
.map_err(|error| {
log::debug!(
target: "xcm::xcm_sender::deliver",
"Failed to place into DMP queue: error: {error:?}, id: {hash:?}",
);
SendError::Transport(&"Error placing into DMP queue")
})
}
#[cfg(feature = "runtime-benchmarks")]
fn ensure_successful_delivery(location: Option<Location>) {
if let Some((0, [Teyrchain(id)])) = location.as_ref().map(|l| l.unpack()) {
dmp::Pallet::<T>::make_teyrchain_reachable(*id);
}
}
}
impl<T: dmp::Config, W, P> InspectMessageQueues for ChildTeyrchainRouter<T, W, P> {
fn clear_messages() {
// Best effort.
let _ = dmp::DownwardMessageQueues::<T>::clear(u32::MAX, None);
}
fn get_messages() -> Vec<(VersionedLocation, Vec<VersionedXcm<()>>)> {
dmp::DownwardMessageQueues::<T>::iter()
.map(|(para_id, messages)| {
let decoded_messages: Vec<VersionedXcm<()>> = messages
.iter()
.map(|downward_message| {
let message = VersionedXcm::<()>::decode_all_with_depth_limit(
MAX_XCM_DECODE_DEPTH,
&mut &downward_message.msg[..],
)
.unwrap();
log::trace!(
target: "xcm::DownwardMessageQueues::get_messages",
"Message: {:?}, sent at: {:?}", message, downward_message.sent_at
);
message
})
.collect();
(
VersionedLocation::from(Location::from(Teyrchain(para_id.into()))),
decoded_messages,
)
})
.collect()
}
}
/// Implementation of `xcm_builder::EnsureDelivery` which helps to ensure delivery to the
/// `ParaId` teyrchain (sibling or child). Deposits existential deposit for origin (if needed).
/// Deposits estimated fee to the origin account (if needed).
/// Allows to trigger additional logic for specific `ParaId` (e.g. open HRMP channel) (if needed).
#[cfg(feature = "runtime-benchmarks")]
pub struct ToTeyrchainDeliveryHelper<
XcmConfig,
ExistentialDeposit,
PriceForDelivery,
ParaId,
ToParaIdHelper,
>(
core::marker::PhantomData<(
XcmConfig,
ExistentialDeposit,
PriceForDelivery,
ParaId,
ToParaIdHelper,
)>,
);
#[cfg(feature = "runtime-benchmarks")]
impl<
XcmConfig: xcm_executor::Config,
ExistentialDeposit: Get<Option<Asset>>,
PriceForDelivery: PriceForMessageDelivery<Id = ParaId>,
Teyrchain: Get<ParaId>,
ToTeyrchainHelper: pezkuwi_runtime_teyrchains::EnsureForTeyrchain,
> xcm_builder::EnsureDelivery
for ToTeyrchainDeliveryHelper<
XcmConfig,
ExistentialDeposit,
PriceForDelivery,
Teyrchain,
ToTeyrchainHelper,
>
{
fn ensure_successful_delivery(
origin_ref: &Location,
dest: &Location,
fee_reason: xcm_executor::traits::FeeReason,
) -> (Option<xcm_executor::FeesMode>, Option<Assets>) {
use alloc::vec;
use xcm::{latest::MAX_ITEMS_IN_ASSETS, MAX_INSTRUCTIONS_TO_DECODE};
use xcm_executor::{
traits::{FeeManager, TransactAsset},
FeesMode,
};
// check if the destination matches the expected `Teyrchain`.
if let Some(Teyrchain(para_id)) = dest.first_interior() {
if ParaId::from(*para_id) != Teyrchain::get().into() {
return (None, None);
}
} else {
return (None, None);
}
// allow more initialization for target teyrchain
ToTeyrchainHelper::ensure(Teyrchain::get());
let mut fees_mode = None;
if !XcmConfig::FeeManager::is_waived(Some(origin_ref), fee_reason) {
// if not waived, we need to set up accounts for paying and receiving fees
// mint ED to origin if needed
if let Some(ed) = ExistentialDeposit::get() {
XcmConfig::AssetTransactor::deposit_asset(&ed, &origin_ref, None).unwrap();
}
// overestimate delivery fee
let mut max_assets: Vec<Asset> = Vec::new();
for i in 0..MAX_ITEMS_IN_ASSETS {
max_assets.push((GeneralIndex(i as u128), 100u128).into());
}
let overestimated_xcm =
vec![WithdrawAsset(max_assets.into()); MAX_INSTRUCTIONS_TO_DECODE as usize].into();
let overestimated_fees =
PriceForDelivery::price_for_delivery(Teyrchain::get(), &overestimated_xcm);
// mint overestimated fee to origin
for fee in overestimated_fees.inner() {
XcmConfig::AssetTransactor::deposit_asset(&fee, &origin_ref, None).unwrap();
}
// expected worst case - direct withdraw
fees_mode = Some(FeesMode { jit_withdraw: true });
}
(fees_mode, None)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::integration_tests::new_test_ext;
use alloc::vec;
use frame_support::{assert_ok, parameter_types};
use pezkuwi_runtime_teyrchains::FeeTracker;
use sp_runtime::FixedU128;
use xcm::MAX_XCM_DECODE_DEPTH;
parameter_types! {
pub const BaseDeliveryFee: u128 = 300_000_000;
pub const TransactionByteFee: u128 = 1_000_000;
pub FeeAssetId: AssetId = AssetId(Here.into());
}
struct TestFeeTracker;
impl FeeTracker for TestFeeTracker {
type Id = ParaId;
fn get_fee_factor(_: Self::Id) -> FixedU128 {
FixedU128::from_rational(101, 100)
}
fn set_fee_factor(_id: Self::Id, _val: FixedU128) {}
fn increase_fee_factor(_: Self::Id, _: u128) {}
fn decrease_fee_factor(_: Self::Id) -> bool {
true
}
}
type TestExponentialPrice =
ExponentialPrice<FeeAssetId, BaseDeliveryFee, TransactionByteFee, TestFeeTracker>;
#[test]
fn exponential_price_correct_price_calculation() {
let id: ParaId = 123.into();
let b: u128 = BaseDeliveryFee::get();
let m: u128 = TransactionByteFee::get();
// F * (B + msg_length * M)
// message_length = 1
let result: u128 = TestFeeTracker::get_fee_factor(id).saturating_mul_int(b + m);
assert_eq!(
TestExponentialPrice::price_for_delivery(id, &Xcm(vec![])),
(FeeAssetId::get(), result).into()
);
// message size = 2
let result: u128 = TestFeeTracker::get_fee_factor(id).saturating_mul_int(b + (2 * m));
assert_eq!(
TestExponentialPrice::price_for_delivery(id, &Xcm(vec![ClearOrigin])),
(FeeAssetId::get(), result).into()
);
// message size = 4
let result: u128 = TestFeeTracker::get_fee_factor(id).saturating_mul_int(b + (4 * m));
assert_eq!(
TestExponentialPrice::price_for_delivery(
id,
&Xcm(vec![SetAppendix(Xcm(vec![ClearOrigin]))])
),
(FeeAssetId::get(), result).into()
);
}
#[test]
fn child_teyrchain_router_validate_nested_xcm_works() {
let dest = Teyrchain(5555);
type Router = ChildTeyrchainRouter<
crate::integration_tests::Test,
(),
NoPriceForMessageDelivery<ParaId>,
>;
// Message that is not too deeply nested:
let mut good = Xcm(vec![ClearOrigin]);
for _ in 0..MAX_XCM_DECODE_DEPTH - 1 {
good = Xcm(vec![SetAppendix(good)]);
}
new_test_ext().execute_with(|| {
configuration::ActiveConfig::<crate::integration_tests::Test>::mutate(|c| {
c.max_downward_message_size = u32::MAX;
});
dmp::Pallet::<crate::integration_tests::Test>::make_teyrchain_reachable(5555);
// Check that the good message is validated:
assert_ok!(<Router as SendXcm>::validate(
&mut Some(dest.into()),
&mut Some(good.clone())
));
// Nesting the message one more time should reject it:
let bad = Xcm(vec![SetAppendix(good)]);
assert_eq!(
Err(ExceedsMaxMessageSize),
<Router as SendXcm>::validate(&mut Some(dest.into()), &mut Some(bad))
);
});
}
}