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::*, frame_support::traits::EnqueueMessage, snowbridge_core::PRIMARY_GOVERNANCE_CHANNEL,
19
    snowbridge_outbound_queue_primitives::SendError, sp_std::marker::PhantomData,
20
};
21

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

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

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

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

            
47
84
        let message = ticket.message.as_bounded_slice();
48
84

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

            
58
84
        Ok(ticket.message_id)
59
84
    }
60
}