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

Commit

Permalink
add swapped event to registrar (#5990)
Browse files Browse the repository at this point in the history
* add swapped event to registrar

* check swapped event on integration test

* check for swapped event in paras_registrar swap tests

* cargo +nightly fmt --all

* add test for para to para swap

* make paraId event consistent

* cargo +nightly fmt
  • Loading branch information
brunopgalvao authored May 3, 2023
1 parent 689623c commit 81456c8
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 3 deletions.
7 changes: 7 additions & 0 deletions runtime/common/src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,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()
));

// Crowdloan Swapped
let fund_2000 = Crowdloan::funds(ParaId::from(2000)).unwrap();
Expand Down
77 changes: 74 additions & 3 deletions runtime/common/src/paras_registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,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 @@ -319,6 +320,7 @@ pub mod pallet {
} else {
return Err(Error::<T>::CannotSwap.into())
}
Self::deposit_event(Event::<T>::Swapped { para_id: id, other_id: other });
PendingSwap::<T>::remove(other);
} else {
PendingSwap::<T>::insert(id, other);
Expand Down Expand Up @@ -806,9 +808,11 @@ mod tests {
)
.unwrap();

pallet_balances::GenesisConfig::<Test> { balances: vec![(1, 10_000_000), (2, 10_000_000)] }
.assimilate_storage(&mut t)
.unwrap();
pallet_balances::GenesisConfig::<Test> {
balances: vec![(1, 10_000_000), (2, 10_000_000), (3, 10_000_000)],
}
.assimilate_storage(&mut t)
.unwrap();

t.into()
}
Expand Down Expand Up @@ -1120,8 +1124,13 @@ mod tests {
assert!(Parachains::is_parathread(para_2));

// 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,));
System::assert_last_event(RuntimeEvent::Registrar(paras_registrar::Event::Swapped {
para_id: para_2,
other_id: para_1,
}));

run_to_session(6);

Expand All @@ -1134,6 +1143,60 @@ 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,));
System::assert_last_event(RuntimeEvent::Registrar(paras_registrar::Event::Swapped {
para_id: para_2,
other_id: para_1,
}));

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

// Parachain to parachain swap
let para_3 = LOWEST_PUBLIC_ID + 2;
assert_ok!(Registrar::reserve(RuntimeOrigin::signed(3)));
assert_ok!(Registrar::register(
RuntimeOrigin::signed(3),
para_3,
test_genesis_head(max_head_size() as usize),
test_validation_code(max_code_size() as usize),
));

run_to_session(8);

// Upgrade para 3 into a parachain
assert_ok!(Registrar::make_parachain(para_3));

// Set some mock swap data.
let mut swap_data = SwapData::get();
swap_data.insert(para_3, 777);
SwapData::set(swap_data);

run_to_session(10);

// Both are parachains
assert!(Parachains::is_parachain(para_3));
assert!(!Parachains::is_parathread(para_3));
assert!(Parachains::is_parachain(para_1));
assert!(!Parachains::is_parathread(para_1));

// Both paras initiate a swap
// Swap between parachain and parachain
assert_ok!(Registrar::swap(para_origin(para_1), para_1, para_3,));
assert_ok!(Registrar::swap(para_origin(para_3), para_3, para_1,));
System::assert_last_event(RuntimeEvent::Registrar(paras_registrar::Event::Swapped {
para_id: para_3,
other_id: para_1,
}));

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

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

run_to_session(5);

Expand All @@ -1263,6 +1330,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,
RuntimeEvent::Registrar(paras_registrar::Event::Swapped { .. })
)));

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

0 comments on commit 81456c8

Please sign in to comment.