1
// Copyright (C) Parity Technologies (UK) Ltd.
2
// This file is part of Polkadot.
3

            
4
// Polkadot is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Polkadot is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.
16

            
17
//! Runtime modules for parachains code.
18
//!
19
//! It is crucial to include all the modules from this crate in the runtime, in
20
//! particular the `Initializer` module, as it is responsible for initializing the state
21
//! of the other modules.
22

            
23
#![cfg_attr(feature = "runtime-benchmarks", recursion_limit = "256")]
24
#![cfg_attr(not(feature = "std"), no_std)]
25

            
26
pub mod assigner_coretime;
27
pub mod assigner_on_demand;
28
pub mod assigner_parachains;
29
pub mod configuration;
30
pub mod coretime;
31
pub mod disputes;
32
pub mod dmp;
33
pub mod hrmp;
34
pub mod inclusion;
35
pub mod initializer;
36
pub mod metrics;
37
pub mod origin;
38
pub mod paras;
39
pub mod paras_inherent;
40
pub mod reward_points;
41
pub mod scheduler;
42
pub mod session_info;
43
pub mod shared;
44

            
45
pub mod runtime_api_impl;
46

            
47
mod util;
48

            
49
#[cfg(any(feature = "runtime-benchmarks", test))]
50
mod builder;
51
#[cfg(test)]
52
mod mock;
53
#[cfg(test)]
54
mod ump_tests;
55

            
56
extern crate alloc;
57

            
58
pub use origin::{ensure_parachain, Origin};
59
pub use paras::{ParaLifecycle, UpgradeStrategy};
60
use polkadot_primitives::{HeadData, Id as ParaId, ValidationCode};
61
use sp_runtime::{DispatchResult, FixedU128};
62

            
63
/// Trait for tracking message delivery fees on a transport protocol.
64
pub trait FeeTracker {
65
	/// Type used for assigning different fee factors to different destinations
66
	type Id;
67
	/// Returns the evolving exponential fee factor which will be used to calculate the delivery
68
	/// fees.
69
	fn get_fee_factor(id: Self::Id) -> FixedU128;
70
	/// Increases the delivery fee factor by a factor based on message size and records the result.
71
	///
72
	/// Returns the new delivery fee factor after the increase.
73
	fn increase_fee_factor(id: Self::Id, message_size_factor: FixedU128) -> FixedU128;
74
	/// Decreases the delivery fee factor by a constant factor and records the result.
75
	///
76
	/// Does not reduce the fee factor below the initial value, which is currently set as 1.
77
	///
78
	/// Returns the new delivery fee factor after the decrease.
79
	fn decrease_fee_factor(id: Self::Id) -> FixedU128;
80
}
81

            
82
/// Schedule a para to be initialized at the start of the next session with the given genesis data.
83
9
pub fn schedule_para_initialize<T: paras::Config>(
84
9
	id: ParaId,
85
9
	genesis: paras::ParaGenesisArgs,
86
9
) -> Result<(), ()> {
87
9
	paras::Pallet::<T>::schedule_para_initialize(id, genesis).map_err(|_| ())
88
9
}
89

            
90
/// Schedule a para to be cleaned up at the start of the next session.
91
78
pub fn schedule_para_cleanup<T: paras::Config>(id: polkadot_primitives::Id) -> Result<(), ()> {
92
78
	paras::Pallet::<T>::schedule_para_cleanup(id).map_err(|_| ())
93
78
}
94

            
95
/// Schedule a parathread (on-demand parachain) to be upgraded to a lease holding parachain.
96
pub fn schedule_parathread_upgrade<T: paras::Config>(id: ParaId) -> Result<(), ()> {
97
	paras::Pallet::<T>::schedule_parathread_upgrade(id).map_err(|_| ())
98
}
99

            
100
/// Schedule a lease holding parachain to be downgraded to an on-demand parachain.
101
pub fn schedule_parachain_downgrade<T: paras::Config>(id: ParaId) -> Result<(), ()> {
102
	paras::Pallet::<T>::schedule_parachain_downgrade(id).map_err(|_| ())
103
}
104

            
105
/// Schedules a validation code upgrade to a parachain with the given id.
106
2298
pub fn schedule_code_upgrade<T: paras::Config>(
107
2298
	id: ParaId,
108
2298
	new_code: ValidationCode,
109
2298
	set_go_ahead: UpgradeStrategy,
110
2298
) -> DispatchResult {
111
2298
	paras::Pallet::<T>::schedule_code_upgrade_external(id, new_code, set_go_ahead)
112
2298
}
113

            
114
/// Sets the current parachain head with the given id.
115
78
pub fn set_current_head<T: paras::Config>(id: ParaId, new_head: HeadData) {
116
78
	paras::Pallet::<T>::set_current_head(id, new_head)
117
78
}