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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
//! # Equilibrium Rate Pallet
//!
//! Equilibrium's Rate Pallet is a Substrate module for processing
//! different fee payments and keeping account balances up to date

#![cfg_attr(not(feature = "std"), no_std)]

mod mock;
pub mod reinit_extension;
mod tests;

pub use eq_oracle;

use eq_bailsman::{BailsmanManager, LtvChecker};
use eq_balances::{BalanceGetter, SignedBalance};
use eq_primitives::{currency::Currency, FeeManager};
use eq_treasury::EqBuyout;
use eq_utils::{eq_ensure, log::eq_log, ok_or_error};
use frame_support::{
    codec::{Decode, Encode},
    debug, decl_error, decl_event, decl_module, decl_storage,
    dispatch::{DispatchError, DispatchResult},
    traits::{Get, OnKilledAccount, OnNewAccount, UnixTime},
    Parameter,
};
use sp_application_crypto::RuntimeAppPublic;
use sp_arithmetic::{FixedI128, FixedI64, FixedPointNumber};
use sp_core::crypto::KeyTypeId;
use sp_runtime::{
    offchain::storage::StorageValueRef,
    traits::{AccountIdConversion, AtLeast32Bit, MaybeSerializeDeserialize, Member, Zero},
    transaction_validity::{
        InvalidTransaction, TransactionPriority, TransactionSource, TransactionValidity,
        ValidTransaction,
    },
    ModuleId, RuntimeDebug,
};
use sp_std::convert::TryInto;
use sp_std::prelude::*;
use system as frame_system;
use system::offchain::{SendTransactionTypes, SubmitTransaction};
use system::{ensure_none, ensure_root, ensure_signed};

pub const KEY_TYPE: KeyTypeId = KeyTypeId(*b"rate");
const DB_PREFIX: &[u8] = b"eq-rate/";

pub type AuthIndex = u32;
type OffchainResult<A> = Result<A, OffchainErr>;

/// Module for crypto signatures
pub mod ed25519 {
    pub use super::KEY_TYPE;
    mod app_ed25519 {
        use sp_application_crypto::{app_crypto, ed25519};
        app_crypto!(ed25519, super::KEY_TYPE);
    }

    sp_application_crypto::with_pair! {
        pub type AuthorityPair = app_ed25519::Pair;
    }
    pub type AuthoritySignature = app_ed25519::Signature;
    pub type AuthorityId = app_ed25519::Public;
}

/// Transfers service data inside Rate Pallet functions
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)]
pub struct ReinitRequest<AccountId, BlockNumber>
where
    AccountId: PartialEq + Eq + Decode + Encode,
    BlockNumber: Decode + Encode,
{
    /// Block number at the time heartbeat is created
    pub account: AccountId,
    /// An index of the authority on the list of validators
    pub authority_index: AuthIndex,
    /// The length of session validator set
    pub validators_len: u32,
    /// Number of a block
    pub block_num: BlockNumber,
}

/// Substrate pallet configuration trait
pub trait Trait: SendTransactionTypes<Call<Self>> + pallet_session::Trait {
    // type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>; // todel

    /// Timestamp provider
    type UnixTime: UnixTime;
    /// Numerical representation of stored balances
    type Balance: Member
        + AtLeast32Bit
        + MaybeSerializeDeserialize
        + Parameter
        + Default
        + From<u64>
        + Into<u64>;
    /// Gets information about account balances
    type BalanceGetter: eq_balances::BalanceGetter<Self::AccountId, Self::Balance>;
    /// The identifier type for an authority.
    type AuthorityId: Member + Parameter + RuntimeAppPublic + Default + Ord;
    /// Used to integrate bailsman operations
    type BailsmanManager: BailsmanManager<Self::AccountId>;
    /// Calculates and checks LTV of user balances
    type LtvChecker: eq_bailsman::LtvChecker<Self::AccountId, Self::Balance>;
    /// Minimum new debt for system reinit
    type MinSurplus: Get<Self::Balance>;
    /// Minimum temp bailsmen balances for Bailsman pallet reinit
    type MinTempBailsman: Get<Self::Balance>;
    /// Receives currency prices from oracle
    type PriceGetter: eq_oracle::PriceGetter;
    /// A configuration for base priority of unsigned transactions.
    ///
    /// This is exposed so that it can be tuned for particular runtime, when
    /// multiple pallets send unsigned transactions.
    type UnsignedPriority: Get<TransactionPriority>;
    /// Manages all fee calculations and withdrawal
    type FeeManager: FeeManager<Self::AccountId, Self::Balance>;
    /// Manager for treasury Eq exchanging transactions
    type EqBuyout: EqBuyout<Self::AccountId, Self::Balance>;
    /// Gets bailsman module account
    type BailsmanModuleId: Get<ModuleId>;
}

