Skip to content

Commit 7b3c391

Browse files
committed
Apply cargo fmt
1 parent 2ec31d0 commit 7b3c391

File tree

17 files changed

+1256
-1206
lines changed

17 files changed

+1256
-1206
lines changed

.snippets/code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallets/custom-pallet/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,4 @@ pub mod pallet {
214214
Ok(())
215215
}
216216
}
217-
}
217+
}

.snippets/code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallets/custom-pallet/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
// SOFTWARE.
1919

2020
use crate::{mock::*, Error, Event, UserInteractions};
21-
use frame::testing_prelude::*;
2221
use frame::deps::sp_runtime;
22+
use frame::testing_prelude::*;
2323

2424
// Verify root can successfully set counter value
2525
#[test]

.snippets/code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallets/template/src/benchmarking.rs

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,37 @@ use frame::{deps::frame_benchmarking::v2::*, prelude::*};
55

66
#[benchmarks]
77
mod benchmarks {
8-
use super::*;
9-
#[cfg(test)]
10-
use crate::pallet::Pallet as Template;
11-
use frame_system::RawOrigin;
8+
use super::*;
9+
#[cfg(test)]
10+
use crate::pallet::Pallet as Template;
11+
use frame_system::RawOrigin;
1212

13-
#[benchmark]
14-
fn do_something() {
15-
let caller: T::AccountId = whitelisted_caller();
16-
#[extrinsic_call]
17-
do_something(RawOrigin::Signed(caller), 100);
13+
#[benchmark]
14+
fn do_something() {
15+
let caller: T::AccountId = whitelisted_caller();
16+
#[extrinsic_call]
17+
do_something(RawOrigin::Signed(caller), 100);
1818

19-
assert_eq!(Something::<T>::get().map(|v| v.block_number), Some(100u32.into()));
20-
}
19+
assert_eq!(
20+
Something::<T>::get().map(|v| v.block_number),
21+
Some(100u32.into())
22+
);
23+
}
2124

22-
#[benchmark]
23-
fn cause_error() {
24-
Something::<T>::put(CompositeStruct { block_number: 100u32.into() });
25-
let caller: T::AccountId = whitelisted_caller();
26-
#[extrinsic_call]
27-
cause_error(RawOrigin::Signed(caller));
25+
#[benchmark]
26+
fn cause_error() {
27+
Something::<T>::put(CompositeStruct {
28+
block_number: 100u32.into(),
29+
});
30+
let caller: T::AccountId = whitelisted_caller();
31+
#[extrinsic_call]
32+
cause_error(RawOrigin::Signed(caller));
2833

29-
assert_eq!(Something::<T>::get().map(|v| v.block_number), Some(101u32.into()));
30-
}
34+
assert_eq!(
35+
Something::<T>::get().map(|v| v.block_number),
36+
Some(101u32.into())
37+
);
38+
}
3139

32-
impl_benchmark_test_suite!(Template, crate::mock::new_test_ext(), crate::mock::Test);
40+
impl_benchmark_test_suite!(Template, crate::mock::new_test_ext(), crate::mock::Test);
3341
}

.snippets/code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallets/template/src/lib.rs

