brush_contracts 1.8.0

Reusable implementations of contracts and traits for interaction with them.
Documentation
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
// Copyright (c) 2012-2022 Supercolony
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the"Software"),
// to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

pub use crate::{
    access_control::*,
    traits::timelock_controller::*,
};
use brush::{
    declare_storage_trait,
    modifier_definition,
    modifiers,
    traits::{
        AccountId,
        Flush,
        Hash,
        Timestamp,
        ZERO_ADDRESS,
    },
};
use core::convert::TryFrom;
pub use derive::TimelockControllerStorage;
use ink_env::{
    call::{
        build_call,
        Call,
        ExecutionInput,
    },
    hash::Blake2x256,
    CallFlags,
    DefaultEnvironment,
};
use ink_prelude::{
    vec,
    vec::Vec,
};
use ink_storage::Mapping;
use scale::Encode;

pub const STORAGE_KEY: [u8; 32] = ink_lang::blake2x256!("brush::TimelockControllerData");

#[derive(Default, Debug)]
#[brush::storage(STORAGE_KEY)]
pub struct TimelockControllerData {
    pub access_control: AccessControlData,
    pub min_delay: Timestamp,
    pub timestamps: Mapping<OperationId, Timestamp>,
    pub _reserved: Option<()>,
}

declare_storage_trait!(TimelockControllerStorage, TimelockControllerData);

impl<T: TimelockControllerStorage> AccessControlStorage for T {
    fn get(&self) -> &AccessControlData {
        &T::get(self).access_control
    }

    fn get_mut(&mut self) -> &mut AccessControlData {
        &mut T::get_mut(self).access_control
    }
}

/// Modifier to make a function callable only by a certain role. In
/// addition to checking the sender's role, zero account's role is also
/// considered. Granting a role to zero account is equivalent to enabling
/// this role for everyone.
#[modifier_definition]
pub fn only_role_or_open_role<T, F, R, E>(instance: &mut T, body: F, role: RoleType) -> Result<R, E>
where
    T: AccessControlStorage,
    F: FnOnce(&mut T) -> Result<R, E>,
    E: From<AccessControlError>,
{
    if !has_role(instance, &role, &ZERO_ADDRESS.into()) {
        check_role(instance, &role, &T::env().caller())?;
    }
    body(instance)
}

pub const TIMELOCK_ADMIN_ROLE: RoleType = ink_lang::selector_id!("TIMELOCK_ADMIN_ROLE");
pub const PROPOSER_ROLE: RoleType = ink_lang::selector_id!("PROPOSER_ROLE");
pub const EXECUTOR_ROLE: RoleType = ink_lang::selector_id!("EXECUTOR_ROLE");

pub const DONE_TIMESTAMP: Timestamp = 1;

impl<T: TimelockControllerStorage + Flush> TimelockController for T {
    default fn is_operation(&self, id: OperationId) -> bool {
        self.get_timestamp(id) > Timestamp::default()
    }

    default fn is_operation_pending(&self, id: OperationId) -> bool {
        self.get_timestamp(id) > Self::_done_timestamp()
    }

    default fn is_operation_ready(&self, id: OperationId) -> bool {
        let timestamp = self.get_timestamp(id);
        timestamp > Self::_done_timestamp() && timestamp <= Self::env().block_timestamp()
    }

    default fn is_operation_done(&self, id: OperationId) -> bool {
        self.get_timestamp(id) == Self::_done_timestamp()
    }

    default fn get_timestamp(&self, id: OperationId) -> Timestamp {
        TimelockControllerStorage::get(self)
            .timestamps
            .get(&id)
            .unwrap_or(Timestamp::default())
    }

    default fn get_min_delay(&self) -> Timestamp {
        TimelockControllerStorage::get(self).min_delay.clone()
    }

    default fn hash_operation(
        &self,
        transaction: Transaction,
        predecessor: Option<OperationId>,
        salt: [u8; 32],
    ) -> Hash {
        self._hash_operation(&transaction, &predecessor, &salt)
    }

