Skip to content

Commit

Permalink
migrate pallet-node-authorization to use umbrella crate (#7040)
Browse files Browse the repository at this point in the history
# Description

Migrate pallet-node-authorization to use umbrella crate. Part of #6504 

## Review Notes

* This PR migrates pallet-node-authorization to use the umbrella crate.
* Some imports like below have not been added to any prelude as they
have very limited usage across the various pallets.
```rust
use sp_core::OpaquePeerId as PeerId;
```
* Added a commonly used runtime trait for testing in the
`testing_prelude` in `substrate/frame/src/lib.rs`:
```rust
pub use sp_runtime::traits::BadOrigin;
```
* `weights.rs` uses the `weights_prelude` like:
```rust
use frame::weights_prelude::*;
```
* `tests.rs` and `mock.rs` use the `testing_prelude`:
```rust
use frame::testing_prelude::*;
```
* `lib.rs` uses the main `prelude` like:
```rust
use frame::prelude::*;
```
* For testing: Checked that local build works and tests run
successfully.
  • Loading branch information
UtkarshBhardwaj007 authored Jan 7, 2025
1 parent 1059be7 commit d2c157a
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 34 deletions.
6 changes: 1 addition & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions prdoc/pr_7040.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json

title: '[pallet-node-authorization] Migrate to using frame umbrella crate'

doc:
- audience: Runtime Dev
description: This PR migrates the pallet-node-authorization to use the frame umbrella crate. This
is part of the ongoing effort to migrate all pallets to use the frame umbrella crate.
The effort is tracked [here](https://github.com/paritytech/polkadot-sdk/issues/6504).

crates:
- name: pallet-node-authorization
bump: minor
- name: polkadot-sdk-frame
bump: minor
16 changes: 3 additions & 13 deletions substrate/frame/node-authorization/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,18 @@ targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { features = ["derive"], workspace = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
frame = { workspace = true, features = ["experimental", "runtime"] }
log = { workspace = true }
scale-info = { features = ["derive"], workspace = true }
sp-core = { workspace = true }
sp-io = { workspace = true }
sp-runtime = { workspace = true }

[features]
default = ["std"]
std = [
"codec/std",
"frame-support/std",
"frame-system/std",
"frame/std",
"log/std",
"scale-info/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"sp-runtime/try-runtime",
"frame/try-runtime",
]
14 changes: 7 additions & 7 deletions substrate/frame/node-authorization/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@ pub mod weights;
extern crate alloc;

use alloc::{collections::btree_set::BTreeSet, vec::Vec};
use frame::{
deps::{sp_core::OpaquePeerId as PeerId, sp_io},
prelude::*,
};
pub use pallet::*;
use sp_core::OpaquePeerId as PeerId;
use sp_runtime::traits::StaticLookup;
pub use weights::WeightInfo;

type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source;

#[frame_support::pallet]
#[frame::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;

#[pallet::pallet]
#[pallet::without_storage_info]
Expand Down Expand Up @@ -111,7 +111,7 @@ pub mod pallet {
StorageMap<_, Blake2_128Concat, PeerId, BTreeSet<PeerId>, ValueQuery>;

#[pallet::genesis_config]
#[derive(frame_support::DefaultNoBound)]
#[derive(DefaultNoBound)]
pub struct GenesisConfig<T: Config> {
pub nodes: Vec<(PeerId, T::AccountId)>,
}
Expand Down Expand Up @@ -171,7 +171,7 @@ pub mod pallet {
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
/// Set reserved node every block. It may not be enabled depends on the offchain
/// worker settings when starting the node.
fn offchain_worker(now: frame_system::pallet_prelude::BlockNumberFor<T>) {
fn offchain_worker(now: BlockNumberFor<T>) {
let network_state = sp_io::offchain::network_state();
match network_state {
Err(_) => log::error!(
Expand Down
8 changes: 3 additions & 5 deletions substrate/frame/node-authorization/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@
use super::*;
use crate as pallet_node_authorization;

use frame_support::{derive_impl, ord_parameter_types, traits::ConstU32};
use frame_system::EnsureSignedBy;
use sp_runtime::BuildStorage;
use frame::testing_prelude::*;

type Block = frame_system::mocking::MockBlock<Test>;

frame_support::construct_runtime!(
construct_runtime!(
pub enum Test
{
System: frame_system,
Expand Down Expand Up @@ -61,7 +59,7 @@ pub fn test_node(id: u8) -> PeerId {
PeerId(vec![id])
}

pub fn new_test_ext() -> sp_io::TestExternalities {
pub fn new_test_ext() -> TestState {
let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
pallet_node_authorization::GenesisConfig::<Test> {
nodes: vec![(test_node(10), 10), (test_node(20), 20), (test_node(30), 30)],
Expand Down
3 changes: 1 addition & 2 deletions substrate/frame/node-authorization/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
use super::*;
use crate::mock::*;
use frame_support::{assert_noop, assert_ok};
use sp_runtime::traits::BadOrigin;
use frame::testing_prelude::*;

#[test]
fn add_well_known_node_works() {
Expand Down
3 changes: 1 addition & 2 deletions substrate/frame/node-authorization/src/weights.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions substrate/frame/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ pub mod testing_prelude {
pub use sp_io::TestExternalities;

pub use sp_io::TestExternalities as TestState;

/// Commonly used runtime traits for testing.
pub use sp_runtime::traits::BadOrigin;
}

/// All of the types and tools needed to build FRAME-based runtimes.
Expand Down

0 comments on commit d2c157a

Please sign in to comment.