Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Housekeeping: Update pallets to FRAME 2.0 syntax (paritytech#527)
Browse files Browse the repository at this point in the history
  • Loading branch information
vgeddes authored Oct 7, 2021
1 parent 9de49f7 commit 12b6851
Show file tree
Hide file tree
Showing 51 changed files with 1,518 additions and 1,262 deletions.
2 changes: 1 addition & 1 deletion parachain/pallets/assets/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use frame_benchmarking::{account, benchmarks, whitelisted_caller, impl_benchmark
use sp_core::H160;

#[allow(unused_imports)]
use crate::Module as Assets;
use crate::Pallet as Assets;

fn set_balance<T: Config>(asset_id: &AssetId, who: &T::AccountId, amount: &U256) {
TotalIssuance::insert(asset_id, amount);
Expand Down
25 changes: 11 additions & 14 deletions parachain/pallets/basic-channel/src/inbound/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
//! BasicInboundChannel pallet benchmarking

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

use super::*;

use frame_system::{RawOrigin, self, EventRecord};
use frame_benchmarking::{benchmarks, whitelisted_caller, impl_benchmark_test_suite};
use hex_literal::hex;
use sp_std::convert::TryInto;
use sp_std::prelude::*;

use snowbridge_core::{ChannelId, Message, MessageId, Proof};
use snowbridge_ethereum::{Log, Header};

#[allow(unused_imports)]
use crate::inbound::Module as BasicInboundChannel;
use crate::inbound::Pallet as BasicInboundChannel;

fn assert_last_event<T: Config>(system_event: <T as frame_system::Config>::Event) {
let events = frame_system::Pallet::<T>::events();
Expand All @@ -39,8 +36,8 @@ benchmarks! {
let envelope: envelope::Envelope = rlp::decode::<Log>(&message.data)
.map(|log| log.try_into().unwrap())
.unwrap();
Nonce::put(envelope.nonce - 1);
SourceChannel::put(envelope.channel);
<Nonce<T>>::put(envelope.nonce - 1);
<SourceChannel<T>>::put(envelope.channel);

T::Verifier::initialize_storage(
vec![header],
Expand All @@ -50,7 +47,7 @@ benchmarks! {

}: _(RawOrigin::Signed(caller.clone()), message)
verify {
assert_eq!(envelope.nonce, Nonce::get());
assert_eq!(envelope.nonce, <Nonce<T>>::get());

let message_id = MessageId::new(ChannelId::Basic, envelope.nonce);
if let Some(event) = T::MessageDispatch::successful_dispatch_event(message_id) {
Expand All @@ -65,8 +62,8 @@ benchmarks! {
let envelope: envelope::Envelope = rlp::decode::<Log>(&message.data)
.map(|log| log.try_into().unwrap())
.unwrap();
Nonce::put(envelope.nonce - 1);
SourceChannel::put(envelope.channel);
<Nonce<T>>::put(envelope.nonce - 1);
<SourceChannel<T>>::put(envelope.channel);

T::Verifier::initialize_storage(
vec![header],
Expand All @@ -76,7 +73,7 @@ benchmarks! {

}: submit(RawOrigin::Signed(caller.clone()), message)
verify {
assert_eq!(envelope.nonce, Nonce::get());
assert_eq!(envelope.nonce, <Nonce<T>>::get());

let message_id = MessageId::new(ChannelId::Basic, envelope.nonce);
if let Some(event) = T::MessageDispatch::successful_dispatch_event(message_id) {
Expand All @@ -91,8 +88,8 @@ benchmarks! {
let envelope: envelope::Envelope = rlp::decode::<Log>(&message.data)
.map(|log| log.try_into().unwrap())
.unwrap();
Nonce::put(envelope.nonce - 1);
SourceChannel::put(envelope.channel);
<Nonce<T>>::put(envelope.nonce - 1);
<SourceChannel<T>>::put(envelope.channel);

T::Verifier::initialize_storage(
vec![header],
Expand All @@ -102,7 +99,7 @@ benchmarks! {

}: submit(RawOrigin::Signed(caller.clone()), message)
verify {
assert_eq!(envelope.nonce, Nonce::get());
assert_eq!(envelope.nonce, <Nonce<T>>::get());

let message_id = MessageId::new(ChannelId::Basic, envelope.nonce);
if let Some(event) = T::MessageDispatch::successful_dispatch_event(message_id) {
Expand Down
116 changes: 68 additions & 48 deletions parachain/pallets/basic-channel/src/inbound/mod.rs
Original file line number Diff line number Diff line change
@@ -1,81 +1,101 @@
use frame_support::{
decl_error, decl_event, decl_module, decl_storage,
dispatch::DispatchResult,
weights::Weight,
};
use frame_system::{self as system, ensure_signed};
mod envelope;

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;

pub mod weights;

#[cfg(test)]
mod test;

use frame_system::ensure_signed;
use sp_core::H160;
use sp_std::prelude::*;
use sp_std::convert::TryFrom;
use snowbridge_core::{
ChannelId, Message, MessageId,
MessageDispatch, Verifier,
};

use envelope::Envelope;
pub use weights::WeightInfo;

mod benchmarking;
pub use pallet::*;

#[cfg(test)]
mod test;
#[frame_support::pallet]
pub mod pallet {

mod envelope;
use super::*;

/// Weight functions needed for this pallet.
pub trait WeightInfo {
fn submit() -> Weight;
}
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;

impl WeightInfo for () {
fn submit() -> Weight { 0 }
}
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);

pub trait Config: system::Config {
type Event: From<Event> + Into<<Self as system::Config>::Event>;
#[pallet::config]
pub trait Config: frame_system::Config {
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;

/// Verifier module for message verification.
type Verifier: Verifier;
/// Verifier module for message verification.
type Verifier: Verifier;

/// Verifier module for message verification.
type MessageDispatch: MessageDispatch<Self, MessageId>;
/// Verifier module for message verification.
type MessageDispatch: MessageDispatch<Self, MessageId>;

/// Weight information for extrinsics in this pallet
type WeightInfo: WeightInfo;
}

decl_storage! {
trait Store for Module<T: Config> as BasicInboundModule {
pub SourceChannel get(fn source_channel) config(): H160;
pub Nonce: u64;
/// Weight information for extrinsics in this pallet
type WeightInfo: WeightInfo;
}
}

decl_event! {
pub enum Event {
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}

}
}
#[pallet::event]
pub enum Event<T> {}

decl_error! {
pub enum Error for Module<T: Config> {
#[pallet::error]
pub enum Error<T> {
/// Message came from an invalid outbound channel on the Ethereum side.
InvalidSourceChannel,
/// Message has an invalid envelope.
InvalidEnvelope,
/// Message has an unexpected nonce.
InvalidNonce,
}
}

decl_module! {
pub struct Module<T: Config> for enum Call where origin: T::Origin {
/// Source channel on the ethereum side
#[pallet::storage]
#[pallet::getter(fn source_channel)]
pub type SourceChannel<T: Config> = StorageValue<_, H160, ValueQuery>;

#[pallet::storage]
pub type Nonce<T: Config> = StorageValue<_, u64, ValueQuery>;

type Error = Error<T>;
#[pallet::genesis_config]
pub struct GenesisConfig {
pub source_channel: H160,
}

fn deposit_event() = default;
#[cfg(feature = "std")]
impl Default for GenesisConfig {
fn default() -> Self {
Self {
source_channel: Default::default(),
}
}
}

#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig {
fn build(&self) {
<SourceChannel<T>>::put(self.source_channel);
}
}

#[weight = T::WeightInfo::submit()]
pub fn submit(origin, message: Message) -> DispatchResult {
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(T::WeightInfo::submit())]
pub fn submit(origin: OriginFor<T>, message: Message) -> DispatchResult {
ensure_signed(origin)?;
// submit message to verifier for verification
let log = T::Verifier::verify(&message)?;
Expand All @@ -85,12 +105,12 @@ decl_module! {

// Verify that the message was submitted to us from a known
// outbound channel on the ethereum side
if envelope.channel != SourceChannel::get() {
if envelope.channel != <SourceChannel<T>>::get() {
return Err(Error::<T>::InvalidSourceChannel.into())
}

// Verify message nonce
Nonce::try_mutate(|nonce| -> DispatchResult {
<Nonce<T>>::try_mutate(|nonce| -> DispatchResult {
if envelope.nonce != *nonce + 1 {
Err(Error::<T>::InvalidNonce.into())
} else {
Expand Down
17 changes: 8 additions & 9 deletions parachain/pallets/basic-channel/src/inbound/test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

use super::*;

use frame_support::traits::GenesisBuild;
use sp_core::{H160, H256};
use frame_support::{
assert_ok, assert_noop,
Expand All @@ -19,7 +19,6 @@ use snowbridge_ethereum::{Header as EthereumHeader, Log, U256};
use hex_literal::hex;

use crate::inbound::Error;

use crate::inbound as basic_inbound_channel;

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
Expand All @@ -32,7 +31,7 @@ frame_support::construct_runtime!(
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Pallet, Call, Storage, Event<T>},
BasicInboundChannel: basic_inbound_channel::{Pallet, Call, Storage, Event},
BasicInboundChannel: basic_inbound_channel::{Pallet, Call, Storage, Event<T>},
}
);

Expand All @@ -43,7 +42,7 @@ parameter_types! {
pub const BlockHashCount: u64 = 250;
}

impl system::Config for Test {
impl frame_system::Config for Test {
type BaseCallFilter = ();
type BlockWeights = ();
type BlockLength = ();
Expand Down Expand Up @@ -89,7 +88,7 @@ impl MessageDispatch<Test, MessageId> for MockMessageDispatch {
fn dispatch(_: H160, _: MessageId, _: &[u8]) {}

#[cfg(feature = "runtime-benchmarks")]
fn successful_dispatch_event(_: MessageId) -> Option<<Test as system::Config>::Event> {
fn successful_dispatch_event(_: MessageId) -> Option<<Test as frame_system::Config>::Event> {
None
}
}
Expand All @@ -110,7 +109,7 @@ pub fn new_tester(source_channel: H160) -> sp_io::TestExternalities {
pub fn new_tester_with_config(config: basic_inbound_channel::GenesisConfig) -> sp_io::TestExternalities {
let mut storage = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();

config.assimilate_storage(&mut storage).unwrap();
GenesisBuild::<Test>::assimilate_storage(&config, &mut storage).unwrap();

let mut ext: sp_io::TestExternalities = storage.into();
ext.execute_with(|| System::set_block_number(1));
Expand Down Expand Up @@ -197,7 +196,7 @@ fn test_submit() {
},
};
assert_ok!(BasicInboundChannel::submit(origin.clone(), message_1));
let nonce: u64 = Nonce::get();
let nonce: u64 = <Nonce<Test>>::get();
assert_eq!(nonce, 1);

// Submit message 2
Expand All @@ -210,7 +209,7 @@ fn test_submit() {
},
};
assert_ok!(BasicInboundChannel::submit(origin.clone(), message_2));
let nonce: u64 = Nonce::get();
let nonce: u64 = <Nonce<Test>>::get();
assert_eq!(nonce, 2);
});
}
Expand All @@ -231,7 +230,7 @@ fn test_submit_with_invalid_nonce() {
},
};
assert_ok!(BasicInboundChannel::submit(origin.clone(), message.clone()));
let nonce: u64 = Nonce::get();
let nonce: u64 = <Nonce<Test>>::get();
assert_eq!(nonce, 1);

// Submit the same again
Expand Down
9 changes: 9 additions & 0 deletions parachain/pallets/basic-channel/src/inbound/weights.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use frame_support::weights::Weight;

pub trait WeightInfo {
fn submit() -> Weight;
}

impl WeightInfo for () {
fn submit() -> Weight { 0 }
}
14 changes: 7 additions & 7 deletions parachain/pallets/basic-channel/src/outbound/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use frame_benchmarking::{
use frame_support::traits::OnInitialize;

#[allow(unused_imports)]
use crate::outbound::Module as BasicOutboundChannel;
use crate::outbound::Pallet as BasicOutboundChannel;

const SEED: u32 = 0;

Expand All @@ -22,7 +22,7 @@ benchmarks! {

for _ in 0 .. m {
let payload: Vec<u8> = (0..).take(p as usize).collect();
MessageQueue::append(Message {
<MessageQueue<T>>::append(Message {
target: H160::zero(),
nonce: 0u64,
payload,
Expand All @@ -33,30 +33,30 @@ benchmarks! {

}: { BasicOutboundChannel::<T>::on_initialize(block_number) }
verify {
assert_eq!(MessageQueue::get().len(), 0);
assert_eq!(<MessageQueue<T>>::get().len(), 0);
}

// Benchmark 'on_initialize` for the best case, i.e. nothing is done
// because it's not a commitment interval.
on_initialize_non_interval {
MessageQueue::append(Message {
<MessageQueue<T>>::append(Message {
target: H160::zero(),
nonce: 0u64,
payload: vec![1u8; T::MaxMessagePayloadSize::get()],
payload: vec![1u8; T::MaxMessagePayloadSize::get() as usize],
});

Interval::<T>::put::<T::BlockNumber>(10u32.into());
let block_number: T::BlockNumber = 11u32.into();

}: { BasicOutboundChannel::<T>::on_initialize(block_number) }
verify {
assert_eq!(MessageQueue::get().len(), 1);
assert_eq!(<MessageQueue<T>>::get().len(), 1);
}

// Benchmark 'on_initialize` for the case where it is a commitment interval
// but there are no messages in the queue.
on_initialize_no_messages {
MessageQueue::kill();
<MessageQueue<T>>::kill();

let block_number = Interval::<T>::get();

Expand Down
Loading

0 comments on commit 12b6851

Please sign in to comment.