decl_storage! {
    trait Store for Module<T: Trait> as EqRate {
        /// Pallet storage for keys
        Keys get(fn keys): Vec<T::AuthorityId>;
        /// Pallet storage - last update timestamps for each `AccountId` that has balances
        pub LastFeeUpdate get (fn last_fee_update): map hasher(blake2_128_concat) T::AccountId => u64;
        /// Pallet storage used for time offset in test builds
        pub NowMillisOffset get(fn now_millis_offset): u64;
    }
    add_extra_genesis {
        config(keys): Vec<T::AuthorityId>;
        build(|config| Module::<T>::initialize_keys(&config.keys))
    }
}

decl_error! {
    /// Pallet's errors
    pub enum Error for Module<T: Trait> {
        /// Error used during time offset in test builds
        InvalidOffset,
    }
}

/// Errors for offchain worker operations
#[allow(dead_code)]
enum OffchainErr {
    /// The signature is invalid
    FailedSigning,
    /// Transaction was not submitted
    SubmitTransaction,
}

decl_module! {
    pub struct Module<T: Trait> for enum Call where origin: T::Origin {
        type Error = Error<T>;

        // fn deposit_event() = default; // todel

        #[weight = 10_000]
        /// Request to check account balance for margin call and withdraw fees.
        /// This function is executed in code of other pallets
        pub fn reinit(origin,
            request: ReinitRequest<T::AccountId, T::BlockNumber>,
            // since signature verification is done in `validate_unsigned`
            // we can skip doing it here again.
            _signature: <T::AuthorityId as RuntimeAppPublic>::Signature) {
            ensure_none(origin)?;

            eq_log!(
                "reinit for {:?} by {:?} with len {}",
                request.account,
                request.authority_index,
                request.validators_len);


            #[allow(unused_must_use)]{
                Self::_reinit(&request.account);
            }
        }

        #[weight = 10_000]
        /// Request to check and redistribute Bailsman pallet balances. This
        /// function is executed in code of other pallets
        pub fn reinit_bailsman(origin,
            request: ReinitRequest<T::AccountId, T::BlockNumber>,
            // since signature verification is done in `validate_unsigned`
            // we can skip doing it here again.
            _signature: <T::AuthorityId as RuntimeAppPublic>::Signature) {
            ensure_none(origin)?;

            eq_log!(
                "reinit_bailsman by {:?} with len {}",
                request.authority_index,
                request.validators_len);
            T::BailsmanManager::reinit();
        }

        #[weight = 10_000]
        /// Request to check account balance for margin call and withdraw fees.
        /// This function is used by any user and executed by substrate transaction
        pub fn reinit_external(origin, owner: <T as system::Trait>::AccountId) -> Result<(),DispatchError>
        {
            ensure_signed(origin)?;
            #[allow(unused_must_use)]
            {
                Self::_reinit(&owner);
            }
            Ok(())
        }

        #[weight = 10_000]
        /// Function used in test builds
        pub fn set_now_millis_offset(origin, offset: u64) -> Result<(),DispatchError>
        {
            ensure_root(origin)?;
            let current_offset = NowMillisOffset::get();
            eq_ensure!(offset > current_offset, Error::<T>::InvalidOffset,
            "{}:{}. Offset to set is lower than current. Offset: {:?}, current offset: {:?}.",
            file!(), line!(), offset, current_offset);
            NowMillisOffset::put(offset);
            eq_log!("Time offset set to {} seconds", offset / 1000);
            Ok(())
        }

        /// Used by offchain workers to check system state and initiate system
        /// events
        fn offchain_worker(now: T::BlockNumber) {
            const LOCKED: () = ();
            // Only send messages if we are a potential validator.
            if sp_io::offchain::is_validator() {
                let key = DB_PREFIX.to_vec();
                let mut storage = StorageValueRef::persistent(&key);
                let can_process = storage.mutate(|is_locked:  Option<Option<bool>>| {
                    match is_locked {
                        Some(Some(true)) => {
                            Err(LOCKED)
                        },
                        _ => Ok(true)
                    }
                });

                match can_process {
                    Ok(Ok(true)) => {
                        for res in Self::check_accounts(now).ok().unwrap() {
                            match res {
                                Ok(_) => {
                                    // eq_log!("offchain_worker:success")
                                },
                                Err(_) => {
                                    // eq_log!("offchain_worker:error")
                                },
                            }
                        }
                        storage.clear();
                    }
                    _ => {
                        // eq_log!("offchain_worker:locked");
                    }
                }
            } else {
                debug::trace!(
                    target: "eqrate",
                    "Skipping reinit at {:?}. Not a validator.",
                    now,
                )
            }
        }
    }
}

