1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//! # ClientsInfo Module
//! Stores information about clients that comprise the network, such as vaults and oracles.

#![deny(warnings)]
#![cfg_attr(test, feature(proc_macro_hygiene))]
#![cfg_attr(not(feature = "std"), no_std)]

use codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;

mod default_weights;

pub use default_weights::WeightInfo;

use frame_support::{dispatch::DispatchResult, traits::Get, transactional, BoundedVec};

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;

#[cfg(test)]
mod mock;

type NameOf<T> = BoundedVec<u8, <T as pallet::Config>::MaxNameLength>;
type UriOf<T> = BoundedVec<u8, <T as pallet::Config>::MaxUriLength>;

#[derive(Encode, Decode, Eq, PartialEq, Clone, Default, TypeInfo, Debug, MaxEncodedLen)]
pub struct ClientRelease<Uri, Hash> {
    /// URI to the client release binary.
    pub uri: Uri,
    /// The SHA256 checksum of the client binary.
    pub checksum: Hash,
}

pub use pallet::*;

#[frame_support::pallet]
pub mod pallet {
    use crate::*;

    use frame_support::pallet_prelude::*;
    use frame_system::pallet_prelude::*;

    #[pallet::pallet]
    pub struct Pallet<T>(_);

    #[pallet::config]
    pub trait Config: frame_system::Config {
        /// The overarching event type.
        type RuntimeEvent: From<Event<Self>>
            + Into<<Self as frame_system::Config>::RuntimeEvent>
            + IsType<<Self as frame_system::Config>::RuntimeEvent>;

        /// Weight information for the extrinsics in this module.
        type WeightInfo: WeightInfo;

        /// The maximum length of a client name.
        #[pallet::constant]
        type MaxNameLength: Get<u32>;

        /// The maximum length of a client URI.
        #[pallet::constant]
        type MaxUriLength: Get<u32>;
    }

    #[pallet::hooks]
    impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
        fn on_runtime_upgrade() -> frame_support::weights::Weight {
            crate::upgrade_client_releases::try_upgrade_current_client_releases::<T>()
        }
    }

    #[pallet::call]
    impl<T: Config> Pallet<T> {
        /// Sets the current client release version, in case of a bug fix or patch.
        /// Clients include the vault, oracle, and faucet.
        ///
        /// # Arguments
        /// * `client_name` - raw byte string representation of the client name (e.g. `b"vault"`, `b"oracle"`,
        ///   `b"faucet"`)
        /// * `release` - The release information for the given `client_name`
        #[pallet::call_index(0)]
        #[pallet::weight(<T as Config>::WeightInfo::set_current_client_release(T::MaxNameLength::get(), T::MaxUriLength::get()))]
        #[transactional]
        pub fn set_current_client_release(
            origin: OriginFor<T>,
            client_name: NameOf<T>,
            release: ClientRelease<UriOf<T>, T::Hash>,
        ) -> DispatchResult {
            ensure_root(origin)?;
            CurrentClientReleases::<T>::insert(client_name, release.clone());
            Self::deposit_event(Event::<T>::ApplyClientRelease { release });
            Ok(())
        }

        /// Sets the pending client release version. To be batched alongside the
        /// `parachainSystem.authorizeUpgrade` Cumulus call.
        /// Clients include the vault, oracle, and faucet.
        ///
        /// # Arguments
        /// * `client_name` - raw byte string representation of the client name (e.g. `b"vault"`, `b"oracle"`,
        ///   `b"faucet"`)
        /// * `release` - The release information for the given `client_name`
        #[pallet::call_index(1)]
        #[pallet::weight(<T as Config>::WeightInfo::set_pending_client_release(T::MaxNameLength::get(), T::MaxUriLength::get()))]
        #[transactional]
        pub fn set_pending_client_release(
            origin: OriginFor<T>,
            client_name: NameOf<T>,
            release: ClientRelease<UriOf<T>, T::Hash>,
        ) -> DispatchResult {
            ensure_root(origin)?;
            PendingClientReleases::<T>::insert(client_name, release.clone());
            Self::deposit_event(Event::<T>::NotifyClientRelease { release });
            Ok(())
        }
    }

    #[pallet::event]
    #[pallet::generate_deposit(pub(super) fn deposit_event)]
    pub enum Event<T: Config> {
        NotifyClientRelease { release: ClientRelease<UriOf<T>, T::Hash> },
        ApplyClientRelease { release: ClientRelease<UriOf<T>, T::Hash> },
    }

    #[pallet::error]
    pub enum Error<T> {}

    /// Mapping of client name (string literal represented as bytes) to its release details.
    #[pallet::storage]
    #[pallet::getter(fn current_client_release)]
    pub(super) type CurrentClientReleases<T: Config> =
        StorageMap<_, Blake2_128Concat, NameOf<T>, ClientRelease<UriOf<T>, T::Hash>, OptionQuery>;

    /// Mapping of client name (string literal represented as bytes) to its pending release details.
    #[pallet::storage]
    #[pallet::getter(fn pending_client_release)]
    pub(super) type PendingClientReleases<T: Config> =
        StorageMap<_, Blake2_128Concat, NameOf<T>, ClientRelease<UriOf<T>, T::Hash>, OptionQuery>;
}

