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 43 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
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.

15 changes: 14 additions & 1 deletion pallets/registrar/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ mod benchmarks {
create_funded_user::<T>("caller", 0, T::DepositAmount::get());

#[extrinsic_call]
Pallet::<T>::register(RawOrigin::Signed(caller), Default::default(), storage);
Pallet::<T>::register(RawOrigin::Signed(caller), Default::default(), storage, None);

// verification code
assert_eq!(pending_verification_len::<T>(), 1usize);
Expand Down Expand Up @@ -165,6 +165,7 @@ mod benchmarks {
proof,
signature,
storage,
None,
);

// verification code
Expand All @@ -188,6 +189,7 @@ mod benchmarks {
RawOrigin::Signed(caller.clone()).into(),
i.into(),
storage.clone(),
None,
)
.unwrap();
// Do not call mark_valid_for_collating, to ensure that the deregister call also executes the cleanup hooks
Expand Down Expand Up @@ -224,6 +226,7 @@ mod benchmarks {
RawOrigin::Signed(caller.clone()).into(),
i.into(),
storage.clone(),
None,
)
.unwrap();
// Call mark_valid_for_collating to ensure that the deregister call
Expand Down Expand Up @@ -273,6 +276,7 @@ mod benchmarks {
RawOrigin::Signed(caller.clone()).into(),
i.into(),
storage.clone(),
None,
)
.unwrap();
// Do not call mark_valid_for_collating, to ensure that the deregister call also executes the cleanup hooks
Expand Down Expand Up @@ -326,6 +330,7 @@ mod benchmarks {
RawOrigin::Signed(caller.clone()).into(),
i.into(),
storage.clone(),
None,
)
.unwrap();
// Call mark_valid_for_collating to ensure that the deregister call
Expand Down Expand Up @@ -393,6 +398,7 @@ mod benchmarks {
RawOrigin::Signed(caller.clone()).into(),
i.into(),
storage.clone(),
None,
)
.unwrap();
}
Expand All @@ -406,6 +412,7 @@ mod benchmarks {
RawOrigin::Signed(caller.clone()).into(),
k.into(),
storage.clone(),
None,
)
.unwrap();
T::RegistrarHooks::benchmarks_ensure_valid_for_collating(k.into());
Expand Down Expand Up @@ -445,6 +452,7 @@ mod benchmarks {
RawOrigin::Signed(caller.clone()).into(),
k.into(),
storage.clone(),
None,
)
.unwrap();
T::RegistrarHooks::benchmarks_ensure_valid_for_collating(k.into());
Expand All @@ -461,6 +469,7 @@ mod benchmarks {
RawOrigin::Signed(caller.clone()).into(),
i.into(),
storage.clone(),
None,
)
.unwrap();
T::RegistrarHooks::benchmarks_ensure_valid_for_collating(i.into());
Expand Down Expand Up @@ -509,6 +518,7 @@ mod benchmarks {
RawOrigin::Signed(caller.clone()).into(),
k.into(),
storage.clone(),
None,
)
.unwrap();
T::RegistrarHooks::benchmarks_ensure_valid_for_collating(k.into());
Expand All @@ -525,6 +535,7 @@ mod benchmarks {
RawOrigin::Signed(caller.clone()).into(),
i.into(),
storage.clone(),
None,
)
.unwrap();
T::RegistrarHooks::benchmarks_ensure_valid_for_collating(i.into());
Expand Down Expand Up @@ -568,6 +579,7 @@ mod benchmarks {
Default::default(),
slot_frequency,
storage,
None,
);

// verification code
Expand Down Expand Up @@ -596,6 +608,7 @@ mod benchmarks {
i.into(),
slot_frequency.clone(),
storage.clone(),
None,
)
.unwrap();
T::RegistrarHooks::benchmarks_ensure_valid_for_collating(i.into());
Expand Down
89 changes: 83 additions & 6 deletions pallets/registrar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub use weights::WeightInfo;
pub use pallet::*;

use {
cumulus_primitives_core::relay_chain::HeadData,
dp_chain_state_snapshot::GenericStateProof,
dp_container_chain_genesis_data::ContainerChainGenesisData,
frame_support::{
Expand All @@ -64,8 +65,8 @@ use {
sp_std::{collections::btree_set::BTreeSet, prelude::*},
tp_traits::{
GetCurrentContainerChains, GetSessionContainerChains, GetSessionIndex, ParaId,
ParathreadParams as ParathreadParamsTy, RelayStorageRootProvider, SessionContainerChains,
SlotFrequency,
ParathreadParams as ParathreadParamsTy, RegistrarHandler, RelayStorageRootProvider,
SessionContainerChains, SlotFrequency,
},
};

Expand Down Expand Up @@ -175,6 +176,8 @@ pub mod pallet {

type RegistrarHooks: RegistrarHooks;

type InnerRegistrar: RegistrarHandler<Self::AccountId>;
Agusrodri marked this conversation as resolved.
Show resolved Hide resolved

type WeightInfo: WeightInfo;
}

Expand Down Expand Up @@ -235,6 +238,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 @@ -320,6 +344,17 @@ 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
let mut weight = Weight::zero().saturating_add(T::DbWeight::get().reads(1));
Agusrodri marked this conversation as resolved.
Show resolved Hide resolved

let buffered_paras = BufferedParasToDeregister::<T>::get();
for _para_id in buffered_paras {
weight += T::InnerRegistrar::deregister_weight();
Agusrodri marked this conversation as resolved.
Show resolved Hide resolved
}
weight
}

#[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 @@ -421,6 +456,13 @@ pub mod pallet {

Ok(())
}

