Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
35e4cb5
remove v0 parachains modules and switch to v1 primitives
rphmeier Jul 30, 2020
7ec0e79
get tests compiling for runtime-common
rphmeier Jul 30, 2020
25de026
remove registrar module
rphmeier Jul 30, 2020
0798117
Add a dummy module
rphmeier Jul 30, 2020
721bd09
remove runtime-parachains
rphmeier Jul 30, 2020
ea76293
mostly remove old parachains code from polkadot-runtime
rphmeier Jul 30, 2020
bb2ddff
remove slots::Trait implementation
rphmeier Jul 30, 2020
edfe078
remove sp_std prelude import
rphmeier Jul 30, 2020
933903b
Merge branch 'master' into rh-remove-v0-runtime
rphmeier Jul 30, 2020
96e8b8a
add a ZeroSizedTypeDifferentiator to dummy
rphmeier Jul 30, 2020
5bbb31e
finish porting over polkadot runtime
rphmeier Jul 30, 2020
03d1452
ZeroSizedTypeDifferentiator was actually unnecessary
rphmeier Jul 30, 2020
c532a8f
westend
rphmeier Jul 31, 2020
19fca5a
kusama
rphmeier Jul 31, 2020
5c98706
Merge branch 'master' into rh-remove-v0-runtime
rphmeier Jul 31, 2020
60fd854
test-runtime (no dummy modules)
rphmeier Jul 31, 2020
5fd65f6
fix warning
rphmeier Jul 31, 2020
0f69588
fix chain-specs
rphmeier Jul 31, 2020
c2e1630
fix test-service
rphmeier Jul 31, 2020
5ec4bb7
test-client
rphmeier Aug 1, 2020
f7cec7d
remove dead import
rphmeier Aug 2, 2020
96ae647
remove unused needed_extrinsics parameter
rphmeier Aug 2, 2020
3d0db7a
Merge branch 'master' into rh-remove-v0-runtime
rphmeier Aug 12, 2020
ef2bc6a
runtimes compile
rphmeier Aug 12, 2020
c1b1ab4
remove rococo-v0
rphmeier Aug 12, 2020
dbcabb3
remove remaining references to Rococo
rphmeier Aug 12, 2020
32c035d
bump versions
rphmeier Aug 12, 2020
fb51b40
Merge branch 'master' into rh-remove-v0-runtime
rphmeier Aug 13, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
get tests compiling for runtime-common
  • Loading branch information
rphmeier committed Jul 30, 2020
commit 7ec0e798170f99af3c33d6f35a94d901c40cbd63
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions runtime/common/src/crowdfund.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,14 +568,14 @@ mod tests {
};
use frame_support::traits::{Contains, ContainsLengthBound};
use sp_core::H256;
use primitives::v0::{Info as ParaInfo, Id as ParaId, Scheduling, ValidationCode};
use primitives::v1::{Id as ParaId, ValidationCode};
// The testing primitives are very useful for avoiding having to work with signatures
// or public keys. `u64` is used as the `AccountId` and no `Signature`s are requried.
use sp_runtime::{
Perbill, Permill, Percent, testing::Header, DispatchResult,
traits::{BlakeTwo256, IdentityLookup},
};
use crate::registrar::Registrar;
use crate::slots::Registrar;

