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
// Copyright 2017 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under (1) the MaidSafe.net Commercial License,
// version 1.0 or later, or (2) The General Public License (GPL), version 3, depending on which
// licence you accepted on initial access to the Software (the "Licences").
//
// By contributing code to the SAFE Network Software, or to this project generally, you agree to be
// bound by the terms of the MaidSafe Contributor Agreement, version 1.0.  This, along with the
// Licenses can be found in the root directory of this project at LICENSE, COPYING and CONTRIBUTOR.
//
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.
//
// Please review the Licences for the specific language governing permissions and limitations
// relating to use of the SAFE Network Software.

use routing::{AccountInfo, MessageId};
use rust_sodium::crypto::sign;
use std::collections::BTreeSet;

/// Default available number of operations per account.
#[cfg(not(feature = "use-mock-crust"))]
pub const DEFAULT_MAX_OPS_COUNT: u64 = 1000;
/// Default available number of mutations per account.
#[cfg(feature = "use-mock-crust")]
pub const DEFAULT_MAX_OPS_COUNT: u64 = 100;

#[derive(Deserialize, Serialize, PartialEq, Eq, Debug, Clone)]
pub struct Account {
    /// Message ids of data operations performed by this account.
    pub data_ops_msg_ids: BTreeSet<MessageId>,
    /// Number of keys operations performed by this account.
    pub keys_ops_count: u64,
    /// App authentication keys.
    pub keys: BTreeSet<sign::PublicKey>,
    /// Dev option to allow clients to make unlimited mutation requests.
    disable_mutation_limit: bool,
}

impl Account {
    pub fn new(disable_mutation_limit: bool) -> Self {
        Account {
            data_ops_msg_ids: BTreeSet::new(),
            keys_ops_count: 0,
            keys: BTreeSet::new(),
            disable_mutation_limit,
        }
    }

    // TODO: Change the `AccountInfo` struct in routing.
    pub fn balance(&self) -> AccountInfo {
        let done = self.data_ops_msg_ids.len() as u64 + self.keys_ops_count;
        let available = if self.disable_mutation_limit {
            u64::max_value()
        } else {
            DEFAULT_MAX_OPS_COUNT.saturating_sub(done)
        };

        AccountInfo {
            mutations_done: done,
            mutations_available: available,
        }
    }

    pub fn has_balance(&self) -> bool {
        self.disable_mutation_limit ||
            self.data_ops_msg_ids.len() as u64 + self.keys_ops_count < DEFAULT_MAX_OPS_COUNT
    }
}

#[cfg(test)]
mod tests {
    use super::{Account, DEFAULT_MAX_OPS_COUNT};
    use routing::MessageId;

    #[test]
    fn balance() {
        let mut account = Account::new(false);
        assert!(account.has_balance());

        account.keys_ops_count = DEFAULT_MAX_OPS_COUNT - 1;
        assert!(account.has_balance());

        let _ = account.data_ops_msg_ids.insert(MessageId::zero());
        assert!(!account.has_balance());

        let mut unlimited_account = Account::new(true);
        assert!(unlimited_account.has_balance());

        unlimited_account.keys_ops_count = DEFAULT_MAX_OPS_COUNT;
        assert!(unlimited_account.has_balance());
    }
}