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

MESH-1225: define Utility::{batch_atomic, batch_optimistic} #559

Merged
merged 1 commit into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion pallets/runtime/tests/src/pips_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ fn assert_state(id: PipId, care_about_pruned: bool, state: ProposalState) {
}
}

fn assert_balance(acc: Public, free: u128, locked: u128) {
pub fn assert_balance(acc: Public, free: u128, locked: u128) {
assert_eq!(Balances::free_balance(&acc), free);
assert_eq!(Balances::usable_balance(&acc), free - locked);
}
Expand Down
168 changes: 112 additions & 56 deletions pallets/runtime/tests/src/utility_test.rs
Original file line number Diff line number Diff line change
@@ -1,80 +1,137 @@
use super::{
storage::{register_keyring_account_with_balance, Call, TestStorage},
pips_test::assert_balance,
storage::{register_keyring_account_with_balance, Call, EventTest, TestStorage},
ExtBuilder,
};
use pallet_balances::{self as balances, Call as BalancesCall};
use pallet_utility as utility;
use pallet_utility::{self as utility, Event};
use polymesh_common_utilities::traits::transaction_payment::CddAndFeeDetails;

use codec::Encode;
use frame_support::{assert_err, assert_ok};
use frame_support::{assert_err, assert_ok, dispatch::DispatchError};
use pallet_utility::UniqueCall;
use polymesh_primitives::Signatory;
use sp_core::sr25519::Signature;
use sp_core::sr25519::{Public, Signature};
use test_client::AccountKeyring;

type Balances = balances::Module<TestStorage>;
type Utility = utility::Module<TestStorage>;
type Error = utility::Error<TestStorage>;
type Origin = <TestStorage as frame_system::Trait>::Origin;
type System = frame_system::Module<TestStorage>;

#[test]
fn batch_with_signed_works() {
ExtBuilder::default()
.build()
.execute_with(batch_with_signed_works_we);
fn transfer(to: Public, amount: u128) -> Call {
Call::Balances(BalancesCall::transfer(to, amount))
}

fn batch_with_signed_works_we() {
let alice = AccountKeyring::Alice.public();
TestStorage::set_payer_context(Some(alice));
let _alice_did = register_keyring_account_with_balance(AccountKeyring::Alice, 1_000).unwrap();
const ERROR: DispatchError = DispatchError::Module {
index: 0,
error: 2,
message: None,
};

let bob = AccountKeyring::Bob.public();
TestStorage::set_payer_context(Some(bob));
let _bob_did = register_keyring_account_with_balance(AccountKeyring::Bob, 1_000).unwrap();
fn assert_event(event: Event) {
assert_eq!(
System::events().pop().unwrap().event,
EventTest::pallet_utility(event)
)
}

fn batch_test(test: impl FnOnce(Public, Public)) {
ExtBuilder::default().build().execute_with(|| {
System::set_block_number(1);

let alice = AccountKeyring::Alice.public();
TestStorage::set_payer_context(Some(alice));
let _ = register_keyring_account_with_balance(AccountKeyring::Alice, 1_000).unwrap();

assert_eq!(Balances::free_balance(alice), 959);
assert_eq!(Balances::free_balance(bob), 959);
let bob = AccountKeyring::Bob.public();
TestStorage::set_payer_context(Some(bob));
let _ = register_keyring_account_with_balance(AccountKeyring::Bob, 1_000).unwrap();

let batched_calls = vec![
Call::Balances(BalancesCall::transfer(bob, 400)),
Call::Balances(BalancesCall::transfer(bob, 400)),
];
assert_balance(alice, 959, 0);
assert_balance(bob, 959, 0);

assert_ok!(Utility::batch(Origin::signed(alice), batched_calls));
assert_eq!(Balances::free_balance(alice), 159);
assert_eq!(Balances::free_balance(bob), 959 + 400 + 400);
test(alice, bob)
});
}

#[test]
fn batch_early_exit_works() {
ExtBuilder::default()
.build()
.execute_with(batch_early_exit_works_we);
fn batch_with_signed_works() {
batch_test(|alice, bob| {
let calls = vec![transfer(bob, 400), transfer(bob, 400)];
assert_ok!(Utility::batch(Origin::signed(alice), calls));
assert_balance(alice, 159, 0);
assert_balance(bob, 959 + 400 + 400, 0);
assert_event(Event::BatchCompleted);
});
}

fn batch_early_exit_works_we() {
let alice = AccountKeyring::Alice.public();
TestStorage::set_payer_context(Some(alice));
let _alice_did = register_keyring_account_with_balance(AccountKeyring::Alice, 1_000).unwrap();
#[test]
fn batch_early_exit_works() {
batch_test(|alice, bob| {
let calls = vec![transfer(bob, 400), transfer(bob, 900), transfer(bob, 400)];
assert_ok!(Utility::batch(Origin::signed(alice), calls));
assert_balance(alice, 559, 0);
assert_balance(bob, 959 + 400, 0);
assert_event(Event::BatchInterrupted(1, ERROR));
})
}

let bob = AccountKeyring::Bob.public();
TestStorage::set_payer_context(Some(bob));
let _bob_did = register_keyring_account_with_balance(AccountKeyring::Bob, 1_000).unwrap();
#[test]
fn batch_optimistic_works() {
batch_test(|alice, bob| {
let calls = vec![transfer(bob, 401), transfer(bob, 402)];
assert_ok!(Utility::batch_optimistic(Origin::signed(alice), calls));
assert_event(Event::BatchCompleted);
assert_balance(alice, 959 - 401 - 402, 0);
assert_balance(bob, 959 + 401 + 402, 0);
});
}

assert_eq!(Balances::free_balance(alice), 959);
assert_eq!(Balances::free_balance(bob), 959);
#[test]
fn batch_optimistic_failures_listed() {
batch_test(|alice, bob| {
assert_ok!(Utility::batch_optimistic(
Origin::signed(alice),
vec![
transfer(bob, 401), // YAY.
transfer(bob, 900), // NAY.
transfer(bob, 800), // NAY.
transfer(bob, 402), // YAY.
transfer(bob, 403), // NAY.
]
));
assert_event(Event::BatchOptimisticFailed(vec![
(1, ERROR),
(2, ERROR),
(4, ERROR),
]));
assert_balance(alice, 959 - 401 - 402, 0);
assert_balance(bob, 959 + 401 + 402, 0);
});
}

let batched_calls = vec![
Call::Balances(BalancesCall::transfer(bob, 400)),
Call::Balances(BalancesCall::transfer(bob, 900)),
Call::Balances(BalancesCall::transfer(bob, 400)),
];
#[test]
fn batch_atomic_works() {
batch_test(|alice, bob| {
let calls = vec![transfer(bob, 401), transfer(bob, 402)];
assert_ok!(Utility::batch_atomic(Origin::signed(alice), calls));
assert_event(Event::BatchCompleted);
assert_balance(alice, 959 - 401 - 402, 0);
assert_balance(bob, 959 + 401 + 402, 0);
});
}

assert_ok!(Utility::batch(Origin::signed(alice), batched_calls));
assert_eq!(Balances::free_balance(alice), 559);
assert_eq!(Balances::free_balance(bob), 959 + 400);
#[test]
fn batch_atomic_early_exit_works() {
batch_test(|alice, bob| {
let calls = vec![transfer(bob, 400), transfer(bob, 900), transfer(bob, 400)];
assert_ok!(Utility::batch_atomic(Origin::signed(alice), calls));
assert_balance(alice, 959, 0);
assert_balance(bob, 959, 0);
assert_event(Event::BatchInterrupted(1, ERROR));
})
}

#[test]
Expand All @@ -86,17 +143,16 @@ fn relay_happy_case() {

fn _relay_happy_case() {
let alice = AccountKeyring::Alice.public();
let _alice_did = register_keyring_account_with_balance(AccountKeyring::Alice, 1_000).unwrap();
let _ = register_keyring_account_with_balance(AccountKeyring::Alice, 1_000).unwrap();

let bob = AccountKeyring::Bob.public();
let _bob_did = register_keyring_account_with_balance(AccountKeyring::Bob, 1_000).unwrap();
let _ = register_keyring_account_with_balance(AccountKeyring::Bob, 1_000).unwrap();

let charlie = AccountKeyring::Charlie.public();
let _charlie_did =
register_keyring_account_with_balance(AccountKeyring::Charlie, 1_000).unwrap();
let _ = register_keyring_account_with_balance(AccountKeyring::Charlie, 1_000).unwrap();

assert_eq!(Balances::free_balance(bob), 1000);
assert_eq!(Balances::free_balance(charlie), 1000);
assert_balance(bob, 1000, 0);
assert_balance(charlie, 1000, 0);

let origin = Origin::signed(alice);
let transaction = UniqueCall::new(
Expand All @@ -111,8 +167,8 @@ fn _relay_happy_case() {
transaction
));

assert_eq!(Balances::free_balance(bob), 950);
assert_eq!(Balances::free_balance(charlie), 1_050);
assert_balance(bob, 950, 0);
assert_balance(charlie, 1_050, 0);
}

#[test]
Expand All @@ -124,7 +180,7 @@ fn relay_unhappy_cases() {

fn _relay_unhappy_cases() {
let alice = AccountKeyring::Alice.public();
let _alice_did = register_keyring_account_with_balance(AccountKeyring::Alice, 1_000).unwrap();
let _ = register_keyring_account_with_balance(AccountKeyring::Alice, 1_000).unwrap();

let bob = AccountKeyring::Bob.public();

Expand Down Expand Up @@ -156,7 +212,7 @@ fn _relay_unhappy_cases() {
Error::TargetCddMissing
);

let _bob_did = register_keyring_account_with_balance(AccountKeyring::Bob, 1_000).unwrap();
let _ = register_keyring_account_with_balance(AccountKeyring::Bob, 1_000).unwrap();

let transaction = UniqueCall::new(
Utility::nonce(bob) + 1,
Expand Down
Loading