Skip to content

Commit 93a7870

Browse files
committed
chore: add pre-push hooks
1 parent 1cef134 commit 93a7870

File tree

11 files changed

+92
-66
lines changed

11 files changed

+92
-66
lines changed

flake.nix

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
pkgs.python310
5252
# Subxt CLI for metadata handling
5353
pkgs.subxt
54+
pkgs.cargo-nextest
5455
# # Code coverage tool
5556
# pkgs.cargo-llvm-cov # marked as broken
5657
];
@@ -59,7 +60,17 @@
5960
checks = pkgs.mkShell {
6061
pre-commit-check = pre-commit-hooks.lib.${system}.run {
6162
src = ./.;
62-
hooks = { rustfmt.enable = true; };
63+
hooks = {
64+
rustfmt.enable = true;
65+
66+
push = {
67+
enable = true;
68+
name = "Tests & Stuff";
69+
entry = "just test";
70+
pass_filenames = false;
71+
stages = ["pre-push"];
72+
};
73+
};
6374
};
6475
};
6576

justfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ build-testnet:
1010

1111
# Development
1212

13-
check:
13+
check: fmt
1414
cargo clippy --tests
1515

16-
test:
17-
cargo test
16+
test: check
17+
cargo nextest run
1818

1919
fmt:
2020
cargo fmt

