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
#![deny(warnings)]
#![cfg_attr(test, feature(proc_macro_hygiene))]
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
use sp_runtime::{traits::*, ArithmeticError};
use sp_std::convert::TryInto;
mod default_weights;
pub use default_weights::WeightInfo;
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
#[cfg(test)]
extern crate mocktopus;
#[cfg(test)]
use mocktopus::macros::mockable;
use codec::Encode;
use frame_support::{dispatch::DispatchError, weights::Weight};
use frame_system::pallet_prelude::BlockNumberFor;
pub use pallet::*;
use sha2::{Digest, Sha256};
use sp_core::{H256, U256};
use sp_std::vec;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
#[pallet::config]
pub trait Config: frame_system::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
type WeightInfo: WeightInfo;
}
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
UpdateActiveBlock { block_number: BlockNumberFor<T> },
Activated,
Deactivated,
}
#[pallet::error]
pub enum Error<T> {}
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(_n: BlockNumberFor<T>) -> Weight {
Self::increment_active_block();
<T as Config>::WeightInfo::on_initialize()
}
}
#[pallet::storage]
pub type Nonce<T: Config> = StorageValue<_, U256, ValueQuery>;
#[pallet::storage]
#[pallet::getter(fn active_block_number)]
pub type ActiveBlockCount<T: Config> = StorageValue<_, BlockNumberFor<T>, ValueQuery>;
#[pallet::storage]
pub type IsDeactivated<T: Config> = StorageValue<_, bool, ValueQuery>;
#[pallet::pallet]
pub struct Pallet<T>(_);
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(T::WeightInfo::activate_counter())]
pub fn activate_counter(origin: OriginFor<T>, is_active: bool) -> DispatchResult {
ensure_root(origin)?;
IsDeactivated::<T>::set(!is_active);
if is_active {
Self::deposit_event(Event::Activated);
} else {
Self::deposit_event(Event::Deactivated);
}
Ok(())
}
}
}
#[cfg_attr(test, mockable)]
impl<T: Config> Pallet<T> {
pub fn parachain_block_expired(
opentime: BlockNumberFor<T>,
period: BlockNumberFor<T>,
) -> Result<bool, DispatchError> {
let expiration_block = opentime.checked_add(&period).ok_or(ArithmeticError::Overflow)?;
Ok(Self::active_block_number() > expiration_block)
}
fn get_nonce() -> U256 {
<Nonce<T>>::mutate(|n| {
let (res, _) = (*n).overflowing_add(U256::one());
*n = res;
*n
})
}
fn increment_active_block() {
if IsDeactivated::<T>::get() {
return;
}
let height = <ActiveBlockCount<T>>::mutate(|n| {
*n = n.saturating_add(1u32.into());
*n
});
Self::deposit_event(Event::UpdateActiveBlock { block_number: height });
}
pub fn get_secure_id(id: &T::AccountId) -> H256 {
let mut hasher = Sha256::default();
hasher.input(id.encode());
hasher.input(Self::get_nonce().encode());
hasher.input(frame_system::Pallet::<T>::parent_hash());
let mut result = [0; 32];
result.copy_from_slice(&hasher.result()[..]);
H256(result)
}
pub fn set_active_block_number(n: BlockNumberFor<T>) {
ActiveBlockCount::<T>::set(n);
}
}