Skip to content

Commit

Permalink
mesh-1269: enforce no dupe did tax
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril committed Oct 15, 2020
1 parent 4e59d11 commit ecb8d4a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
22 changes: 13 additions & 9 deletions pallets/corporate-actions/src/lib.rs
Expand Up @@ -384,6 +384,15 @@ decl_module! {
let next_id = local_id.0.checked_add(1).map(LocalCAId).ok_or(Error::<T>::LocalCAIdOverflow)?;
let id = CAId { ticker, local_id };

// Ensure there are no duplicates in withholding tax overrides.
let mut wt = wt;
if let Some(wt) = &mut wt {
let before = wt.len();
wt.sort_unstable_by_key(|&(did, _)| did);
wt.dedup_by_key(|&mut (did, _)| did);
ensure!(before == wt.len(), Error::<T>::DuplicateDidTax);
}

// Create a checkpoint at `record_date`, if any.
if let Some(record_date) = record_date {
<Asset<T>>::_create_checkpoint_emit(ticker, record_date, caa)?;
Expand All @@ -397,15 +406,7 @@ decl_module! {
.map(|t| t.dedup())
.unwrap_or_else(|| Self::default_target_identities(ticker));
let dwt = default_wt.unwrap_or_else(|| Self::default_withholding_tax(ticker));
let wt = match wt {
None => Self::did_withholding_tax(ticker),
Some(mut wt) => {
// Nuke duplicates.
wt.sort_unstable_by_key(|&(did, _)| did);
wt.dedup_by_key(|&mut (did, _)| did);
wt
},
};
let wt = wt.unwrap_or_else(|| Self::did_withholding_tax(ticker));

// Commit CA to storage.
CorporateActions::insert(ticker, id.local_id, CorporateAction {
Expand Down Expand Up @@ -467,6 +468,9 @@ decl_error! {
/// There have been too many CAs for this ticker and the ID would overflow.
/// This won't occur in practice.
LocalCAIdOverflow,
/// A withholding tax override for a given DID was specified more than once.
/// The chain refused to make a choice, and hence there was an error.
DuplicateDidTax,
}
}

Expand Down
9 changes: 9 additions & 0 deletions pallets/runtime/tests/src/corporate_actions_test.rs
Expand Up @@ -463,6 +463,15 @@ fn initiate_corporate_action_did_tax() {
});
}

#[test]
#[should_panic]
fn initiate_corporate_action_did_tax_dupe() {
test(|ticker, [owner, foo, bar]| {
let wt = Some(vec![(bar.did, P75), (foo.did, P0), (bar.did, P50)]);
basic_ca(owner, ticker, None, None, wt);
});
}

#[test]
fn initiate_corporate_action_targets() {
test(|ticker, [owner, foo, bar]| {
Expand Down

0 comments on commit ecb8d4a

Please sign in to comment.