impl_outer_origin! {
pub enum Origin for Test where system = system {}
Expand Down Expand Up @@ -701,7 +701,7 @@ mod tests {

fn register_para(
id: ParaId,
_info: ParaInfo,
_parachain: bool,
code: ValidationCode,
initial_head_data: HeadData,
) -> DispatchResult {
Expand Down
3 changes: 1 addition & 2 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

pub mod claims;
pub mod slot_range;
pub mod registrar;
//pub mod registrar;
pub mod slots;
pub mod crowdfund;
pub mod purchase;
Expand All @@ -42,7 +42,6 @@ pub use staking::StakerStatus;
pub use sp_runtime::BuildStorage;
pub use timestamp::Call as TimestampCall;
pub use balances::Call as BalancesCall;
pub use attestations::{Call as AttestationsCall, MORE_ATTESTATIONS_IDENTIFIER};

/// Implementations of some helper traits passed into runtime modules as associated types.
pub use impls::{CurrencyToVoteHandler, ToAuthor};
Expand Down
170 changes: 2 additions & 168 deletions runtime/common/src/registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,37 +106,13 @@ pub trait Trait: parachains::Trait {

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;
}
Expand Down Expand Up @@ -240,17 +216,6 @@ decl_module! {
<Self as Registrar<T::AccountId>>::deregister_para(id)
}

/// Reset the number of parathreads that can pay to be scheduled in a single block.
///
/// - `count`: The number of parathreads.
///
/// Must be called from Root origin.
#[weight = 0]
fn set_thread_count(origin, count: u32) {
ensure_root(origin)?;
ThreadCount::put(count);
}

/// Register a parathread for immediate use.
///
/// Must be sent from a Signed origin that is able to have ParathreadDeposit reserved.
Expand Down Expand Up @@ -342,7 +307,8 @@ decl_module! {
/// and the auction deposit are switched.
#[weight = 0]
fn swap(origin, #[compact] other: ParaId) {
let id = parachains::ensure_parachain(<T as Trait>::Origin::from(origin))?;
// TODO [now]: check that the `origin` is a parachain origin.
let id: ParaId = unimplemented!();

if PendingSwap::get(other) == Some(id) {
// actually do the swap.
Expand All @@ -351,12 +317,6 @@ decl_module! {
// Remove intention to swap.
PendingSwap::remove(other);
Self::force_unschedule(|i| i == id || i == other);
Parachains::mutate(|ids| swap_ordered_existence(ids, id, other));
Paras::mutate(id, |i|
Paras::mutate(other, |j|
sp_std::mem::swap(i, j)
)
);

<Debtors<T>>::mutate(id, |i|
<Debtors<T>>::mutate(other, |j|
Expand All @@ -368,132 +328,6 @@ decl_module! {
PendingSwap::insert(id, other);
}
}

/// Block initializer. Clears SelectedThreads and constructs/replaces Active.
fn on_initialize() -> Weight {
let next_up = SelectedThreads::mutate(|t| {
let r = if t.len() >= T::QueueSize::get() {
// Take the first set of parathreads in queue
t.remove(0)
} else {
vec![]
};
while t.len() < T::QueueSize::get() {
t.push(vec![]);
}
r
});
// mutable so that we can replace with `None` if parathread appears in new schedule.
let mut retrying = Self::take_next_retry();
if let Some(((para, _), _)) = retrying {
// this isn't really ideal: better would be if there were an earlier pass that set
// retrying to the first item in the Missed queue that isn't already scheduled, but
// this is potentially O(m*n) in terms of missed queue size and parathread pool size.
if next_up.iter().any(|x| x.0 == para) {
retrying = None
}
}

let mut paras = Parachains::get().into_iter()
.map(|id| (id, None))
.chain(next_up.into_iter()
.map(|(para, collator)|
(para, Some((collator, Retriable::WithRetries(0))))
)
).chain(retrying.into_iter()
.map(|((para, collator), retries)|
(para, Some((collator, Retriable::WithRetries(retries + 1))))
)
).collect::<Vec<_>>();
// for Rust's timsort algorithm, sorting a concatenation of two sorted ranges is near
// O(N).
paras.sort_by_key(|&(ref id, _)| *id);

Active::put(paras);

0
}

fn on_finalize() {
// a block without this will panic, but let's not panic here.
if let Some(proceeded_vec) = parachains::DidUpdate::get() {
// Active is sorted and DidUpdate is a sorted subset of its elements.
//
// We just go through the contents of active and find any items that don't appear in
// DidUpdate *and* which are enabled for retry.
let mut proceeded = proceeded_vec.into_iter();
let mut i = proceeded.next();
for sched in Active::get().into_iter() {
match i {
// Scheduled parachain proceeded properly. Move onto next item.
Some(para) if para == sched.0 => i = proceeded.next(),
// Scheduled `sched` missed their block.
// Queue for retry if it's allowed.
_ => if let (i, Some((c, Retriable::WithRetries(n)))) = sched {
Self::retry_later((i, c), n)
},
}
}
}
}
}
}

decl_event!{
pub enum Event {
/// A parathread was registered; its new ID is supplied.
ParathreadRegistered(ParaId),

/// The parathread of the supplied ID was de-registered.
ParathreadDeregistered(ParaId),
}
}

impl<T: Trait> Module<T> {
/// Ensures that the given `ParaId` corresponds to a registered parathread, and returns a descriptor if so.
pub fn ensure_thread_id(id: ParaId) -> Option<ParaInfo> {
Paras::get(id).and_then(|info| if let Scheduling::Dynamic = info.scheduling {
Some(info)
} else {
None
})
}

fn retry_later(sched: (ParaId, CollatorId), retries: u32) {
if retries < T::MaxRetries::get() {
RetryQueue::mutate(|q| {
q.resize(T::MaxRetries::get() as usize, vec![]);
q[retries as usize].push(sched);
});
}
}

fn take_next_retry() -> Option<((ParaId, CollatorId), u32)> {
RetryQueue::mutate(|q| {
for (i, q) in q.iter_mut().enumerate() {
if !q.is_empty() {
return Some((q.remove(0), i as u32));
}
}
None
})
}

/// Forcibly remove the threads matching `m` from all current and future scheduling.
fn force_unschedule(m: impl Fn(ParaId) -> bool) {
RetryQueue::mutate(|qs| for q in qs.iter_mut() {
q.retain(|i| !m(i.0))
});
SelectedThreads::mutate(|qs| for q in qs.iter_mut() {
q.retain(|i| !m(i.0))
});
Active::mutate(|a| for i in a.iter_mut() {
if m(i.0) {
if let Some((_, ref mut r)) = i.1 {
*r = Retriable::Never;
}
}
});
}
}

Expand Down
70 changes: 64 additions & 6 deletions runtime/common/src/slots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ use frame_support::{
weights::{DispatchClass, Weight},
};
use primitives::v1::{
SwapAux, PARACHAIN_INFO, Id as ParaId, ValidationCode, HeadData,
Id as ParaId, ValidationCode, HeadData,
};
use system::{ensure_signed, ensure_root};
use crate::registrar::{Registrar, swap_ordered_existence};
use crate::slot_range::{SlotRange, SLOT_RANGE_COUNT};

type BalanceOf<T> = <<T as Trait>::Currency as Currency<<T as system::Trait>::AccountId>>::Balance;
Expand All @@ -58,6 +57,50 @@ pub trait Trait: system::Trait {
type Randomness: Randomness<Self::Hash>;
}

/// 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,
_parachain: bool,
code: ValidationCode,
initial_head_data: HeadData,
) -> DispatchResult;

/// Deregister a parachain with given `id`. If `id` is not currently registered, an error is returned.
fn deregister_para(id: ParaId) -> DispatchResult;
}

/// Auxilliary for when there's an attempt to swap two parachains/parathreads.
pub trait SwapAux {
/// Result describing whether it is possible to swap two parachains. Doesn't mutate state.
fn ensure_can_swap(one: ParaId, other: ParaId) -> Result<(), &'static str>;

/// Updates any needed state/references to enact a logical swap of two parachains. Identity,
/// code and `head_data` remain equivalent for all parachains/threads, however other properties
/// such as leases, deposits held and thread/chain nature are swapped.
///
/// May only be called on a state that `ensure_can_swap` has previously returned `Ok` for: if this is
/// not the case, the result is undefined. May only return an error if `ensure_can_swap` also returns
/// an error.
fn on_swap(one: ParaId, other: ParaId) -> Result<(), &'static str>;
}

/// A sub-bidder identifier. Used to distinguish between different logical bidders coming from the
/// same account ID.
pub type SubId = u32;
Expand Down Expand Up @@ -187,6 +230,21 @@ decl_storage! {
}
}

/// Swap the existence of two items, provided by value, within an ordered list.
///
/// If neither item exists, or if both items exist this will do nothing. If exactly one of the
/// items exists, then it will be removed and the other inserted.
fn swap_ordered_existence<T: PartialOrd + Ord + Copy>(ids: &mut [T], one: T, other: T) {
let maybe_one_pos = ids.binary_search(&one);
let maybe_other_pos = ids.binary_search(&other);
match (maybe_one_pos, maybe_other_pos) {
(Ok(one_pos), Err(_)) => ids[one_pos] = other,
(Err(_), Ok(other_pos)) => ids[other_pos] = one,
_ => return,
};
ids.sort();
}

impl<T: Trait> SwapAux for Module<T> {
fn ensure_can_swap(one: ParaId, other: ParaId) -> Result<(), &'static str> {
if <Onboarding<T>>::contains_key(one) || <Onboarding<T>>::contains_key(other) {
Expand Down Expand Up @@ -470,7 +528,7 @@ decl_module! {
// parachain for its immediate start.
<Onboarding<T>>::remove(&para_id);
let _ = T::Parachains::
register_para(para_id, PARACHAIN_INFO, code, initial_head_data);
register_para(para_id, true, code, initial_head_data);
}

Ok(())
Expand Down Expand Up @@ -719,7 +777,7 @@ impl<T: Trait> Module<T> {
// The chain's deployment data is set; go ahead and register it, and remove the
// now-redundant on-boarding entry.
let _ = T::Parachains::
register_para(para_id.clone(), PARACHAIN_INFO, code, initial_head_data);
register_para(para_id.clone(), true, code, initial_head_data);
// ^^ not much we can do if it fails for some reason.
<Onboarding<T>>::remove(para_id)
}
Expand Down Expand Up @@ -890,7 +948,7 @@ mod tests {
traits::{OnInitialize, OnFinalize}
};
use balances;
use primitives::v1::{BlockNumber, Header, Id as ParaId, Info as ParaInfo, Scheduling};
use primitives::v1::{BlockNumber, Header, Id as ParaId};

impl_outer_origin! {
pub enum Origin for Test where system = system {}
Expand Down Expand Up @@ -976,7 +1034,7 @@ mod tests {

fn register_para(
id: ParaId,
_info: ParaInfo,
parachain: bool,
code: ValidationCode,
initial_head_data: HeadData,
) -> DispatchResult {
Expand Down