impl<T: Trait> Module<T> {
    fn initialize_keys(keys: &[T::AuthorityId]) {
        if !keys.is_empty() {
            assert!(Keys::<T>::get().is_empty(), "Keys are already initialized!");
            Keys::<T>::put(keys);
        }
    }

    #[cfg(test)]
    #[allow(dead_code)]
    fn set_keys(keys: Vec<T::AuthorityId>) {
        Keys::<T>::put(&keys)
    }

    //#[cfg(test)]
    pub fn set_last_update(accounts: Vec<&T::AccountId>) {
        let offset = NowMillisOffset::get();
        let duration = core::time::Duration::from_millis(offset);
        let now = T::UnixTime::now().as_secs() + duration.as_secs();
        for account in accounts {
            <LastFeeUpdate<T>>::insert(account, now);
        }
    }

    fn check_accounts(
        block_number: T::BlockNumber,
    ) -> OffchainResult<impl Iterator<Item = OffchainResult<()>>> {
        // check if already (Lock)

        let validators_len = <pallet_session::Module<T>>::validators().len() as u32;

        let keys = Self::local_authority_keys();

        let res = Ok(keys.map(move |(authority_index, key)| {
            Self::check_accounts_for_single_auth(authority_index, key, block_number, validators_len)
        }));
        res
    }

    // why not 1 key?
    fn check_accounts_for_single_auth(
        authority_index: u32,
        key: T::AuthorityId,
        block_number: T::BlockNumber,
        validators_len: u32,
    ) -> OffchainResult<()> {
        // calc fee for acc % len = index
        let bailman_acc_id: T::AccountId = T::BailsmanModuleId::get().into_account();
        for (_, balance) in T::BalanceGetter::iterate_balances()
            .iter()
            .enumerate()
            .filter(|(index, balance)| {
                (*index as u32) % validators_len == authority_index && *balance.0 != bailman_acc_id
            })
        {
            // eq_log!("check reinit {:?} {:?}", authority_index, balance.0.clone());
            let last_update = <LastFeeUpdate<T>>::get(&balance.0);
            let debt = match T::FeeManager::calc_fee(&balance.0, &last_update) {
                Ok(d) => d,
                Err(_) => continue,
            };
            let zero_balance = From::<u64>::from(0 as u64);
            let change: SignedBalance<T::Balance> = SignedBalance::Positive(zero_balance);
            // dont margincall if check_ltv returns Err
            let good_position =
                T::LtvChecker::check_ltv(&balance.0, &change, &Currency::Usd).unwrap_or(true);
            if debt.clone() > T::MinSurplus::get() || !good_position {
                let reinit_data = ReinitRequest::<T::AccountId, T::BlockNumber> {
                    account: balance.0.clone(),
                    authority_index,
                    validators_len,
                    block_num: block_number,
                };
                eq_log!("try to reinit {:?}", balance.0.clone());
                let option_signature = key.sign(&reinit_data.encode());
                let signature = ok_or_error!(option_signature, OffchainErr::FailedSigning,
                "{}:{}. Couldn't sign. Key: {:?}, request account: {:?}, authority_index: {:?}, validators_len: {:?}, block_num:{:?}.", 
                file!(), line!(), key, &reinit_data.account, &reinit_data.authority_index, &reinit_data.validators_len, &reinit_data.block_num)?;
                let acc = reinit_data.account.clone();
                let index = reinit_data.authority_index.clone();
                let len = reinit_data.validators_len.clone();
                let block = reinit_data.block_num.clone();
                let sign = signature.clone();
                let call = Call::reinit(reinit_data, signature);
                SubmitTransaction::<T, Call<T>>::submit_unsigned_transaction(call.into())
                    .map_err(|_| {
                        debug::error!("{}:{}. Submit reinit error. Signature: {:?}, request account: {:?}, authority_index: {:?}, validators_len: {:?}, block_num:{:?}.", 
                        file!(), line!(), sign, acc, index, len, block);
                        OffchainErr::SubmitTransaction
                    })?;
            } else {
                eq_log!("no need to reinit {:?}", balance.0.clone());
            }
        }

        let accuracy = FixedI128::accuracy() / FixedI64::accuracy() as i128;
        let bailsman_temp_balance = T::BailsmanManager::get_temp_balances_usd();
        let min_aggregates =
            FixedI128::from_inner(Into::<u64>::into(T::MinTempBailsman::get()) as i128 * accuracy);
        // maybe abs(sum(aggregates)) ?
        // if block is not u64, always first validator?
        let block_number_u = TryInto::<u64>::try_into(block_number).unwrap_or(0);
        let is_my_block = (block_number_u) % (validators_len as u64) == authority_index as u64; //  only 1 validator can reinit bailsman in  block
        let need_to_reinit_bailsman = bailsman_temp_balance
            .map(|balance| is_my_block && (balance.saturating_abs() > min_aggregates))
            .unwrap_or(false);
        if need_to_reinit_bailsman {
            let reinit_data = ReinitRequest::<T::AccountId, T::BlockNumber> {
                account: Default::default(),
                authority_index,
                validators_len,
                block_num: block_number,
            };
            let option_signature = key.sign(&reinit_data.encode());
            let signature = ok_or_error!(option_signature, OffchainErr::FailedSigning,
            "{}:{}. Couldn't sign. Key: {:?}, request account: {:?}, authority_index: {:?}, validators_len: {:?}, block_num:{:?}.", 
            file!(), line!(), key, &reinit_data.account, &reinit_data.authority_index, &reinit_data.validators_len, &reinit_data.block_num)?;
            let acc = reinit_data.account.clone();
            let index = reinit_data.authority_index.clone();
            let len = reinit_data.validators_len.clone();
            let block = reinit_data.block_num.clone();
            let sign = signature.clone();
            let call = Call::reinit_bailsman(reinit_data, signature);
            SubmitTransaction::<T, Call<T>>::submit_unsigned_transaction(call.into())
                .map_err(|_| {
                    debug::error!("{}:{}. Submit reinit bailsman error. Signature: {:?}, request account: {:?}, authority_index: {:?}, validators_len: {:?}, block_num:{:?}.", 
                    file!(), line!(), sign, acc, index, len, block);
                    OffchainErr::SubmitTransaction
                })?;
        }
        Ok(())
    }

