Skip to content

Commit

Permalink
backport updates in fellows/runtimes
Browse files Browse the repository at this point in the history
  • Loading branch information
brenzi committed Nov 2, 2023
1 parent c855af2 commit f715c59
Show file tree
Hide file tree
Showing 11 changed files with 376 additions and 61 deletions.
4 changes: 3 additions & 1 deletion polkadot-parachains/encointer-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ parachains-common = { default-features = false, version = "3.0.0" }
# Cumulus dependencies
cumulus-pallet-aura-ext = { default-features = false, version = "0.3.0" }
cumulus-pallet-dmp-queue = { default-features = false, version = "0.3.0" }
cumulus-pallet-parachain-system = { default-features = false, version = "0.3.0" }
cumulus-pallet-parachain-system = { default-features = false, features = [
"parameterized-consensus-hook",
], version = "0.3.0" }
cumulus-pallet-xcm = { default-features = false, version = "0.3.0" }
cumulus-pallet-xcmp-queue = { default-features = false, version = "0.3.0" }
cumulus-primitives-core = { default-features = false, version = "0.3.0" }
Expand Down
45 changes: 45 additions & 0 deletions polkadot-parachains/encointer-runtime/src/deal_with_fees.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2023 Encointer Association
// This file is part of Encointer
//
// Encointer 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.
//
// Encointer 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 Encointer. If not, see <http://www.gnu.org/licenses/>.

use frame_support::traits::{Currency, Imbalance, OnUnbalanced};
use sp_runtime::sp_std::marker::PhantomData;

/// Type alias to conveniently refer to the `Currency::NegativeImbalance` associated type.
pub type NegativeImbalance<T> = <pallet_balances::Pallet<T> as Currency<
<T as frame_system::Config>::AccountId,
>>::NegativeImbalance;

/// Moves all the fees to the treasury.
///
/// This does only handle the native currency. The community currencies are managed by the
/// `pallet-asset-tx-payment`.
pub struct FeesToTreasury<Runtime>(PhantomData<Runtime>);

impl<Runtime> OnUnbalanced<NegativeImbalance<Runtime>> for FeesToTreasury<Runtime>
where
Runtime: pallet_balances::Config + pallet_treasury::Config,
{
fn on_unbalanceds<B>(mut fees_then_tips: impl Iterator<Item = NegativeImbalance<Runtime>>) {
if let Some(mut fees) = fees_then_tips.next() {
// no burning, add all fees and tips to the treasury

if let Some(tips) = fees_then_tips.next() {
tips.merge_into(&mut fees);
}
<FeesToTreasury<Runtime> as OnUnbalanced<_>>::on_unbalanced(fees);
}
}
}
91 changes: 45 additions & 46 deletions polkadot-parachains/encointer-runtime/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019 Alain Brenzikofer
// Copyright (c) 2023 Encointer Association
// This file is part of Encointer
//
// Encointer is free software: you can redistribute it and/or modify
Expand All @@ -20,7 +20,7 @@
//! * perform ceremonies and receive a community income
//! * pay fees in the respective community currency
//!
//! The configuration (especially XCM) is almost identical to `statemine`. Therefore, upstream
//! The configuration (especially XCM) is almost identical to `asset-hub`. Therefore, upstream
//! updates should always check the diff to see if there are some configuration updates.

#![cfg_attr(not(feature = "std"), no_std)]
Expand All @@ -31,10 +31,11 @@
#[cfg(feature = "std")]
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));

mod deal_with_fees;
mod weights;
pub mod xcm_config;

use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases;
use cumulus_pallet_parachain_system::RelayNumberMonotonicallyIncreases;
use sp_api::impl_runtime_apis;
use sp_core::{crypto::KeyTypeId, OpaqueMetadata};
use sp_runtime::{
Expand All @@ -56,22 +57,22 @@ use frame_support::{
dispatch::DispatchClass,
parameter_types,
traits::{
tokens::ConversionToAssetBalance, ConstBool, ConstU32, Contains, EitherOfDiverse,
EqualPrivilegeOnly, InstanceFilter,
tokens::ConversionToAssetBalance, ConstBool, Contains, EitherOfDiverse, EqualPrivilegeOnly,
InstanceFilter,
},
weights::{ConstantMultiplier, Weight},
PalletId, RuntimeDebug,
PalletId,
};
use frame_system::{
limits::{BlockLength, BlockWeights},
EnsureRoot,
};
pub use parachains_common as common;
pub use parachains_common::MILLISECS_PER_BLOCK;
use sp_runtime::RuntimeDebug;

use parachains_common::{
AuraId, AVERAGE_ON_INITIALIZE_RATIO, DAYS, HOURS, MAXIMUM_BLOCK_WEIGHT, NORMAL_DISPATCH_RATIO,
SLOT_DURATION,
kusama::consensus::RELAY_CHAIN_SLOT_DURATION_MILLIS, AuraId, AVERAGE_ON_INITIALIZE_RATIO, DAYS,
HOURS, MAXIMUM_BLOCK_WEIGHT, NORMAL_DISPATCH_RATIO,
};
use xcm_config::{KsmLocation, XcmConfig, XcmOriginToTransactDispatchOrigin};

Expand All @@ -97,6 +98,7 @@ pub use encointer_primitives::{
communities::{CommunityIdentifier, Location},
scheduler::CeremonyPhaseType,
};
use sp_core::ConstU32;

// XCM imports
// Polkadot imports
Expand All @@ -105,18 +107,30 @@ use polkadot_runtime_common::{BlockHashCount, SlowAdjustingFeeUpdate};
use xcm::latest::BodyId;
use xcm_executor::XcmExecutor;

// Added by encointer
pub(crate) use runtime_common::{
currency::*,
deal_with_fees::FeesToTreasury,
fee::WeightToFee,
weights::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight},
};
// adopt all currency-related constants identical to other system chains
use deal_with_fees::FeesToTreasury;
use parachains_common::kusama::{currency::*, fee::WeightToFee};
use weights::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight};

