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
    babe_primitives::AuthorityId as BabeId,
19
    cumulus_primitives_core::relay_chain::{
20
        AccountId, AssignmentId, AuthorityDiscoveryId, ValidatorId,
21
    },
22
    frame_support::traits::OnIdle,
23
    frame_system::pallet_prelude::BlockNumberFor,
24
    frame_system::Pallet as SystemPallet,
25
    parity_scale_codec::Encode,
26
    sc_consensus_grandpa::AuthorityId as GrandpaId,
27
    sp_consensus_aura::AURA_ENGINE_ID,
28
    sp_core::{crypto::get_public_from_string_or_panic, sr25519},
29
    sp_runtime::generic::DigestItem,
30
    sp_runtime::traits::Convert,
31
    sp_runtime::Digest,
32
    sp_std::marker::PhantomData,
33
    sp_weights::Weight,
34
    xcm_emulator::{HeaderT, Network, Parachain, RelayChain},
35
};
36

            
37
pub mod accounts;
38
pub mod impls;
39
pub mod snowbridge;
40
pub mod validators;
41

            
42
2
pub fn force_process_bridge<R, P>(weight: Weight)
43
2
where
44
2
    R: RelayChain,
45
2
    P: Parachain<Network = R::Network>,
46
2
    R::Runtime: pallet_message_queue::Config,
47
2
{
48
2
    // Process MessageQueue on relay chain to consume the message we want to send to eth
49
2
    R::execute_with(|| {
50
2
        <pallet_message_queue::Pallet<R::Runtime>>::on_idle(
51
2
            SystemPallet::<R::Runtime>::block_number(),
52
2
            weight,
53
2
        );
54
2
    });
55
2

            
56
2
    // Execute empty block in parachain to trigger bridge message
57
2
    P::execute_with(|| {});
58
2
}
59

            
60
/// Helper function to generate stash, controller and session key from seed
61
4480
pub fn get_authority_keys_from_seed_no_beefy(
62
4480
    seed: &str,
63
4480
) -> (
64
4480
    AccountId,
65
4480
    AccountId,
66
4480
    BabeId,
67
4480
    GrandpaId,
68
4480
    ValidatorId,
69
4480
    AssignmentId,
70
4480
    AuthorityDiscoveryId,
71
4480
) {
72
4480
    (
73
4480
        get_public_from_string_or_panic::<sr25519::Public>(&format!("{}//stash", seed)).into(),
74
4480
        get_public_from_string_or_panic::<sr25519::Public>(seed).into(),
75
4480
        get_public_from_string_or_panic::<BabeId>(seed),
76
4480
        get_public_from_string_or_panic::<GrandpaId>(seed),
77
4480
        get_public_from_string_or_panic::<ValidatorId>(seed),
78
4480
        get_public_from_string_or_panic::<AssignmentId>(seed),
79
4480
        get_public_from_string_or_panic::<AuthorityDiscoveryId>(seed),
80
4480
    )
81
4480
}
82

            
83
pub struct TestDigestProvider<R: frame_system::Config, N: Network>(PhantomData<(R, N)>);
84

            
85
impl<R: frame_system::Config, N: Network> Convert<BlockNumberFor<R>, Digest> for TestDigestProvider<R, N>
86
where u64: From<<<<R as frame_system::Config>::Block as cumulus_primitives_core::BlockT>::Header as HeaderT>::Number> {
87
297
    fn convert(_block_number: BlockNumberFor<R>) -> Digest {
88
297
        let relay_block = N::relay_block_number();
89
297
        let slot = u64::from(relay_block.into());
90
297

            
91
297
        let new_slot_digest: Digest = Digest {
92
297
            logs: vec![
93
297
                DigestItem::PreRuntime(AURA_ENGINE_ID, slot.encode()),
94
297
            ],
95
297
        };
96
297
        new_slot_digest
97
297
    }
98
}