    default fn hash_operation_batch(
        &self,
        transactions: Vec<Transaction>,
        predecessor: Option<OperationId>,
        salt: [u8; 32],
    ) -> Hash {
        self._hash_operation_batch(&transactions, &predecessor, &salt)
    }

    #[modifiers(only_role(Self::_proposal_role()))]
    default fn schedule(
        &mut self,
        transaction: Transaction,
        predecessor: Option<OperationId>,
        salt: [u8; 32],
        delay: Timestamp,
    ) -> Result<(), TimelockControllerError> {
        let id = self._hash_operation(&transaction, &predecessor, &salt);

        self._schedule(id, &delay)?;

        self._emit_call_scheduled_event(id, 0, transaction, predecessor, delay);
        Ok(())
    }

    #[modifiers(only_role(Self::_proposal_role()))]
    default fn schedule_batch(
        &mut self,
        transactions: Vec<Transaction>,
        predecessor: Option<OperationId>,
        salt: [u8; 32],
        delay: Timestamp,
    ) -> Result<(), TimelockControllerError> {
        let id = self._hash_operation_batch(&transactions, &predecessor, &salt);

        self._schedule(id, &delay)?;

        for (i, transaction) in transactions.into_iter().enumerate() {
            self._emit_call_scheduled_event(id.clone(), i as u8, transaction, predecessor.clone(), delay.clone());
        }
        Ok(())
    }

    #[modifiers(only_role(Self::_proposal_role()))]
    default fn cancel(&mut self, id: OperationId) -> Result<(), TimelockControllerError> {
        if !self.is_operation_pending(id) {
            return Err(TimelockControllerError::OperationCannonBeCanceled)
        }
        TimelockControllerStorage::get_mut(self).timestamps.remove(&id);

        self._emit_cancelled_event(id);
        Ok(())
    }

    #[modifiers(only_role_or_open_role(Self::_executor_role()))]
    default fn execute(
        &mut self,
        transaction: Transaction,
        predecessor: Option<OperationId>,
        salt: [u8; 32],
    ) -> Result<(), TimelockControllerError> {
        let id = self._hash_operation(&transaction, &predecessor, &salt);

        self._before_call(predecessor)?;
        self._call(id, 0, transaction)?;
        self._after_call(id)
    }

    #[modifiers(only_role_or_open_role(Self::_executor_role()))]
    default fn execute_batch(
        &mut self,
        transactions: Vec<Transaction>,
        predecessor: Option<OperationId>,
        salt: [u8; 32],
    ) -> Result<(), TimelockControllerError> {
        let id = self._hash_operation_batch(&transactions, &predecessor, &salt);

        self._before_call(predecessor)?;

        for (i, transaction) in transactions.into_iter().enumerate() {
            self._call(id, i as u8, transaction)?;
        }
        self._after_call(id)
    }

    default fn update_delay(&mut self, new_delay: Timestamp) -> Result<(), TimelockControllerError> {
        if Self::env().account_id() != Self::env().caller() {
            return Err(TimelockControllerError::CallerMustBeTimeLock)
        }

        let old_delay = TimelockControllerStorage::get(self).min_delay.clone();
        self._emit_min_delay_change_event(old_delay, new_delay);

        TimelockControllerStorage::get_mut(self).min_delay = new_delay;
        Ok(())
    }
}

pub trait TimelockControllerInternal {
    /// User must override this method in their contract.
    fn _emit_min_delay_change_event(&self, _old_delay: Timestamp, _new_delay: Timestamp);

    /// User must override this method in their contract.
    fn _emit_call_scheduled_event(
        &self,
        _id: OperationId,
        _index: u8,
        _transaction: Transaction,
        _predecessor: Option<OperationId>,
        _delay: Timestamp,
    );

    /// User must override this method in their contract.
    fn _emit_cancelled_event(&self, _id: OperationId);

