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
195
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
use borsh::{BorshDeserialize, BorshSerialize};
use serde::{Deserialize, Serialize};
use std::io;
pub use near_account_id as id;
use crate::hash::CryptoHash;
use crate::serialize::{option_u128_dec_format, u128_dec_format_compatible};
use crate::types::{Balance, Nonce, StorageUsage};
#[derive(
BorshSerialize, BorshDeserialize, Serialize, Deserialize, PartialEq, Eq, Debug, Clone, Copy,
)]
pub enum AccountVersion {
V1,
}
impl Default for AccountVersion {
fn default() -> Self {
AccountVersion::V1
}
}
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
pub struct Account {
#[serde(with = "u128_dec_format_compatible")]
amount: Balance,
#[serde(with = "u128_dec_format_compatible")]
locked: Balance,
code_hash: CryptoHash,
storage_usage: StorageUsage,
#[serde(default)]
version: AccountVersion,
}
impl Account {
pub const MAX_ACCOUNT_DELETION_STORAGE_USAGE: u64 = 10_000;
pub fn new(
amount: Balance,
locked: Balance,
code_hash: CryptoHash,
storage_usage: StorageUsage,
) -> Self {
Account { amount, locked, code_hash, storage_usage, version: AccountVersion::V1 }
}
#[inline]
pub fn amount(&self) -> Balance {
self.amount
}
#[inline]
pub fn locked(&self) -> Balance {
self.locked
}
#[inline]
pub fn code_hash(&self) -> CryptoHash {
self.code_hash
}
#[inline]
pub fn storage_usage(&self) -> StorageUsage {
self.storage_usage
}
#[inline]
pub fn version(&self) -> AccountVersion {
self.version
}
#[inline]
pub fn set_amount(&mut self, amount: Balance) {
self.amount = amount;
}
#[inline]
pub fn set_locked(&mut self, locked: Balance) {
self.locked = locked;
}
#[inline]
pub fn set_code_hash(&mut self, code_hash: CryptoHash) {
self.code_hash = code_hash;
}
#[inline]
pub fn set_storage_usage(&mut self, storage_usage: StorageUsage) {
self.storage_usage = storage_usage;
}
pub fn set_version(&mut self, version: AccountVersion) {
self.version = version;
}
}
#[derive(BorshSerialize, BorshDeserialize)]
struct LegacyAccount {
amount: Balance,
locked: Balance,
code_hash: CryptoHash,
storage_usage: StorageUsage,
}
impl BorshDeserialize for Account {
fn deserialize(buf: &mut &[u8]) -> Result<Self, io::Error> {
if buf.len() == std::mem::size_of::<LegacyAccount>() {
let deserialized_account = LegacyAccount::deserialize(buf)?;
Ok(Account {
amount: deserialized_account.amount,
locked: deserialized_account.locked,
code_hash: deserialized_account.code_hash,
storage_usage: deserialized_account.storage_usage,
version: AccountVersion::V1,
})
} else {
unreachable!();
}
}
}
impl BorshSerialize for Account {
fn serialize<W: io::Write>(&self, writer: &mut W) -> io::Result<()> {
match self.version {
AccountVersion::V1 => LegacyAccount {
amount: self.amount,
locked: self.locked,
code_hash: self.code_hash,
storage_usage: self.storage_usage,
}
.serialize(writer),
}
}
}
#[derive(
BorshSerialize, BorshDeserialize, Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Debug,
)]
pub struct AccessKey {
pub nonce: Nonce,
pub permission: AccessKeyPermission,
}
impl AccessKey {
pub const ACCESS_KEY_NONCE_RANGE_MULTIPLIER: u64 = 1_000_000;
pub fn full_access() -> Self {
Self { nonce: 0, permission: AccessKeyPermission::FullAccess }
}
}
#[derive(
BorshSerialize, BorshDeserialize, Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Debug,
)]
pub enum AccessKeyPermission {
FunctionCall(FunctionCallPermission),
FullAccess,
}
#[derive(
BorshSerialize, BorshDeserialize, Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Debug,
)]
pub struct FunctionCallPermission {
#[serde(with = "option_u128_dec_format")]
pub allowance: Option<Balance>,
pub receiver_id: String,
pub method_names: Vec<String>,
}
#[cfg(test)]
mod tests {
use borsh::BorshSerialize;
use crate::hash::hash;
use crate::serialize::to_base;
use super::*;
#[test]
fn test_account_serialization() {
let acc = Account::new(1_000_000, 1_000_000, CryptoHash::default(), 100);
let bytes = acc.try_to_vec().unwrap();
assert_eq!(to_base(&hash(&bytes)), "EVk5UaxBe8LQ8r8iD5EAxVBs6TJcMDKqyH7PBuho6bBJ");
}
#[test]
fn test_account_deserialization() {
let old_account = LegacyAccount {
amount: 100,
locked: 200,
code_hash: CryptoHash::default(),
storage_usage: 300,
};
let mut old_bytes = &old_account.try_to_vec().unwrap()[..];
let new_account = <Account as BorshDeserialize>::deserialize(&mut old_bytes).unwrap();
assert_eq!(new_account.amount, old_account.amount);
assert_eq!(new_account.locked, old_account.locked);
assert_eq!(new_account.code_hash, old_account.code_hash);
assert_eq!(new_account.storage_usage, old_account.storage_usage);
assert_eq!(new_account.version, AccountVersion::V1);
let mut new_bytes = &new_account.try_to_vec().unwrap()[..];
let deserialized_account =
<Account as BorshDeserialize>::deserialize(&mut new_bytes).unwrap();
assert_eq!(deserialized_account, new_account);
}
}