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
use crate::traits::{AccessGenesis, VerifyShape};
use codec::{Decode, Encode};
use frame_support::Parameter;
use sp_runtime::{traits::Zero, RuntimeDebug};
use sp_std::prelude::*;

#[derive(PartialEq, Eq, Default, Copy, Clone, Encode, Decode, RuntimeDebug)]
/// Atomic share profile reserves the total share amount every time but (might) have a limit on total reservations
pub struct AtomicShareProfile<Shares> {
    /// The total number of shares owned by this participant
    total: Shares,
    /// The reference count for the number of votes that this is used, initialized at 0
    times_reserved: u32,
    /// Tells us if the shares can be used in another vote
    locked: bool,
}

impl<
        Shares: Copy
            + Default
            + Parameter
            + sp_std::ops::Add<Output = Shares>
            + sp_std::ops::Sub<Output = Shares>
            + Zero,
    > AtomicShareProfile<Shares>
{
    pub fn total(&self) -> Shares {
        self.total
    }

    pub fn times_reserved(&self) -> u32 {
        self.times_reserved
    }

    pub fn is_zero(&self) -> bool {
        self.total == Shares::zero()
    }

    pub fn new_shares(total: Shares) -> AtomicShareProfile<Shares> {
        AtomicShareProfile {
            total,
            ..Default::default()
        }
    }

    pub fn add_shares(self, amount: Shares) -> AtomicShareProfile<Shares> {
        let total = self.total + amount;
        AtomicShareProfile { total, ..self }
    }

    pub fn subtract_shares(self, amount: Shares) -> AtomicShareProfile<Shares> {
        let total = self.total - amount;
        AtomicShareProfile { total, ..self }
    }

    pub fn iterate_times_reserved(self, amount: u32) -> AtomicShareProfile<Shares> {
        let times_reserved = self.times_reserved + amount;
        AtomicShareProfile {
            times_reserved,
            ..self
        }
    }

    pub fn decrement_times_reserved(self, amount: u32) -> AtomicShareProfile<Shares> {
        let times_reserved = self.times_reserved - amount;
        AtomicShareProfile {
            times_reserved,
            ..self
        }
    }

    pub fn lock(self) -> AtomicShareProfile<Shares> {
        AtomicShareProfile {
            locked: true,
            ..self
        }
    }

    pub fn unlock(self) -> AtomicShareProfile<Shares> {
        AtomicShareProfile {
            locked: false,
            ..self
        }
    }

    pub fn is_unlocked(&self) -> bool {
        !self.locked
    }
}

#[derive(PartialEq, Eq, Default, Clone, Encode, Decode, RuntimeDebug)]
/// The account ownership for the share genesis
pub struct SimpleShareGenesis<AccountId, Shares> {
    total: Shares,
    account_ownership: Vec<(AccountId, Shares)>,
}

impl<AccountId: Clone, Shares: Parameter + From<u32>> AccessGenesis<AccountId, Shares>
    for SimpleShareGenesis<AccountId, Shares>
{
    fn total(&self) -> Shares {
        self.total.clone()
    }
    fn account_ownership(&self) -> Vec<(AccountId, Shares)> {
        self.account_ownership.clone()
    }
}

impl<AccountId: Parameter, Shares: Parameter + From<u32> + sp_std::ops::AddAssign>
    From<Vec<(AccountId, Shares)>> for SimpleShareGenesis<AccountId, Shares>
{
    fn from(genesis: Vec<(AccountId, Shares)>) -> SimpleShareGenesis<AccountId, Shares> {
        let mut total: Shares = 0u32.into();
        let mut dedup_genesis = genesis;
        dedup_genesis.dedup(); // deduplicated
        for account_shares in dedup_genesis.clone() {
            total += account_shares.1;
        }
        SimpleShareGenesis {
            total,
            account_ownership: dedup_genesis,
        }
    }
}

impl<
        AccountId: Parameter,
        Shares: Copy + Parameter + From<u32> + sp_std::ops::Add<Output = Shares>,
    > VerifyShape for SimpleShareGenesis<AccountId, Shares>
{
    fn verify_shape(&self) -> bool {
        // TODO: clean up and optimize
        let mut sum: Shares = 0u32.into();
        for ac in self.account_ownership.iter() {
            sum = sum + ac.1
        }
        sum == self.total
    }
}