Skip to content

Commit

Permalink
Update Settlement pallet.
Browse files Browse the repository at this point in the history
  • Loading branch information
Neopallium committed Dec 31, 2024
1 parent e090d39 commit 06ea9f8
Show file tree
Hide file tree
Showing 7 changed files with 947 additions and 610 deletions.
36 changes: 20 additions & 16 deletions pallets/runtime/tests/src/settlement_pallet/execute_instruction.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use frame_support::{assert_ok, StorageDoubleMap, StorageMap};
use frame_support::assert_ok;
use sp_keyring::AccountKeyring;

use pallet_asset::BalanceOf;
use pallet_portfolio::PortfolioLockedAssets;
use pallet_settlement::{
AffirmsReceived, Error, InstructionAffirmsPending, InstructionDetails, InstructionLegStatus,
InstructionLegs, InstructionMediatorsAffirmations, InstructionStatuses, OffChainAffirmations,
RawEvent, UserAffirmations, VenueInstructions,
AffirmsReceived, Error, Event, InstructionAffirmsPending, InstructionDetails,
InstructionLegStatus, InstructionLegs, InstructionMediatorsAffirmations, InstructionStatuses,
OffChainAffirmations, UserAffirmations, VenueInstructions,
};
use polymesh_primitives::settlement::{
AffirmationStatus, Instruction, InstructionId, InstructionStatus, Leg, LegId, SettlementType,
Expand Down Expand Up @@ -60,13 +60,16 @@ fn execute_instruction_storage_pruning() {
next_block();

// Asserts all storage have been pruned
assert_eq!(InstructionAffirmsPending::get(instruction_id), 0);
assert_eq!(
VenueInstructions::iter_prefix_values(venue_id.unwrap()).next(),
InstructionAffirmsPending::<TestStorage>::get(instruction_id),
0
);
assert_eq!(
VenueInstructions::<TestStorage>::iter_prefix_values(venue_id.unwrap()).next(),
None
);
assert_eq!(
InstructionLegs::iter_prefix_values(instruction_id).next(),
InstructionLegs::<TestStorage>::iter_prefix_values(instruction_id).next(),
None
);
assert_eq!(
Expand All @@ -78,11 +81,11 @@ fn execute_instruction_storage_pruning() {
None
);
assert_eq!(
OffChainAffirmations::iter_prefix_values(instruction_id).next(),
OffChainAffirmations::<TestStorage>::iter_prefix_values(instruction_id).next(),
None
);
assert_eq!(
AffirmsReceived::iter_prefix_values(instruction_id).next(),
AffirmsReceived::<TestStorage>::iter_prefix_values(instruction_id).next(),
None
);
assert_eq!(
Expand All @@ -91,11 +94,11 @@ fn execute_instruction_storage_pruning() {
None
);
assert_eq!(
UserAffirmations::get(alice_default_portfolio, instruction_id),
UserAffirmations::<TestStorage>::get(alice_default_portfolio, instruction_id),
AffirmationStatus::Unknown
);
assert_eq!(
UserAffirmations::get(bob_default_portfolio, instruction_id),
UserAffirmations::<TestStorage>::get(bob_default_portfolio, instruction_id),
AffirmationStatus::Unknown
);
assert_eq!(
Expand Down Expand Up @@ -164,32 +167,33 @@ fn execute_instruction_storage_rollback() {
1_000
);
assert_eq!(
UserAffirmations::get(alice_default_portfolio, instruction_id),
UserAffirmations::<TestStorage>::get(alice_default_portfolio, instruction_id),
AffirmationStatus::Affirmed
);
assert_eq!(
UserAffirmations::get(bob_default_portfolio, instruction_id),
UserAffirmations::<TestStorage>::get(bob_default_portfolio, instruction_id),
AffirmationStatus::Affirmed
);
assert_eq!(
InstructionStatuses::<TestStorage>::get(instruction_id),
InstructionStatus::Failed
);
let all_legs = InstructionLegs::iter_prefix_values(instruction_id).collect::<Vec<_>>();
let all_legs =
InstructionLegs::<TestStorage>::iter_prefix_values(instruction_id).collect::<Vec<_>>();
assert_eq!(all_legs.len(), 2);
// Asserts the events are being emitted
let mut system_events = System::events();
system_events.pop().unwrap();
assert_eq!(
system_events.pop().unwrap().event,
crate::storage::EventTest::Settlement(RawEvent::FailedToExecuteInstruction(
crate::storage::EventTest::Settlement(Event::FailedToExecuteInstruction(
instruction_id,
Error::<TestStorage>::FailedToReleaseLockOrTransferAssets.into()
))
);
assert_eq!(
system_events.pop().unwrap().event,
crate::storage::EventTest::Settlement(RawEvent::LegFailedExecution(
crate::storage::EventTest::Settlement(Event::LegFailedExecution(
SettlementDID.as_id(),
instruction_id,
LegId(1)
Expand Down
65 changes: 41 additions & 24 deletions pallets/runtime/tests/src/settlement_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use codec::Encode;
use frame_support::dispatch::DispatchErrorWithPostInfo;
use frame_support::{
assert_err_ignore_postinfo, assert_noop, assert_ok, assert_storage_noop, traits::TryCollect,
BoundedBTreeSet, IterableStorageDoubleMap, StorageDoubleMap, StorageMap,
BoundedBTreeSet,
};
use rand::{prelude::*, thread_rng};
use sp_runtime::{AccountId32, AnySignature};
Expand All @@ -17,9 +17,9 @@ use pallet_nft::NumberOfNFTs;
use pallet_portfolio::{PortfolioLockedNFT, PortfolioNFT};
use pallet_scheduler as scheduler;
use pallet_settlement::{
AffirmsReceived, InstructionAffirmsPending, InstructionLegs, InstructionMediatorsAffirmations,
InstructionMemos, NumberOfVenueSigners, OffChainAffirmations, RawEvent, UserAffirmations,
UserVenues, VenueInstructions,
AffirmsReceived, Event, InstructionAffirmsPending, InstructionLegs,
InstructionMediatorsAffirmations, InstructionMemos, NumberOfVenueSigners, OffChainAffirmations,
UserAffirmations, UserVenues, VenueInstructions,
};
use polymesh_primitives::asset::{AssetId, AssetType, NonFungibleType};
use polymesh_primitives::asset_metadata::{
Expand Down Expand Up @@ -196,11 +196,13 @@ fn venue_details_length_limited() {
}

fn venue_instructions(id: VenueId) -> Vec<InstructionId> {
VenueInstructions::iter_prefix(id).map(|(i, _)| i).collect()
VenueInstructions::<TestStorage>::iter_prefix(id)
.map(|(i, _)| i)
.collect()
}

fn user_venues(did: IdentityId) -> Vec<VenueId> {
let mut venues = UserVenues::iter_prefix(did)
let mut venues = UserVenues::<TestStorage>::iter_prefix(did)
.map(|(i, _)| i)
.collect::<Vec<_>>();
venues.sort();
Expand Down Expand Up @@ -468,7 +470,7 @@ fn token_swap() {

for i in 0..legs.len() {
assert_eq!(
InstructionLegs::get(&instruction_id, &LegId(i as u64)),
InstructionLegs::<TestStorage>::get(&instruction_id, &LegId(i as u64)),
legs[i].clone().into()
);
}
Expand Down Expand Up @@ -598,7 +600,7 @@ fn settle_on_block() {

for i in 0..legs.len() {
assert_eq!(
InstructionLegs::get(&instruction_id, &LegId(i as u64)),
InstructionLegs::<TestStorage>::get(&instruction_id, &LegId(i as u64)),
legs[i].clone().into()
);
}
Expand Down Expand Up @@ -734,7 +736,7 @@ fn failed_execution() {

for i in 0..legs.len() {
assert_eq!(
InstructionLegs::get(&instruction_id, &LegId(i as u64)),
InstructionLegs::<TestStorage>::get(&instruction_id, &LegId(i as u64)),
legs[i].clone().into()
);
}
Expand Down Expand Up @@ -1242,11 +1244,11 @@ fn claim_multiple_receipts_during_authorization() {

assert_affirms_pending(id, 0);
assert_eq!(
OffChainAffirmations::get(id, LegId(0)),
OffChainAffirmations::<TestStorage>::get(id, LegId(0)),
AffirmationStatus::Affirmed
);
assert_eq!(
OffChainAffirmations::get(id, LegId(1)),
OffChainAffirmations::<TestStorage>::get(id, LegId(1)),
AffirmationStatus::Affirmed
);
assert_leg_status(
Expand Down Expand Up @@ -1941,7 +1943,10 @@ fn dirty_storage_with_tx() {
let total_amount = amount1 + amount2;
assert_eq!(Settlement::instruction_affirms_pending(instruction_id), 0);
next_block();
assert_eq!(InstructionLegs::iter_prefix(instruction_id).count(), 0);
assert_eq!(
InstructionLegs::<TestStorage>::iter_prefix(instruction_id).count(),
0
);

// Ensure proper balance transfers
alice.assert_balance_decreased(&asset_id, total_amount);
Expand Down Expand Up @@ -2179,7 +2184,10 @@ fn assert_number_of_venue_signers() {
initial_signers.clone(),
VenueType::Exchange
));
assert_eq!(NumberOfVenueSigners::get(venue_id), max_signers);
assert_eq!(
NumberOfVenueSigners::<TestStorage>::get(venue_id),
max_signers
);
// Verifies that an error will be thrown when the limit is exceeded
assert_noop!(
Settlement::update_venue_signers(
Expand All @@ -2197,15 +2205,21 @@ fn assert_number_of_venue_signers() {
initial_signers[0..3].to_vec(),
false
));
assert_eq!(NumberOfVenueSigners::get(venue_id), max_signers - 3);
assert_eq!(
NumberOfVenueSigners::<TestStorage>::get(venue_id),
max_signers - 3
);
// Verifies that the count is being updated when adding adding new signers
assert_ok!(Settlement::update_venue_signers(
alice.origin(),
venue_id,
initial_signers[0..2].to_vec(),
true
));
assert_eq!(NumberOfVenueSigners::get(venue_id), max_signers - 1);
assert_eq!(
NumberOfVenueSigners::<TestStorage>::get(venue_id),
max_signers - 1
);
})
}

Expand Down Expand Up @@ -2541,14 +2555,14 @@ fn settle_manual_instruction_with_portfolio() {
let mut system_events = System::events();
assert_eq!(
system_events.pop().unwrap().event,
super::storage::EventTest::Settlement(RawEvent::SettlementManuallyExecuted(
super::storage::EventTest::Settlement(Event::SettlementManuallyExecuted(
alice.did,
instruction_id
))
);
assert_eq!(
system_events.pop().unwrap().event,
super::storage::EventTest::Settlement(RawEvent::InstructionExecuted(
super::storage::EventTest::Settlement(Event::InstructionExecuted(
alice.did,
instruction_id
))
Expand Down Expand Up @@ -4182,38 +4196,41 @@ fn assert_add_instruction_storage(
) {
portfolios_pending_approval.iter().for_each(|portfolio_id| {
assert_eq!(
UserAffirmations::get(portfolio_id, instruction_id),
UserAffirmations::<TestStorage>::get(portfolio_id, instruction_id),
AffirmationStatus::Pending
)
});
portfolios_pre_approved.iter().for_each(|portfolio_id| {
assert_eq!(
UserAffirmations::get(portfolio_id, instruction_id),
UserAffirmations::<TestStorage>::get(portfolio_id, instruction_id),
AffirmationStatus::Affirmed
);
assert_eq!(
AffirmsReceived::get(instruction_id, portfolio_id),
AffirmsReceived::<TestStorage>::get(instruction_id, portfolio_id),
AffirmationStatus::Affirmed
)
});
offchain_legs.iter().for_each(|leg_id| {
assert_eq!(
OffChainAffirmations::get(instruction_id, leg_id),
OffChainAffirmations::<TestStorage>::get(instruction_id, leg_id),
AffirmationStatus::Pending
);
});
assert_eq!(
InstructionAffirmsPending::get(instruction_id),
InstructionAffirmsPending::<TestStorage>::get(instruction_id),
portfolios_pending_approval.len() as u64
+ offchain_legs.len() as u64
+ mediators_pending_approval.len() as u64
);

assert_eq!(InstructionMemos::get(instruction_id), instruction_memo);
assert_eq!(
InstructionMemos::<TestStorage>::get(instruction_id),
instruction_memo
);

(0..legs.len()).for_each(|i| {
assert_eq!(
InstructionLegs::get(instruction_id, LegId(i as u64)),
InstructionLegs::<TestStorage>::get(instruction_id, LegId(i as u64)),
Some(legs[i].clone())
)
});
Expand Down
8 changes: 4 additions & 4 deletions pallets/settlement/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ fn create_venue_<T: Config>(did: IdentityId, signers: Vec<T::AccountId>) -> Venu
};
// NB: Venue counter starts with 1.
let venue_counter = Pallet::<T>::venue_counter();
VenueInfo::insert(venue_counter, venue);
VenueInfo::<T>::insert(venue_counter, venue);
for signer in signers {
<VenueSigners<T>>::insert(venue_counter, signer, true);
VenueSigners::<T>::insert(venue_counter, signer, true);
}
VenueCounter::put(venue_counter.checked_inc().unwrap());
VenueCounter::<T>::put(venue_counter.checked_inc().unwrap());
venue_counter
}

Expand Down Expand Up @@ -344,7 +344,7 @@ benchmarks! {
}: _(origin, venue_details, signers, venue_type)
verify {
assert_eq!(Pallet::<T>::venue_counter(), VenueId(2), "Invalid venue counter");
assert!(UserVenues::contains_key(did.unwrap(), VenueId(1)), "Invalid venue id");
assert!(UserVenues::<T>::contains_key(did.unwrap(), VenueId(1)), "Invalid venue id");
assert!(Pallet::<T>::venue_info(VenueId(1)).is_some(), "Incorrect venue info set");
}

Expand Down
Loading

0 comments on commit 06ea9f8

Please sign in to comment.