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
use {
18
    super::*,
19
    frame_support::traits::EnqueueMessage,
20
    snowbridge_core::{outbound::SendError, PRIMARY_GOVERNANCE_CHANNEL},
21
    sp_std::marker::PhantomData,
22
};
23

            
24
/// Alternative to [snowbridge_pallet_outbound_queue::Pallet::deliver] using a different
25
/// origin.
26
pub struct CustomSendMessage<T, GetAggregateMessageOrigin>(
27
    PhantomData<(T, GetAggregateMessageOrigin)>,
28
);
29

            
30
impl<T, GetAggregateMessageOrigin> DeliverMessage
31
    for CustomSendMessage<T, GetAggregateMessageOrigin>
32
where
33
    T: snowbridge_pallet_outbound_queue::Config,
34
    GetAggregateMessageOrigin:
35
        Convert<ChannelId, <T as snowbridge_pallet_outbound_queue::Config>::AggregateMessageOrigin>,
36
{
37
    type Ticket = Ticket<T>;
38

            
39
84
    fn deliver(ticket: Self::Ticket) -> Result<sp_core::H256, SendError> {
40
84
        let origin = GetAggregateMessageOrigin::convert(ticket.channel_id);
41
84

            
42
84
        if ticket.channel_id != PRIMARY_GOVERNANCE_CHANNEL {
43
            ensure!(
44
                !<snowbridge_pallet_outbound_queue::Pallet<T>>::operating_mode().is_halted(),
45
                SendError::Halted
46
            );
47
84
        }
48

            
49
84
        let message = ticket.message.as_bounded_slice();
50
84

            
51
84
        <T as snowbridge_pallet_outbound_queue::Config>::MessageQueue::enqueue_message(
52
84
            message, origin,
53
84
        );
54
84
        snowbridge_pallet_outbound_queue::Pallet::<T>::deposit_event(
55
84
            snowbridge_pallet_outbound_queue::Event::MessageQueued {
56
84
                id: ticket.message_id,
57
84
            },
58
84
        );
59
84

            
60
84
        Ok(ticket.message_id)
61
84
    }
62
}