Skip to content

Commit

Permalink
Format files and add license
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLucca committed Jan 13, 2025
1 parent 061aa1a commit 7a50528
Show file tree
Hide file tree
Showing 18 changed files with 1,155 additions and 1,006 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
//! Benchmarking setup for pallet-template
// This file is part of 'custom-pallet'.

// SPDX-License-Identifier: MIT-0

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#![cfg(feature = "runtime-benchmarks")]

use super::{Pallet as CustomPallet, *};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
// This file is part of 'custom-pallet'.

// SPDX-License-Identifier: MIT-0

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#![cfg_attr(not(feature = "std"), no_std)]

pub use pallet::*;
Expand All @@ -14,7 +33,7 @@ mod benchmarking;
pub mod weights;
use crate::weights::WeightInfo;

#[frame_support::pallet(dev_mode)]
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
// This file is part of 'custom-pallet'.

// SPDX-License-Identifier: MIT-0

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use crate as custom_pallet;
use frame_support::{derive_impl, parameter_types};
use sp_runtime::BuildStorage;
Expand Down Expand Up @@ -50,4 +69,4 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
.build_storage()
.unwrap()
.into()
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
// This file is part of 'custom-pallet'.

// SPDX-License-Identifier: MIT-0

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use crate::{mock::*, Error, Event, UserInteractions};
use frame_support::{assert_noop, assert_ok};

Expand Down Expand Up @@ -46,15 +65,18 @@ fn it_works_for_increment() {
System::set_block_number(1);
// Initialize the counter value to 0
assert_ok!(CustomPallet::set_counter_value(RuntimeOrigin::root(), 0));

// Increment the counter by 5
assert_ok!(CustomPallet::increment(RuntimeOrigin::signed(1), 5));
// Check that the event emitted matches the increment operation
System::assert_last_event(Event::CounterIncremented {
counter_value: 5,
who: 1,
incremented_amount: 5
}.into());
System::assert_last_event(
Event::CounterIncremented {
counter_value: 5,
who: 1,
incremented_amount: 5,
}
.into(),
);
});
}

Expand Down Expand Up @@ -94,15 +116,18 @@ fn it_works_for_decrement() {
System::set_block_number(1);
// Initialize counter value to 8
assert_ok!(CustomPallet::set_counter_value(RuntimeOrigin::root(), 8));

// Decrement counter by 3
assert_ok!(CustomPallet::decrement(RuntimeOrigin::signed(1), 3));
// Ensure the event matches the decrement action
System::assert_last_event(Event::CounterDecremented {
counter_value: 5,
who: 1,
decremented_amount: 3
}.into());
System::assert_last_event(
Event::CounterDecremented {
counter_value: 5,
who: 1,
decremented_amount: 3,
}
.into(),
);
});
}

Expand Down Expand Up @@ -145,7 +170,7 @@ fn user_interactions_overflow() {
System::set_block_number(1);
// Initialize counter value to 0
assert_ok!(CustomPallet::set_counter_value(RuntimeOrigin::root(), 0));

// Set user interactions to max value (u32::MAX)
UserInteractions::<Test>::insert(1, u32::MAX);
// Ensure that incrementing by 5 fails due to overflow in user interactions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,37 @@ use frame_benchmarking::v2::*;

#[benchmarks]
mod benchmarks {
use super::*;
#[cfg(test)]
use crate::pallet::Pallet as Template;
use frame_system::RawOrigin;
use super::*;
#[cfg(test)]
use crate::pallet::Pallet as Template;
use frame_system::RawOrigin;

#[benchmark]
fn do_something() {
let caller: T::AccountId = whitelisted_caller();
#[extrinsic_call]
do_something(RawOrigin::Signed(caller), 100);
#[benchmark]
fn do_something() {
let caller: T::AccountId = whitelisted_caller();
#[extrinsic_call]
do_something(RawOrigin::Signed(caller), 100);

assert_eq!(Something::<T>::get().map(|v| v.block_number), Some(100u32.into()));
}
assert_eq!(
Something::<T>::get().map(|v| v.block_number),
Some(100u32.into())
);
}

#[benchmark]
fn cause_error() {
Something::<T>::put(CompositeStruct { block_number: 100u32.into() });
let caller: T::AccountId = whitelisted_caller();
#[extrinsic_call]
cause_error(RawOrigin::Signed(caller));
#[benchmark]
fn cause_error() {
Something::<T>::put(CompositeStruct {
block_number: 100u32.into(),
});
let caller: T::AccountId = whitelisted_caller();
#[extrinsic_call]
cause_error(RawOrigin::Signed(caller));

assert_eq!(Something::<T>::get().map(|v| v.block_number), Some(101u32.into()));
}
assert_eq!(
Something::<T>::get().map(|v| v.block_number),
Some(101u32.into())
);
}

impl_benchmark_test_suite!(Template, crate::mock::new_test_ext(), crate::mock::Test);
impl_benchmark_test_suite!(Template, crate::mock::new_test_ext(), crate::mock::Test);
}
Loading

0 comments on commit 7a50528

Please sign in to comment.