pub mod upgrade_client_releases {

    use crate::*;
    use frame_support::weights::Weight;

    /// For each pending client release, set the current release to that.
    /// The pending release entry is removed.
    pub fn try_upgrade_current_client_releases<T: Config>() -> Weight {
        let mut reads = 0;
        for (key, release) in PendingClientReleases::<T>::drain() {
            log::info!("Upgrading client release for key {:?}", key);
            CurrentClientReleases::<T>::insert(key, release.clone());
            Pallet::<T>::deposit_event(Event::<T>::ApplyClientRelease { release });
            reads += 1;
        }
        T::DbWeight::get().reads_writes(reads, reads * 2)
    }

    #[cfg(test)]
    #[test]
    fn test_client_pending_release_migration() {
        use crate::mock::Test;
        use sp_core::H256;
        use sp_std::vec;
        use std::collections::HashMap;

        crate::mock::run_test(|| {
            let vault_key = b"vault".to_vec();
            let oracle_key = b"oracle".to_vec();
            let faucet_key = b"faucet".to_vec();

            let pre_migration_pending_releases: HashMap<_, _> = vec![
                (
                    vault_key.clone(),
                    ClientRelease {
                        uri: BoundedVec::try_from(b"https://github.com/interlay/interbtc-clients/releases/download/1.15.0/vault-standalone-metadata"
                            .to_vec()).unwrap(),
                        checksum: H256::default(),
                    }
                ),
                (
                    oracle_key.clone(),
                    ClientRelease {
                        uri: BoundedVec::try_from(b"https://github.com/interlay/interbtc-clients/releases/download/1.15.0/oracle-standalone-metadata"
                            .to_vec()).unwrap(),
                        checksum: H256::default(),
                    }
                )
            ].into_iter().collect();
            pre_migration_pending_releases.iter().for_each(|(key, value)| {
                PendingClientReleases::<Test>::insert(BoundedVec::try_from(key.clone()).unwrap(), value.clone());
            });

            let pre_migration_current_releases: HashMap<_, _> = vec![
                (
                    vault_key.clone(),
                    ClientRelease {
                        uri: BoundedVec::try_from(b"https://github.com/interlay/interbtc-clients/releases/download/1.14.0/vault-standalone-metadata"
                            .to_vec()).unwrap(),
                        checksum: H256::default(),
                    }
                ),
                (
                    oracle_key.clone(),
                        ClientRelease {
                        uri: BoundedVec::try_from(b"https://github.com/interlay/interbtc-clients/releases/download/1.14.0/oracle-standalone-metadata"
                            .to_vec()).unwrap(),
                        checksum: H256::default(),
                    }
                ),
                (
                    faucet_key.clone(),
                    ClientRelease {
                        uri: BoundedVec::try_from(b"https://github.com/interlay/interbtc-clients/releases/download/1.14.0/faucet-standalone-metadata"
                            .to_vec()).unwrap(),
                        checksum: H256::default(),
                    }
                )
            ].into_iter().collect();
            pre_migration_current_releases.iter().for_each(|(key, value)| {
                CurrentClientReleases::<Test>::insert(BoundedVec::try_from(key.clone()).unwrap(), value.clone());
            });

            try_upgrade_current_client_releases::<Test>();

            let pending_releases = PendingClientReleases::<Test>::iter_values().collect::<Vec<_>>();
            assert_eq!(pending_releases.is_empty(), true);

            let current_releases = CurrentClientReleases::<Test>::iter()
                .map(|(key, value)| (key.to_vec(), value))
                .collect::<HashMap<_, _>>();
            assert_eq!(
                current_releases.get(&vault_key),
                pre_migration_pending_releases.get(&vault_key)
            );
            assert_eq!(
                current_releases.get(&oracle_key),
                pre_migration_pending_releases.get(&oracle_key)
            );
            // The faucet release should not be updated
            assert_eq!(
                current_releases.get(&faucet_key),
                pre_migration_current_releases.get(&faucet_key)
            );
        });
    }
}

pub mod v0 {
    use super::*;
    use frame_support::pallet_prelude::*;
    use sp_std::prelude::*;

    #[frame_support::storage_alias]
    pub(super) type CurrentClientReleases<T: Config> = StorageMap<
        Pallet<T>,
        Blake2_128Concat,
        Vec<u8>,
        ClientRelease<Vec<u8>, <T as frame_system::Config>::Hash>,
        OptionQuery,
    >;

    #[cfg(test)]
    #[test]
    fn test_decode_bounded_vec() {
        use crate::mock::Test;
        use sp_core::H256;

        crate::mock::run_test(|| {
            let key = vec![0; 255];

            CurrentClientReleases::<Test>::insert(
                key.clone(),
                ClientRelease {
                    uri: vec![1; 255],
                    checksum: H256::default(),
                },
            );

            let client_release = crate::CurrentClientReleases::<Test>::get(BoundedVec::try_from(key).unwrap());
            assert_eq!(client_release.map(|c| c.uri.to_vec()), Some(vec![1; 255]));
        });
    }
}