impls.rs 4.82 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 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;
20
use sr_primitives::weights::Weight;
21
use sr_primitives::traits::{Convert, Saturating};
22
use sr_primitives::{Fixed64, Perbill};
23
use paint_support::traits::{OnUnbalanced, Currency, Get};
24
use crate::{Balances, System, Authorship, MaximumBlockWeight, NegativeImbalance};
25
26
27
28
29

/// Logic for the author to get a portion of fees.
pub struct ToAuthor;

impl OnUnbalanced<NegativeImbalance> for ToAuthor {
Gavin Wood's avatar
Gavin Wood committed
30
	fn on_nonzero_unbalanced(amount: NegativeImbalance) {
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
		Balances::resolve_creating(&Authorship::author(), amount);
	}
}

/// Converter for currencies to votes.
pub struct CurrencyToVoteHandler;

impl CurrencyToVoteHandler {
	fn factor() -> u128 { (Balances::total_issuance() / u64::max_value() as u128).max(1) }
}

impl Convert<u128, u64> for CurrencyToVoteHandler {
	fn convert(x: u128) -> u64 { (x / Self::factor()) as u64 }
}

impl Convert<u128, u128> for CurrencyToVoteHandler {
	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)
	}
}

69
/// Update the given multiplier based on the following formula
70
///
71
///   diff = (target_weight - previous_block_weight)
72
73
74
///   v = 0.00004
///   next_weight = weight * (1 + (v . diff) + (v . diff)^2 / 2)
///
75
/// Where `target_weight` must be given as the `Get` implementation of the `T` generic type.
76
/// https://research.web3.foundation/en/latest/polkadot/Token%20Economics/#relay-chain-transaction-fees
77
pub struct TargetedFeeAdjustment<T>(rstd::marker::PhantomData<T>);
78

79
80
81
impl<T: Get<Perbill>> Convert<Fixed64, Fixed64> for TargetedFeeAdjustment<T> {
	fn convert(multiplier: Fixed64) -> Fixed64 {
		let block_weight = System::all_extrinsics_weight();
82
		let max_weight = MaximumBlockWeight::get();
83
		let target_weight = (T::get() * max_weight) as u128;
84
85
86
87
88
89
90
91
92
93
94
		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);
95
96
		// 0.00004^2 = 16/10^10 ~= 2/10^9. Taking the future /2 into account, then it is just 1
		// parts from a billionth.
97
98
99
100
101
102
103
104
105
106
107
		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);
108
			multiplier.saturating_add(excess)
109
		} else {
110
			// Proof: first_term > second_term. Safe subtraction.
111
			let negative = first_term - second_term;
112
			multiplier.saturating_sub(negative)
113
114
115
116
117
				// 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.
118
				.max(Fixed64::from_rational(-1, 1))
119
120
121
		}
	}
}