Newer
Older
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
// Copyright 2017 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/>.
//! Module to handle which parachains/parathreads (collectively referred to as "paras") are
//! registered and which are scheduled. Doesn't manage any of the actual execution/validation logic
//! which is left to `parachains.rs`.
use rstd::{prelude::*, result};
#[cfg(any(feature = "std", test))]
use rstd::marker::PhantomData;
use codec::{Encode, Decode};
use sr_primitives::{
weights::{SimpleDispatchInfo, DispatchInfo},
transaction_validity::{TransactionValidityError, ValidTransaction, TransactionValidity},
traits::{Hash as HashT, SignedExtension}
};
use srml_support::{
decl_storage, decl_module, decl_event, ensure,
dispatch::{Result, IsSubType}, traits::{Get, Currency, ReservableCurrency}
};
use system::{self, ensure_root, ensure_signed};
use primitives::parachain::{
Id as ParaId, CollatorId, Scheduling, LOWEST_USER_ID, SwapAux, Info as ParaInfo, ActiveParas,
Retriable
};
use crate::parachains;
use sr_primitives::transaction_validity::InvalidTransaction;
/// Parachain registration API.
pub trait Registrar<AccountId> {
/// Create a new unique parachain identity for later registration.
fn new_id() -> ParaId;
/// Register a parachain with given `code` and `initial_head_data`. `id` must not yet be registered or it will
/// result in a error.
fn register_para(
id: ParaId,
info: ParaInfo,
code: Vec<u8>,
initial_head_data: Vec<u8>,
) -> Result;
/// Deregister a parachain with given `id`. If `id` is not currently registered, an error is returned.
fn deregister_para(id: ParaId) -> Result;
}
impl<T: Trait> Registrar<T::AccountId> for Module<T> {
fn new_id() -> ParaId {
<NextFreeId>::mutate(|n| { let r = *n; *n = ParaId::from(u32::from(*n) + 1); r })
}
fn register_para(
id: ParaId,
info: ParaInfo,
code: Vec<u8>,
initial_head_data: Vec<u8>,
) -> Result {
ensure!(!Paras::exists(id), "Parachain already exists");
if let Scheduling::Always = info.scheduling {
Parachains::mutate(|parachains|
match parachains.binary_search(&id) {
Ok(_) => Err("Parachain already exists"),
Err(idx) => {
parachains.insert(idx, id);
Ok(())
}
}
)?;
}
<parachains::Module<T>>::initialize_para(id, code, initial_head_data);
Paras::insert(id, info);
Ok(())
}
fn deregister_para(id: ParaId) -> Result {
let info = Paras::take(id).ok_or("Invalid id")?;
if let Scheduling::Always = info.scheduling {
Parachains::mutate(|parachains|
parachains.binary_search(&id)
.map(|index| parachains.remove(index))
.map_err(|_| "Invalid id")
)?;
}
<parachains::Module<T>>::cleanup_para(id);
Paras::remove(id);
Ok(())
}
}
type BalanceOf<T> =
<<T as Trait>::Currency as Currency<<T as system::Trait>::AccountId>>::Balance;
pub trait Trait: parachains::Trait {
/// The overarching event type.
type Event: From<Event> + Into<<Self as system::Trait>::Event>;
/// The aggregated origin type must support the parachains origin. We require that we can
/// infallibly convert between this origin and the system origin, but in reality, they're the
/// same type, we just can't express that to the Rust type system without writing a `where`
/// clause everywhere.
type Origin: From<<Self as system::Trait>::Origin>
+ Into<result::Result<parachains::Origin, <Self as Trait>::Origin>>;
/// The system's currency for parathread payment.
type Currency: ReservableCurrency<Self::AccountId>;
/// The deposit to be paid to run a parathread.
type ParathreadDeposit: Get<BalanceOf<Self>>;
/// Handler for when two ParaIds are swapped.
type SwapAux: SwapAux;
/// The number of items in the parathread queue, aka the number of blocks in advance to schedule
/// parachain execution.
type QueueSize: Get<usize>;
/// The number of rotations that you will have as grace if you miss a block.
type MaxRetries: Get<u32>;
}
decl_storage! {
trait Store for Module<T: Trait> as Registrar {
// Vector of all parachain IDs, in ascending order.
Parachains: Vec<ParaId>;
/// The number of threads to schedule per block.
ThreadCount: u32;
/// An array of the queue of set of threads scheduled for the coming blocks; ordered by
/// ascending para ID. There can be no duplicates of para ID in each list item.
SelectedThreads: Vec<Vec<(ParaId, CollatorId)>>;
/// Parathreads/chains scheduled for execution this block. If the collator ID is set, then
/// a particular collator has already been chosen for the next block, and no other collator
/// may provide the block. In this case we allow the possibility of the combination being
/// retried in a later block, expressed by `Retriable`.
///
/// Ordered by ParaId.
Active: Vec<(ParaId, Option<(CollatorId, Retriable)>)>;
/// The next unused ParaId value. Start this high in order to keep low numbers for
/// system-level chains.
NextFreeId: ParaId = LOWEST_USER_ID;
/// Pending swap operations.
PendingSwap: map ParaId => Option<ParaId>;
/// Map of all registered parathreads/chains.
Paras get(paras): map ParaId => Option<ParaInfo>;
/// The current queue for parathreads that should be retried.
RetryQueue get(retry_queue): Vec<Vec<(ParaId, CollatorId)>>;
/// Users who have paid a parathread's deposit
Debtors: map ParaId => T::AccountId;
}
add_extra_genesis {
config(parachains): Vec<(ParaId, Vec<u8>, Vec<u8>)>;
config(_phdata): PhantomData<T>;
build(build::<T>);
}
}
#[cfg(feature = "std")]
fn build<T: Trait>(config: &GenesisConfig<T>) {
use sr_primitives::traits::Zero;
let mut p = config.parachains.clone();
p.sort_unstable_by_key(|&(ref id, _, _)| *id);
p.dedup_by_key(|&mut (ref id, _, _)| *id);
let only_ids: Vec<ParaId> = p.iter().map(|&(ref id, _, _)| id).cloned().collect();
Parachains::put(&only_ids);
for (id, code, genesis) in p {
Paras::insert(id, &primitives::parachain::PARACHAIN_INFO);
// no ingress -- a chain cannot be routed to until it is live.
<parachains::Code>::insert(&id, &code);
<parachains::Heads>::insert(&id, &genesis);
<parachains::Watermarks<T>>::insert(&id, T::BlockNumber::zero());
// Save initial parachains in registrar
Paras::insert(id, ParaInfo { scheduling: Scheduling::Always })
}
}
Loading full blame...