    /// User must override this method in their contract.
    fn _emit_call_executed_event(&self, _id: OperationId, _index: u8, _transaction: Transaction);

    fn _init_with_caller(&mut self, min_delay: Timestamp, proposers: Vec<AccountId>, executors: Vec<AccountId>);

    fn _init_with_admin(
        &mut self,
        admin: AccountId,
        min_delay: Timestamp,
        proposers: Vec<AccountId>,
        executors: Vec<AccountId>,
    );

    fn _hash_operation(
        &self,
        transaction: &Transaction,
        predecessor: &Option<OperationId>,
        salt: &[u8; 32],
    ) -> OperationId;

    fn _hash_operation_batch(
        &self,
        transactions: &Vec<Transaction>,
        predecessor: &Option<OperationId>,
        salt: &[u8; 32],
    ) -> OperationId;

    /// Schedule an operation that is to becomes valid after a given delay.
    fn _schedule(&mut self, id: OperationId, delay: &Timestamp) -> Result<(), TimelockControllerError>;

    /// Checks before execution of an operation's calls.
    fn _before_call(&self, predecessor: Option<OperationId>) -> Result<(), TimelockControllerError>;

    /// Checks after execution of an operation's calls.
    fn _after_call(&mut self, id: OperationId) -> Result<(), TimelockControllerError>;

    /// Execute an operation's call.
    ///
    /// Emits a `CallExecuted` event.
    fn _call(&mut self, id: OperationId, i: u8, transaction: Transaction) -> Result<(), TimelockControllerError>;

    fn _timelock_admin_role() -> RoleType;

    fn _proposal_role() -> RoleType;

    fn _executor_role() -> RoleType;

    fn _done_timestamp() -> Timestamp;
}

impl<T: TimelockControllerStorage + Flush> TimelockControllerInternal for T {
    default fn _emit_min_delay_change_event(&self, _old_delay: Timestamp, _new_delay: Timestamp) {}

    default fn _emit_call_scheduled_event(
        &self,
        _id: OperationId,
        _index: u8,
        _transaction: Transaction,
        _predecessor: Option<OperationId>,
        _delay: Timestamp,
    ) {
    }

    default fn _emit_cancelled_event(&self, _id: OperationId) {}

    default fn _emit_call_executed_event(&self, _id: OperationId, _index: u8, _transaction: Transaction) {}

    default fn _init_with_caller(
        &mut self,
        min_delay: Timestamp,
        proposers: Vec<AccountId>,
        executors: Vec<AccountId>,
    ) {
        let caller = Self::env().caller();
        TimelockControllerInternal::_init_with_admin(self, caller, min_delay, proposers, executors);
    }

    default fn _init_with_admin(
        &mut self,
        admin: AccountId,
        min_delay: Timestamp,
        proposers: Vec<AccountId>,
        executors: Vec<AccountId>,
    ) {
        self._set_role_admin(Self::_timelock_admin_role(), Self::_timelock_admin_role());
        self._set_role_admin(Self::_proposal_role(), Self::_proposal_role());
        self._set_role_admin(Self::_executor_role(), Self::_executor_role());

        // admin + self administration
        self._setup_role(Self::_timelock_admin_role(), Self::env().account_id());
        self._setup_role(Self::_timelock_admin_role(), admin);

        // register proposers
        proposers
            .into_iter()
            .for_each(|proposer| self._setup_role(Self::_proposal_role(), proposer));
        // register executors
        executors
            .into_iter()
            .for_each(|executor| self._setup_role(Self::_executor_role(), executor));

        let old_delay = TimelockControllerStorage::get(self).min_delay.clone();
        TimelockControllerStorage::get_mut(self).min_delay = min_delay;
        self._emit_min_delay_change_event(old_delay, min_delay);
    }

