casper_types/chainspec/
accounts_config.rs

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
//! The accounts config is a set of configuration options that is used to create accounts at
//! genesis, and set up auction contract with validators and delegators.
mod account_config;
mod delegator_config;
mod genesis;
mod validator_config;
#[cfg(feature = "datasize")]
use datasize::DataSize;
use serde::{Deserialize, Deserializer, Serialize};

#[cfg(any(feature = "testing", test))]
use crate::testing::TestRng;
use crate::{
    bytesrepr::{self, FromBytes, ToBytes},
    PublicKey,
};

pub use account_config::AccountConfig;
pub use delegator_config::DelegatorConfig;
pub use genesis::{AdministratorAccount, GenesisAccount, GenesisValidator};
pub use validator_config::ValidatorConfig;

fn sorted_vec_deserializer<'de, T, D>(deserializer: D) -> Result<Vec<T>, D::Error>
where
    T: Deserialize<'de> + Ord,
    D: Deserializer<'de>,
{
    let mut vec = Vec::<T>::deserialize(deserializer)?;
    vec.sort_unstable();
    Ok(vec)
}

/// Configuration values associated with accounts.toml
#[derive(PartialEq, Eq, Serialize, Deserialize, Debug, Clone, Default)]
#[cfg_attr(feature = "datasize", derive(DataSize))]
pub struct AccountsConfig {
    #[serde(deserialize_with = "sorted_vec_deserializer")]
    accounts: Vec<AccountConfig>,
    #[serde(default, deserialize_with = "sorted_vec_deserializer")]
    delegators: Vec<DelegatorConfig>,
    #[serde(
        default,
        deserialize_with = "sorted_vec_deserializer",
        skip_serializing_if = "Vec::is_empty"
    )]
    administrators: Vec<AdministratorAccount>,
}

impl AccountsConfig {
    /// Create new accounts config instance.
    pub fn new(
        accounts: Vec<AccountConfig>,
        delegators: Vec<DelegatorConfig>,
        administrators: Vec<AdministratorAccount>,
    ) -> Self {
        Self {
            accounts,
            delegators,
            administrators,
        }
    }

    /// Accounts.
    pub fn accounts(&self) -> &[AccountConfig] {
        &self.accounts
    }

    /// Delegators.
    pub fn delegators(&self) -> &[DelegatorConfig] {
        &self.delegators
    }

    /// Administrators.
    pub fn administrators(&self) -> &[AdministratorAccount] {
        &self.administrators
    }

    /// Account.
    pub fn account(&self, public_key: &PublicKey) -> Option<&AccountConfig> {
        self.accounts
            .iter()
            .find(|account| &account.public_key == public_key)
    }

    /// All of the validators.
    pub fn validators(&self) -> impl Iterator<Item = &AccountConfig> {
        self.accounts
            .iter()
            .filter(|account| account.validator.is_some())
    }

    /// Is the provided public key in the set of genesis validator public keys.
    pub fn is_genesis_validator(&self, public_key: &PublicKey) -> bool {
        match self.account(public_key) {
            None => false,
            Some(account_config) => account_config.is_genesis_validator(),
        }
    }

    #[cfg(any(feature = "testing", test))]
    /// Generates a random instance using a `TestRng`.
    pub fn random(rng: &mut TestRng) -> Self {
        use rand::Rng;

        use crate::Motes;

        let alpha = AccountConfig::random(rng);
        let accounts = vec![
            alpha.clone(),
            AccountConfig::random(rng),
            AccountConfig::random(rng),
            AccountConfig::random(rng),
        ];

        let mut delegator = DelegatorConfig::random(rng);
        delegator.validator_public_key = alpha.public_key;

        let delegators = vec![delegator];

        let admin_balance: u32 = rng.gen();
        let administrators = vec![AdministratorAccount::new(
            PublicKey::random(rng),
            Motes::new(admin_balance),
        )];

        AccountsConfig {
            accounts,
            delegators,
            administrators,
        }
    }
}

impl ToBytes for AccountsConfig {
    fn to_bytes(&self) -> Result<Vec<u8>, bytesrepr::Error> {
        let mut buffer = bytesrepr::allocate_buffer(self)?;
        buffer.extend(self.accounts.to_bytes()?);
        buffer.extend(self.delegators.to_bytes()?);
        buffer.extend(self.administrators.to_bytes()?);
        Ok(buffer)
    }

    fn serialized_length(&self) -> usize {
        self.accounts.serialized_length()
            + self.delegators.serialized_length()
            + self.administrators.serialized_length()
    }
}

impl FromBytes for AccountsConfig {
    fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), bytesrepr::Error> {
        let (accounts, remainder) = FromBytes::from_bytes(bytes)?;
        let (delegators, remainder) = FromBytes::from_bytes(remainder)?;
        let (administrators, remainder) = FromBytes::from_bytes(remainder)?;
        let accounts_config = AccountsConfig::new(accounts, delegators, administrators);
        Ok((accounts_config, remainder))
    }
}

impl From<AccountsConfig> for Vec<GenesisAccount> {
    fn from(accounts_config: AccountsConfig) -> Self {
        let mut genesis_accounts = Vec::with_capacity(accounts_config.accounts.len());
        for account_config in accounts_config.accounts {
            let genesis_account = account_config.into();
            genesis_accounts.push(genesis_account);
        }
        for delegator_config in accounts_config.delegators {
            let genesis_account = delegator_config.into();
            genesis_accounts.push(genesis_account);
        }

        for administrator_config in accounts_config.administrators {
            let administrator_account = administrator_config.into();
            genesis_accounts.push(administrator_account);
        }

        genesis_accounts
    }
}

#[cfg(any(feature = "testing", test))]
mod tests {
    #[cfg(test)]
    use crate::{bytesrepr, testing::TestRng, AccountsConfig};

    #[test]
    fn serialization_roundtrip() {
        let mut rng = TestRng::new();
        let accounts_config = AccountsConfig::random(&mut rng);
        bytesrepr::test_serialization_roundtrip(&accounts_config);
    }
}