Skip to content

Commit a3d2498

Browse files
committed
refactor: Consolidate V3 opt-out metadata events and fix the order of new storage, events and errors declarations
1 parent c0243d7 commit a3d2498

4 files changed

Lines changed: 50 additions & 48 deletions

File tree

substrate-node/pallets/pallet-tfgrid/src/benchmarking.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,7 @@ benchmarks! {
873873
verify {
874874
let stored = TfgridModule::<T>::node_v3_opt_out_metadata(node_id).unwrap();
875875
assert_eq!(stored.to_vec(), metadata);
876-
assert_has_event::<T>(Event::NodeV3OptOutMetadataSet { node_id, metadata }.into());
876+
assert_has_event::<T>(Event::NodeV3OptOutMetadataUpdated { node_id, metadata: Some(metadata) }.into());
877877
}
878878

879879
// add_twin_admin()

substrate-node/pallets/pallet-tfgrid/src/lib.rs

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -254,26 +254,6 @@ pub mod pallet {
254254
#[pallet::getter(fn zos_version)]
255255
pub type ZosVersion<T> = StorageValue<_, Vec<u8>, ValueQuery>;
256256

257-
// Keyed by node_id. Present = node has opted out of v3 billing.
258-
#[pallet::storage]
259-
#[pallet::getter(fn node_v3_billing_opt_out)]
260-
pub type NodeV3BillingOptOut<T> =
261-
StorageMap<_, Blake2_128Concat, u32, u64, OptionQuery>;
262-
263-
// Keyed by node_id. Stores optional metadata for opted-out nodes (e.g. v4 account).
264-
// Max 256 bytes of arbitrary UTF-8 content. Node must be opted out before metadata can be set.
265-
#[pallet::storage]
266-
#[pallet::getter(fn node_v3_opt_out_metadata)]
267-
pub type NodeV3OptOutMetadata<T> =
268-
StorageMap<_, Blake2_128Concat, u32, BoundedVec<u8, ConstU32<256>>, OptionQuery>;
269-
270-
// Global list of accounts authorized to deploy on opted-out nodes.
271-
// None = list not initialized (treat as empty = no one allowed).
272-
// Bounded to prevent unbounded storage growth.
273-
#[pallet::storage]
274-
#[pallet::getter(fn allowed_twin_admins)]
275-
pub type AllowedTwinAdmins<T: Config> =
276-
StorageValue<_, BoundedVec<T::AccountId, T::MaxTwinAdmins>, OptionQuery>;
277257

278258
// This storage map maps a node ID to a power state, they node can modify this state
279259
// to indicate that it has shut down or came back alive
@@ -312,6 +292,27 @@ pub mod pallet {
312292
pub type PendingTransferByTwin<T: Config> =
313293
StorageMap<_, Blake2_128Concat, u32, u64, OptionQuery>;
314294

295+
// Keyed by node_id. Present = node has opted out of v3 billing.
296+
#[pallet::storage]
297+
#[pallet::getter(fn node_v3_billing_opt_out)]
298+
pub type NodeV3BillingOptOut<T> =
299+
StorageMap<_, Blake2_128Concat, u32, u64, OptionQuery>;
300+
301+
// Keyed by node_id. Stores optional metadata for opted-out nodes (e.g. v4 account).
302+
// Max 256 bytes of arbitrary UTF-8 content. Node must be opted out before metadata can be set.
303+
#[pallet::storage]
304+
#[pallet::getter(fn node_v3_opt_out_metadata)]
305+
pub type NodeV3OptOutMetadata<T> =
306+
StorageMap<_, Blake2_128Concat, u32, BoundedVec<u8, ConstU32<256>>, OptionQuery>;
307+
308+
// Global list of accounts authorized to deploy on opted-out nodes.
309+
// None = list not initialized (treat as empty = no one allowed).
310+
// Bounded to prevent unbounded storage growth.
311+
#[pallet::storage]
312+
#[pallet::getter(fn allowed_twin_admins)]
313+
pub type AllowedTwinAdmins<T: Config> =
314+
StorageValue<_, BoundedVec<T::AccountId, T::MaxTwinAdmins>, OptionQuery>;
315+
315316
#[pallet::config]
316317
pub trait Config: frame_system::Config + pallet_timestamp::Config {
317318
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
@@ -494,12 +495,6 @@ pub mod pallet {
494495
power_state: PowerState<BlockNumberFor<T>>,
495496
},
496497

497-
// V3 billing opt-out
498-
NodeV3BillingOptedOut { node_id: u32, opted_out_at: u64 },
499-
NodeV3OptOutMetadataSet { node_id: u32, metadata: Vec<u8> },
500-
NodeV3OptOutMetadataCleared { node_id: u32 },
501-
TwinAdminAdded(T::AccountId),
502-
TwinAdminRemoved(T::AccountId),
503498

504499
// Twin ownership transfer lifecycle
505500
TwinTransferRequested {
@@ -520,6 +515,12 @@ pub mod pallet {
520515
from: T::AccountId,
521516
to: T::AccountId,
522517
},
518+
519+
// V3 billing opt-out
520+
NodeV3BillingOptedOut { node_id: u32, opted_out_at: u64 },
521+
TwinAdminAdded(T::AccountId),
522+
TwinAdminRemoved(T::AccountId),
523+
NodeV3OptOutMetadataUpdated { node_id: u32, metadata: Option<Vec<u8>> },
523524
}
524525

525526
#[pallet::error]
@@ -664,11 +665,11 @@ pub mod pallet {
664665

665666
// V3 billing opt-out errors
666667
NodeV3BillingOptOutAlreadyEnabled,
667-
NodeNotOptedOutOfV3Billing,
668-
NodeV3OptOutMetadataTooLong,
669668
AlreadyTwinAdmin,
670669
NotTwinAdmin,
671670
TwinAdminListFull,
671+
NodeNotOptedOutOfV3Billing,
672+
NodeV3OptOutMetadataTooLong,
672673
}
673674

674675
#[pallet::genesis_config]
@@ -1375,20 +1376,6 @@ pub mod pallet {
13751376
Self::_opt_out_of_v3_billing(account_id, node_id)
13761377
}
13771378

1378-
// Farmer sets metadata for an opted-out node (e.g. v4 account address).
1379-
// Node must already be opted out. Caller must be the farm owner.
1380-
// Pass empty metadata to clear.
1381-
#[pallet::call_index(46)]
1382-
#[pallet::weight(<T as Config>::WeightInfo::set_node_v3_opt_out_metadata())]
1383-
pub fn set_node_v3_opt_out_metadata(
1384-
origin: OriginFor<T>,
1385-
node_id: u32,
1386-
metadata: Vec<u8>,
1387-
) -> DispatchResultWithPostInfo {
1388-
let account_id = ensure_signed(origin)?;
1389-
Self::_set_node_v3_opt_out_metadata(account_id, node_id, metadata)
1390-
}
1391-
13921379
// Council adds an account to the twin admin list (allowed to deploy on opted-out nodes)
13931380
#[pallet::call_index(44)]
13941381
#[pallet::weight(<T as Config>::WeightInfo::add_twin_admin())]
@@ -1410,5 +1397,19 @@ pub mod pallet {
14101397
T::RestrictedOrigin::ensure_origin(origin)?;
14111398
Self::_remove_twin_admin(account)
14121399
}
1400+
1401+
// Farmer sets metadata for an opted-out node (e.g. v4 account address).
1402+
// Node must already be opted out. Caller must be the farm owner.
1403+
// Pass empty metadata to clear.
1404+
#[pallet::call_index(46)]
1405+
#[pallet::weight(<T as Config>::WeightInfo::set_node_v3_opt_out_metadata())]
1406+
pub fn set_node_v3_opt_out_metadata(
1407+
origin: OriginFor<T>,
1408+
node_id: u32,
1409+
metadata: Vec<u8>,
1410+
) -> DispatchResultWithPostInfo {
1411+
let account_id = ensure_signed(origin)?;
1412+
Self::_set_node_v3_opt_out_metadata(account_id, node_id, metadata)
1413+
}
14131414
}
14141415
}

substrate-node/pallets/pallet-tfgrid/src/tests.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2948,9 +2948,9 @@ fn test_set_node_v3_opt_out_metadata_works() {
29482948
let our_events = System::events();
29492949
assert!(our_events.iter().any(|e| matches!(
29502950
&e.event,
2951-
MockEvent::TfgridModule(TfgridEvent::<TestRuntime>::NodeV3OptOutMetadataSet {
2951+
MockEvent::TfgridModule(TfgridEvent::<TestRuntime>::NodeV3OptOutMetadataUpdated {
29522952
node_id: 1,
2953-
..
2953+
metadata: Some(_),
29542954
})
29552955
)));
29562956
});
@@ -2988,8 +2988,9 @@ fn test_set_node_v3_opt_out_metadata_clear_works() {
29882988
let our_events = System::events();
29892989
assert!(our_events.iter().any(|e| matches!(
29902990
&e.event,
2991-
MockEvent::TfgridModule(TfgridEvent::<TestRuntime>::NodeV3OptOutMetadataCleared {
2991+
MockEvent::TfgridModule(TfgridEvent::<TestRuntime>::NodeV3OptOutMetadataUpdated {
29922992
node_id: 1,
2993+
metadata: None,
29932994
})
29942995
)));
29952996
});

substrate-node/pallets/pallet-tfgrid/src/v3_billing_opt_out.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ impl<T: Config> crate::Pallet<T> {
6666

6767
if metadata.is_empty() {
6868
NodeV3OptOutMetadata::<T>::remove(node_id);
69-
Self::deposit_event(Event::NodeV3OptOutMetadataCleared { node_id });
69+
Self::deposit_event(Event::NodeV3OptOutMetadataUpdated { node_id, metadata: None });
7070
} else {
7171
let bounded: BoundedVec<u8, frame_support::traits::ConstU32<256>> =
7272
metadata.clone().try_into().map_err(|_| Error::<T>::NodeV3OptOutMetadataTooLong)?;
7373
NodeV3OptOutMetadata::<T>::insert(node_id, bounded);
74-
Self::deposit_event(Event::NodeV3OptOutMetadataSet { node_id, metadata });
74+
Self::deposit_event(Event::NodeV3OptOutMetadataUpdated { node_id, metadata: Some(metadata) });
7575
}
7676

7777
Ok(().into())

0 commit comments

Comments
 (0)