accounts.rs 11 KB
Newer Older
1
// Copyright 2018-2021 Parity Technologies (UK) Ltd.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::{
    super::{
        OffChainError,
        TypedEncodedError,
    },
    OffAccountId,
    OffBalance,
};
23
use crate::{
24
25
    Environment,
    Error,
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
};
use core::cell::Cell;
use derive_more::From;
use ink_prelude::collections::BTreeMap;
use ink_primitives::Key;

/// Errors encountered upon interacting with the accounts database.
#[derive(Debug, From, PartialEq, Eq)]
pub enum AccountError {
    TypedEncoded(TypedEncodedError),
    #[from(ignore)]
    UnexpectedUserAccount,
    #[from(ignore)]
    NoAccountForId(OffAccountId),
}

42
impl From<AccountError> for Error {
43
    fn from(account_error: AccountError) -> Self {
44
        Error::OffChain(OffChainError::Account(account_error))
45
46
47
48
49
50
51
    }
}

impl AccountError {
    /// Creates a new error to indicate a missing account.
    pub fn no_account_for_id<T>(account_id: &T::AccountId) -> Self
    where
52
        T: Environment,
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
    {
        Self::NoAccountForId(OffAccountId::new(account_id))
    }
}

impl From<scale::Error> for AccountError {
    fn from(err: scale::Error) -> Self {
        AccountError::TypedEncoded(err.into())
    }
}

/// Result type encountered while operating on accounts.
pub type Result<T> = core::result::Result<T, AccountError>;

/// The database that stores all accounts.
pub struct AccountsDb {
    /// The mapping from account ID to an actual account.
    accounts: BTreeMap<OffAccountId, Account>,
}

impl AccountsDb {
    /// Creates a new empty accounts database.
    pub fn new() -> Self {
        Self {
            accounts: BTreeMap::new(),
        }
    }

81
82
83
84
85
    /// Resets the account DB to uninitialized state.
    pub fn reset(&mut self) {
        self.accounts.clear()
    }

86
87
88
    /// Returns the account at the given account ID or creates it.
    pub fn get_or_create_account<T>(&mut self, at: &T::AccountId) -> &mut Account
    where
89
        T: Environment,
90
91
92
93
94
    {
        // Note: We cannot do a normal match for `Some(account)` here since
        //       the borrow-checker somehow cannot make sense of it according
        //       to its lifetime analysis. Consider this to be a hack until
        //       the borrow-checker eventually let's us do this.
95
        if self.get_account::<T>(at).is_some() {
96
97
98
            self.get_account_mut::<T>(at)
                .expect("just checked that account exists")
        } else {
99
            self.add_user_account::<T>(at.clone(), 0u32.into());
100
101
102
103
104
            self.get_account_mut::<T>(at)
                .expect("just added the account so it must exist")
        }
    }

105
106
107
    /// Returns the account for the given account ID if any.
    pub fn get_account<T>(&self, at: &T::AccountId) -> Option<&Account>
    where
108
        T: Environment,
109
110
111
112
113
114
115
    {
        self.accounts.get(&OffAccountId::new(at))
    }

    /// Returns the account for the given account ID if any.
    pub fn get_account_mut<T>(&mut self, at: &T::AccountId) -> Option<&mut Account>
    where
116
        T: Environment,
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
    {
        self.accounts.get_mut(&OffAccountId::new(at))
    }

    /// Returns the account for the given off-account ID if any.
    pub fn get_account_off<'a>(&'a self, at: &OffAccountId) -> Option<&'a Account> {
        self.accounts.get(at)
    }

    /// Returns the account for the given off-account ID if any.
    pub fn get_account_off_mut(&mut self, at: &OffAccountId) -> Option<&mut Account> {
        self.accounts.get_mut(at)
    }

    /// Adds the given user account with the initial balance.
    pub fn add_user_account<T>(
        &mut self,
        account_id: T::AccountId,
        initial_balance: T::Balance,
    ) where
137
        T: Environment,
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
    {
        self.accounts.insert(
            OffAccountId::new(&account_id),
            Account {
                balance: OffBalance::new(&initial_balance),
                kind: AccountKind::User,
            },
        );
    }

    /// Creates a new contract account.
    pub fn add_contract_account<T>(
        &mut self,
        account_id: T::AccountId,
        initial_balance: T::Balance,
        rent_allowance: T::Balance,
    ) where
155
        T: Environment,
156
157
158
159
160
161
162
163
164
    {
        self.accounts.insert(
            OffAccountId::new(&account_id),
            Account {
                balance: OffBalance::new(&initial_balance),
                kind: AccountKind::Contract(ContractAccount::new::<T>(rent_allowance)),
            },
        );
    }
165
166
167
168
169
170
171
172

    /// Removes an account.
    pub fn remove_account<T>(&mut self, account_id: T::AccountId)
    where
        T: Environment,
    {
        self.accounts.remove(&OffAccountId::new(&account_id));
    }
173
174
175
176
177
178
179
180
181
182
183
184
185
186
}

/// An account within the chain.
pub struct Account {
    /// The balance of the account.
    balance: OffBalance,
    /// The kind of the account.
    kind: AccountKind,
}

impl Account {
    /// Returns the balance of the account.
    pub fn balance<T>(&self) -> Result<T::Balance>
    where
187
        T: Environment,
188
189
190
191
192
193
194
    {
        self.balance.decode().map_err(Into::into)
    }

    /// Sets the balance of the account.
    pub fn set_balance<T>(&mut self, new_balance: T::Balance) -> Result<()>
    where
195
        T: Environment,
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
    {
        self.balance.assign(&new_balance).map_err(Into::into)
    }

