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
// This file is part of Gear.

// Copyright (C) 2024 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Accounts storage.

use std::{cell::RefCell, collections::HashMap, fmt};

use crate::{default_users_list, Value, DEFAULT_USERS_INITIAL_BALANCE, EXISTENTIAL_DEPOSIT};
use gear_common::ProgramId;

fn init_default_accounts(storage: &mut HashMap<ProgramId, Balance>) {
    for &id in default_users_list() {
        let id = id.into();
        storage.insert(id, Balance::new(DEFAULT_USERS_INITIAL_BALANCE));
    }
}

thread_local! {
    static ACCOUNT_STORAGE: RefCell<HashMap<ProgramId, Balance>> = RefCell::new({
        let mut storage = HashMap::new();
        init_default_accounts(&mut storage);
        storage
    });
}

#[derive(Debug)]
struct Balance {
    amount: Value,
}

impl Balance {
    fn new(amount: Value) -> Self {
        if amount < EXISTENTIAL_DEPOSIT {
            panic!(
                "Failed to create balance: the amount {} cannot be lower than the existential deposit",
                amount
            );
        }

        Self { amount }
    }

    fn balance(&self) -> Value {
        self.amount
    }

    fn reducible_balance(&self) -> Value {
        self.amount - EXISTENTIAL_DEPOSIT
    }

    fn decrease(&mut self, amount: Value) {
        self.amount -= amount;
    }

    fn increase(&mut self, amount: Value) {
        self.amount += amount;
    }
}

pub(crate) struct Accounts;

impl Accounts {
    // Checks if account by program id exists.
    pub(crate) fn exists(id: ProgramId) -> bool {
        Self::balance(id) != 0
    }

    // Returns account balance.
    pub(crate) fn balance(id: ProgramId) -> Value {
        ACCOUNT_STORAGE.with_borrow(|storage| {
            storage
                .get(&id)
                .map(|balance| balance.balance())
                .unwrap_or_default()
        })
    }

    // Returns account reducible balance.
    pub(crate) fn reducible_balance(id: ProgramId) -> Value {
        ACCOUNT_STORAGE.with_borrow(|storage| {
            storage
                .get(&id)
                .map(|balance| balance.reducible_balance())
                .unwrap_or_default()
        })
    }

    // Decreases account balance.
    pub(crate) fn decrease(id: ProgramId, amount: Value, keep_alive: bool) {
        ACCOUNT_STORAGE.with_borrow_mut(|storage| {
            if let Some(balance) = storage.get_mut(&id) {
                if keep_alive && balance.reducible_balance() < amount {
                    panic!(
                        "Not enough balance to decrease, reducible: {}, value: {amount}",
                        balance.reducible_balance(),
                    );
                }
                if !keep_alive && balance.balance() < amount {
                    panic!(
                        "Not enough balance to decrease, total: {}, value: {amount}",
                        balance.balance(),
                    );
                }

                balance.decrease(amount);
                if balance.balance() < EXISTENTIAL_DEPOSIT {
                    log::debug!(
                        "Removing account {id:?} with balance {} below the existential deposit",
                        balance.balance()
                    );
                    storage.remove(&id);
                }
            } else {
                panic!("Failed to decrease balance for account {id:?}, balance is zero");
            }
        });
    }

    // Increases account balance.
    pub(crate) fn increase(id: ProgramId, amount: Value) {
        ACCOUNT_STORAGE.with_borrow_mut(|storage| {
            let balance = storage.get(&id).map(Balance::balance).unwrap_or_default();

            if balance + amount < EXISTENTIAL_DEPOSIT {
                panic!(
                    "Failed to increase balance: the sum ({}) of the total balance ({}) \
                    and the value ({}) cannot be lower than the existential deposit ({EXISTENTIAL_DEPOSIT})",
                    balance + amount,
                    balance,
                    amount
                );
            }

            storage
                .entry(id)
                .and_modify(|balance| balance.increase(amount))
                .or_insert_with(|| Balance::new(amount));
        });
    }

    // Transfers value between accounts.
    pub(crate) fn transfer(from: ProgramId, to: ProgramId, amount: Value, keep_alive: bool) {
        Self::decrease(from, amount, keep_alive);
        Self::increase(to, amount);
    }

    // Overrides account balance.
    pub(crate) fn override_balance(id: ProgramId, amount: Value) {
        if amount < EXISTENTIAL_DEPOSIT {
            panic!(
                "Failed to override balance: the amount {} cannot be lower than the existential deposit",
                amount
            );
        }

        ACCOUNT_STORAGE.with_borrow_mut(|storage| {
            storage.insert(id, Balance::new(amount));
        });
    }

    // Checks if value can be deposited to account.
    pub(crate) fn can_deposit(id: ProgramId, amount: Value) -> bool {
        Accounts::balance(id) + amount >= EXISTENTIAL_DEPOSIT
    }

    // Clears accounts storage.
    pub(crate) fn clear() {
        ACCOUNT_STORAGE.with_borrow_mut(|storage| {
            storage.clear();
            init_default_accounts(storage);
        });
    }
}

impl fmt::Debug for Accounts {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        ACCOUNT_STORAGE.with_borrow(|storage| f.debug_map().entries(storage.iter()).finish())
    }
}