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
    tp_traits::{apply, derive_scale_codec, derive_storage_traits},
20
};
21

            
22
/// Old profile structure.
23
/// Keep it until migration is removed.
24
#[apply(derive_scale_codec)]
25
#[derive(RuntimeDebugNoBound, PartialEqNoBound, EqNoBound, CloneNoBound, MaxEncodedLen)]
26
#[scale_info(skip_type_params(T))]
27
pub struct OldProfile<T: Config> {
28
    pub url: BoundedVec<u8, T::MaxStringLen>,
29
    pub para_ids: ParaIdsFilter<T>,
30
    pub mode: ProfileMode,
31
    pub assignment_request: ProviderRequestOf<T>,
32
}
33

            
34
#[apply(derive_storage_traits)]
35
#[derive(MaxEncodedLen, DecodeWithMemTracking)]
36
pub enum ProfileMode {
37
    Bootnode,
38
    Rpc { supports_ethereum_rpcs: bool },
39
}
40

            
41
/// Profile with additional data:
42
/// - the account id which created (and manage) the profile
43
/// - the amount deposited to register the profile
44
#[apply(derive_scale_codec)]
45
#[derive(RuntimeDebugNoBound, PartialEqNoBound, EqNoBound, CloneNoBound, MaxEncodedLen)]
46
#[scale_info(skip_type_params(T))]
47
pub struct OldRegisteredProfile<T: Config> {
48
    pub account: T::AccountId,
49
    pub deposit: BalanceOf<T>,
50
    pub profile: OldProfile<T>,
51
    /// There can be at most 1 assignment per profile.
52
    pub assignment: Option<(ParaId, AssignmentWitnessOf<T>)>,
53
}
54

            
55
1
pub fn migrate_profiles_content<T: Config>(_available_weight: Weight) -> Weight {
56
1
    let mut count = 0;
57

            
58
3
    crate::Profiles::<T>::translate(|_key, profile: OldRegisteredProfile<T>| {
59
3
        count += 1;
60

            
61
        let OldRegisteredProfile {
62
3
            account,
63
3
            deposit,
64
3
            profile,
65
3
            assignment,
66
3
        } = profile;
67

            
68
3
        let mut direct_rpc_urls = BoundedVec::new();
69
3
        let mut bootnode_url = None;
70
3
        let mut node_type = NodeType::Substrate;
71

            
72
3
        match profile.mode {
73
1
            ProfileMode::Bootnode => {
74
1
                bootnode_url = Some(profile.url);
75
1
            }
76
            ProfileMode::Rpc {
77
2
                supports_ethereum_rpcs,
78
            } => {
79
2
                direct_rpc_urls
80
2
                    .try_push(profile.url)
81
2
                    .expect("limit to be at least 1");
82

            
83
2
                if supports_ethereum_rpcs {
84
1
                    node_type = NodeType::Frontier
85
1
                };
86
            }
87
        }
88

            
89
3
        Some(RegisteredProfile {
90
3
            account,
91
3
            deposit,
92
3
            assignment,
93
3
            profile: Profile {
94
3
                para_ids: profile.para_ids,
95
3
                assignment_request: profile.assignment_request,
96
3
                direct_rpc_urls,
97
3
                proxy_rpc_urls: BoundedVec::new(),
98
3
                bootnode_url,
99
3
                node_type,
100
3
                additional_info: BoundedVec::new(),
101
3
            },
102
3
        })
103
3
    });
104

            
105
1
    let db_weights = T::DbWeight::get();
106
1
    db_weights.reads_writes(count, count)
107
1
}