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
#[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

            
37
    #[pallet::pallet]
38
    #[pallet::storage_version(STORAGE_VERSION)]
39
    pub struct Pallet<T>(_);
40

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

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

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

            
64
        pub fn record_commitment_root(commitment: H256) {
65
            RecordedCommitment::<T>::put(commitment);
66
            Pallet::<T>::deposit_event(Event::<T>::NewCommitmentRootRecorded { commitment });
67
        }
68
    }
69
}