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(test)]
18

            
19
use {
20
    crate::{
21
        bridge_to_ethereum_config::BridgeReward, filter_events, tests::common::*, BridgeRelayers,
22
        RuntimeEvent, SnowbridgeFeesAccount,
23
    },
24
    alloc::vec,
25
    frame_support::{assert_noop, assert_ok},
26
};
27

            
28
#[test]
29
1
fn test_register_new_relayer() {
30
1
    ExtBuilder::default()
31
1
        .with_balances(vec![
32
1
            // Alice gets 10k extra tokens for her mapping deposit
33
1
            (AccountId::from(ALICE), 210_000 * UNIT),
34
1
            (AccountId::from(BOB), 100_000 * UNIT),
35
1
            (AccountId::from(CHARLIE), 100_000 * UNIT),
36
1
            (AccountId::from(DAVE), 100_000 * UNIT),
37
1
        ])
38
1
        .build()
39
1
        .execute_with(|| {
40
1
            run_to_block(2);
41

            
42
1
            let relayer_origin =
43
1
                <Runtime as frame_system::Config>::RuntimeOrigin::signed(AccountId::from(BOB));
44

            
45
1
            assert_ok!(BridgeRelayers::register(relayer_origin.clone(), 100));
46

            
47
            // We expect 0 rewards for just registered relayer
48
1
            assert_noop!(
49
1
                BridgeRelayers::claim_rewards(
50
1
                    relayer_origin,
51
1
                    BridgeReward::SnowbridgeRewardInbound
52
                ),
53
1
                pallet_bridge_relayers::Error::<Runtime>::NoRewardForRelayer,
54
            );
55
1
        });
56
1
}
57

            
58
#[test]
59
1
fn relayer_can_claim_reward() {
60
1
    ExtBuilder::default()
61
1
        .with_balances(vec![
62
1
            // Alice gets 10k extra tokens for her mapping deposit
63
1
            (AccountId::from(ALICE), 210_000 * UNIT),
64
1
            (AccountId::from(BOB), 100_000 * UNIT),
65
1
            (AccountId::from(CHARLIE), 100_000 * UNIT),
66
1
            (AccountId::from(DAVE), 100_000 * UNIT),
67
1
            (SnowbridgeFeesAccount::get(), 100_000 * UNIT),
68
1
        ])
69
1
        .build()
70
1
        .execute_with(|| {
71
1
            run_to_block(2);
72

            
73
1
            let relayer_origin =
74
1
                <Runtime as frame_system::Config>::RuntimeOrigin::signed(AccountId::from(BOB));
75

            
76
1
            assert_ok!(BridgeRelayers::register(relayer_origin.clone(), 100));
77
1
            let reward_params = BridgeReward::SnowbridgeRewardInbound;
78

            
79
1
            pallet_bridge_relayers::RelayerRewards::<Runtime>::insert(
80
1
                AccountId::from(BOB),
81
1
                reward_params,
82
                100,
83
            );
84
1
            assert_ok!(BridgeRelayers::claim_rewards(
85
1
                RuntimeOrigin::signed(AccountId::from(BOB)),
86
1
                reward_params,
87
            ));
88

            
89
1
            assert_eq!(
90
1
                filter_events!(RuntimeEvent::BridgeRelayers(
91
                    pallet_bridge_relayers::Event::RewardPaid { .. },
92
                ))
93
1
                .count(),
94
                1,
95
                "RewardPaid event should be emitted!"
96
            );
97
1
        });
98
1
}
99

            
100
#[test]
101
1
fn relayer_register_doesnt_withdraw_from_rewards_account() {
102
1
    ExtBuilder::default()
103
1
        .with_balances(vec![
104
1
            // Alice gets 10k extra tokens for her mapping deposit
105
1
            (AccountId::from(ALICE), 210_000 * UNIT),
106
1
            (AccountId::from(BOB), 100_000 * UNIT),
107
1
            (AccountId::from(CHARLIE), 100_000 * UNIT),
108
1
            (AccountId::from(DAVE), 100_000 * UNIT),
109
1
        ])
110
1
        .build()
111
1
        .execute_with(|| {
112
1
            run_to_block(2);
113

            
114
1
            let balance_before = System::account(AccountId::from(BOB)).data.free;
115

            
116
1
            let relayer_origin =
117
1
                <Runtime as frame_system::Config>::RuntimeOrigin::signed(AccountId::from(BOB));
118

            
119
1
            assert_ok!(BridgeRelayers::register(relayer_origin.clone(), 100));
120

            
121
1
            assert_eq!(
122
1
                filter_events!(RuntimeEvent::BridgeRelayers(
123
                    pallet_bridge_relayers::Event::RegistrationUpdated { .. },
124
                ))
125
1
                .count(),
126
                1,
127
                "BridgeRelayers event should be emitted!"
128
            );
129

            
130
1
            let balance_after = System::account(AccountId::from(BOB)).data.free;
131

            
132
1
            assert_eq!(balance_before, balance_after);
133
1
        });
134
1
}
135

            
136
#[test]
137
1
fn relayer_deregister_is_working() {
138
1
    ExtBuilder::default()
139
1
        .with_balances(vec![
140
1
            // Alice gets 10k extra tokens for her mapping deposit
141
1
            (AccountId::from(ALICE), 210_000 * UNIT),
142
1
            (AccountId::from(BOB), 100_000 * UNIT),
143
1
            (AccountId::from(CHARLIE), 100_000 * UNIT),
144
1
            (AccountId::from(DAVE), 100_000 * UNIT),
145
1
        ])
146
1
        .build()
147
1
        .execute_with(|| {
148
1
            run_to_block(2);
149

            
150
1
            let balance_before = System::account(AccountId::from(BOB)).data.free;
151

            
152
1
            let relayer_origin =
153
1
                <Runtime as frame_system::Config>::RuntimeOrigin::signed(AccountId::from(BOB));
154

            
155
1
            let blocks_for_registration_to_be_active = 100;
156
1
            assert_ok!(BridgeRelayers::register(
157
1
                relayer_origin.clone(),
158
1
                blocks_for_registration_to_be_active
159
            ));
160

            
161
1
            assert_eq!(
162
1
                filter_events!(RuntimeEvent::BridgeRelayers(
163
                    pallet_bridge_relayers::Event::RegistrationUpdated { .. },
164
                ))
165
1
                .count(),
166
                1,
167
                "BridgeRelayers event should be emitted!"
168
            );
169

            
170
            // Skip some blocks to make sure registration is not active anymore
171
1
            run_to_block(blocks_for_registration_to_be_active + 5);
172

            
173
1
            assert_ok!(BridgeRelayers::deregister(relayer_origin.clone()));
174

            
175
1
            assert_eq!(
176
1
                filter_events!(RuntimeEvent::BridgeRelayers(
177
                    pallet_bridge_relayers::Event::Deregistered { .. },
178
                ))
179
1
                .count(),
180
                1,
181
                "Deregistered event should be emitted!"
182
            );
183

            
184
1
            let balance_after = System::account(AccountId::from(BOB)).data.free;
185

            
186
1
            assert_eq!(balance_before, balance_after);
187
1
        });
188
1
}