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
//! Commitment recorder pallet.
18
//!
19
//! A pallet to record outbound message commitment.
20
//!
21

            
22
#![cfg_attr(not(feature = "std"), no_std)]
23

            
24
pub use pallet::*;
25

            
26
2
#[frame_support::pallet]
27
pub mod pallet {
28
    use {frame_support::pallet_prelude::*, sp_core::H256};
29

            
30
    /// The current storage version.
31
    const STORAGE_VERSION: StorageVersion = StorageVersion::new(0);
32

            
33
    /// Configure the pallet by specifying the parameters and types on which it depends.
34
    #[pallet::config]
35
    pub trait Config: frame_system::Config {
36
        /// Overarching event type.
37
        type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
38
    }
39

            
40
6
    #[pallet::pallet]
41
    #[pallet::storage_version(STORAGE_VERSION)]
42
    pub struct Pallet<T>(_);
43

            
44
    #[pallet::event]
45
    #[pallet::generate_deposit(pub(super) fn deposit_event)]
46
    pub enum Event<T: Config> {
47
        NewCommitmentRootRecorded { commitment: H256 },
48
        CommitmentRootRead { commitment: H256 },
49
    }
50

            
51
    /// Message commitment from last block.
52
    /// This will be set only when there are messages to relay.
53
14568
    #[pallet::storage]
54
    pub type RecordedCommitment<T: Config> = StorageValue<_, H256, OptionQuery>;
55

            
56
    impl<T: Config> Pallet<T> {
57
7284
        pub fn take_commitment_root() -> Option<H256> {
58
7284
            let maybe_commitment = RecordedCommitment::<T>::take();
59
7284
            if let Some(commitment) = maybe_commitment {
60
                Pallet::<T>::deposit_event(Event::<T>::CommitmentRootRead { commitment });
61
                Some(commitment)
62
            } else {
63
7284
                None
64
            }
65
7284
        }
66

            
67
        pub fn record_commitment_root(commitment: H256) {
68
            RecordedCommitment::<T>::put(commitment);
69
            Pallet::<T>::deposit_event(Event::<T>::NewCommitmentRootRecorded { commitment });
70
        }
71
    }
72
}