1
// Copyright (C) Moondance Labs Ltd.
2
// This file is part of Tanssi.
3

            
4
// Tanssi is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Tanssi is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Tanssi.  If not, see <http://www.gnu.org/licenses/>
16

            
17
#![cfg_attr(not(feature = "std"), no_std)]
18
use frame_system::offchain::CreateInherent;
19
use frame_system::offchain::CreateSignedTransaction;
20
use {
21
    frame_system::{
22
        self as system, ensure_none, ensure_root, offchain::SubmitTransaction,
23
        pallet_prelude::BlockNumberFor,
24
    },
25
    sp_runtime::transaction_validity::{InvalidTransaction, TransactionValidity, ValidTransaction},
26
};
27

            
28
pub use pallet::*;
29
#[frame_support::pallet]
30
pub mod pallet {
31
    use {super::*, frame_support::pallet_prelude::*, frame_system::pallet_prelude::*};
32

            
33
1874
    #[pallet::pallet]
34
    pub struct Pallet<T>(_);
35

            
36
    #[pallet::config]
37
    pub trait Config:
38
        CreateSignedTransaction<Call<Self>> + CreateInherent<Call<Self>> + frame_system::Config
39
    {
40
        /// The overarching event type.
41
        type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
42

            
43
        /// Number of blocks of cooldown after unsigned transaction is included.
44
        ///
45
        /// This ensures that we only accept unsigned transactions once, every `UnsignedInterval`
46
        /// blocks.
47
        #[pallet::constant]
48
        type UnsignedInterval: Get<BlockNumberFor<Self>>;
49
    }
50

            
51
    #[pallet::storage]
52
    pub(super) type OffchainWorkerTestEnabled<T> = StorageValue<_, bool, ValueQuery>;
53

            
54
    /// Defines the block when next unsigned transaction will be accepted.
55
    ///
56
    /// To prevent spam of unsigned (and unpaid!) transactions on the network,
57
    /// we only allow one transaction every `T::UnsignedInterval` blocks.
58
    /// This storage entry defines when new transaction is going to be accepted.
59
    #[pallet::storage]
60
    pub(super) type NextUnsignedAt<T: Config> = StorageValue<_, BlockNumberFor<T>, ValueQuery>;
61

            
62
    #[pallet::genesis_config]
63
    pub struct GenesisConfig<T: Config> {
64
        pub _phantom_data: PhantomData<T>,
65
    }
66

            
67
    #[pallet::genesis_build]
68
    impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
69
        fn build(&self) {
70
            <OffchainWorkerTestEnabled<T>>::put(&false);
71
        }
72
    }
73

            
74
1
    #[pallet::hooks]
75
    impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
76
        /// Offchain worker entry point.
77
        ///
78
        /// By implementing `fn offchain_worker` you declare a new offchain worker.
79
        /// This function will be called when the node is fully synced and a new best block is
80
        /// successfully imported.
81
        /// Note that it's not guaranteed for offchain workers to run on EVERY block, there might
82
        /// be cases where some blocks are skipped, or for some the worker runs twice (re-orgs),
83
        /// so the code should be able to handle that.
84
        fn offchain_worker(block_number: BlockNumberFor<T>) {
85
            log::info!("Entering off-chain worker.");
86
            // The entry point of your code called by off-chain worker
87
            let res = Self::send_raw_unsigned_transaction(block_number);
88
            if let Err(e) = res {
89
                log::error!("Error: {}", e);
90
            }
91
        }
92
    }
93
    #[pallet::call]
94
    impl<T: Config> Pallet<T> {
95
        /// Switches on or off the offchain worker
96
        ///
97
        /// Only root (or specified authority account) should be able to switch
98
        /// the off-chain worker on and off to avoid enabling it by default in production
99
        #[pallet::call_index(0)]
100
        #[pallet::weight(T::DbWeight::get().write)]
101
        pub fn set_offchain_worker(
102
            origin: OriginFor<T>,
103
            is_testing_enabled: bool,
104
        ) -> DispatchResultWithPostInfo {
105
            ensure_root(origin)?;
106

            
107
            OffchainWorkerTestEnabled::<T>::put(is_testing_enabled);
108
            Ok(().into())
109
        }
110

            
111
        /// Submits unsigned transaction that emits an event
112
        ///
113
        /// Can be triggered only by an offchain worker
114
        #[pallet::call_index(1)]
115
        #[pallet::weight(T::DbWeight::get().write)]
116
        pub fn submit_event_unsigned(
117
            origin: OriginFor<T>,
118
            _block_number: BlockNumberFor<T>,
119
        ) -> DispatchResultWithPostInfo {
120
            // This ensures that the function can only be called via unsigned transaction.
121
            ensure_none(origin)?;
122

            
123
            ensure!(
124
                OffchainWorkerTestEnabled::<T>::get(),
125
                Error::<T>::OffchainWorkerNotEnabled,
126
            );
127

            
128
            // Increment the block number at which we expect next unsigned transaction.
129
            let current_block = <frame_system::Pallet<T>>::block_number();
130

            
131
            // Emits offchain event
132
            Self::deposit_event(Event::SimpleOffchainEvent);
133

            
134
            <NextUnsignedAt<T>>::put(current_block + T::UnsignedInterval::get());
135
            Ok(().into())
136
        }
137
    }
