-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathlib.rs
221 lines (185 loc) · 7.94 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// KILT Blockchain – https://botlabs.org
// Copyright (C) 2019-2024 BOTLabs GmbH
// The KILT Blockchain 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.
// The KILT Blockchain 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 this program. If not, see <https://www.gnu.org/licenses/>.
// If you feel like getting in touch with us, you can do so at info@botlabs.org
#![cfg_attr(not(feature = "std"), no_std)]
use constants::{AVERAGE_ON_INITIALIZE_RATIO, MAXIMUM_BLOCK_WEIGHT, NORMAL_DISPATCH_RATIO};
use fees::SplitFeesByRatio;
pub use sp_consensus_aura::sr25519::AuthorityId;
pub use opaque::*;
pub use frame_support::weights::constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight};
use frame_support::{
dispatch::DispatchClass,
parameter_types,
traits::{
fungible::{Balanced, Credit},
Contains, ContainsLengthBound, Currency, Get, OnUnbalanced, SortedMembers,
},
};
use frame_system::limits;
use pallet_balances::Pallet as PalletBalance;
use pallet_transaction_payment::{Multiplier, TargetedFeeAdjustment};
use sp_runtime::{
generic,
traits::{BlakeTwo256, Bounded, IdentifyAccount, Verify},
FixedPointNumber, MultiSignature, Perquintill, SaturatedConversion,
};
use sp_std::marker::PhantomData;
pub mod assets;
pub mod authorization;
pub mod constants;
pub mod dip;
pub mod errors;
pub mod fees;
pub mod migrations;
pub mod pallet_id;
pub mod xcm_config;
#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarks;
/// Opaque types. These are used by the CLI to instantiate machinery that don't
/// need to know the specifics of the runtime. They can then be made to be
/// agnostic over specific formats of data like extrinsics, allowing for them to
/// continue syncing the network through upgrades to even the core data
/// structures.
pub mod opaque {
use super::*;
use sp_runtime::{generic, traits::BlakeTwo256};
pub use sp_runtime::OpaqueExtrinsic as UncheckedExtrinsic;
/// Opaque block header type.
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
/// Opaque block type.
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
/// Opaque block identifier type.
pub type BlockId = generic::BlockId<Block>;
}
/// An index to a block.
pub type BlockNumber = u64;
pub(crate) type CreditOf<T> = Credit<<T as frame_system::Config>::AccountId, PalletBalance<T, ()>>;
/// Alias to 512-bit hash when used in the context of a transaction signature on
/// the chain.
pub type Signature = MultiSignature;
/// Alias to the public key used for this chain, actually a `MultiSigner`. Like
/// the signature, this also isn't a fixed size when encoded, as different
/// cryptos have different size public keys.
pub type AccountPublic = <Signature as Verify>::Signer;
/// Alias to the opaque account ID type for this chain, actually a
/// `AccountId32`. This is always 32 bytes.
pub type AccountId = <AccountPublic as IdentifyAccount>::AccountId;
/// The type for looking up accounts. We don't expect more than 4 billion of
/// them, but you never know...
pub type AccountIndex = u32;
/// Identifier for a chain. 32-bit should be plenty.
pub type ChainId = u32;
/// Balance of an account.
pub type Balance = u128;
pub type Amount = i128;
/// Nonce of a transaction in the chain.
pub type Nonce = u64;
/// Hasher for chain data.
pub type Hasher = BlakeTwo256;
/// A hash of some data used by the chain.
pub type Hash = <BlakeTwo256 as sp_core::Hasher>::Out;
/// Digest item type.
pub type DigestItem = generic::DigestItem;
/// A Kilt DID subject identifier.
pub type DidIdentifier = AccountId;
pub type NegativeImbalanceOf<T> =
<pallet_balances::Pallet<T> as Currency<<T as frame_system::Config>::AccountId>>::NegativeImbalance;
// Common constants used in all runtimes.
parameter_types! {
pub const BlockHashCount: BlockNumber = 2400;
/// 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(3, 100_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, 1_000_000u128);
/// 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);
/// Block weights base values and limits.
pub BlockWeights: limits::BlockWeights = limits::BlockWeights::builder()
.base_block(BlockExecutionWeight::get())
.for_class(DispatchClass::all(), |weights| {
weights.base_extrinsic = 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 some 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();
/// Fee split ratio between treasury and block author (order is important).
pub const FeeSplitRatio: (u32, u32) = (50, 50);
}
/// Split the fees using a preconfigured Ratio
/// (`runtime_common::FeeSplitRatio`).
pub type FeeSplit<R, B1, B2> = SplitFeesByRatio<R, FeeSplitRatio, B1, B2>;
/// Parameterized slow adjusting fee updated based on
/// <https://w3f-research.readthedocs.io/en/latest/polkadot/Token%20Economics.html#-2.-slow-adjusting-mechanism>
pub type SlowAdjustingFeeUpdate<R> =
TargetedFeeAdjustment<R, TargetBlockFullness, AdjustmentVariable, MinimumMultiplier, MaximumMultiplier>;
pub struct Tippers<R, I>(PhantomData<R>, PhantomData<I>);
impl<R, I: 'static> ContainsLengthBound for Tippers<R, I>
where
R: pallet_membership::Config<I>,
{
fn max_len() -> usize {
<R as pallet_membership::Config<I>>::MaxMembers::get().saturated_into()
}
fn min_len() -> usize {
0
}
}
impl<R, I: 'static> SortedMembers<R::AccountId> for Tippers<R, I>
where
R: pallet_membership::Config<I>,
pallet_membership::Pallet<R, I>: SortedMembers<R::AccountId> + Contains<R::AccountId>,
{
fn sorted_members() -> sp_std::vec::Vec<R::AccountId> {
pallet_membership::Pallet::<R, I>::sorted_members()
}
#[cfg(feature = "runtime-benchmarks")]
fn add(who: &R::AccountId) {
pallet_membership::Members::<R, I>::mutate(|members| match members.binary_search_by(|m| m.cmp(who)) {
Ok(_) => (),
Err(pos) => members
.try_insert(pos, who.clone())
.expect("Should not fail to add members"),
})
}
}
pub struct SendDustAndFeesToTreasury<T>(sp_std::marker::PhantomData<T>);
impl<T> OnUnbalanced<CreditOf<T>> for SendDustAndFeesToTreasury<T>
where
T: pallet_balances::Config,
T: pallet_treasury::Config,
{
fn on_nonzero_unbalanced(amount: CreditOf<T>) {
let treasury_account_id = pallet_treasury::Pallet::<T>::account_id();
let result = pallet_balances::Pallet::<T>::resolve(&treasury_account_id, amount);
debug_assert!(result.is_ok(), "The whole credit cannot be countered");
}
}