Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Starlight: Wire ContainerRegistrar and Registrar pallets #653

Merged
merged 59 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
6b2f98d
first steps
Agusrodri Aug 7, 2024
a07c3ef
refactor and test
Agusrodri Aug 14, 2024
ecfaaed
Merge remote-tracking branch 'origin/master' into agustin-wire-regist…
Agusrodri Aug 14, 2024
1345124
add tuple conversion for ParaRegistrationParams in tests
Agusrodri Aug 14, 2024
14e0250
add changes for mark_valid_for_collating and deregister
Agusrodri Aug 15, 2024
35d34b2
modify RegistrarHandler trait and add more tests
Agusrodri Aug 15, 2024
4eb4a71
add TS test
Agusrodri Aug 20, 2024
80eedbc
Merge remote-tracking branch 'origin/master' into agustin-wire-regist…
Agusrodri Aug 20, 2024
86afe09
add more TS tests
Agusrodri Aug 21, 2024
9e93f72
fix syntax
Agusrodri Aug 21, 2024
138f6db
remove clone
Agusrodri Aug 21, 2024
b645892
modify MaxEncodedGenesisDataSize
Agusrodri Aug 22, 2024
9975471
add BufferedParasToDeregister
Agusrodri Aug 22, 2024
dca620e
fix deregistration rust test
Agusrodri Aug 22, 2024
749b44f
TS tests now working
Agusrodri Aug 22, 2024
b9c069a
fmt
Agusrodri Aug 22, 2024
f0d0dec
TS lint
Agusrodri Aug 22, 2024
8541763
Merge remote-tracking branch 'origin/master' into agustin-wire-regist…
Agusrodri Aug 22, 2024
7a92da1
use run_block() instead
Agusrodri Aug 23, 2024
88c54c9
at the end run_block is not needed in test
Agusrodri Aug 23, 2024
2f0a142
PR suggestions in pallet_registrar
Agusrodri Aug 23, 2024
ea7e018
add comment
Agusrodri Aug 23, 2024
427d048
account for proper weight inside on_initialize()
Agusrodri Aug 23, 2024
c0b4efe
add test for deregistering two paras
Agusrodri Aug 24, 2024
146ec2d
fmt
Agusrodri Aug 24, 2024
4edc2ff
make all trait functions to behave as hooks
Agusrodri Aug 27, 2024
4f0a4a5
set MaxGenesisDataSize to 5MB again
Agusrodri Aug 27, 2024
2299e61
add head_data param to register functions
Agusrodri Aug 28, 2024
de0933f
fix tests
Agusrodri Aug 28, 2024
8060b00
Merge remote-tracking branch 'origin/master' into agustin-wire-regist…
Agusrodri Aug 28, 2024
be84061
fmt
Agusrodri Aug 28, 2024
7ebedb7
fix build
Agusrodri Aug 28, 2024
5cc9f78
fmt
Agusrodri Aug 28, 2024
8ad6dc3
make trait functions fallible again
Agusrodri Aug 28, 2024
59cef34
fix rust tests
Agusrodri Aug 29, 2024
cc6e792
fix dancebox and flashbox TS tests
Agusrodri Aug 29, 2024
150dd7b
fix dev_tanssi_relay TS tests
Agusrodri Aug 29, 2024
3775849
fix zombie tests
Agusrodri Aug 29, 2024
3823f2d
clippy
Agusrodri Aug 30, 2024
d952d1e
fmt
Agusrodri Aug 30, 2024
67b8a88
generate new api-augment interfaces
Agusrodri Aug 30, 2024
48a4a7e
Merge remote-tracking branch 'origin/master' into agustin-wire-regist…
Agusrodri Aug 30, 2024
b762692
api-augment again
Agusrodri Aug 30, 2024
9a76e31
add more docs
Agusrodri Aug 30, 2024
ecb457d
fix core scheduling test
Agusrodri Aug 30, 2024
cf97156
add test for invalid wasm
Agusrodri Aug 30, 2024
1da1601
add registrar errors to ContainerRegistrar
Agusrodri Aug 30, 2024
368163e
fmt
Agusrodri Aug 30, 2024
293d3bf
Merge remote-tracking branch 'origin/master' into agustin-wire-regist…
Agusrodri Aug 30, 2024
694f2f1
cargo fmt
Agusrodri Aug 30, 2024
31b4455
api-augment
Agusrodri Aug 30, 2024
29bf2d8
add a way to test InnerRegistrar calls in pallet registrar
Agusrodri Sep 2, 2024
e51b495
Merge remote-tracking branch 'origin/master' into agustin-wire-regist…
Agusrodri Sep 2, 2024
1d7a9ca
adapt previous tests
Agusrodri Sep 3, 2024
433dfea
comment
Agusrodri Sep 3, 2024
5992697
Merge remote-tracking branch 'origin/master' into agustin-wire-regist…
Agusrodri Sep 3, 2024
73daab1
pr comments
Agusrodri Sep 3, 2024
b5517b4
Avoid clone of genesis storage in dancebox
tmpolaczyk Sep 4, 2024
e83f4a4
fix mock
Agusrodri Sep 4, 2024
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
50 changes: 49 additions & 1 deletion pallets/registrar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,27 @@ pub mod pallet {
ValueQuery,
>;

/// This storage aims to act as a 'buffer' for paraIds that must be deregistered at the
/// end of the block execution by calling 'T::InnerRegistrar::deregister()' implementation.
///
/// We need this buffer because when we are using this pallet on a relay-chain environment
/// like Starlight (where 'T::InnerRegistrar' implementation is usually the
/// 'paras_registrar' pallet) we need to deregister (via 'paras_registrar::deregister')
/// the same paraIds we have in 'PendingToRemove<T>', and we need to do this deregistration
/// process inside 'on_finalize' hook.
///
/// It can be the case that some paraIds need to be downgraded to a parathread before
/// deregistering on 'paras_registrar'. This process usually takes 2 sessions,
/// and the actual downgrade happens when the block finalizes.
///
/// Therefore, if we tried to perform this relay deregistration process at the beginning
/// of the session/block inside ('on_initialize') initializer_on_new_session() as we do
/// for this pallet, it would fail due to the downgrade process could have not taken
/// place yet.
#[pallet::storage]
pub type BufferedParasToDeregister<T: Config> =
StorageValue<_, BoundedVec<ParaId, T::MaxLengthParaIds>, ValueQuery>;

pub type DepositBalanceOf<T> =
<<T as Config>::Currency as Inspect<<T as frame_system::Config>::AccountId>>::Balance;

Expand Down Expand Up @@ -315,6 +336,11 @@ pub mod pallet {

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(_n: BlockNumberFor<T>) -> Weight {
// Account for on_finalize weight
Weight::zero().saturating_add(T::DbWeight::get().reads(1))
Agusrodri marked this conversation as resolved.
Show resolved Hide resolved
}

#[cfg(feature = "try-runtime")]
fn try_state(_n: BlockNumberFor<T>) -> Result<(), sp_runtime::TryRuntimeError> {
use {scale_info::prelude::format, sp_std::collections::btree_set::BTreeSet};
Expand Down Expand Up @@ -416,6 +442,12 @@ pub mod pallet {

Ok(())
}

fn on_finalize(_: BlockNumberFor<T>) {
if let Some(para_id) = BufferedParasToDeregister::<T>::take().pop() {
Agusrodri marked this conversation as resolved.
Show resolved Hide resolved
T::InnerRegistrar::deregister(para_id);
}
}
}

#[pallet::call]
Expand Down Expand Up @@ -825,6 +857,14 @@ pub mod pallet {
Self::deposit_event(Event::ParaIdDeregistered { para_id });
// Cleanup immediately
Self::cleanup_deregistered_para_id(para_id);
if let Err(id) = BufferedParasToDeregister::<T>::try_mutate(|v| v.try_push(para_id))
{
log::error!(
target: LOG_TARGET,
girazoki marked this conversation as resolved.
Show resolved Hide resolved
"Failed to add paraId {:?} to deregistration list",
id
);
}
} else {
Self::schedule_paused_parachain_change(|para_ids, paused| {
// We have to find out where, in the sorted vec the para id is, if anywhere.
Expand Down Expand Up @@ -1168,6 +1208,15 @@ pub mod pallet {
for para_id in new_paras {
Self::cleanup_deregistered_para_id(*para_id);
removed_para_ids.insert(*para_id);
if let Err(id) =
BufferedParasToDeregister::<T>::try_mutate(|v| v.try_push(*para_id))
{
log::error!(
target: LOG_TARGET,
"Failed to add paraId {:?} to deregistration list",
Agusrodri marked this conversation as resolved.
Show resolved Hide resolved
id
);
}
}
}

Expand Down Expand Up @@ -1210,7 +1259,6 @@ pub mod pallet {
ParaManager::<T>::remove(para_id);

T::RegistrarHooks::para_deregistered(para_id);
T::InnerRegistrar::deregister(para_id);
}

fn schedule_parachain_cleanup(para_id: ParaId) -> DispatchResult {
Expand Down
2 changes: 1 addition & 1 deletion primitives/traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ pub trait RegistrarHandler<AccountId> {
id: ParaId,
genesis_storage: Vec<ContainerChainGenesisDataItem>,
) -> DispatchResult;

fn schedule_para_upgrade(id: ParaId) -> DispatchResult;
fn schedule_para_downgrade(id: ParaId) -> DispatchResult;
fn deregister(id: ParaId) -> Weight;
Expand Down
12 changes: 9 additions & 3 deletions solo-chains/runtime/starlight/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1490,7 +1490,7 @@ parameter_types! {
pub const DepositAmount: Balance = 100 * UNITS;
#[derive(Clone)]
pub const MaxLengthParaIds: u32 = 100u32;
pub const MaxEncodedGenesisDataSize: u32 = 5_000_000u32; // 5MB
pub const MaxEncodedGenesisDataSize: u32 = 1_000_000u32; // 1MB
Agusrodri marked this conversation as resolved.
Show resolved Hide resolved
}

pub struct InnerStarlightRegistrar<AccountId, RegistrarManager>(
Expand Down Expand Up @@ -1526,11 +1526,17 @@ where
}

fn schedule_para_upgrade(id: ParaId) -> DispatchResult {
RegistrarManager::make_parachain(id)
if !RegistrarManager::is_parachain(id) {
return RegistrarManager::make_parachain(id);
}
Ok(())
}

fn schedule_para_downgrade(id: ParaId) -> DispatchResult {
RegistrarManager::make_parathread(id)
if !RegistrarManager::is_parathread(id) {
return RegistrarManager::make_parathread(id);
}
Ok(())
Agusrodri marked this conversation as resolved.
Show resolved Hide resolved
}

fn deregister(id: ParaId) -> Weight {
Expand Down
3 changes: 2 additions & 1 deletion solo-chains/runtime/starlight/src/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ use {

pub use crate::{
genesis_config_presets::get_authority_keys_from_seed, AccountId, AuthorNoting, Babe, Balance,
Grandpa, Initializer, Runtime, Session, System, TanssiAuthorityAssignment,
ContainerRegistrar, Grandpa, Initializer, Runtime, Session, System, TanssiAuthorityAssignment,
TanssiCollatorAssignment, TransactionPayment,
};

Expand Down Expand Up @@ -221,6 +221,7 @@ pub fn end_block() {
Session::on_finalize(System::block_number());
Initializer::on_finalize(System::block_number());
TransactionPayment::on_finalize(System::block_number());
ContainerRegistrar::on_finalize(System::block_number());
}

pub fn run_block() {
Expand Down
21 changes: 19 additions & 2 deletions solo-chains/runtime/starlight/src/tests/relay_registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
#![cfg(test)]

use {
crate::tests::common::*,
crate::{Configuration, ContainerRegistrar, Paras, Registrar, System},
crate::{tests::common::*, Configuration, ContainerRegistrar, Paras, Registrar, System},
frame_support::{assert_noop, assert_ok},
pallet_registrar::Event as ContainerRegistrarEvent,
pallet_registrar_runtime_api::{
Expand Down Expand Up @@ -351,6 +350,13 @@ fn deregister_calls_schedule_para_cleanup() {
Configuration::set_max_head_data_size(root_origin(), 20500),
()
);

// Call end_block() to ensure that parachains_shared::CurrentSessionIndex
// storage gets updated properly inside on_finalize() and
// matches the one inside pallet_session::CurrentIndex.
end_block();
start_block();
Agusrodri marked this conversation as resolved.
Show resolved Hide resolved

run_to_session(4u32);
assert_eq!(
parachains_configuration::ActiveConfig::<Runtime>::get().max_head_data_size,
Expand All @@ -376,6 +382,8 @@ fn deregister_calls_schedule_para_cleanup() {
),
()
);
end_block();
start_block();

// Now let's check if the para was preoperly registered in the relay.
// Run to next session.
Expand All @@ -389,6 +397,9 @@ fn deregister_calls_schedule_para_cleanup() {
root_origin(),
validation_code.into()
));
end_block();
start_block();

run_to_session(7);

// Now the para should be a parathread.
Expand All @@ -401,6 +412,8 @@ fn deregister_calls_schedule_para_cleanup() {
ContainerRegistrar::mark_valid_for_collating(root_origin(), 1003.into()),
()
);
end_block();
start_block();

// The change should be applied after 2 sessions.
run_to_session(9);
Expand All @@ -422,8 +435,12 @@ fn deregister_calls_schedule_para_cleanup() {
}
.into(),
);
end_block();
start_block();

run_to_session(11);
end_block();

assert_eq!(Runtime::genesis_data(1003.into()).as_ref(), None);

// Para should be offboarding after 2 sessions.
Expand Down
11 changes: 3 additions & 8 deletions test/suites/dev-tanssi-relay/registrar/test_registrars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { jumpSessions } from "../../../util/block";

describeSuite({
id: "DT0107",
id: "DT0102",
title: "ContainerRegistrar <> relay Registrar",
foundationMethods: "dev",
testCases: ({ it, context }) => {
Expand Down Expand Up @@ -114,7 +114,7 @@

// Check we can't register via relay Registrar
const tx2 = polkadotJs.tx.registrar.register(2002, "0x", "0x0102030405060708091011").signAsync(alice);
let { result: result2 } = await context.createBlock([tx2]);

Check failure on line 117 in test/suites/dev-tanssi-relay/registrar/test_registrars.ts

View workflow job for this annotation

GitHub Actions / typescript-linting

'result2' is never reassigned. Use 'const' instead
expect(result2[0].successful).to.be.false;
expect(result2[0].error.section).to.eq("registrar");
expect(result2[0].error.name).to.eq("AlreadyRegistered");
Expand All @@ -129,23 +129,18 @@
const isParachain = await polkadotJs.query.paras.paraLifecycles(2002);
expect(isParachain.toString()).to.eq("Parachain");

const onChainGenesisDataBefore = await polkadotJs.query.containerRegistrar.paraGenesisData(2002);
console.log("ON CHAIN GEN: ", onChainGenesisDataBefore.toHuman());

const tx = polkadotJs.tx.containerRegistrar.deregister(2002);
await context.createBlock([await polkadotJs.tx.sudo.sudo(tx).signAsync(alice)], {
allowFailures: false,
});

await jumpSessions(context, 2);
await context.createBlock();

// Check that the on chain genesis data is now empty
const onChainGenesisData = await polkadotJs.query.containerRegistrar.paraGenesisData(2002);
expect(onChainGenesisData).to.be.null;
const onChainGenesisDataAfter = await polkadotJs.query.containerRegistrar.paraGenesisData(2002);
expect(onChainGenesisDataAfter.toHuman()).to.be.null;

// Para should be offboarding
// TODO: check why this is failing and not behaving the same as in rust tests
const isOffboarding = await polkadotJs.query.paras.paraLifecycles(2002);
expect(isOffboarding.toString()).to.eq("OffboardingParathread");
},
Expand Down
Loading