Skip to content

Commit 4d4c563

Browse files
chore(permission0): allow empty recipients (#137)
Allows creating stream permissions with empty recipients when the permission is revokable. This allows the specified manager fields to handle the filling of the recipients. Closes CHAIN-126.
2 parents da51c0d + 4d5d9ea commit 4d4c563

File tree

3 files changed

+72
-17
lines changed

3 files changed

+72
-17
lines changed

pallets/permission0/src/ext/stream_impl.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ pub(crate) fn delegate_stream_permission_impl<T: Config>(
129129
Error::<T>::NotRegisteredAgent
130130
);
131131

132-
validate_stream_permission_recipients::<T>(&delegator, &recipients)?;
132+
validate_stream_permission_recipients::<T>(&delegator, &revocation, &recipients)?;
133133

134134
match &allocation {
135135
StreamAllocation::Streams(streams) => {
@@ -355,7 +355,11 @@ pub(crate) fn update_stream_permission<T: Config>(
355355
return Err(Error::<T>::NotAuthorizedToEdit.into());
356356
}
357357

358-
validate_stream_permission_recipients::<T>(&permission.delegator, &new_recipients)?;
358+
validate_stream_permission_recipients::<T>(
359+
&permission.delegator,
360+
&permission.revocation,
361+
&new_recipients,
362+
)?;
359363

360364
// Remove old indices for current recipients
361365
crate::permission::remove_permission_from_indices::<T>(
@@ -444,9 +448,12 @@ fn validate_stream_managers<T: Config>(
444448

445449
fn validate_stream_permission_recipients<T: Config>(
446450
delegator: &T::AccountId,
451+
revocation: &RevocationTerms<T>,
447452
recipients: &BoundedBTreeMap<T::AccountId, u16, T::MaxRecipientsPerPermission>,
448453
) -> DispatchResult {
449-
ensure!(!recipients.is_empty(), Error::<T>::NoRecipientsSpecified);
454+
if !revocation.is_revokable() {
455+
ensure!(!recipients.is_empty(), Error::<T>::NoRecipientsSpecified);
456+
}
450457

451458
for (recipient, weight) in recipients {
452459
ensure!(delegator != recipient, Error::<T>::InvalidRecipientWeight);

pallets/permission0/src/permission.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -350,13 +350,7 @@ impl<T: Config> PermissionContract<T> {
350350
}
351351

352352
pub fn is_updatable(&self) -> bool {
353-
let current_block = frame_system::Pallet::<T>::block_number();
354-
355-
match &self.revocation {
356-
RevocationTerms::RevocableByDelegator => true,
357-
RevocationTerms::RevocableAfter(block) => &current_block > block,
358-
_ => false,
359-
}
353+
self.revocation.is_revokable()
360354
}
361355
}
362356

@@ -428,6 +422,18 @@ pub enum RevocationTerms<T: Config> {
428422
}
429423

430424
impl<T: Config> RevocationTerms<T> {
425+
/// Returns true if this revocation term is revocable by the delegator
426+
/// or the current block is past the one defined.
427+
pub fn is_revokable(&self) -> bool {
428+
let current_block = frame_system::Pallet::<T>::block_number();
429+
430+
match &self {
431+
RevocationTerms::RevocableByDelegator => true,
432+
RevocationTerms::RevocableAfter(block) => &current_block > block,
433+
_ => false,
434+
}
435+
}
436+
431437
/// Checks if the child revocation terms are weaker than the parent.
432438
pub fn is_weaker(parent: &Self, child: &Self) -> bool {
433439
match (parent, child) {

pallets/permission0/tests/stream.rs

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,22 +1395,64 @@ fn index_consistency_during_complex_recipient_updates() {
13951395
assert!(!participants_2.contains(&permission_id)); // Removed
13961396
assert!(participants_3.contains(&permission_id)); // Still there
13971397
assert!(participants_4.contains(&permission_id)); // Added
1398+
});
1399+
}
13981400

1399-
// Test complete removal of all recipients should delete permission
1400-
let empty_recipients = BoundedBTreeMap::new();
1401+
#[test]
1402+
fn cannot_create_empty_recipients_for_irrevocable_permissions() {
1403+
new_test_ext().execute_with(|| {
1404+
zero_min_burn();
1405+
let agent_0 = 0;
1406+
register_empty_agent(agent_0);
1407+
1408+
add_balance(agent_0, as_tors(10) + 1);
1409+
1410+
let stream_id = generate_root_stream_id(&agent_0);
1411+
let mut streams = BTreeMap::new();
1412+
streams.insert(stream_id, Percent::from_percent(50));
14011413

1402-
// This should fail as permissions need at least one recipient
14031414
assert_err!(
1404-
pallet_permission0::Pallet::<Test>::update_stream_permission(
1415+
pallet_permission0::Pallet::<Test>::delegate_stream_permission(
14051416
get_origin(agent_0),
1406-
permission_id,
1407-
Some(empty_recipients),
1417+
Default::default(),
1418+
pallet_permission0::StreamAllocation::Streams(streams.clone().try_into().unwrap()),
1419+
pallet_permission0::DistributionControl::Manual,
1420+
pallet_permission0::PermissionDuration::Indefinite,
1421+
pallet_permission0::RevocationTerms::Irrevocable,
1422+
pallet_permission0::EnforcementAuthority::None,
14081423
None,
14091424
None,
1425+
),
1426+
pallet_permission0::Error::<Test>::NoRecipientsSpecified
1427+
);
1428+
1429+
assert_err!(
1430+
pallet_permission0::Pallet::<Test>::delegate_stream_permission(
1431+
get_origin(agent_0),
1432+
Default::default(),
1433+
pallet_permission0::StreamAllocation::Streams(streams.clone().try_into().unwrap()),
1434+
pallet_permission0::DistributionControl::Manual,
1435+
pallet_permission0::PermissionDuration::Indefinite,
1436+
pallet_permission0::RevocationTerms::RevocableAfter(10),
1437+
pallet_permission0::EnforcementAuthority::None,
1438+
None,
14101439
None,
1411-
None
14121440
),
14131441
pallet_permission0::Error::<Test>::NoRecipientsSpecified
14141442
);
1443+
1444+
assert_ok!(
1445+
pallet_permission0::Pallet::<Test>::delegate_stream_permission(
1446+
get_origin(agent_0),
1447+
Default::default(),
1448+
pallet_permission0::StreamAllocation::Streams(streams.try_into().unwrap()),
1449+
pallet_permission0::DistributionControl::Manual,
1450+
pallet_permission0::PermissionDuration::Indefinite,
1451+
pallet_permission0::RevocationTerms::RevocableByDelegator,
1452+
pallet_permission0::EnforcementAuthority::None,
1453+
None,
1454+
None,
1455+
)
1456+
);
14151457
});
14161458
}

0 commit comments

Comments
 (0)