138

            
139
    /// Events for the pallet.
140
    #[pallet::event]
141
    #[pallet::generate_deposit(pub(super) fn deposit_event)]
142
    pub enum Event<T: Config> {
143
        /// Simple offchain event
144
        SimpleOffchainEvent,
145
    }
146

            
147
    #[pallet::error]
148
    pub enum Error<T> {
149
        OffchainWorkerNotEnabled,
150
    }
151

            
152
    #[pallet::validate_unsigned]
153
    impl<T: Config> ValidateUnsigned for Pallet<T> {
154
        type Call = Call<T>;
155

            
156
        /// Validate unsigned call to this module.
157
        ///
158
        /// By default unsigned transactions are disallowed, but implementing the validator
159
        /// here we make sure that some particular calls (the ones produced by offchain worker)
160
        /// are being whitelisted and marked as valid.
161
        fn validate_unsigned(_source: TransactionSource, call: &Self::Call) -> TransactionValidity {
162
            if let Call::submit_event_unsigned { block_number } = call {
163
                Self::validate_transaction_parameters(block_number)
164
            } else {
165
                InvalidTransaction::Call.into()
166
            }
167
        }
168
    }
169
}
170

            
171
impl<T: Config> Pallet<T> {
172
    /// A helper function to sign payload and send an unsigned transaction
173
    fn send_raw_unsigned_transaction(block_number: BlockNumberFor<T>) -> Result<(), &'static str> {
174
        // Make sure offchain worker testing is enabled
175
        let is_offchain_worker_enabled = OffchainWorkerTestEnabled::<T>::get();
176
        if !is_offchain_worker_enabled {
177
            return Err("Offchain worker is not enabled");
178
        }
179
        // Make sure transaction can be sent
180
        let next_unsigned_at = NextUnsignedAt::<T>::get();
181
        if next_unsigned_at > block_number {
182
            return Err("Too early to send unsigned transaction");
183
        }
184

            
185
        let call = Call::submit_event_unsigned { block_number };
186

            
187
        let xt = T::create_inherent(call.into());
188
        SubmitTransaction::<T, Call<T>>::submit_transaction(xt)
189
            .map_err(|()| "Unable to submit unsigned transaction.")?;
190

            
191
        Ok(())
192
    }
193

            
194
    fn validate_transaction_parameters(block_number: &BlockNumberFor<T>) -> TransactionValidity {
195
        // Make sure offchain worker testing is enabled
196
        let is_offchain_worker_enabled = OffchainWorkerTestEnabled::<T>::get();
197
        if !is_offchain_worker_enabled {
198
            return InvalidTransaction::Call.into();
199
        }
200
        // Now let's check if the transaction has any chance to succeed.
201
        let next_unsigned_at = NextUnsignedAt::<T>::get();
202
        if &next_unsigned_at > block_number {
203
            return InvalidTransaction::Stale.into();
204
        }
205
        // Let's make sure to reject transactions from the future.
206
        let current_block = <system::Pallet<T>>::block_number();
207
        if &current_block < block_number {
208
            return InvalidTransaction::Future.into();
209
        }
210
        ValidTransaction::with_tag_prefix("ExampleOffchainWorker")
211
            // We set base priority to 2**20 and hope it's included before any other
212
            // transactions in the pool. Next we tweak the priority depending on how much
213
            // it differs from the current average. (the more it differs the more priority it
214
            // has).
215
            .priority(2u64.pow(20))
216
            // This transaction does not require anything else to go before into the pool.
217
            // In theory we could require `previous_unsigned_at` transaction to go first,
218
            // but it's not necessary in our case.
219
            //.and_requires()
220
            // We set the `provides` tag to be the same as `next_unsigned_at`. This makes
221
            // sure only one transaction produced after `next_unsigned_at` will ever
222
            // get to the transaction pool and will end up in the block.
223
            // We can still have multiple transactions compete for the same "spot",
224
            // and the one with higher priority will replace other one in the pool.
225
            .and_provides(next_unsigned_at)
226
            // The transaction is only valid for next 5 blocks. After that it's
227
            // going to be revalidated by the pool.
228
            .longevity(6)
229
            // It's fine to propagate that transaction to other peers, which means it can be
230
            // created even by nodes that don't produce blocks.
231
            // Note that sometimes it's better to keep it for yourself (if you are the block
232
            // producer), since for instance in some schemes others may copy your solution and
233
            // claim a reward.
234
            .propagate(true)
235
            .build()
236
    }
237
}