-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlib.rs
246 lines (200 loc) · 7.93 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#![cfg_attr(not(feature = "std"), no_std)]
/// Edit this file to define custom logic or remove it if it is not needed.
/// Learn more about FRAME and the core library of Substrate FRAME pallets:
/// <https://docs.substrate.io/v3/runtime/frame>
pub use pallet::*;
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
use sp_runtime::traits::Hash;
#[frame_support::pallet]
pub mod pallet {
use frame_support::{dispatch::DispatchResult, pallet_prelude::*};
use frame_system::pallet_prelude::*;
use sp_runtime::traits::Saturating;
use sp_runtime::traits::Zero;
/// Configure the pallet by specifying the parameters and types on which it depends.
#[pallet::config]
pub trait Config: frame_system::Config {
/// Because this pallet emits events, it depends on the runtime's definition of an event.
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
type WeightInfo: crate::WeightInfo;
}
pub trait WeightInfo {
fn store_something(_s: u32, ) -> Weight;
fn benign_repeat_hashing(i: u32, ) -> Weight;
}
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);
// The pallet's runtime storage items.
// https://docs.substrate.io/v3/runtime/storage
#[pallet::storage]
#[pallet::getter(fn something)]
// Learn more about declaring storage items:
// https://docs.substrate.io/v3/runtime/storage#declaring-storage-items
pub type Something<T> = StorageValue<_, u32>;
#[pallet::storage]
pub type Sum<T> = StorageValue<_, u32>;
// Pallets use events to inform users when important changes are made.
// https://docs.substrate.io/v3/runtime/events-and-errors
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// Event documentation should end with an array that provides descriptive names for event
/// parameters. [something, who]
SomethingStored(u32, T::AccountId),
SomethingIs(u32),
/// The resulting hash of all our work
TheHash(T::Hash),
/// The calculated sum
TheSum(u32),
}
// Errors inform users that something went wrong.
#[pallet::error]
pub enum Error<T> {
/// Error names should be descriptive.
NoneValue,
/// An overflow occured while calculating.
Overflow,
}
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(n: T::BlockNumber) -> frame_support::weights::Weight {
if (n.saturating_add(1u8.into()) % 10u8.into()).is_zero() {
// EVEN WORSE; ESPECIALLY DON'T USE UNWRAP IN ON_INITIALIZE.
let something = Self::something().unwrap();
Self::deposit_event(Event::<T>::SomethingIs(something));
T::DbWeight::get().reads(1)
} else {
0
}
}
}
// Dispatchable functions allows users to interact with the pallet and invoke state changes.
// These functions materialize as "extrinsics", which are often compared to transactions.
// Dispatchable functions must be annotated with a weight and must return a DispatchResult.
#[pallet::call]
impl<T: Config> Pallet<T> {
/// An example dispatchable that takes a singles value as a parameter, writes the value to
/// storage and emits an event. This function must be dispatched by a signed extrinsic.
#[pallet::weight(T::WeightInfo::store_something(*something))]
pub fn store_something(origin: OriginFor<T>, something: u32) -> DispatchResult {
// Check that the extrinsic was signed and get the signer.
// This function will return an error if the extrinsic is not signed.
// https://docs.substrate.io/v3/runtime/origins
let who = ensure_signed(origin)?;
// Update storage.
<Something<T>>::put(something);
// Emit an event.
Self::deposit_event(Event::SomethingStored(something, who));
// Return a successful DispatchResultWithPostInfo
Ok(())
}
/// Removes the `Something` storage item.
#[pallet::weight(10_000_000 + T::DbWeight::get().writes(1))]
pub fn remove_something(origin: OriginFor<T>) -> DispatchResult {
let _who = ensure_signed(origin)?;
<Something<T>>::take();
Ok(())
}
/// An example dispatchable that may throw a custom error.
#[pallet::weight(10_000_000 + T::DbWeight::get().reads_writes(1, 1))]
pub fn cause_error(origin: OriginFor<T>) -> DispatchResult {
let _who = ensure_signed(origin)?;
// Read a value from storage.
match <Something<T>>::get() {
// Return an error if the value has not been set.
None => Err(Error::<T>::NoneValue)?,
Some(old) => {
// Increment the value read from storage; will error in the event of overflow.
let new = old.checked_add(1).ok_or(Error::<T>::Overflow)?;
// Update the value in storage with the incremented result.
<Something<T>>::put(new);
Ok(())
},
}
}
/// Does repeated hashing based on the given parameter. Can take a very very long time for
/// big enough numbers and create overweight blocks.
#[pallet::weight(10_000_000)]
pub fn dangerous_repeat_hashing(origin: OriginFor<T>, times: u32) -> DispatchResult {
let who = ensure_signed(origin)?;
Self::do_repeat_hashing(times, &who);
Ok(())
}
/// Does repeated hashing based on the given parameter. Can only take up to the maximum
/// block weight in computation because it is benchmarked and weighted.
#[pallet::weight(T::WeightInfo::benign_repeat_hashing(*times))]
pub fn benign_repeat_hashing(origin: OriginFor<T>, times: u32) -> DispatchResult {
let who = ensure_signed(origin)?;
Self::do_repeat_hashing(times, &who);
Ok(())
}
/// Triggers an overflow if the given value is big enough.
#[pallet::weight(10_000_000 + T::DbWeight::get().reads_writes(1, 1))]
pub fn overflow(origin: OriginFor<T>, added: u32) -> DispatchResult {
let who = ensure_signed(origin)?;
let prev = Something::<T>::get().unwrap_or(0);
let val= prev + added;
Something::<T>::put(val);
Self::deposit_event(Event::SomethingStored(val, who));
Ok(())
}
/// Does not overflow because of checked addition.
#[pallet::weight(10_000_000 + T::DbWeight::get().reads_writes(1, 1))]
pub fn no_overflow(origin: OriginFor<T>, added: u32) -> DispatchResult {
let who = ensure_signed(origin)?;
let prev = Something::<T>::get().unwrap_or(0);
let val = prev.checked_add(added).ok_or(Error::<T>::Overflow)?;
Something::<T>::put(val);
Self::deposit_event(Event::SomethingStored(val, who));
Ok(())
}
/// Unwrap is BAD.
#[pallet::weight(10_000_000 + T::DbWeight::get().reads_writes(1, 1))]
pub fn unwrap_is_bad(origin: OriginFor<T>) -> DispatchResult {
let who = ensure_signed(origin)?;
// `.unwrap()` is BAD, DON'T USE IT in the runtime.
let prev = Something::<T>::get().unwrap();
let val = prev.checked_add(1).ok_or(Error::<T>::Overflow)?;
Something::<T>::put(val);
Self::deposit_event(Event::SomethingStored(val, who));
Ok(())
}
/// Does not update storage transactionally.
#[pallet::weight(10_000_000 + T::DbWeight::get().reads_writes(1, 2))]
pub fn non_transactional_sum(origin: OriginFor<T>, val: u32) -> DispatchResult {
let _who = ensure_signed(origin)?;
let prev = Something::<T>::get().unwrap_or(0);
Something::<T>::put(val);
let sum = prev.checked_add(val).ok_or(Error::<T>::Overflow)?;
Sum::<T>::put(sum);
Self::deposit_event(Event::TheSum(sum));
Ok(())
}
/// Updates either both or none of the storage items -> transactional.
#[pallet::weight(10_000_000 + T::DbWeight::get().reads_writes(1, 2))]
pub fn transactional_sum(origin: OriginFor<T>, val: u32) -> DispatchResult {
let _who = ensure_signed(origin)?;
let prev = Something::<T>::get().unwrap_or(0);
let sum = prev.checked_add(val).ok_or(Error::<T>::Overflow)?;
Something::<T>::put(val);
Sum::<T>::put(sum);
Self::deposit_event(Event::TheSum(sum));
Ok(())
}
}
}
impl<T: Config> Pallet<T> {
fn do_repeat_hashing(times: u32, acc: &T::AccountId) {
let mut hashed = T::Hashing::hash_of(acc);
for _i in 0..times {
hashed = T::Hashing::hash(&hashed.as_ref());
}
Self::deposit_event(Event::TheHash(hashed));
}
}