fn on_finalize(_: BlockNumberFor<T>) {
let buffered_paras = BufferedParasToDeregister::<T>::take();
for para_id in buffered_paras {
T::InnerRegistrar::deregister(para_id);
}
}
}

#[pallet::call]
Expand All @@ -432,9 +474,10 @@ pub mod pallet {
origin: OriginFor<T>,
para_id: ParaId,
genesis_data: ContainerChainGenesisData,
head_data: Option<HeadData>,
) -> DispatchResult {
let account = ensure_signed(origin)?;
Self::do_register(account, para_id, genesis_data)?;
Self::do_register(account, para_id, genesis_data, head_data)?;
Self::deposit_event(Event::ParaIdRegistered { para_id });

Ok(())
Expand Down Expand Up @@ -538,9 +581,10 @@ pub mod pallet {
para_id: ParaId,
slot_frequency: SlotFrequency,
genesis_data: ContainerChainGenesisData,
head_data: Option<HeadData>,
) -> DispatchResult {
let account = ensure_signed(origin)?;
Self::do_register(account, para_id, genesis_data)?;
Self::do_register(account, para_id, genesis_data, head_data)?;
// Insert parathread params
let params = ParathreadParamsTy { slot_frequency };
ParathreadParams::<T>::insert(para_id, params);
Expand Down Expand Up @@ -605,6 +649,7 @@ pub mod pallet {
relay_storage_proof: sp_trie::StorageProof,
manager_signature: cumulus_primitives_core::relay_chain::Signature,
genesis_data: ContainerChainGenesisData,
head_data: Option<HeadData>,
) -> DispatchResult {
let account = T::RegisterWithRelayProofOrigin::ensure_origin(origin)?;
let relay_storage_root =
Expand Down Expand Up @@ -633,7 +678,7 @@ pub mod pallet {
return Err(Error::<T>::InvalidRelayManagerSignature.into());
}

Self::do_register(account, para_id, genesis_data)?;
Self::do_register(account, para_id, genesis_data, head_data)?;
// Insert parathread params
if let Some(parathread_params) = parathread_params {
ParathreadParams::<T>::insert(para_id, parathread_params);
Expand Down Expand Up @@ -750,7 +795,12 @@ pub mod pallet {
(T::Currency::minimum_balance() + T::DepositAmount::get()) * 2u32.into();
let account = create_funded_user::<T>("caller", 1000, new_balance).0;
let origin = RawOrigin::Signed(account);
assert_ok!(Self::register(origin.into(), *para_id, Default::default()));
assert_ok!(Self::register(
origin.into(),
*para_id,
Default::default(),
None
));
}

let deposit_info = RegistrarDeposit::<T>::get(para_id).expect("Cannot return signed origin for a container chain that was registered by root. Try using a different para id");
Expand All @@ -767,6 +817,7 @@ pub mod pallet {
account: T::AccountId,
para_id: ParaId,
genesis_data: ContainerChainGenesisData,
head_data: Option<HeadData>,
) -> DispatchResult {
let deposit = T::DepositAmount::get();
// Verify we can hold
Expand Down Expand Up @@ -805,6 +856,13 @@ pub mod pallet {
// Hold the deposit, we verified we can do this
T::Currency::hold(&HoldReason::RegistrarDeposit.into(), &account, deposit)?;

T::InnerRegistrar::register(
account.clone(),
para_id,
genesis_data.clone().storage,
head_data,
)?;

// Update DepositInfo
RegistrarDeposit::<T>::insert(
para_id,
Expand All @@ -828,6 +886,13 @@ pub mod pallet {
Self::deposit_event(Event::ParaIdDeregistered { para_id });
// Cleanup immediately
Self::cleanup_deregistered_para_id(para_id);
BufferedParasToDeregister::<T>::try_mutate(|v| v.try_push(para_id)).map_err(
|_e| {
DispatchError::Other(
"Failed to add paraId to deregistration list: buffer is full",
)
},
)?;
} 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 All @@ -853,6 +918,7 @@ pub mod pallet {
})?;
// Mark this para id for cleanup later
Self::schedule_parachain_cleanup(para_id)?;
T::InnerRegistrar::schedule_para_downgrade(para_id)?;
Agusrodri marked this conversation as resolved.
Show resolved Hide resolved
Self::deposit_event(Event::ParaIdDeregistered { para_id });
}

Expand Down Expand Up @@ -889,6 +955,8 @@ pub mod pallet {

T::RegistrarHooks::para_marked_valid_for_collating(para_id);

T::InnerRegistrar::schedule_para_upgrade(para_id)?;

Ok(())
}

Expand Down Expand Up @@ -1168,6 +1236,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
1 change: 1 addition & 0 deletions pallets/registrar/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ impl pallet_registrar::Config for Test {
type DepositAmount = DepositAmount;
type RuntimeHoldReason = RuntimeHoldReason;
type RegistrarHooks = Mock;
type InnerRegistrar = ();
girazoki marked this conversation as resolved.
Show resolved Hide resolved
type WeightInfo = ();
}

Expand Down
Loading
Loading