Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

add swapped event to registrar #5990

Merged
merged 10 commits into from
May 3, 2023
7 changes: 7 additions & 0 deletions runtime/common/src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,13 @@ fn parachain_swap_works() {
ParaId::from(2001),
ParaId::from(2000)
));
assert!(contains_event(
paras_registrar::Event::<Test>::Swapped {
para_id: ParaId::from(2001),
other_id: ParaId::from(2000)
}
.into()
));
brunopgalvao marked this conversation as resolved.
Show resolved Hide resolved

// Crowdloan Swapped
let fund_2000 = Crowdloan::funds(ParaId::from(2000)).unwrap();
Expand Down
53 changes: 49 additions & 4 deletions runtime/common/src/paras_registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ pub mod pallet {
Registered { para_id: ParaId, manager: T::AccountId },
Deregistered { para_id: ParaId },
Reserved { para_id: ParaId, who: T::AccountId },
Swapped { para_id: ParaId, other_id: ParaId },
}

#[pallet::error]
Expand Down Expand Up @@ -304,6 +305,7 @@ pub mod pallet {
// need to do for their lifecycle management, just swap the underlying
// data.
T::OnSwap::on_swap(id, other);
Self::deposit_event(Event::<T>::Swapped { para_id: id, other_id: other });
} else {
return Err(Error::<T>::CannotSwap.into())
}
Expand Down Expand Up @@ -584,6 +586,7 @@ impl<T: Config> Pallet<T> {
let res2 = runtime_parachains::schedule_parathread_upgrade::<T>(to_upgrade);
debug_assert!(res2.is_ok());
T::OnSwap::on_swap(to_upgrade, to_downgrade);
brunopgalvao marked this conversation as resolved.
Show resolved Hide resolved
Self::deposit_event(Event::<T>::Swapped { para_id: to_upgrade, other_id: to_downgrade });
brunopgalvao marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -597,7 +600,7 @@ mod tests {
parameter_types,
traits::{GenesisBuild, OnFinalize, OnInitialize},
};
use frame_system::limits;
use frame_system::{limits, EventRecord, Phase};
use pallet_balances::Error as BalancesError;
use primitives::v2::{Balance, BlockNumber, Header};
use runtime_parachains::{configuration, origin, shared};
Expand Down Expand Up @@ -1052,9 +1055,21 @@ mod tests {
assert!(!Parachains::is_parachain(para_2));
assert!(Parachains::is_parathread(para_2));

// Both paras initiate a swap
assert_ok!(Registrar::swap(para_origin(para_1), para_1, para_2,));
assert_ok!(Registrar::swap(para_origin(para_2), para_2, para_1,));
// Both paras initiate a swap
// Swap between parachain and parathread
assert_ok!(Registrar::swap(para_origin(para_1), para_1, para_2,));
assert_ok!(Registrar::swap(para_origin(para_2), para_2, para_1,));
assert_eq!(
System::events(),
vec![EventRecord {
phase: Phase::Initialization,
event: Event::Registrar(paras_registrar::Event::Swapped {
para_id: para_2,
other_id: para_1
}),
topics: vec![],
}]
);
brunopgalvao marked this conversation as resolved.
Show resolved Hide resolved

run_to_session(6);

Expand All @@ -1067,6 +1082,28 @@ mod tests {
// Data is swapped
assert_eq!(SwapData::get().get(&para_1).unwrap(), &1337);
assert_eq!(SwapData::get().get(&para_2).unwrap(), &69);

// Both paras initiate a swap
// Swap between parathread and parachain
assert_ok!(Registrar::swap(para_origin(para_1), para_1, para_2,));
assert_ok!(Registrar::swap(para_origin(para_2), para_2, para_1,));
assert_eq!(
System::events().last().unwrap(),
vec![EventRecord {
phase: Phase::Initialization,
event: Event::Registrar(paras_registrar::Event::Swapped {
para_id: para_1,
other_id: para_2
}),
topics: vec![],
}]
.last()
.unwrap()
);
brunopgalvao marked this conversation as resolved.
Show resolved Hide resolved

// Data is swapped
assert_eq!(SwapData::get().get(&para_1).unwrap(), &69);
assert_eq!(SwapData::get().get(&para_2).unwrap(), &1337);
});
}

Expand Down Expand Up @@ -1179,6 +1216,10 @@ mod tests {
// Swap works here.
assert_ok!(Registrar::swap(Origin::root(), para_1, para_2));
assert_ok!(Registrar::swap(Origin::root(), para_2, para_1));
assert!(System::events().iter().any(|r| matches!(
r.event,
Event::Registrar(paras_registrar::Event::Swapped { .. })
)));

run_to_session(5);

Expand All @@ -1194,6 +1235,10 @@ mod tests {
// Swap worked!
assert!(Parachains::is_parachain(para_2));
assert!(Parachains::is_parathread(para_1));
assert!(System::events().iter().any(|r| matches!(
r.event,
Event::Registrar(paras_registrar::Event::Swapped { .. })
)));

// Something starts to downgrade a para
assert_ok!(Registrar::make_parathread(para_2));
Expand Down