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
//! # Security Module
//! Based on the [specification](https://spec.interlay.io/spec/security.html).

#![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::*;

    /// ## Configuration
    /// The pallet's configuration trait.
    #[pallet::config]
    pub trait Config: frame_system::Config {
        /// The overarching event type.
        type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

        /// Weight information for the extrinsics in this module.
        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()
        }
    }

    /// Integer increment-only counter, used to prevent collisions when generating identifiers
    /// for e.g. issue, redeem or replace requests (for OP_RETURN field in Bitcoin).
    #[pallet::storage]
    pub type Nonce<T: Config> = StorageValue<_, U256, ValueQuery>;

    /// Like frame_system::block_number, but this one only increments if the parachain status is RUNNING.
    /// This variable is used to keep track of durations, such as the issue/redeem/replace expiry. If the
    /// parachain is not RUNNING, no payment proofs can be submitted, and it wouldn't be fair to punish
    /// the user/vault. By using this variable we ensure that they have sufficient time to submit their
    /// proof.
    #[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>(_);

    // The pallet's dispatchable functions.
    #[pallet::call]
    impl<T: Config> Pallet<T> {
        /// Activate or deactivate active block counting.
        #[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 is negative so that we don't need migration
            IsDeactivated::<T>::set(!is_active);

            if is_active {
                Self::deposit_event(Event::Activated);
            } else {
                Self::deposit_event(Event::Deactivated);
            }
            Ok(())
        }
    }
}
// "Internal" functions, callable by code.
#[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)
    }

    /// Increment and return the `Nonce`.
    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 });
    }

    /// Generates a 256-bit unique hash from an `AccountId` and the
    /// internal (auto-incrementing) `Nonce` to prevent replay attacks.
    ///
    /// # Arguments
    ///
    /// * `id`: Parachain account identifier.
    pub fn get_secure_id(id: &T::AccountId) -> H256 {
        let mut hasher = Sha256::default();
        hasher.input(id.encode());
        hasher.input(Self::get_nonce().encode());
        // supplement with prev block hash to prevent replays
        // even if the `Nonce` is reset (i.e. purge-chain)
        hasher.input(frame_system::Pallet::<T>::parent_hash());
        let mut result = [0; 32];
        result.copy_from_slice(&hasher.result()[..]);
        H256(result)
    }

    /// for testing purposes only!
    pub fn set_active_block_number(n: BlockNumberFor<T>) {
        ActiveBlockCount::<T>::set(n);
    }
}