mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 14:01:06 +00:00
Support both polkadot and kusama runtimes (#704)
* Allow both polkadot and kusama runtimes * Allow both polkadot and kusama runtimes * Make `collator` build * Removed kusama runtime * Introduced common runtime * Updated for latest substrate * Updated CI targets * Updated CI version check * Removed unused dependency * Pulled latests substrate * Pulled latest substrate * Fixed version * Apply suggestions from code review Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com> * NEW_HEADS_IDENTIFIER moved to primitives * Updated CI check script * Fixed script * Set epoch duration for polkadot * ci: check_runtime for both runtimes Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> Co-authored-by: gabriel klawitter <gabreal@users.noreply.github.com>
This commit is contained in:
committed by
Gavin Wood
parent
9a9bbd1c2d
commit
a00d74d825
@@ -0,0 +1,149 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Polkadot.
|
||||
|
||||
// Polkadot 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.
|
||||
|
||||
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Auxillary struct/enums for polkadot runtime.
|
||||
|
||||
use primitives::Balance;
|
||||
use sp_runtime::traits::{Convert, Saturating};
|
||||
use sp_runtime::{Fixed64, Perbill};
|
||||
use frame_support::weights::Weight;
|
||||
use frame_support::traits::{OnUnbalanced, Imbalance, Currency, Get};
|
||||
use crate::{MaximumBlockWeight, NegativeImbalance};
|
||||
|
||||
/// Logic for the author to get a portion of fees.
|
||||
pub struct ToAuthor<R>(rstd::marker::PhantomData<R>);
|
||||
|
||||
impl<R> OnUnbalanced<NegativeImbalance<R>> for ToAuthor<R>
|
||||
where
|
||||
R: balances::Trait + authorship::Trait,
|
||||
<R as system::Trait>::AccountId: From<primitives::AccountId>,
|
||||
<R as system::Trait>::AccountId: Into<primitives::AccountId>,
|
||||
<R as system::Trait>::Event: From<balances::RawEvent<
|
||||
<R as system::Trait>::AccountId,
|
||||
<R as balances::Trait>::Balance,
|
||||
balances::DefaultInstance>
|
||||
>,
|
||||
{
|
||||
fn on_nonzero_unbalanced(amount: NegativeImbalance<R>) {
|
||||
let numeric_amount = amount.peek();
|
||||
let author = <authorship::Module<R>>::author();
|
||||
<balances::Module<R>>::resolve_creating(&<authorship::Module<R>>::author(), amount);
|
||||
<system::Module<R>>::deposit_event(balances::RawEvent::Deposit(author, numeric_amount));
|
||||
}
|
||||
}
|
||||
|
||||
/// Converter for currencies to votes.
|
||||
pub struct CurrencyToVoteHandler<R>(rstd::marker::PhantomData<R>);
|
||||
|
||||
impl<R> CurrencyToVoteHandler<R>
|
||||
where
|
||||
R: balances::Trait,
|
||||
R::Balance: Into<u128>,
|
||||
{
|
||||
fn factor() -> u128 {
|
||||
let issuance: u128 = <balances::Module<R>>::total_issuance().into();
|
||||
(issuance / u64::max_value() as u128).max(1)
|
||||
}
|
||||
}
|
||||
|
||||
impl<R> Convert<u128, u64> for CurrencyToVoteHandler<R>
|
||||
where
|
||||
R: balances::Trait,
|
||||
R::Balance: Into<u128>,
|
||||
{
|
||||
fn convert(x: u128) -> u64 { (x / Self::factor()) as u64 }
|
||||
}
|
||||
|
||||
impl<R> Convert<u128, u128> for CurrencyToVoteHandler<R>
|
||||
where
|
||||
R: balances::Trait,
|
||||
R::Balance: Into<u128>,
|
||||
{
|
||||
fn convert(x: u128) -> u128 { x * Self::factor() }
|
||||
}
|
||||
|
||||
/// Handles converting a weight scalar to a fee value, based on the scale and granularity of the
|
||||
/// node's balance type.
|
||||
///
|
||||
/// This should typically create a mapping between the following ranges:
|
||||
/// - [0, system::MaximumBlockWeight]
|
||||
/// - [Balance::min, Balance::max]
|
||||
///
|
||||
/// Yet, it can be used for any other sort of change to weight-fee. Some examples being:
|
||||
/// - Setting it to `0` will essentially disable the weight fee.
|
||||
/// - Setting it to `1` will cause the literal `#[weight = x]` values to be charged.
|
||||
pub struct WeightToFee;
|
||||
impl Convert<Weight, Balance> for WeightToFee {
|
||||
fn convert(x: Weight) -> Balance {
|
||||
// in Polkadot a weight of 10_000 (smallest non-zero weight) to be mapped to 10^7 units of
|
||||
// fees (1/10 CENT), hence:
|
||||
Balance::from(x).saturating_mul(1_000)
|
||||
}
|
||||
}
|
||||
|
||||
/// Update the given multiplier based on the following formula
|
||||
///
|
||||
/// diff = (target_weight - previous_block_weight)
|
||||
/// v = 0.00004
|
||||
/// next_weight = weight * (1 + (v . diff) + (v . diff)^2 / 2)
|
||||
///
|
||||
/// Where `target_weight` must be given as the `Get` implementation of the `T` generic type.
|
||||
/// https://research.web3.foundation/en/latest/polkadot/Token%20Economics/#relay-chain-transaction-fees
|
||||
pub struct TargetedFeeAdjustment<T, R>(rstd::marker::PhantomData<(T, R)>);
|
||||
|
||||
impl<T: Get<Perbill>, R: system::Trait> Convert<Fixed64, Fixed64> for TargetedFeeAdjustment<T, R> {
|
||||
fn convert(multiplier: Fixed64) -> Fixed64 {
|
||||
let block_weight = <system::Module<R>>::all_extrinsics_weight();
|
||||
let max_weight = MaximumBlockWeight::get();
|
||||
let target_weight = (T::get() * max_weight) as u128;
|
||||
let block_weight = block_weight as u128;
|
||||
|
||||
// determines if the first_term is positive
|
||||
let positive = block_weight >= target_weight;
|
||||
let diff_abs = block_weight.max(target_weight) - block_weight.min(target_weight);
|
||||
// diff is within u32, safe.
|
||||
let diff = Fixed64::from_rational(diff_abs as i64, max_weight as u64);
|
||||
let diff_squared = diff.saturating_mul(diff);
|
||||
|
||||
// 0.00004 = 4/100_000 = 40_000/10^9
|
||||
let v = Fixed64::from_rational(4, 100_000);
|
||||
// 0.00004^2 = 16/10^10 ~= 2/10^9. Taking the future /2 into account, then it is just 1
|
||||
// parts from a billionth.
|
||||
let v_squared_2 = Fixed64::from_rational(1, 1_000_000_000);
|
||||
|
||||
let first_term = v.saturating_mul(diff);
|
||||
// It is very unlikely that this will exist (in our poor perbill estimate) but we are giving
|
||||
// it a shot.
|
||||
let second_term = v_squared_2.saturating_mul(diff_squared);
|
||||
|
||||
if positive {
|
||||
// Note: this is merely bounded by how big the multiplier and the inner value can go,
|
||||
// not by any economical reasoning.
|
||||
let excess = first_term.saturating_add(second_term);
|
||||
multiplier.saturating_add(excess)
|
||||
} else {
|
||||
// Proof: first_term > second_term. Safe subtraction.
|
||||
let negative = first_term - second_term;
|
||||
multiplier.saturating_sub(negative)
|
||||
// despite the fact that apply_to saturates weight (final fee cannot go below 0)
|
||||
// it is crucially important to stop here and don't further reduce the weight fee
|
||||
// multiplier. While at -1, it means that the network is so un-congested that all
|
||||
// transactions have no weight fee. We stop here and only increase if the network
|
||||
// became more busy.
|
||||
.max(Fixed64::from_rational(-1, 1))
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user