pallets/faucet/tests/faucet.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ impl pallet_governance::Config for Test {
232232

233233
parameter_types! {
234234
pub const PermissionPalletId: PalletId = PalletId(*b"torusper");
235-
pub const MaxTargetsPerPermission: u32 = 100;
235+
pub const MaxRecipientsPerPermission: u32 = 100;
236236
pub const MaxStreamsPerPermission: u32 = 100;
237237
pub const MaxRevokersPerPermission: u32 = 10;
238238
pub const MaxControllersPerPermission: u32 = 10;
@@ -250,7 +250,7 @@ impl pallet_permission0::Config for Test {
250250

251251
type Torus = Torus0;
252252

253-
type MaxTargetsPerPermission = MaxTargetsPerPermission;
253+
type MaxRecipientsPerPermission = MaxRecipientsPerPermission;
254254

255255
type MaxStreamsPerPermission = MaxStreamsPerPermission;
256256

pallets/permission0/src/ext/emission_impl.rs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use polkadot_sdk::{
1717
polkadot_sdk_frame::prelude::{BlockNumberFor, OriginFor},
1818
sp_core::{Get, TryCollect},
1919
sp_runtime::{
20-
BoundedBTreeMap, DispatchError, Percent, Vec,
20+
BoundedBTreeMap, BoundedBTreeSet, DispatchError, Percent, Vec,
2121
traits::{CheckedAdd, Saturating, Zero},
2222
},
2323
};
@@ -113,7 +113,7 @@ impl<T: Config>
113113
#[allow(clippy::too_many_arguments)]
114114
pub(crate) fn delegate_emission_permission_impl<T: Config>(
115115
delegator: T::AccountId,
116-
recipients: BoundedBTreeMap<T::AccountId, u16, T::MaxTargetsPerPermission>,
116+
recipients: BoundedBTreeMap<T::AccountId, u16, T::MaxRecipientsPerPermission>,
117117
allocation: EmissionAllocation<T>,
118118
distribution: DistributionControl<T>,
119119
duration: PermissionDuration<T>,
@@ -160,8 +160,8 @@ pub(crate) fn delegate_emission_permission_impl<T: Config>(
160160
allocation: allocation.clone(),
161161
distribution,
162162
accumulating: true, // Start with accumulation enabled by default
163-
recipient_manager,
164-
weight_setter,
163+
recipient_managers: validate_emission_managers::<T>(&delegator, recipient_manager)?,
164+
weight_setters: validate_emission_managers::<T>(&delegator, weight_setter)?,
165165
});
166166

167167
let permission_id = generate_permission_id::<T>(&delegator, &scope)?;
@@ -317,7 +317,7 @@ pub fn toggle_permission_accumulation_impl<T: Config>(
317317
pub(crate) fn update_emission_permission<T: Config>(
318318
origin: OriginFor<T>,
319319
permission_id: PermissionId,
320-
new_recipients: Option<BoundedBTreeMap<T::AccountId, u16, T::MaxTargetsPerPermission>>,
320+
new_recipients: Option<BoundedBTreeMap<T::AccountId, u16, T::MaxRecipientsPerPermission>>,
321321
new_streams: Option<BoundedBTreeMap<StreamId, Percent, T::MaxStreamsPerPermission>>,
322322
new_distribution_control: Option<DistributionControl<T>>,
323323
new_recipient_manager: Option<Option<T::AccountId>>,
@@ -338,16 +338,8 @@ pub(crate) fn update_emission_permission<T: Config>(
338338
};
339339

340340
let allowed_delegator = permission.delegator == caller;
341-
let allowed_weights = allowed_delegator
342-
|| scope
343-
.weight_setter
344-
.as_ref()
345-
.is_some_and(|weight_setter| weight_setter == &caller);
346-
let allowed_recipients = allowed_delegator
347-
|| scope
348-
.recipient_manager
349-
.as_ref()
350-
.is_some_and(|recipient_manager| recipient_manager == &caller);
341+
let allowed_weights = allowed_delegator || scope.weight_setters.contains(&caller);
342+
let allowed_recipients = allowed_delegator || scope.recipient_managers.contains(&caller);
351343

352344
if !allowed_delegator && !allowed_weights && !allowed_recipients {
353345
return Err(Error::<T>::NotAuthorizedToEdit.into());
@@ -421,12 +413,14 @@ pub(crate) fn update_emission_permission<T: Config>(
421413

422414
if let Some(new_recipient_manager) = new_recipient_manager {
423415
ensure!(allowed_delegator, Error::<T>::NotAuthorizedToEdit);
424-
scope.recipient_manager = new_recipient_manager;
416+
scope.recipient_managers =
417+
validate_emission_managers::<T>(&permission.delegator, new_recipient_manager)?;
425418
}
426419

427420
if let Some(new_weight_setter) = new_weight_setter {
428421
ensure!(allowed_delegator, Error::<T>::NotAuthorizedToEdit);
429-
scope.weight_setter = new_weight_setter;
422+
scope.weight_setters =
423+
validate_emission_managers::<T>(&permission.delegator, new_weight_setter)?;
430424
}
431425

432426
permission.scope = PermissionScope::Emission(scope);
@@ -435,9 +429,21 @@ pub(crate) fn update_emission_permission<T: Config>(
435429
Ok(())
436430
}
437431

432+
fn validate_emission_managers<T: Config>(
433+
delegator: &T::AccountId,
434+
entry: Option<T::AccountId>,
435+
) -> Result<BoundedBTreeSet<T::AccountId, T::MaxControllersPerPermission>, DispatchError> {
436+
let mut set = BoundedBTreeSet::new();
437+
let _ = set.try_insert(delegator.clone());
438+
if let Some(entry) = entry {
439+
let _ = set.try_insert(entry);
440+
}
441+
Ok(set)
442+
}
443+
438444
fn validate_emission_permission_recipients<T: Config>(
439445
delegator: &T::AccountId,
440-
recipients: &BoundedBTreeMap<T::AccountId, u16, T::MaxTargetsPerPermission>,
446+
recipients: &BoundedBTreeMap<T::AccountId, u16, T::MaxRecipientsPerPermission>,
441447
) -> DispatchResult {
442448
ensure!(!recipients.is_empty(), Error::<T>::NoTargetsSpecified);
443449

@@ -452,6 +458,7 @@ fn validate_emission_permission_recipients<T: Config>(
452458

453459
Ok(())
454460
}
461+
455462
fn validate_emission_permission_streams<T: Config>(
456463
streams: &BoundedBTreeMap<StreamId, Percent, T::MaxStreamsPerPermission>,
457464
delegator: &T::AccountId,

pallets/permission0/src/lib.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub mod pallet {
6666

6767
/// Maximum number of targets per permission.
6868
#[pallet::constant]
69-
type MaxTargetsPerPermission: Get<u32>;
69+
type MaxRecipientsPerPermission: Get<u32>;
7070

7171
/// Maximum number of delegated streams per permission.
7272
#[pallet::constant]
@@ -112,7 +112,7 @@ pub mod pallet {
112112
_,
113113
Identity,
114114
(T::AccountId, T::AccountId),
115-
BoundedBTreeSet<PermissionId, T::MaxTargetsPerPermission>,
115+
BoundedBTreeSet<PermissionId, T::MaxRecipientsPerPermission>,
116116
ValueQuery,
117117
>;
118118

@@ -122,7 +122,7 @@ pub mod pallet {
122122
_,
123123
Identity,
124124
T::AccountId,
125-
BoundedBTreeSet<PermissionId, T::MaxTargetsPerPermission>,
125+
BoundedBTreeSet<PermissionId, T::MaxRecipientsPerPermission>,
126126
ValueQuery,
127127
>;
128128

@@ -132,7 +132,7 @@ pub mod pallet {
132132
_,
133133
Identity,
134134
T::AccountId,
135-
BoundedBTreeSet<PermissionId, T::MaxTargetsPerPermission>,
135+
BoundedBTreeSet<PermissionId, T::MaxRecipientsPerPermission>,
136136
ValueQuery,
137137
>;
138138

@@ -316,6 +316,8 @@ pub mod pallet {
316316
NotEnoughInstances,
317317
/// Too many children for a permission.
318318
TooManyChildren,
319+
/// Emission managers must have up to two entries and always contain the delegator,
320+
InvalidEmissionManagers,
319321
/// Revocation terms are too strong for a permission re-delegation.
320322
RevocationTermsTooStrong,
321323
/// Too many curator permissions being delegated in a single permission.
@@ -338,7 +340,7 @@ pub mod pallet {
338340
#[pallet::weight(T::WeightInfo::delegate_emission_permission())]
339341
pub fn delegate_emission_permission(
340342
origin: OriginFor<T>,
341-
recipients: BoundedBTreeMap<T::AccountId, u16, T::MaxTargetsPerPermission>,
343+
recipients: BoundedBTreeMap<T::AccountId, u16, T::MaxRecipientsPerPermission>,
342344
allocation: EmissionAllocation<T>,
343345
distribution: DistributionControl<T>,
344346
duration: PermissionDuration<T>,
@@ -491,7 +493,9 @@ pub mod pallet {
491493
pub fn update_emission_permission(
492494
origin: OriginFor<T>,
493495
permission_id: PermissionId,
494-
new_recipients: Option<BoundedBTreeMap<T::AccountId, u16, T::MaxTargetsPerPermission>>,
496+
new_recipients: Option<
497+
BoundedBTreeMap<T::AccountId, u16, T::MaxRecipientsPerPermission>,
498+
>,
495499
new_streams: Option<BoundedBTreeMap<StreamId, Percent, T::MaxStreamsPerPermission>>,
496500
new_distribution_control: Option<DistributionControl<T>>,
497501
new_recipient_manager: Option<Option<T::AccountId>>,

pallets/permission0/src/migrations.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
pub mod v6 {
22
use polkadot_sdk::{
33
frame_support::{migrations::VersionedMigration, traits::UncheckedOnRuntimeUpgrade},
4+
sp_runtime::BoundedBTreeSet,
45
sp_tracing::{error, info, warn},
56
sp_weights::Weight,
67
};
@@ -36,7 +37,7 @@ pub mod v6 {
3637
pub struct OldEmissionScope<T: Config> {
3738
pub allocation: EmissionAllocation<T>,
3839
pub distribution: DistributionControl<T>,
39-
pub targets: BoundedBTreeMap<T::AccountId, u16, T::MaxTargetsPerPermission>,
40+
pub targets: BoundedBTreeMap<T::AccountId, u16, T::MaxRecipientsPerPermission>,
4041
pub accumulating: bool,
4142
}
4243

@@ -113,14 +114,17 @@ pub mod v6 {
113114
permission_id,
114115
);
115116

117+
let mut managers = BoundedBTreeSet::new();
118+
let _ = managers.try_insert(old_contract.delegator.clone());
119+
116120
let new_emission = EmissionScope::<T> {
117121
recipients: old_emission.targets, // Field renamed from targets to recipients
118122
allocation: old_emission.allocation,
119123
distribution: old_emission.distribution,
120124
accumulating: old_emission.accumulating,
121125
// New manager fields introduced in v6
122-
recipient_manager: None,
123-
weight_setter: None,
126+
recipient_managers: managers.clone(),
127+
weight_setters: managers,
124128
};
125129

126130
// Add new indices for all emission targets

pallets/permission0/src/permission/emission.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub type StreamId = H256;
1717
#[scale_info(skip_type_params(T))]
1818
pub struct EmissionScope<T: Config> {
1919
/// Recipients of the emissions and its weights
20-
pub recipients: BoundedBTreeMap<T::AccountId, u16, T::MaxTargetsPerPermission>,
20+
pub recipients: BoundedBTreeMap<T::AccountId, u16, T::MaxRecipientsPerPermission>,
2121
/// What portion of emissions this permission applies to
2222
pub allocation: EmissionAllocation<T>,
2323
/// Distribution control parameters
@@ -26,10 +26,10 @@ pub struct EmissionScope<T: Config> {
2626
pub accumulating: bool,
2727
/// An account responsible for managing the recipients to this permission's streams.
2828
/// If left empty, the delegator will be
29-
pub recipient_manager: Option<T::AccountId>,
29+
pub recipient_managers: BoundedBTreeSet<T::AccountId, T::MaxControllersPerPermission>,
3030
/// An account responsible for updating the weights of existing recipients. Useful
3131
/// for third-party agents to manage how the streams will be distributed.
32-
pub weight_setter: Option<T::AccountId>,
32+
pub weight_setters: BoundedBTreeSet<T::AccountId, T::MaxControllersPerPermission>,
3333
}
3434

3535
impl<T: Config> EmissionScope<T> {

pallets/permission0/tests/namespace.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn get_last_delegated_permission_id(delegator: AccountId) -> PermissionId {
3636
None
3737
}
3838
})
39-
.last() // Get most recent
39+
.next_back() // Get most recent
4040
.expect("No PermissionDelegated event found")
4141
}
4242

@@ -80,7 +80,7 @@ fn get_permission_id_for_recipient(recipient: AccountId) -> PermissionId {
8080
_ => continue,
8181
}
8282
}
83-
panic!("No permission found for recipient: {:?}", recipient);
83+
panic!("No permission found for recipient: {recipient:?}");
8484
}
8585

8686
fn register_agent(id: AccountId) {

0 commit comments

Comments
 (0)