    fn local_authority_keys() -> impl Iterator<Item = (u32, T::AuthorityId)> {
        let authorities = Keys::<T>::get();
        let mut local_keys = T::AuthorityId::all();

        local_keys.sort();

        authorities
            .into_iter()
            .enumerate()
            .filter_map(move |(index, authority)| {
                local_keys
                    .binary_search(&authority)
                    .ok()
                    .map(|location| (index as u32, local_keys[location].clone()))
            })
    }

    fn try_margincall(
        owner: &<T as system::Trait>::AccountId,
        can_margincall_good_position: bool,
    ) -> Result<(), DispatchError> {
        let change: SignedBalance<T::Balance> = SignedBalance::zero();
        let good_position = T::LtvChecker::check_ltv(&owner, &change, &Currency::Usd)?;
        if good_position && !can_margincall_good_position {
            return Err({
                // no need to log, when try mc good position
                DispatchError::Other("This is a good position.")
            });
        }

        eq_log!("margincall owner='{:?}' position.", owner);
        T::BailsmanManager::receive_position(owner);

        Ok(())
    }

    fn _reinit(owner: &<T as system::Trait>::AccountId) -> Result<(), Error<T>> {
        let bailman_acc_id: T::AccountId = T::BailsmanModuleId::get().into_account();
        if bailman_acc_id == *owner {
            return Ok(());
        }
        eq_log!("REINIT!!!! {:?}", owner);
        let last_update = <LastFeeUpdate<T>>::get(owner);
        if Self::try_margincall(owner, false).is_ok() {
            Self::set_last_update(vec![owner]);
            // eq_log!(
            //     "margincalled owner='{:?}' position before rates applied {:?}.",
            //     owner,
            //     last_update
            // );
            return Ok(());
        }
        let debt = T::FeeManager::charge_fee(owner, &last_update);
        #[allow(unused_must_use)]
        if let Ok(_) = debt {
            let current_eq_balance =
                T::BalanceGetter::get_balance(owner, &eq_primitives::currency::Currency::Eq);

            if let SignedBalance::Negative(negative_current_eq) = current_eq_balance {
                eq_log!("buyout {:?}", negative_current_eq);
                T::EqBuyout::eq_buyout(owner, negative_current_eq);
            }

            Self::set_last_update(vec![owner]);
            Self::try_margincall(owner, false);
        // eq_log!(
        //     "margincalled owner='{:?}' position after rates {:?}.",
        //     owner,
        //     debt
        // );
        } else {
            if last_update == 0 {
                // we need to init account
                Self::set_last_update(vec![owner]);
            }
        }
        Ok(())
    }
}