    /// Returns the contract account or an error if it is a user account.
    fn contract_or_err(&self) -> Result<&ContractAccount> {
        match &self.kind {
            AccountKind::User => {
                Err(AccountError::UnexpectedUserAccount).map_err(Into::into)
            }
            AccountKind::Contract(contract_account) => Ok(contract_account),
        }
    }

    /// Returns the contract account or an error if it is a user account.
    fn contract_or_err_mut(&mut self) -> Result<&mut ContractAccount> {
        match &mut self.kind {
            AccountKind::User => {
                Err(AccountError::UnexpectedUserAccount).map_err(Into::into)
            }
            AccountKind::Contract(contract_account) => Ok(contract_account),
        }
    }

    /// Returns the rent allowance of the contract account or an error.
    pub fn rent_allowance<T>(&self) -> Result<T::Balance>
    where
223
        T: Environment,
224
225
226
227
228
229
230
231
    {
        self.contract_or_err()
            .and_then(|contract| contract.rent_allowance.decode().map_err(Into::into))
    }

    /// Sets the rent allowance for the contract account or returns an error.
    pub fn set_rent_allowance<T>(&mut self, new_rent_allowance: T::Balance) -> Result<()>
    where
232
        T: Environment,
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
    {
        self.contract_or_err_mut().and_then(|contract| {
            contract
                .rent_allowance
                .assign(&new_rent_allowance)
                .map_err(Into::into)
        })
    }

    /// Sets the contract storage of key to the new value.
    pub fn set_storage<T>(&mut self, at: Key, new_value: &T) -> Result<()>
    where
        T: scale::Encode,
    {
        self.contract_or_err_mut()
            .map(|contract| contract.storage.set_storage::<T>(at, new_value))
    }

    /// Clears the contract storage at key.
    pub fn clear_storage(&mut self, at: Key) -> Result<()> {
        self.contract_or_err_mut()
            .map(|contract| contract.storage.clear_storage(at))
    }

    /// Returns the value stored in the contract storage at the given key.
Hero Bird's avatar
Hero Bird committed
258
    pub fn get_storage<T>(&self, at: Key) -> Result<Option<T>>
259
260
261
262
263
264
265
266
267
268
269
    where
        T: scale::Decode,
    {
        self.contract_or_err()
            .and_then(|contract| contract.storage.get_storage::<T>(at))
    }

    /// Returns the total number of reads and write from and to the contract's storage.
    pub fn get_storage_rw(&self) -> Result<(usize, usize)> {
        self.contract_or_err().map(|contract| contract.get_rw())
    }
270
271
272
273
274
275

    /// Returns the amount of used storage entries.
    pub fn count_used_storage_cells(&self) -> Result<usize> {
        self.contract_or_err()
            .map(|contract| contract.count_used_storage_cells())
    }
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
}

/// The kind of the account.
///
/// Can be either a user account or a (more complicated) contract account.
pub enum AccountKind {
    User,
    Contract(ContractAccount),
}

/// Extraneous fields for contract accounts.
pub struct ContractAccount {
    /// The contract's rent allowance.
    rent_allowance: OffBalance,
    /// The contract storage.
    pub storage: ContractStorage,
}

impl ContractAccount {
    /// Creates a new contract account with the given initial rent allowance.
    pub fn new<T>(rent_allowance: T::Balance) -> Self
    where
298
        T: Environment,
299
300
301
302
303
304
305
306
307
308
309
    {
        Self {
            rent_allowance: OffBalance::new(&rent_allowance),
            storage: ContractStorage::new(),
        }
    }

    /// Returns the number of reads and writes from and to the contract storage.
    pub fn get_rw(&self) -> (usize, usize) {
        self.storage.get_rw()
    }
310
311
312
313
314

    /// Returns the number of used storage entries.
    pub fn count_used_storage_cells(&self) -> usize {
        self.storage.count_used_storage_cells()
    }
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
}

/// The storage of a contract instance.
pub struct ContractStorage {
    /// The entries within the contract storage.
    entries: BTreeMap<Key, Vec<u8>>,
    /// The total number of reads to the storage.
    count_reads: Cell<usize>,
    /// The total number of writes to the storage.
    count_writes: usize,
}

impl ContractStorage {
    /// Creates a new empty contract storage.
    pub fn new() -> Self {
        Self {
            entries: BTreeMap::new(),
            count_reads: Cell::new(0),
            count_writes: 0,
        }
    }

    /// Returns the number of reads and writes from and to the contract storage.
    pub fn get_rw(&self) -> (usize, usize) {
        (self.count_reads.get(), self.count_writes)
    }

    /// Returns the decoded storage at the key if any.
    pub fn get_storage<T>(&self, at: Key) -> Result<Option<T>>
    where
        T: scale::Decode,
    {
        self.count_reads.set(self.count_reads.get() + 1);
        self.entries
            .get(&at)
            .map(|encoded| T::decode(&mut &encoded[..]))
            .transpose()
            .map_err(Into::into)
    }

    /// Writes the encoded value into the contract storage at the given key.
    pub fn set_storage<T>(&mut self, at: Key, new_value: &T)
    where
        T: scale::Encode,
    {
        self.count_writes += 1;
        self.entries.insert(at, new_value.encode());
    }

    /// Removes the value from storage entries at the given key.
    pub fn clear_storage(&mut self, at: Key) {
        self.count_writes += 1;
        self.entries.remove(&at);
    }
369
370
371
372
373

    /// Returns the number of used storage entries.
    pub fn count_used_storage_cells(&self) -> usize {
        self.entries.len()
    }
374
}