Newer
Older
// Copyright 2017-2020 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 sp_std::{prelude::*, result};
transaction_validity::{TransactionValidityError, ValidTransaction, TransactionValidity},
traits::{Hash as HashT, SignedExtension}
};
decl_storage, decl_module, decl_event, decl_error, ensure,
dispatch::{DispatchResult, IsSubType}, traits::{Get, Currency, ReservableCurrency},
weights::{SimpleDispatchInfo, DispatchInfo},
};
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 sp_runtime::transaction_validity::InvalidTransaction;
/// Parachain registration API.
pub trait Registrar<AccountId> {
/// Create a new unique parachain identity for later registration.
fn new_id() -> ParaId;
/// Checks whether the given initial head data size falls within the limit.
fn head_data_size_allowed(head_data_size: u32) -> bool;
/// Checks whether the given validation code falls within the limit.
fn code_size_allowed(code_size: u32) -> bool;
/// Register a parachain with given `code` and `initial_head_data`. `id` must not yet be registered or it will
/// result in a error.
///
/// This does not enforce any code size or initial head data limits, as these
/// are governable and parameters for parachain initialization are often
/// determined long ahead-of-time. Not checking these values ensures that changes to limits
/// do not invalidate in-progress auction winners.
fn register_para(
id: ParaId,
info: ParaInfo,
code: Vec<u8>,
initial_head_data: Vec<u8>,
/// Deregister a parachain with given `id`. If `id` is not currently registered, an error is returned.
fn deregister_para(id: ParaId) -> DispatchResult;
}
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 head_data_size_allowed(head_data_size: u32) -> bool {
head_data_size <= <T as parachains::Trait>::MaxHeadDataSize::get()
}
fn code_size_allowed(code_size: u32) -> bool {
code_size <= <T as parachains::Trait>::MaxCodeSize::get()
}
fn register_para(
id: ParaId,
info: ParaInfo,
code: Vec<u8>,
initial_head_data: Vec<u8>,
ensure!(!Paras::contains_key(id), Error::<T>::ParaAlreadyExists);
if let Scheduling::Always = info.scheduling {
Parachains::mutate(|parachains|
match parachains.binary_search(&id) {
Ok(_) => Err(Error::<T>::ParaAlreadyExists),
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) -> DispatchResult {
let info = Paras::take(id).ok_or(Error::<T>::InvalidChainId)?;
if let Scheduling::Always = info.scheduling {
Parachains::mutate(|parachains|
parachains.binary_search(&id)
.map(|index| parachains.remove(index))
.map_err(|_| Error::<T>::InvalidChainId)
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
)?;
}
<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 hasher(twox_64_concat) ParaId => Option<ParaId>;
/// Map of all registered parathreads/chains.
Paras get(fn paras): map hasher(twox_64_concat) ParaId => Option<ParaInfo>;
/// The current queue for parathreads that should be retried.
RetryQueue get(fn retry_queue): Vec<Vec<(ParaId, CollatorId)>>;
/// Users who have paid a parathread's deposit
Debtors: map hasher(twox_64_concat) 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>) {
let mut p = config.parachains.clone();
Loading full blame...