    default fn _hash_operation(
        &self,
        transaction: &Transaction,
        predecessor: &Option<OperationId>,
        salt: &[u8; 32],
    ) -> OperationId {
        let mut hash_data: Vec<u8> = vec![];

        hash_data.append(&mut transaction.encode());
        if predecessor.is_some() {
            hash_data.append(&mut predecessor.unwrap().encode());
        }
        hash_data.append(&mut salt.encode());

        Hash::try_from(Self::env().hash_bytes::<Blake2x256>(&hash_data).as_ref()).unwrap()
    }

    default fn _hash_operation_batch(
        &self,
        transactions: &Vec<Transaction>,
        predecessor: &Option<OperationId>,
        salt: &[u8; 32],
    ) -> OperationId {
        let mut hash_data: Vec<u8> = vec![];

        hash_data.append(&mut transactions.encode());
        if predecessor.is_some() {
            hash_data.append(&mut predecessor.unwrap().encode());
        }
        hash_data.append(&mut salt.encode());

        Hash::try_from(Self::env().hash_bytes::<Blake2x256>(&hash_data).as_ref()).unwrap()
    }

    default fn _schedule(&mut self, id: OperationId, delay: &Timestamp) -> Result<(), TimelockControllerError> {
        if self.is_operation(id) {
            return Err(TimelockControllerError::OperationAlreadyScheduled)
        }
        if delay < &TimelockControllerStorage::get(self).min_delay {
            return Err(TimelockControllerError::InsufficientDelay)
        }

        TimelockControllerStorage::get_mut(self)
            .timestamps
            .insert(&id, &(Self::env().block_timestamp() + delay));
        Ok(())
    }

    default fn _before_call(&self, predecessor: Option<OperationId>) -> Result<(), TimelockControllerError> {
        if predecessor.is_some() && !self.is_operation_done(predecessor.unwrap()) {
            return Err(TimelockControllerError::MissingDependency)
        }
        Ok(())
    }

    default fn _after_call(&mut self, id: OperationId) -> Result<(), TimelockControllerError> {
        if !self.is_operation_ready(id) {
            return Err(TimelockControllerError::OperationIsNotReady)
        }

        TimelockControllerStorage::get_mut(self)
            .timestamps
            .insert(&id, &Self::_done_timestamp());
        Ok(())
    }

    default fn _call(
        &mut self,
        id: OperationId,
        i: u8,
        transaction: Transaction,
    ) -> Result<(), TimelockControllerError> {
        // Flush the state into storage before the cross call.
        // Because during cross call we cann call this contract(for example for `update_delay` method).
        self.flush();
        let result = build_call::<DefaultEnvironment>()
            .call_type(
                Call::new()
                    .callee(transaction.callee)
                    .gas_limit(transaction.gas_limit)
                    .transferred_value(transaction.transferred_value),
            )
            .exec_input(ExecutionInput::new(transaction.selector.into()).push_arg(CallInput(&transaction.input)))
            .returns::<()>()
            .call_flags(CallFlags::default().set_allow_reentry(true))
            .fire()
            .map_err(|_| TimelockControllerError::UnderlyingTransactionReverted);

        // Load the sate of the contract after the cross call.
        self.load();

        result?;
        self._emit_call_executed_event(id, i, transaction);
        Ok(())
    }

    default fn _timelock_admin_role() -> RoleType {
        TIMELOCK_ADMIN_ROLE
    }

    default fn _proposal_role() -> RoleType {
        PROPOSER_ROLE
    }

    default fn _executor_role() -> RoleType {
        EXECUTOR_ROLE
    }

    default fn _done_timestamp() -> Timestamp {
        DONE_TIMESTAMP
    }
}

/// A wrapper that allows us to encode a blob of bytes.
///
/// We use this to pass the set of untyped (bytes) parameters to the `CallBuilder`.
pub struct CallInput<'a>(&'a [u8]);

impl<'a> scale::Encode for CallInput<'a> {
    fn encode_to<T: scale::Output + ?Sized>(&self, dest: &mut T) {
        dest.write(self.0);
    }
}