Skip to content

Commit

Permalink
Update after review
Browse files Browse the repository at this point in the history
  • Loading branch information
simonsso committed Mar 22, 2024
1 parent 7cc3738 commit 0cb333d
Showing 1 changed file with 34 additions and 72 deletions.
106 changes: 34 additions & 72 deletions pallets/sponsorship/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use frame_support::{
traits::{Currency, ReservableCurrency},
};
use sp_runtime::transaction_validity::ValidTransaction;
use sp_runtime::BoundedVec;
use sp_runtime::{
traits::SignedExtension,
transaction_validity::{InvalidTransaction, TransactionValidityError},
Expand Down Expand Up @@ -2521,9 +2522,10 @@ fn sponsor_call_is_invalid_if_sponsor_account_is_running_low() {
}

#[test]
fn compound_transation_pass() {
fn compound_transaction_pass() {
new_test_ext().execute_with(|| {
let pot = 3;
let collection = 101;
System::set_block_number(1);
let pot_fee_quota = 100_000_000_000;
let pot_reserve_quota = 100_000_000_000;
Expand Down Expand Up @@ -2559,24 +2561,28 @@ fn compound_transation_pass() {
));
let user_details = User::<Test>::get(pot, user).unwrap();

let unique_create_call = vec![
let metadata = BoundedVec::truncate_from(vec![0xda, 0xda, 0xda, 0xd0]);
let batch_of_uniques_calls = vec![
Box::new(RuntimeCall::Uniques(pallet_uniques::Call::create {
collection: 0u32,
admin: user,
collection,
admin: user_details.proxy,
})),
Box::new(RuntimeCall::Uniques(pallet_uniques::Call::create {
collection: 1u32,
admin: user,
Box::new(RuntimeCall::Uniques(pallet_uniques::pallet::Call::mint {
collection,
item: 1,
owner: user_details.proxy,
})),
Box::new(RuntimeCall::Uniques(pallet_uniques::Call::create {
collection: 2u32,
admin: user,
Box::new(RuntimeCall::Uniques(pallet_uniques::pallet::Call::set_metadata {
collection,
item: 1,
data: metadata,
is_frozen: true,
})),
];

let sponsor_for_uniques_create_call = Box::new(RuntimeCall::SponsorshipModule(Call::sponsor_for {
pot: 3,
calls: unique_create_call.clone(),
calls: batch_of_uniques_calls.clone(),
}));

let pre_dispatch_details = ChargeSponsor::<Test>::default()
Expand All @@ -2590,7 +2596,7 @@ fn compound_transation_pass() {
assert_ok!(SponsorshipModule::sponsor_for(
RuntimeOrigin::signed(user),
pot,
unique_create_call.clone()
batch_of_uniques_calls.clone()
));

assert_ok!(ChargeSponsor::<Test>::post_dispatch(
Expand All @@ -2611,8 +2617,8 @@ fn compound_transation_pass() {
fee
);

assert_eq!(user_details_post_dispatch.reserve_quota.balance(), 7);
assert_eq!(pot_details_post_dispatch.reserve_quota.balance(), 7);
assert_eq!(user_details_post_dispatch.reserve_quota.balance(), 9);
assert_eq!(pot_details_post_dispatch.reserve_quota.balance(), 9);

System::assert_last_event(
Event::TransactionFeePaid {
Expand All @@ -2636,7 +2642,7 @@ fn compound_transation_pass() {
}

#[test]
fn compound_transation_should_be_filtered() {
fn batching_unsupported_call_type_should_fail() {
new_test_ext().execute_with(|| {
let pot = 3;
System::set_block_number(1);
Expand Down Expand Up @@ -2672,9 +2678,9 @@ fn compound_transation_should_be_filtered() {
user_fee_quota,
user_reserve_quota
));
let user_details = User::<Test>::get(pot, user).unwrap();
let _user_details = User::<Test>::get(pot, user).unwrap();

let unique_create_call = vec![
let batched_uniques_and_non_uniques_calls = vec![
Box::new(RuntimeCall::Uniques(pallet_uniques::Call::create {
collection: 0u32,
admin: user,
Expand All @@ -2695,63 +2701,30 @@ fn compound_transation_should_be_filtered() {

let sponsor_for_uniques_create_call = Box::new(RuntimeCall::SponsorshipModule(Call::sponsor_for {
pot: 3,
calls: unique_create_call.clone(),
calls: batched_uniques_and_non_uniques_calls.clone(),
}));

let pre_dispatch_details = ChargeSponsor::<Test>::default()
let _pre_dispatch_details = ChargeSponsor::<Test>::default()
.pre_dispatch(
&user,
&sponsor_for_uniques_create_call,
&sponsor_for_uniques_create_call.get_dispatch_info(),
0,
)
.ok();
assert!(SponsorshipModule::sponsor_for(RuntimeOrigin::signed(user), pot, unique_create_call.clone()).is_err());

assert_ok!(ChargeSponsor::<Test>::post_dispatch(
pre_dispatch_details,
&sponsor_for_uniques_create_call.get_dispatch_info(),
&().into(),
0,
&DispatchResult::Ok(())
));

let user_details_post_dispatch = User::<Test>::get(pot, user).unwrap();
let pot_details_post_dispatch = Pot::<Test>::get(pot).unwrap();

let fee = pot_details_post_dispatch.fee_quota.balance() - pot_details.fee_quota.balance();
assert_ne!(fee, 0);
assert_eq!(
user_details_post_dispatch.fee_quota.balance() - user_details.fee_quota.balance(),
fee
);

assert_eq!(user_details_post_dispatch.reserve_quota.balance(), 0);
assert_eq!(pot_details_post_dispatch.reserve_quota.balance(), 0);

System::assert_last_event(
Event::TransactionFeePaid {
sponsor: pot_details.sponsor,
fee,
}
.into(),
);
assert_eq!(
Balances::free_balance(pot_details.sponsor),
pot_reserve_quota
- fee - user_details_post_dispatch.reserve_quota.balance()
- PotDeposit::get()
- UserDeposit::get()
);
assert_eq!(
Balances::total_balance(&user_details.proxy),
user_details_post_dispatch.reserve_quota.balance()
assert_noop!(
SponsorshipModule::sponsor_for(
RuntimeOrigin::signed(user),
pot,
batched_uniques_and_non_uniques_calls.clone()
),
frame_system::Error::<Test>::CallFiltered
);
});
}

#[test]
fn compound_transaction_illegal_content_should_be_filtered() {
fn sponsor_for_uniques_is_not_uniques_and_should_not_be() {
new_test_ext().execute_with(|| {
let pot = 3;
System::set_block_number(1);
Expand Down Expand Up @@ -2787,8 +2760,6 @@ fn compound_transaction_illegal_content_should_be_filtered() {
user_fee_quota,
user_reserve_quota
));
let _user_details = User::<Test>::get(pot, user).unwrap();

let unique_create_call = RuntimeCall::Uniques(pallet_uniques::Call::create {
collection: 0u32,
admin: user,
Expand All @@ -2799,20 +2770,11 @@ fn compound_transaction_illegal_content_should_be_filtered() {
calls: vec![Box::new(unique_create_call)],
}))];

let sponsor_for_uniques_create_call = Box::new(RuntimeCall::SponsorshipModule(Call::sponsor_for {
let _sponsor_for_uniques_create_call = Box::new(RuntimeCall::SponsorshipModule(Call::sponsor_for {
pot: 3,
calls: compound_call.clone(),
}));

let _pre_dispatch_details = ChargeSponsor::<Test>::default()
.pre_dispatch(
&user,
&sponsor_for_uniques_create_call,
&sponsor_for_uniques_create_call.get_dispatch_info(),
0,
)
.ok();

assert_noop!(
SponsorshipModule::sponsor_for(RuntimeOrigin::signed(user), pot, compound_call),
frame_system::Error::<Test>::CallFiltered
Expand Down

0 comments on commit 0cb333d

Please sign in to comment.