impl<T: Trait> OnKilledAccount<T::AccountId> for Module<T> {
    #[allow(unused_must_use)]
    fn on_killed_account(who: &T::AccountId) {
        Self::try_margincall(who, true);
    }
}

/// Sets timestamp of last update on account creation
impl<T: Trait> OnNewAccount<T::AccountId> for Module<T> {
    fn on_new_account(who: &T::AccountId) {
        Self::set_last_update(vec![who]);
    }
}

impl<T: Trait> sp_runtime::BoundToRuntimeAppPublic for Module<T> {
    type Public = T::AuthorityId;
}

impl<T: Trait> pallet_session::OneSessionHandler<T::AccountId> for Module<T> {
    type Key = T::AuthorityId;

    fn on_genesis_session<'a, I: 'a>(validators: I)
    where
        I: Iterator<Item = (&'a T::AccountId, T::AuthorityId)>,
    {
        let keys = validators.map(|x| x.1).collect::<Vec<_>>();
        Self::initialize_keys(&keys);
    }

    fn on_new_session<'a, I: 'a>(_changed: bool, validators: I, _queued_validators: I)
    where
        I: Iterator<Item = (&'a T::AccountId, T::AuthorityId)>,
    {
        // Remember who the authorities are for the new session.
        //  check changed?
        Keys::<T>::put(validators.map(|x| x.1).collect::<Vec<_>>());
    }

    fn on_disabled(_i: usize) {
        // ignore
    }
}

const INVALID_VALIDATORS_LEN: u8 = 10;

impl<T: Trait> frame_support::unsigned::ValidateUnsigned for Module<T> {
    type Call = Call<T>;

    fn validate_unsigned(_source: TransactionSource, call: &Self::Call) -> TransactionValidity {
        if let Call::reinit(request, signature) = call {
            // verify that the incoming (unverified) pubkey is actually an authority id
            let keys = Keys::<T>::get();
            if keys.len() as u32 != request.validators_len {
                return InvalidTransaction::Custom(INVALID_VALIDATORS_LEN).into();
            }
            let authority_id = match keys.get(request.authority_index as usize) {
                Some(id) => id,
                None => return InvalidTransaction::BadProof.into(),
            };

            // check signature (this is expensive so we do it last).
            let signature_valid = request.using_encoded(|encoded_heartbeat| {
                authority_id.verify(&encoded_heartbeat, &signature)
            });

            if !signature_valid {
                return InvalidTransaction::BadProof.into();
            }

            ValidTransaction::with_tag_prefix("EqFee")
                .priority(T::UnsignedPriority::get())
                .and_provides((request.account.clone(), request.block_num))
                .longevity(64) // TODO add to config
                .propagate(true)
                .build()
        } else if let Call::reinit_bailsman(request, signature) = call {
            let keys = Keys::<T>::get();
            if keys.len() as u32 != request.validators_len {
                return InvalidTransaction::Custom(INVALID_VALIDATORS_LEN).into();
            }
            let authority_id = match keys.get(request.authority_index as usize) {
                Some(id) => id,
                None => return InvalidTransaction::BadProof.into(),
            };

            // check signature (this is expensive so we do it last).
            let signature_valid = request.using_encoded(|encoded_heartbeat| {
                authority_id.verify(&encoded_heartbeat, &signature)
            });

            if !signature_valid {
                return InvalidTransaction::BadProof.into();
            }
            ValidTransaction::with_tag_prefix("EqBails")
                .priority(T::UnsignedPriority::get())
                .and_provides(request.block_num)
                .longevity(64) // TODO add to config
                .propagate(true)
                .build()
        } else {
            InvalidTransaction::Call.into()
        }
    }
}

impl<T: Trait> UnixTime for Module<T> {
    fn now() -> core::time::Duration {
        let offset = NowMillisOffset::get();
        let duration = core::time::Duration::from_millis(offset);
        let now = T::UnixTime::now();

        now + duration
    }
}