Skip to content

Commit 971e456

Browse files
Disallow custody remove cdd (#1828)
* Remove cdd and did requirement when nominating * Block adding custodians to default portfolios * Move check to accept auth; Remove old benchmark
1 parent 680ff78 commit 971e456

File tree

10 files changed

+73
-167
lines changed

10 files changed

+73
-167
lines changed

pallets/identity/src/auth.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ impl<T: Config> Pallet<T> {
5959
number_of_given_auths < T::MaxGivenAuths::get(),
6060
Error::<T>::ExceededNumberOfGivenAuths
6161
);
62+
6263
NumberOfGivenAuths::<T>::insert(from, number_of_given_auths.saturating_add(1));
6364

6465
let new_auth_id = CurrentAuthId::<T>::get().saturating_add(1);

pallets/portfolio/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,8 @@ pub mod pallet {
382382
SelfAdditionNotAllowed,
383383
/// The extrinsic expected a different `AuthorizationType` than what the `data.auth_type()` is.
384384
BadAuthorizationType,
385+
/// Default portfolios cannot have custodians.
386+
DefaultPortfoliosCannotHaveCustodians,
385387
}
386388

387389
#[pallet::call]
@@ -899,6 +901,11 @@ impl<T: Config> Pallet<T> {
899901
pallet_identity::Pallet::<T>::accept_auth_with(&to.into(), auth_id, |data, from| {
900902
let pid = extract_auth!(data, PortfolioCustody(p));
901903

904+
ensure!(
905+
pid.kind != PortfolioKind::Default,
906+
Error::<T>::DefaultPortfoliosCannotHaveCustodians
907+
);
908+
902909
let curr = Self::custodian(&pid);
903910
pallet_identity::Pallet::<T>::ensure_auth_by(from, curr)?;
904911

pallets/runtime/tests/src/asset_pallet/issue.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,13 @@ fn issue_tokens_assigned_custody() {
130130
ExtBuilder::default().build().execute_with(|| {
131131
let bob = User::new(AccountKeyring::Bob);
132132
let alice = User::new(AccountKeyring::Alice);
133-
let portfolio_id = PortfolioId::new(alice.did, PortfolioKind::Default);
133+
let portfolio_kind = PortfolioKind::User(PortfolioNumber(1));
134+
let portfolio_id = PortfolioId::new(alice.did, portfolio_kind);
135+
136+
assert_ok!(Portfolio::create_portfolio(
137+
alice.origin(),
138+
PortfolioName(b"AliceUserPortfolio".to_vec())
139+
));
134140

135141
let asset_id = Asset::generate_asset_id(alice.acc(), false);
136142
assert_ok!(Asset::create_asset(
@@ -157,7 +163,7 @@ fn issue_tokens_assigned_custody() {
157163
alice.origin(),
158164
asset_id,
159165
1_000,
160-
PortfolioKind::Default
166+
portfolio_kind
161167
));
162168
assert_eq!(BalanceOf::<TestStorage>::get(asset_id, alice.did), 1_000);
163169
assert_eq!(

pallets/runtime/tests/src/asset_test.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,7 +1750,13 @@ fn redeem_token_assigned_custody() {
17501750
ExtBuilder::default().build().execute_with(|| {
17511751
let bob = User::new(AccountKeyring::Bob);
17521752
let alice = User::new(AccountKeyring::Alice);
1753-
let portfolio_id = PortfolioId::new(alice.did, PortfolioKind::Default);
1753+
let portfolio_kind = PortfolioKind::User(PortfolioNumber(1));
1754+
let portfolio_id = PortfolioId::new(alice.did, portfolio_kind);
1755+
1756+
assert_ok!(Portfolio::create_portfolio(
1757+
alice.origin(),
1758+
PortfolioName(b"AliceUserPortfolio".to_vec())
1759+
));
17541760

17551761
let asset_id = create_and_issue_sample_asset(&alice);
17561762
// Change custody of the default portfolio
@@ -1767,12 +1773,7 @@ fn redeem_token_assigned_custody() {
17671773
));
17681774

17691775
assert_noop!(
1770-
Asset::redeem(
1771-
alice.origin(),
1772-
asset_id,
1773-
ISSUE_AMOUNT,
1774-
PortfolioKind::Default
1775-
),
1776+
Asset::redeem(alice.origin(), asset_id, ISSUE_AMOUNT, portfolio_kind),
17761777
PortfolioError::UnauthorizedCustodian
17771778
);
17781779
})

pallets/runtime/tests/src/nft.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use polymesh_primitives::settlement::{InstructionId, Leg, SettlementType};
1616
use polymesh_primitives::{
1717
with_transaction, AuthorizationData, Claim, ClaimType, Condition, ConditionType, CountryCode,
1818
IdentityId, NFTCollectionId, NFTCollectionKeys, NFTId, NFTMetadataAttribute, NFTs, PortfolioId,
19-
PortfolioKind, PortfolioNumber, PortfolioUpdateReason, Scope, Signatory, TrustedFor,
20-
TrustedIssuer, WeightMeter,
19+
PortfolioKind, PortfolioName, PortfolioNumber, PortfolioUpdateReason, Scope, Signatory,
20+
TrustedFor, TrustedIssuer, WeightMeter,
2121
};
2222
use sp_keyring::AccountKeyring;
2323

@@ -506,11 +506,17 @@ fn burn_nft_no_custody() {
506506

507507
let bob: User = User::new(AccountKeyring::Bob);
508508
let alice: User = User::new(AccountKeyring::Alice);
509+
let portfolio_kind = PortfolioKind::User(PortfolioNumber(1));
510+
let portfolio_id = PortfolioId::new(alice.did, portfolio_kind);
509511

510-
let portfolio_id = PortfolioId::new(alice.did, PortfolioKind::Default);
511512
let collection_keys: NFTCollectionKeys =
512513
vec![AssetMetadataKey::Local(AssetMetadataLocalKey(1))].into();
513514

515+
assert_ok!(Portfolio::create_portfolio(
516+
alice.origin(),
517+
PortfolioName(b"AliceUserPortfolio".to_vec())
518+
));
519+
514520
let asset_id = create_nft_collection(
515521
alice.clone(),
516522
AssetType::NonFungible(NonFungibleType::Derivative),
@@ -537,18 +543,12 @@ fn burn_nft_no_custody() {
537543
key: AssetMetadataKey::Local(AssetMetadataLocalKey(1)),
538544
value: AssetMetadataValue(b"test".to_vec()),
539545
}],
540-
PortfolioKind::Default,
546+
portfolio_kind,
541547
)
542548
.unwrap();
543549

544550
assert_noop!(
545-
NFT::redeem_nft(
546-
alice.origin(),
547-
asset_id,
548-
NFTId(1),
549-
PortfolioKind::Default,
550-
None
551-
),
551+
NFT::redeem_nft(alice.origin(), asset_id, NFTId(1), portfolio_kind, None),
552552
PortfolioError::UnauthorizedCustodian
553553
);
554554
});

pallets/runtime/tests/src/portfolio.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,3 +1153,25 @@ fn allow_identity_to_create_portfolios_not_allowed() {
11531153
);
11541154
});
11551155
}
1156+
1157+
#[test]
1158+
fn assign_custody_of_default_portfolio() {
1159+
ExtBuilder::default().build().execute_with(|| {
1160+
let bob = User::new(AccountKeyring::Bob);
1161+
let alice = User::new(AccountKeyring::Alice);
1162+
let portfolio_id = PortfolioId::new(alice.did, PortfolioKind::Default);
1163+
1164+
let auth_id = Identity::add_auth(
1165+
alice.did,
1166+
Signatory::from(bob.did),
1167+
AuthorizationData::PortfolioCustody(portfolio_id),
1168+
None,
1169+
)
1170+
.unwrap();
1171+
1172+
assert_noop!(
1173+
Portfolio::accept_portfolio_custody(bob.origin(), auth_id),
1174+
Error::DefaultPortfoliosCannotHaveCustodians
1175+
);
1176+
});
1177+
}

pallets/runtime/tests/src/staking/mod.rs

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -878,12 +878,7 @@ fn double_staking_should_fail() {
878878
Error::<Test>::NotController
879879
);
880880
// 2 = controller => nominating should work.
881-
assert_noop!(
882-
Staking::nominate(RuntimeOrigin::signed(2), vec![1]),
883-
Error::<Test>::StashIdentityDoesNotExist
884-
);
885-
provide_did_to_user(1);
886-
assert_ok!(Staking::nominate(RuntimeOrigin::signed(2), vec![1]));
881+
assert_ok!(Staking::nominate(RuntimeOrigin::signed(2), vec![1]),);
887882
});
888883
}
889884

@@ -6586,7 +6581,7 @@ fn create_on_offence_now(offender: u64) {
65866581
}
65876582

65886583
#[test]
6589-
fn add_nominator_with_invalid_expiry() {
6584+
fn add_nominator_without_cdd() {
65906585
ExtBuilder::default().nominate(true).build_and_execute(|| {
65916586
let alice_acc = 500;
65926587
let alice_controller_acc = 501;
@@ -6606,11 +6601,8 @@ fn add_nominator_with_invalid_expiry() {
66066601
));
66076602

66086603
set_timestamp(Utc::now().timestamp() as u64);
6609-
assert_noop!(
6610-
Staking::nominate(alice_controller_signed, vec![10, 20, 30]),
6611-
Error::<Test>::StashIdentityNotCDDed,
6612-
);
6613-
assert!(Staking::nominators(&alice_acc).is_none());
6604+
assert_ok!(Staking::nominate(alice_controller_signed, vec![10, 20, 30]),);
6605+
assert!(Staking::nominators(&alice_acc).is_some());
66146606
});
66156607
}
66166608

@@ -6681,18 +6673,6 @@ fn validate_nominators_with_valid_cdd() {
66816673

66826674
assert_ok!(Staking::nominate(eve_controller_signed, vec![11, 21, 31]));
66836675
assert!(!Staking::nominators(&eve_acc).is_none());
6684-
6685-
set_timestamp((Utc::now().timestamp() as u64) + 800_u64);
6686-
assert_ok!(Staking::validate_cdd_expiry_nominators(
6687-
RuntimeOrigin::root(),
6688-
vec![alice_acc.clone(), eve_acc.clone()]
6689-
));
6690-
assert!(Staking::nominators(&alice_acc).is_none());
6691-
assert!(!Staking::nominators(&eve_acc).is_none());
6692-
6693-
let ledger_data = Staking::ledger(&alice_controller_acc).unwrap();
6694-
assert_eq!(ledger_data.active, 0);
6695-
assert_eq!(ledger_data.unlocking.len(), 1);
66966676
});
66976677
}
66986678