/// A type to hold UTC unix epoch [ms]
pub type Moment = u64;
pub const ONE_DAY: Moment = 86_400_000;

/// Maximum number of blocks simultaneously accepted by the Runtime, not yet included
/// into the relay chain.
const UNINCLUDED_SEGMENT_CAPACITY: u32 = 1;
/// How many parachain blocks are processed by the relay chain per parent. Limits the
/// number of blocks authored per slot.
const BLOCK_PROCESSING_VELOCITY: u32 = 1;
/// This determines the average expected block time that we are targeting.
/// Blocks will be produced at a minimum duration defined by `SLOT_DURATION`.
/// `SLOT_DURATION` is picked up by `pallet_timestamp` which is in turn picked
/// up by `pallet_aura` to implement `fn slot_duration()`.
///
/// Change this to adjust the block time.
pub const MILLISECS_PER_BLOCK: u64 = 12000;
pub const SLOT_DURATION: u64 = MILLISECS_PER_BLOCK;

pub type AssetId = AssetIdOf<Runtime>;
pub type AssetBalance = AssetBalanceOf<Runtime>;

Expand Down Expand Up @@ -353,7 +367,8 @@ impl pallet_treasury::Config for Runtime {
type ProposalBond = ProposalBond;
type ProposalBondMinimum = ProposalBondMinimum;
type ProposalBondMaximum = ProposalBondMaximum;
type SpendPeriod = SpendPeriod; //Cannot be 0: Error: Thread 'tokio-runtime-worker' panicked at 'attempt to calculate the remainder with a divisor of zero
type SpendPeriod = SpendPeriod; //Cannot be 0: Error: Thread 'tokio-runtime-worker' panicked at 'attempt to calculate the
// remainder with a divisor of zero
type Burn = (); //No burn
type BurnDestination = (); //No burn
type SpendFunds = (); //No spend, no bounty
Expand Down Expand Up @@ -402,7 +417,13 @@ impl cumulus_pallet_parachain_system::Config for Runtime {
type OutboundXcmpMessageSource = XcmpQueue;
type XcmpMessageHandler = XcmpQueue;
type ReservedXcmpWeight = ReservedXcmpWeight;
type CheckAssociatedRelayNumber = RelayNumberStrictlyIncreases;
type CheckAssociatedRelayNumber = RelayNumberMonotonicallyIncreases;
type ConsensusHook = cumulus_pallet_aura_ext::FixedVelocityConsensusHook<
Runtime,
RELAY_CHAIN_SLOT_DURATION_MILLIS,
BLOCK_PROCESSING_VELOCITY,
UNINCLUDED_SEGMENT_CAPACITY,
>;
}

// Added by encointer
Expand Down Expand Up @@ -446,7 +467,6 @@ parameter_types! {
parameter_types! {
pub const MomentsPerDay: Moment = 86_400_000; // [ms/d]
pub const DefaultDemurrage: Demurrage = Demurrage::from_bits(0x0000000000000000000001E3F0A8A973_i128);
// 0.000005
pub const EncointerExistentialDeposit: BalanceType = BalanceType::from_bits(0x0000000000000000000053e2d6238da4_u128);
pub const MeetupSizeTarget: u64 = 10;
pub const MeetupMinSize: u64 = 3;
Expand Down Expand Up @@ -506,7 +526,7 @@ impl pallet_encointer_reputation_commitments::Config for Runtime {

impl pallet_encointer_faucet::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type ControllerOrigin = MoreThanHalfCouncil;
type ControllerOrigin = EnsureRoot<AccountId>;
type Currency = Balances;
type PalletId = FaucetPalletId;
type WeightInfo = weights::pallet_encointer_faucet::WeightInfo<Runtime>;
Expand All @@ -517,6 +537,8 @@ impl pallet_aura::Config for Runtime {
type DisabledValidators = ();
type MaxAuthorities = MaxAuthorities;
type AllowMultipleBlocksPerSlot = ConstBool<false>;
#[cfg(feature = "experimental")]
type SlotDuration = ConstU64<SLOT_DURATION>;
}

parameter_types! {
Expand Down Expand Up @@ -687,7 +709,7 @@ mod benches {
impl_runtime_apis! {
impl sp_consensus_aura::AuraApi<Block, AuraId> for Runtime {
fn slot_duration() -> sp_consensus_aura::SlotDuration {
sp_consensus_aura::SlotDuration::from_millis(Aura::slot_duration())
sp_consensus_aura::SlotDuration::from_millis(SLOT_DURATION)
}

fn authorities() -> Vec<AuraId> {
Expand Down Expand Up @@ -894,7 +916,8 @@ impl_runtime_apis! {
fn dispatch_benchmark(
config: frame_benchmarking::BenchmarkConfig
) -> Result<Vec<frame_benchmarking::BenchmarkBatch>, sp_runtime::RuntimeString> {
use frame_benchmarking::{Benchmarking, BenchmarkBatch, TrackedStorageKey};
use frame_benchmarking::{Benchmarking, BenchmarkBatch};
use frame_support::traits::TrackedStorageKey;

use frame_system_benchmarking::Pallet as SystemBench;
impl frame_system_benchmarking::Config for Runtime {}
Expand Down Expand Up @@ -924,31 +947,7 @@ impl_runtime_apis! {
}
}

struct CheckInherents;

impl cumulus_pallet_parachain_system::CheckInherents<Block> for CheckInherents {
fn check_inherents(
block: &Block,
relay_state_proof: &cumulus_pallet_parachain_system::RelayChainStateProof,
) -> sp_inherents::CheckInherentsResult {
let relay_chain_slot = relay_state_proof
.read_slot()
.expect("Could not read the relay chain slot from the proof");

let inherent_data =
cumulus_primitives_timestamp::InherentDataProvider::from_relay_chain_slot_and_duration(
relay_chain_slot,
sp_std::time::Duration::from_secs(6),
)
.create_inherent_data()
.expect("Could not create the timestamp inherent data");

inherent_data.check_extrinsics(block)
}
}

cumulus_pallet_parachain_system::register_validate_block! {
Runtime = Runtime,
BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::<Runtime, Executive>,
CheckInherents = CheckInherents,
}
53 changes: 53 additions & 0 deletions polkadot-parachains/encointer-runtime/src/weights/block_weights.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// This file is part of Substrate.

// 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.

pub mod constants {
use frame_support::{
parameter_types,
weights::{constants, Weight},
};

parameter_types! {
/// Importing a block with 0 Extrinsics.
pub const BlockExecutionWeight: Weight =
Weight::from_parts(constants::WEIGHT_REF_TIME_PER_NANOS.saturating_mul(5_000_000), 0);
}

#[cfg(test)]
mod test_weights {
use frame_support::weights::constants;

/// Checks that the weight exists and is sane.
// NOTE: If this test fails but you are sure that the generated values are fine,
// you can delete it.
#[test]
fn sane() {
let w = super::constants::BlockExecutionWeight::get();

// At least 100 µs.
assert!(
w.ref_time() >= 100u64 * constants::WEIGHT_REF_TIME_PER_MICROS,
"Weight should be at least 100 µs."
);
// At most 50 ms.
assert!(
w.ref_time() <= 50u64 * constants::WEIGHT_REF_TIME_PER_MILLIS,
"Weight should be at most 50 ms."
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// This file is part of Substrate.

// 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.

pub mod constants {
use frame_support::{
parameter_types,
weights::{constants, Weight},
};

parameter_types! {
/// Executing a NO-OP `System::remarks` Extrinsic.
pub const ExtrinsicBaseWeight: Weight =
Weight::from_parts(constants::WEIGHT_REF_TIME_PER_NANOS.saturating_mul(125_000), 0);
}

#[cfg(test)]
mod test_weights {
use frame_support::weights::constants;

/// Checks that the weight exists and is sane.
// NOTE: If this test fails but you are sure that the generated values are fine,
// you can delete it.
#[test]
fn sane() {
let w = super::constants::ExtrinsicBaseWeight::get();

// At least 10 µs.
assert!(
w.ref_time() >= 10u64 * constants::WEIGHT_REF_TIME_PER_MICROS,
"Weight should be at least 10 µs."
);
// At most 1 ms.
assert!(
w.ref_time() <= constants::WEIGHT_REF_TIME_PER_MILLIS,
"Weight should be at most 1 ms."
);
}
}
}
9 changes: 9 additions & 0 deletions polkadot-parachains/encointer-runtime/src/weights/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
// the generated files do not pass clippy
#![allow(clippy::all)]

pub mod block_weights;
pub mod cumulus_pallet_xcmp_queue;
pub mod extrinsic_weights;
pub mod frame_system;
pub mod pallet_balances;
pub mod pallet_collective;
Expand All @@ -35,3 +37,10 @@ pub mod pallet_proxy;
pub mod pallet_timestamp;
pub mod pallet_treasury;
pub mod pallet_utility;
pub mod paritydb_weights;
pub mod rocksdb_weights;

pub use block_weights::constants::BlockExecutionWeight;
pub use extrinsic_weights::constants::ExtrinsicBaseWeight;
pub use paritydb_weights::constants::ParityDbWeight;
pub use rocksdb_weights::constants::RocksDbWeight;
Loading

0 comments on commit f715c59

Please sign in to comment.