Lines changed: 114 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -68,115 +68,118 @@ mod benchmarking;
6868
// <https://paritytech.github.io/polkadot-sdk/master/frame_support/pallet_macros/index.html>
6969
#[frame::pallet]
7070
pub mod pallet {
71-
use frame::prelude::*;
72-
73-
/// Configure the pallet by specifying the parameters and types on which it depends.
74-
#[pallet::config]
75-
pub trait Config: frame_system::Config {
76-
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
77-
78-
/// A type representing the weights required by the dispatchables of this pallet.
79-
type WeightInfo: crate::weights::WeightInfo;
80-
}
81-
82-
#[pallet::pallet]
83-
pub struct Pallet<T>(_);
84-
85-
/// A struct to store a single block-number. Has all the right derives to store it in storage.
86-
/// <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/reference_docs/frame_storage_derives/index.html>
87-
#[derive(
88-
Encode, Decode, MaxEncodedLen, TypeInfo, CloneNoBound, PartialEqNoBound, DefaultNoBound,
89-
)]
90-
#[scale_info(skip_type_params(T))]
91-
pub struct CompositeStruct<T: Config> {
92-
/// A block number.
93-
pub(crate) block_number: BlockNumberFor<T>,
94-
}
95-
96-
/// The pallet's storage items.
97-
/// <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/guides/your_first_pallet/index.html#storage>
98-
/// <https://paritytech.github.io/polkadot-sdk/master/frame_support/pallet_macros/attr.storage.html>
99-
#[pallet::storage]
100-
pub type Something<T: Config> = StorageValue<_, CompositeStruct<T>>;
101-
102-
/// Pallets use events to inform users when important changes are made.
103-
/// <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/guides/your_first_pallet/index.html#event-and-error>
104-
#[pallet::event]
105-
#[pallet::generate_deposit(pub(super) fn deposit_event)]
106-
pub enum Event<T: Config> {
107-
/// We usually use passive tense for events.
108-
SomethingStored { block_number: BlockNumberFor<T>, who: T::AccountId },
109-
}
110-
111-
/// Errors inform users that something went wrong.
112-
/// <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/guides/your_first_pallet/index.html#event-and-error>
113-
#[pallet::error]
114-
pub enum Error<T> {
115-
/// Error names should be descriptive.
116-
NoneValue,
117-
/// Errors should have helpful documentation associated with them.
118-
StorageOverflow,
119-
}
120-
121-
#[pallet::hooks]
122-
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
123-
124-
/// Dispatchable functions allows users to interact with the pallet and invoke state changes.
125-
/// These functions materialize as "extrinsics", which are often compared to transactions.
126-
/// Dispatchable functions must be annotated with a weight and must return a DispatchResult.
127-
/// <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/guides/your_first_pallet/index.html#dispatchables>
128-
#[pallet::call]
129-
impl<T: Config> Pallet<T> {
130-
/// An example dispatchable that takes a singles value as a parameter, writes the value to
131-
/// storage and emits an event. This function must be dispatched by a signed extrinsic.
132-
#[pallet::call_index(0)]
133-
#[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().writes(1))]
134-
pub fn do_something(origin: OriginFor<T>, bn: u32) -> DispatchResultWithPostInfo {
135-
// Check that the extrinsic was signed and get the signer.
136-
// This function will return an error if the extrinsic is not signed.
137-
// <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/reference_docs/frame_origin/index.html>
138-
let who = ensure_signed(origin)?;
139-
140-
// Convert the u32 into a block number. This is possible because the set of trait bounds
141-
// defined in [`frame_system::Config::BlockNumber`].
142-
let block_number: BlockNumberFor<T> = bn.into();
143-
144-
// Update storage.
145-
<Something<T>>::put(CompositeStruct { block_number });
146-
147-
// Emit an event.
148-
Self::deposit_event(Event::SomethingStored { block_number, who });
149-
150-
// Return a successful [`DispatchResultWithPostInfo`] or [`DispatchResult`].
151-
Ok(().into())
152-
}
153-
154-
/// An example dispatchable that may throw a custom error.
155-
#[pallet::call_index(1)]
156-
#[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().reads_writes(1,1))]
157-
pub fn cause_error(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
158-
let _who = ensure_signed(origin)?;
159-
160-
// Read a value from storage.
161-
match <Something<T>>::get() {
162-
// Return an error if the value has not been set.
163-
None => Err(Error::<T>::NoneValue)?,
164-
Some(mut old) => {
165-
// Increment the value read from storage; will error in the event of overflow.
166-
old.block_number = old
167-
.block_number
168-
.checked_add(&One::one())
169-
// ^^ equivalent is to:
170-
// .checked_add(&1u32.into())
171-
// both of which build a `One` instance for the type `BlockNumber`.
172-
.ok_or(Error::<T>::StorageOverflow)?;
173-
// Update the value in storage with the incremented result.
174-
<Something<T>>::put(old);
175-
// Explore how you can rewrite this using
176-
// [`frame_support::storage::StorageValue::mutate`].
177-
Ok(().into())
178-
},
179-
}
180-
}
181-
}
71+
use frame::prelude::*;
72+
73+
/// Configure the pallet by specifying the parameters and types on which it depends.
74+
#[pallet::config]
75+
pub trait Config: frame_system::Config {
76+
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
77+
78+
/// A type representing the weights required by the dispatchables of this pallet.
79+
type WeightInfo: crate::weights::WeightInfo;
80+
}
81+
82+
#[pallet::pallet]
83+
pub struct Pallet<T>(_);
84+
85+
/// A struct to store a single block-number. Has all the right derives to store it in storage.
86+
/// <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/reference_docs/frame_storage_derives/index.html>
87+
#[derive(
88+
Encode, Decode, MaxEncodedLen, TypeInfo, CloneNoBound, PartialEqNoBound, DefaultNoBound,
89+
)]
90+
#[scale_info(skip_type_params(T))]
91+
pub struct CompositeStruct<T: Config> {
92+
/// A block number.
93+
pub(crate) block_number: BlockNumberFor<T>,
94+
}
95+
96+
/// The pallet's storage items.
97+
/// <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/guides/your_first_pallet/index.html#storage>
98+
/// <https://paritytech.github.io/polkadot-sdk/master/frame_support/pallet_macros/attr.storage.html>
99+
#[pallet::storage]
100+
pub type Something<T: Config> = StorageValue<_, CompositeStruct<T>>;
101+
102+
/// Pallets use events to inform users when important changes are made.
103+
/// <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/guides/your_first_pallet/index.html#event-and-error>
104+
#[pallet::event]
105+
#[pallet::generate_deposit(pub(super) fn deposit_event)]
106+
pub enum Event<T: Config> {
107+
/// We usually use passive tense for events.
108+
SomethingStored {
109+
block_number: BlockNumberFor<T>,
110+
who: T::AccountId,
111+
},
112+
}
113+
114+
/// Errors inform users that something went wrong.
115+
/// <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/guides/your_first_pallet/index.html#event-and-error>
116+
#[pallet::error]
117+
pub enum Error<T> {
118+
/// Error names should be descriptive.
119+
NoneValue,
120+
/// Errors should have helpful documentation associated with them.
121+
StorageOverflow,
122+
}
123+
124+
#[pallet::hooks]
125+
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
126+
127+
/// Dispatchable functions allows users to interact with the pallet and invoke state changes.
128+
/// These functions materialize as "extrinsics", which are often compared to transactions.
129+
/// Dispatchable functions must be annotated with a weight and must return a DispatchResult.
130+
/// <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/guides/your_first_pallet/index.html#dispatchables>
131+
#[pallet::call]
132+
impl<T: Config> Pallet<T> {
133+
/// An example dispatchable that takes a singles value as a parameter, writes the value to
134+
/// storage and emits an event. This function must be dispatched by a signed extrinsic.
135+
#[pallet::call_index(0)]
136+
#[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().writes(1))]
137+
pub fn do_something(origin: OriginFor<T>, bn: u32) -> DispatchResultWithPostInfo {
138+
// Check that the extrinsic was signed and get the signer.
139+
// This function will return an error if the extrinsic is not signed.
140+
// <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/reference_docs/frame_origin/index.html>
141+
let who = ensure_signed(origin)?;
142+
143+
// Convert the u32 into a block number. This is possible because the set of trait bounds
144+
// defined in [`frame_system::Config::BlockNumber`].
145+
let block_number: BlockNumberFor<T> = bn.into();
146+
147+
// Update storage.
148+
<Something<T>>::put(CompositeStruct { block_number });
149+
150+
// Emit an event.
151+
Self::deposit_event(Event::SomethingStored { block_number, who });
152+
153+
// Return a successful [`DispatchResultWithPostInfo`] or [`DispatchResult`].
154+
Ok(().into())
155+
}
156+
157+
/// An example dispatchable that may throw a custom error.
158+
#[pallet::call_index(1)]
159+
#[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().reads_writes(1,1))]
160+
pub fn cause_error(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
161+
let _who = ensure_signed(origin)?;
162+
163+
// Read a value from storage.
164+
match <Something<T>>::get() {
165+
// Return an error if the value has not been set.
166+
None => Err(Error::<T>::NoneValue)?,
167+
Some(mut old) => {
168+
// Increment the value read from storage; will error in the event of overflow.
169+
old.block_number = old
170+
.block_number
171+
.checked_add(&One::one())
172+
// ^^ equivalent is to:
173+
// .checked_add(&1u32.into())
174+
// both of which build a `One` instance for the type `BlockNumber`.
175+
.ok_or(Error::<T>::StorageOverflow)?;
176+
// Update the value in storage with the incremented result.
177+
<Something<T>>::put(old);
178+
// Explore how you can rewrite this using
179+
// [`frame_support::storage::StorageValue::mutate`].
180+
Ok(().into())
181+
}
182+
}
183+
}
184+
}
182185
}
Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,50 @@
11
use frame::{
2-
deps::{frame_support::weights::constants::RocksDbWeight, frame_system::GenesisConfig},
3-
prelude::*,
4-
runtime::prelude::*,
5-
testing_prelude::*,
2+
deps::{frame_support::weights::constants::RocksDbWeight, frame_system::GenesisConfig},
3+
prelude::*,
4+
runtime::prelude::*,
5+
testing_prelude::*,
66
};
77

