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
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
pub mod weights;
pub use weights::*;
pub use pallet::*;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use sha2::{Digest, Sha256};
use sp_core::{H256, U256};
use sp_runtime::traits::Saturating;
use tidefi_primitives::{pallet::SecurityExt, Hash, StatusCode};
#[pallet::config]
pub trait Config: frame_system::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
type WeightInfo: WeightInfo;
}
#[pallet::pallet]
#[pallet::generate_store(pub (super) trait Store)]
pub struct Pallet<T>(_);
#[pallet::storage]
pub type Nonce<T: Config> = StorageValue<_, U256, ValueQuery>;
#[pallet::storage]
#[pallet::getter(fn status)]
pub type ChainStatus<T: Config> = StorageValue<_, StatusCode, ValueQuery>;
#[pallet::storage]
#[pallet::getter(fn current_block_number)]
pub type CurrentBlockCount<T: Config> = StorageValue<_, T::BlockNumber, ValueQuery>;
#[pallet::genesis_config]
pub struct GenesisConfig {
pub status: StatusCode,
}
#[cfg(feature = "std")]
impl Default for GenesisConfig {
fn default() -> Self {
Self {
status: StatusCode::Running,
}
}
}
#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig {
fn build(&self) {
<ChainStatus<T>>::set(self.status.clone());
}
}
#[pallet::event]
#[pallet::generate_deposit(pub (super) fn deposit_event)]
pub enum Event<T: Config> {
StatusChanged(StatusCode),
UpdateCurrentBlock(T::BlockNumber),
}
#[pallet::error]
pub enum Error<T> {
ChainMaintenanceMode,
}
#[pallet::hooks]
impl<T: Config> Hooks<T::BlockNumber> for Pallet<T> {
fn on_initialize(_current_block: T::BlockNumber) -> Weight {
if Self::status() == StatusCode::Running {
let height = <CurrentBlockCount<T>>::mutate(|n| {
*n = n.saturating_add(1u32.into());
*n
});
Self::deposit_event(Event::UpdateCurrentBlock(height));
}
Weight::from_ref_time(0)
}
}
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(<T as pallet::Config>::WeightInfo::set_status())]
pub fn set_status(origin: OriginFor<T>, status_code: StatusCode) -> DispatchResultWithPostInfo {
ensure_root(origin)?;
<ChainStatus<T>>::set(status_code.clone());
Self::deposit_event(Event::StatusChanged(status_code));
Ok(().into())
}
}
impl<T: Config> Pallet<T> {
fn get_nonce() -> U256 {
<Nonce<T>>::mutate(|n| {
let (res, _) = (*n).overflowing_add(U256::one());
*n = res;
*n
})
}
fn get_next_id(id: &T::AccountId) -> H256 {
let mut hasher = Sha256::default();
hasher.update(id.encode());
hasher.update(Self::get_nonce().encode());
hasher.update(frame_system::Pallet::<T>::parent_hash());
let mut result = [0; 32];
result.copy_from_slice(&hasher.finalize()[..]);
H256(result)
}
}
impl<T: Config> SecurityExt<T::AccountId, T::BlockNumber> for Pallet<T> {
fn is_chain_running() -> bool {
Self::status() == StatusCode::Running
}
fn get_current_block_count() -> T::BlockNumber {
Self::current_block_number()
}
fn get_unique_id(account_id: T::AccountId) -> Hash {
Self::get_next_id(&account_id)
}
}
}