referrerpolicy=no-referrer-when-downgrade

pallet_remark/
lib.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Remark storage pallet. Indexes remarks and stores them off chain.
19
20// Ensure we're `no_std` when compiling for Wasm.
21#![cfg_attr(not(feature = "std"), no_std)]
22
23mod benchmarking;
24pub mod weights;
25
26#[cfg(test)]
27mod mock;
28#[cfg(test)]
29mod tests;
30
31extern crate alloc;
32
33use alloc::vec::Vec;
34
35// Re-export pallet items so that they can be accessed from the crate namespace.
36pub use pallet::*;
37pub use weights::WeightInfo;
38
39#[frame_support::pallet]
40pub mod pallet {
41	use super::*;
42	use frame_support::pallet_prelude::*;
43	use frame_system::pallet_prelude::*;
44
45	#[pallet::config]
46	pub trait Config: frame_system::Config {
47		/// The overarching event type.
48		#[allow(deprecated)]
49		type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
50		/// Weight information for extrinsics in this pallet.
51		type WeightInfo: WeightInfo;
52	}
53
54	#[pallet::error]
55	pub enum Error<T> {
56		/// Attempting to store empty data.
57		Empty,
58		/// Attempted to call `store` outside of block execution.
59		BadContext,
60	}
61
62	#[pallet::pallet]
63	pub struct Pallet<T>(_);
64
65	#[pallet::call]
66	impl<T: Config> Pallet<T> {
67		/// Index and store data off chain.
68		#[pallet::call_index(0)]
69		#[pallet::weight(T::WeightInfo::store(remark.len() as u32))]
70		pub fn store(origin: OriginFor<T>, remark: Vec<u8>) -> DispatchResultWithPostInfo {
71			ensure!(!remark.is_empty(), Error::<T>::Empty);
72			let sender = ensure_signed(origin)?;
73			let content_hash = sp_io::hashing::blake2_256(&remark);
74			let extrinsic_index = <frame_system::Pallet<T>>::extrinsic_index()
75				.ok_or_else(|| Error::<T>::BadContext)?;
76			sp_io::transaction_index::index(extrinsic_index, remark.len() as u32, content_hash);
77			Self::deposit_event(Event::Stored { sender, content_hash: content_hash.into() });
78			Ok(().into())
79		}
80	}
81
82	#[pallet::event]
83	#[pallet::generate_deposit(pub(super) fn deposit_event)]
84	pub enum Event<T: Config> {
85		/// Stored data off chain.
86		Stored { sender: T::AccountId, content_hash: sp_core::H256 },
87	}
88}