pallets/staking/src/benchmarking.rs

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ type MaxNominators<T> = <<T as Config>::BenchmarkingConfig as BenchmarkingConfig
5151
// -----------------------------------------------------------------
5252

5353
use pallet_identity::benchmarking::{User, UserBuilder};
54-
use polymesh_primitives::identity_claim::ClaimType;
5554
use polymesh_primitives::{IdentityId, Permissions};
5655

5756
use crate::types::SlashingSwitch;
@@ -990,39 +989,6 @@ benchmarks! {
990989
});
991990
}
992991

993-
validate_cdd_expiry_nominators {
994-
let n in 1 .. T::MaxNominations::get();
995-
996-
clear_validators_and_nominators::<T>();
997-
998-
let (validator, nominators) = create_validator_with_nominators::<T>(
999-
n,
1000-
T::MaxNominatorRewardedPerValidator::get() as u32,
1001-
true,
1002-
RewardDestination::Controller,
1003-
Some(10_000_000)
1004-
)?;
1005-
1006-
for nominator in &nominators {
1007-
let claim_first = pallet_identity::Claim1stKey {
1008-
target: nominator.0.did(),
1009-
claim_type: ClaimType::CustomerDueDiligence
1010-
};
1011-
let _ = pallet_identity::Claims::<T>::clear_prefix(claim_first, 1, None);
1012-
}
1013-
1014-
let nominators: Vec<T::AccountId> = nominators.iter().map(|x| x.0.account()).collect();
1015-
for nominator in &nominators {
1016-
assert!(Nominators::<T>::contains_key(nominator));
1017-
}
1018-
1019-
}: _(RawOrigin::Root, nominators.clone())
1020-
verify {
1021-
for nominator in nominators {
1022-
assert!(!Nominators::<T>::contains_key(nominator));
1023-
}
1024-
}
1025-
1026992
// -----------------------------------------------------------------
1027993
}
1028994