88
// Configure a mock runtime to test the pallet.
99
#[frame_construct_runtime]
1010
mod test_runtime {
11-
#[runtime::runtime]
12-
#[runtime::derive(
13-
RuntimeCall,
14-
RuntimeEvent,
15-
RuntimeError,
16-
RuntimeOrigin,
17-
RuntimeFreezeReason,
18-
RuntimeHoldReason,
19-
RuntimeSlashReason,
20-
RuntimeLockId,
21-
RuntimeTask
22-
)]
23-
pub struct Test;
11+
#[runtime::runtime]
12+
#[runtime::derive(
13+
RuntimeCall,
14+
RuntimeEvent,
15+
RuntimeError,
16+
RuntimeOrigin,
17+
RuntimeFreezeReason,
18+
RuntimeHoldReason,
19+
RuntimeSlashReason,
20+
RuntimeLockId,
21+
RuntimeTask
22+
)]
23+
pub struct Test;
2424

25-
#[runtime::pallet_index(0)]
26-
pub type System = frame_system;
27-
#[runtime::pallet_index(1)]
28-
pub type Template = crate;
25+
#[runtime::pallet_index(0)]
26+
pub type System = frame_system;
27+
#[runtime::pallet_index(1)]
28+
pub type Template = crate;
2929
}
3030

3131
#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
3232
impl frame_system::Config for Test {
33-
type Nonce = u64;
34-
type Block = MockBlock<Test>;
35-
type BlockHashCount = ConstU64<250>;
36-
type DbWeight = RocksDbWeight;
33+
type Nonce = u64;
34+
type Block = MockBlock<Test>;
35+
type BlockHashCount = ConstU64<250>;
36+
type DbWeight = RocksDbWeight;
3737
}
3838

3939
impl crate::Config for Test {
40-
type RuntimeEvent = RuntimeEvent;
41-
type WeightInfo = ();
40+
type RuntimeEvent = RuntimeEvent;
41+
type WeightInfo = ();
4242
}
4343

4444
// Build genesis storage according to the mock runtime.
4545
pub fn new_test_ext() -> TestState {
46-
GenesisConfig::<Test>::default().build_storage().unwrap().into()
46+
GenesisConfig::<Test>::default()
47+
.build_storage()
48+
.unwrap()
49+
.into()
4750
}

0 commit comments

Comments
 (0)