Diff
Not logged in

Differences From Artifact [23137e0724]:

To Artifact [633a7b8f2a]:


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
..
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
...
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
...
173
174
175
176
177
178
179

180
181
182
183
184
185
186
// Copyright 2016 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, Authority, ClientError, ImmutableData, MutableData, XorName};
use rust_sodium::crypto::hash::sha256;
use rust_sodium::crypto::sign;
use std::collections::{BTreeSet, HashMap};

pub const DEFAULT_MAX_MUTATIONS: u64 = 100;

#[derive(RustcDecodable, RustcEncodable)]
pub struct Vault {
    client_manager: HashMap<XorName, Account>,
    nae_manager: HashMap<XorName, Data>,
}

impl Vault {
    pub fn new() -> Self {
        sync::load().unwrap_or_else(|| {
            Vault {
                client_manager: HashMap::new(),
                nae_manager: HashMap::new(),
            }
        })
    }

    // Get account for the client manager name.
    pub fn get_account(&self, name: &XorName) -> Option<&Account> {
        self.client_manager.get(name)
    }

................................................................................

    // Create account for the given client manager name.
    pub fn insert_account(&mut self, name: XorName) {
        let _ = self.client_manager.insert(name, Account::new());
    }

    // Authorise read (non-mutation) operation.
    pub fn authorise_read(&self, dst: &Authority, data_name: &XorName) -> bool {
        match *dst {
            Authority::NaeManager(name) if name == *data_name => true,
            _ => false,
        }
    }

    // Authorise mutation operation.
    pub fn authorise_mutation(&self, dst: &Authority, sign_pk: &sign::PublicKey) -> bool {
        let dst_name = match *dst {
            Authority::ClientManager(name) => name,
            x => {
                println!("Unexpected authority for mutation: {:?}", x);
                return false;
            }
        };
................................................................................

    // Synchronize the storage with the disk.
    pub fn sync(&self) {
        sync::save(self)
    }
}

#[derive(Clone, RustcDecodable, RustcEncodable)]
pub enum Data {
    Immutable(ImmutableData),
    Mutable(MutableData),
}

#[derive(RustcDecodable, RustcEncodable)]
pub struct Account {
    account_info: AccountInfo,
    auth_keys: BTreeSet<sign::PublicKey>,
    version: u64,
}

impl Account {
................................................................................
    pub fn auth_keys(&self) -> &BTreeSet<sign::PublicKey> {
        &self.auth_keys
    }

    pub fn increment_mutations_counter(&mut self) {
        self.account_info.mutations_done += 1;
        self.account_info.mutations_available -= 1;

    }

    fn validate_version(&self, version: u64) -> Result<(), ClientError> {
        if version == self.version + 1 {
            Ok(())
        } else {
            Err(ClientError::InvalidSuccessor)


|
|
<
|

|
|
<
<
|

|
|
|
<

|
|








|








|
|
|
|
|







 







|







|







 







|





|







 







>







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
..
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
...
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
...
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2016 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.  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, Authority, ClientError, ImmutableData, MutableData, XorName};
use rust_sodium::crypto::hash::sha256;
use rust_sodium::crypto::sign;
use std::collections::{BTreeSet, HashMap};

pub const DEFAULT_MAX_MUTATIONS: u64 = 100;

#[derive(Deserialize, Serialize)]
pub struct Vault {
    client_manager: HashMap<XorName, Account>,
    nae_manager: HashMap<XorName, Data>,
}

impl Vault {
    pub fn new() -> Self {
        sync::load().unwrap_or_else(|| {
                                        Vault {
                                            client_manager: HashMap::new(),
                                            nae_manager: HashMap::new(),
                                        }
                                    })
    }

    // Get account for the client manager name.
    pub fn get_account(&self, name: &XorName) -> Option<&Account> {
        self.client_manager.get(name)
    }

................................................................................

    // Create account for the given client manager name.
    pub fn insert_account(&mut self, name: XorName) {
        let _ = self.client_manager.insert(name, Account::new());
    }

    // Authorise read (non-mutation) operation.
    pub fn authorise_read(&self, dst: &Authority<XorName>, data_name: &XorName) -> bool {
        match *dst {
            Authority::NaeManager(name) if name == *data_name => true,
            _ => false,
        }
    }

    // Authorise mutation operation.
    pub fn authorise_mutation(&self, dst: &Authority<XorName>, sign_pk: &sign::PublicKey) -> bool {
        let dst_name = match *dst {
            Authority::ClientManager(name) => name,
            x => {
                println!("Unexpected authority for mutation: {:?}", x);
                return false;
            }
        };
................................................................................

    // Synchronize the storage with the disk.
    pub fn sync(&self) {
        sync::save(self)
    }
}

#[derive(Clone, Deserialize, Serialize)]
pub enum Data {
    Immutable(ImmutableData),
    Mutable(MutableData),
}

#[derive(Deserialize, Serialize)]
pub struct Account {
    account_info: AccountInfo,
    auth_keys: BTreeSet<sign::PublicKey>,
    version: u64,
}

impl Account {
................................................................................
    pub fn auth_keys(&self) -> &BTreeSet<sign::PublicKey> {
        &self.auth_keys
    }

    pub fn increment_mutations_counter(&mut self) {
        self.account_info.mutations_done += 1;
        self.account_info.mutations_available -= 1;
        self.version += 1;
    }

    fn validate_version(&self, version: u64) -> Result<(), ClientError> {
        if version == self.version + 1 {
            Ok(())
        } else {
            Err(ClientError::InvalidSuccessor)