pallets/staking/src/pallet/impls.rs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -890,17 +890,18 @@ impl<T: Config> Pallet<T> {
890890

891891
if let Some(Nominations { targets, .. }) = <Nominators<T>>::get(&voter) {
892892
nominators_seen.saturating_inc();
893-
if Self::is_nominator_compliant(&voter) {
894-
let voter_weight = weight_of(&voter);
895-
if !targets.is_empty() {
896-
all_voters.push((voter.clone(), voter_weight, targets));
897-
}
898-
min_active_stake = if voter_weight < min_active_stake {
893+
894+
let voter_weight = weight_of(&voter);
895+
if !targets.is_empty() {
896+
all_voters.push((voter.clone(), voter_weight, targets));
897+
}
898+
min_active_stake = {
899+
if voter_weight < min_active_stake {
899900
voter_weight
900901
} else {
901902
min_active_stake
902-
};
903-
}
903+
}
904+
};
904905
} else if Validators::<T>::contains_key(&voter) {
905906
validators_seen.saturating_inc();
906907
if Self::is_validator_compliant(&voter)
@@ -1117,18 +1118,6 @@ impl<T: Config> Pallet<T> {
11171118
})
11181119
}
11191120

1120-
/// Returns `true` if `who` has a valid cdd claim. Otherwise, returns `false`.
1121-
pub(crate) fn is_nominator_compliant(who: &T::AccountId) -> bool {
1122-
pallet_identity::Pallet::<T>::get_identity(who)
1123-
.map_or(false, |id| pallet_identity::Pallet::<T>::has_valid_cdd(id))
1124-
}
1125-
1126-
pub(crate) fn get_bonding_duration_period() -> u64 {
1127-
(T::SessionsPerEra::get() * T::BondingDuration::get()) as u64 // total session
1128-
* T::EpochDuration::get() // session length
1129-
* T::ExpectedBlockTime::get().saturated_into::<u64>()
1130-
}
1131-
11321121
/// Decrease the running count of validators by 1 for the stash identity.
11331122
pub(crate) fn release_running_validator(stash: &T::AccountId) {
11341123
if !<Validators<T>>::contains_key(stash) {

0 commit comments

Comments
 (0)