-
Notifications
You must be signed in to change notification settings - Fork 47
/
account_id.rs
654 lines (561 loc) · 24.8 KB
/
account_id.rs
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
use alloc::{
string::{String, ToString},
vec::Vec,
};
use core::fmt;
use super::{
get_account_seed, AccountError, ByteReader, Deserializable, DeserializationError, Digest, Felt,
Hasher, Serializable, Word, ZERO,
};
use crate::{crypto::merkle::LeafIndex, utils::hex_to_bytes, ACCOUNT_TREE_DEPTH};
// CONSTANTS
// ================================================================================================
// The higher two bits of the most significant nibble determines the account storage mode
pub const ACCOUNT_STORAGE_MASK_SHIFT: u64 = 62;
pub const ACCOUNT_STORAGE_MASK: u64 = 0b11 << ACCOUNT_STORAGE_MASK_SHIFT;
// The lower two bits of the most significant nibble determines the account type
pub const ACCOUNT_TYPE_MASK_SHIFT: u64 = 60;
pub const ACCOUNT_TYPE_MASK: u64 = 0b11 << ACCOUNT_TYPE_MASK_SHIFT;
pub const ACCOUNT_ISFAUCET_MASK: u64 = 0b10 << ACCOUNT_TYPE_MASK_SHIFT;
// ACCOUNT TYPES
// ================================================================================================
pub const FUNGIBLE_FAUCET: u64 = 0b10;
pub const NON_FUNGIBLE_FAUCET: u64 = 0b11;
pub const REGULAR_ACCOUNT_IMMUTABLE_CODE: u64 = 0b00;
pub const REGULAR_ACCOUNT_UPDATABLE_CODE: u64 = 0b01;
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u64)]
pub enum AccountType {
FungibleFaucet = FUNGIBLE_FAUCET,
NonFungibleFaucet = NON_FUNGIBLE_FAUCET,
RegularAccountImmutableCode = REGULAR_ACCOUNT_IMMUTABLE_CODE,
RegularAccountUpdatableCode = REGULAR_ACCOUNT_UPDATABLE_CODE,
}
/// Extracts the [AccountType] encoded in an u64.
///
/// The account id is encoded in the bits `[62,60]` of the u64, see [ACCOUNT_TYPE_MASK].
///
/// # Note
///
/// This function does not validate the u64, it is assumed the value is valid [Felt].
pub const fn account_type_from_u64(value: u64) -> AccountType {
debug_assert!(
ACCOUNT_TYPE_MASK.count_ones() == 2,
"This method assumes there are only 2bits in the mask"
);
let bits = (value & ACCOUNT_TYPE_MASK) >> ACCOUNT_TYPE_MASK_SHIFT;
match bits {
REGULAR_ACCOUNT_UPDATABLE_CODE => AccountType::RegularAccountUpdatableCode,
REGULAR_ACCOUNT_IMMUTABLE_CODE => AccountType::RegularAccountImmutableCode,
FUNGIBLE_FAUCET => AccountType::FungibleFaucet,
NON_FUNGIBLE_FAUCET => AccountType::NonFungibleFaucet,
_ => {
// "account_type mask contains only 2bits, there are 4 options total"
unreachable!()
},
}
}
/// Returns the [AccountType] given an integer representation of `account_id`.
impl From<u64> for AccountType {
fn from(value: u64) -> Self {
account_type_from_u64(value)
}
}
// ACCOUNT STORAGE TYPES
// ================================================================================================
pub const ON_CHAIN: u64 = 0b00;
pub const OFF_CHAIN: u64 = 0b10;
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u64)]
pub enum AccountStorageType {
OnChain = ON_CHAIN,
OffChain = OFF_CHAIN,
}
// ACCOUNT ID
// ================================================================================================
/// Unique identifier of an account.
///
/// Account ID consists of 1 field element (~64 bits). The most significant bits in the id are used
/// to encode the account' storage and type.
///
/// The top two bits are used to encode the storage type. The values [OFF_CHAIN] and [ON_CHAIN]
/// encode the account's storage type. The next two bits encode the account type. The values
/// [FUNGIBLE_FAUCET], [NON_FUNGIBLE_FAUCET], [REGULAR_ACCOUNT_IMMUTABLE_CODE], and
/// [REGULAR_ACCOUNT_UPDATABLE_CODE] encode the account's type.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct AccountId(Felt);
impl AccountId {
/// Specifies a minimum number of trailing zeros required in the last element of the seed
/// digest.
///
/// Note: The account id includes 4 bits of metadata, these bits determine the account type
/// (normal account, fungible token, non-fungible token), the storage type (on/off chain), and
/// for the normal accounts if the code is updatable or not. These metadata bits are also
/// checked by the PoW and add to the total work defined below.
#[cfg(not(any(feature = "testing", test)))]
pub const REGULAR_ACCOUNT_SEED_DIGEST_MIN_TRAILING_ZEROS: u32 = 23;
#[cfg(not(any(feature = "testing", test)))]
pub const FAUCET_SEED_DIGEST_MIN_TRAILING_ZEROS: u32 = 31;
#[cfg(any(feature = "testing", test))]
pub const REGULAR_ACCOUNT_SEED_DIGEST_MIN_TRAILING_ZEROS: u32 = 5;
#[cfg(any(feature = "testing", test))]
pub const FAUCET_SEED_DIGEST_MIN_TRAILING_ZEROS: u32 = 6;
/// Specifies a minimum number of ones for a valid account ID.
pub const MIN_ACCOUNT_ONES: u32 = 5;
// CONSTRUCTORS
// --------------------------------------------------------------------------------------------
/// Returns a new account ID derived from the specified seed, code commitment and storage root.
///
/// The account ID is computed by hashing the seed, code commitment and storage root and using 1
/// element of the resulting digest to form the ID. Specifically we take element 0. We also
/// require that the last element of the seed digest has at least `23` trailing zeros if it
/// is a regular account, or `31` trailing zeros if it is a faucet account.
///
/// The seed digest is computed using a sequential hash over
/// hash(SEED, CODE_COMMITMENT, STORAGE_ROOT, ZERO). This takes two permutations.
///
/// # Errors
/// Returns an error if the resulting account ID does not comply with account ID rules:
/// - the metadata embedded in the ID (i.e., the first 4 bits) is valid.
/// - the ID has at least `5` ones.
/// - the last element of the seed digest has at least `23` trailing zeros for regular accounts.
/// - the last element of the seed digest has at least `31` trailing zeros for faucet accounts.
pub fn new(
seed: Word,
code_commitment: Digest,
storage_root: Digest,
) -> Result<Self, AccountError> {
let seed_digest = compute_digest(seed, code_commitment, storage_root);
Self::validate_seed_digest(&seed_digest)?;
seed_digest[0].try_into()
}
/// Creates a new [AccountId] without checking its validity.
///
/// This function requires that the provided value is a valid [Felt] representation of an
/// [AccountId].
pub fn new_unchecked(value: Felt) -> Self {
Self(value)
}
/// Creates a new dummy [AccountId] for testing purposes.
#[cfg(any(feature = "testing", test))]
pub fn new_dummy(init_seed: [u8; 32], account_type: AccountType) -> Self {
let code_commitment = Digest::default();
let storage_root = Digest::default();
let seed = get_account_seed(
init_seed,
account_type,
AccountStorageType::OnChain,
code_commitment,
storage_root,
)
.unwrap();
Self::new(seed, code_commitment, storage_root).unwrap()
}
// PUBLIC ACCESSORS
// --------------------------------------------------------------------------------------------
/// Returns the type of this account ID.
pub const fn account_type(&self) -> AccountType {
account_type_from_u64(self.0.as_int())
}
/// Returns true if an account with this ID is a faucet (can issue assets).
pub fn is_faucet(&self) -> bool {
matches!(
self.account_type(),
AccountType::FungibleFaucet | AccountType::NonFungibleFaucet
)
}
/// Returns true if an account with this ID is a regular account.
pub fn is_regular_account(&self) -> bool {
is_regular_account(self.0.as_int())
}
/// Returns the storage type of this account (e.g., on-chain or off-chain).
pub fn storage_type(&self) -> AccountStorageType {
let bits = (self.0.as_int() & ACCOUNT_STORAGE_MASK) >> ACCOUNT_STORAGE_MASK_SHIFT;
match bits {
ON_CHAIN => AccountStorageType::OnChain,
OFF_CHAIN => AccountStorageType::OffChain,
_ => panic!("Account with invalid storage bits created"),
}
}
/// Returns true if an account with this ID is an on-chain account.
pub fn is_on_chain(&self) -> bool {
self.storage_type() == AccountStorageType::OnChain
}
/// Finds and returns a seed suitable for creating an account ID for the specified account type
/// using the provided initial seed as a starting point.
pub fn get_account_seed(
init_seed: [u8; 32],
account_type: AccountType,
storage_type: AccountStorageType,
code_commitment: Digest,
storage_root: Digest,
) -> Result<Word, AccountError> {
get_account_seed(init_seed, account_type, storage_type, code_commitment, storage_root)
}
/// Creates an Account Id from a hex string. Assumes the string starts with "0x" and
/// that the hexadecimal characters are big-endian encoded.
pub fn from_hex(hex_value: &str) -> Result<AccountId, AccountError> {
hex_to_bytes(hex_value)
.map_err(|err| AccountError::HexParseError(err.to_string()))
.and_then(|mut bytes: [u8; 8]| {
// `bytes` ends up being parsed as felt, and the input to that is assumed to be
// little-endian so we need to reverse the order
bytes.reverse();
bytes.try_into()
})
}
/// Returns a big-endian, hex-encoded string.
pub fn to_hex(&self) -> String {
format!("0x{:016x}", self.0.as_int())
}
// UTILITY METHODS
// --------------------------------------------------------------------------------------------
/// Returns an error if:
/// - There are fewer then:
/// - 24 trailing ZEROs in the last element of the seed digest for regular accounts.
/// - 32 trailing ZEROs in the last element of the seed digest for faucet accounts.
pub(super) fn validate_seed_digest(digest: &Digest) -> Result<(), AccountError> {
// check the id satisfies the proof-of-work requirement.
let required_zeros = if is_regular_account(digest[0].as_int()) {
Self::REGULAR_ACCOUNT_SEED_DIGEST_MIN_TRAILING_ZEROS
} else {
Self::FAUCET_SEED_DIGEST_MIN_TRAILING_ZEROS
};
let trailing_zeros = digest_pow(*digest);
if required_zeros > trailing_zeros {
return Err(AccountError::SeedDigestTooFewTrailingZeros {
expected: required_zeros,
actual: trailing_zeros,
});
}
Ok(())
}
}
impl PartialOrd for AccountId {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for AccountId {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.0.as_int().cmp(&other.0.as_int())
}
}
impl fmt::Display for AccountId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "0x{:016x}", self.0.as_int())
}
}
// CONVERSIONS FROM ACCOUNT ID
// ================================================================================================
impl From<AccountId> for Felt {
fn from(id: AccountId) -> Self {
id.0
}
}
impl From<AccountId> for [u8; 8] {
fn from(id: AccountId) -> Self {
let mut result = [0_u8; 8];
result[..8].copy_from_slice(&id.0.as_int().to_le_bytes());
result
}
}
impl From<AccountId> for u64 {
fn from(id: AccountId) -> Self {
id.0.as_int()
}
}
/// Account IDs are used as indexes in the account database, which is a tree of depth 64.
impl From<AccountId> for LeafIndex<ACCOUNT_TREE_DEPTH> {
fn from(id: AccountId) -> Self {
LeafIndex::new_max_depth(id.0.as_int())
}
}
// CONVERSIONS TO ACCOUNT ID
// ================================================================================================
/// Returns an [AccountId] instantiated with the provided field element.
///
/// # Errors
/// Returns an error if:
/// - If there are fewer than [AccountId::MIN_ACCOUNT_ONES] in the provided value.
/// - If the provided value contains invalid account ID metadata (i.e., the first 4 bits).
pub const fn account_id_from_felt(value: Felt) -> Result<AccountId, AccountError> {
let int_value = value.as_int();
let count = int_value.count_ones();
if count < AccountId::MIN_ACCOUNT_ONES {
return Err(AccountError::AccountIdTooFewOnes(AccountId::MIN_ACCOUNT_ONES, count));
}
let bits = (int_value & ACCOUNT_STORAGE_MASK) >> ACCOUNT_STORAGE_MASK_SHIFT;
match bits {
ON_CHAIN | OFF_CHAIN => (),
_ => return Err(AccountError::InvalidAccountStorageType),
};
Ok(AccountId(value))
}
impl TryFrom<Felt> for AccountId {
type Error = AccountError;
/// Returns an [AccountId] instantiated with the provided field element.
///
/// # Errors
/// Returns an error if:
/// - If there are fewer than [AccountId::MIN_ACCOUNT_ONES] in the provided value.
/// - If the provided value contains invalid account ID metadata (i.e., the first 4 bits).
fn try_from(value: Felt) -> Result<Self, Self::Error> {
account_id_from_felt(value)
}
}
impl TryFrom<[u8; 8]> for AccountId {
type Error = AccountError;
// Expects little-endian byte order
fn try_from(value: [u8; 8]) -> Result<Self, Self::Error> {
let element = parse_felt(&value[..8])?;
Self::try_from(element)
}
}
impl TryFrom<u64> for AccountId {
type Error = AccountError;
fn try_from(value: u64) -> Result<Self, Self::Error> {
let element = parse_felt(&value.to_le_bytes())?;
Self::try_from(element)
}
}
// SERIALIZATION
// ================================================================================================
impl Serializable for AccountId {
fn write_into<W: miden_crypto::utils::ByteWriter>(&self, target: &mut W) {
self.0.write_into(target);
}
}
impl Deserializable for AccountId {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
Felt::read_from(source)?
.try_into()
.map_err(|err: AccountError| DeserializationError::InvalidValue(err.to_string()))
}
}
// HELPER FUNCTIONS
// ================================================================================================
fn parse_felt(bytes: &[u8]) -> Result<Felt, AccountError> {
Felt::try_from(bytes).map_err(|err| AccountError::AccountIdInvalidFieldElement(err.to_string()))
}
/// Returns the digest of two hashing permutations over the seed, code commitment, storage root and
/// padding.
pub(super) fn compute_digest(seed: Word, code_commitment: Digest, storage_root: Digest) -> Digest {
let mut elements = Vec::with_capacity(16);
elements.extend(seed);
elements.extend(*code_commitment);
elements.extend(*storage_root);
elements.resize(16, ZERO);
Hasher::hash_elements(&elements)
}
/// Given a [Digest] returns its proof-of-work.
pub(super) fn digest_pow(digest: Digest) -> u32 {
digest.as_elements()[3].as_int().trailing_zeros()
}
/// Returns true if an account with this ID is a regular account.
fn is_regular_account(account_id: u64) -> bool {
let account_type = account_id.into();
matches!(
account_type,
AccountType::RegularAccountUpdatableCode | AccountType::RegularAccountImmutableCode
)
}
// TESTING
// ================================================================================================
#[cfg(any(feature = "testing", test))]
pub mod testing {
use super::{
AccountStorageType, AccountType, ACCOUNT_STORAGE_MASK_SHIFT, ACCOUNT_TYPE_MASK_SHIFT,
};
// CONSTANTS
// --------------------------------------------------------------------------------------------
// REGULAR ACCOUNTS - OFF-CHAIN
pub const ACCOUNT_ID_SENDER: u64 = account_id(
AccountType::RegularAccountImmutableCode,
AccountStorageType::OffChain,
0b0001_1111,
);
pub const ACCOUNT_ID_OFF_CHAIN_SENDER: u64 = account_id(
AccountType::RegularAccountImmutableCode,
AccountStorageType::OffChain,
0b0010_1111,
);
pub const ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_OFF_CHAIN: u64 = account_id(
AccountType::RegularAccountUpdatableCode,
AccountStorageType::OffChain,
0b0011_1111,
);
// REGULAR ACCOUNTS - ON-CHAIN
pub const ACCOUNT_ID_REGULAR_ACCOUNT_IMMUTABLE_CODE_ON_CHAIN: u64 = account_id(
AccountType::RegularAccountImmutableCode,
AccountStorageType::OnChain,
0b0001_1111,
);
pub const ACCOUNT_ID_REGULAR_ACCOUNT_IMMUTABLE_CODE_ON_CHAIN_2: u64 = account_id(
AccountType::RegularAccountImmutableCode,
AccountStorageType::OnChain,
0b0010_1111,
);
pub const ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_ON_CHAIN: u64 = account_id(
AccountType::RegularAccountUpdatableCode,
AccountStorageType::OnChain,
0b0011_1111,
);
pub const ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_ON_CHAIN_2: u64 = account_id(
AccountType::RegularAccountUpdatableCode,
AccountStorageType::OnChain,
0b0100_1111,
);
// FUNGIBLE TOKENS - OFF-CHAIN
pub const ACCOUNT_ID_FUNGIBLE_FAUCET_OFF_CHAIN: u64 =
account_id(AccountType::FungibleFaucet, AccountStorageType::OffChain, 0b0001_1111);
// FUNGIBLE TOKENS - ON-CHAIN
pub const ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN: u64 =
account_id(AccountType::FungibleFaucet, AccountStorageType::OnChain, 0b0001_1111);
pub const ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_1: u64 =
account_id(AccountType::FungibleFaucet, AccountStorageType::OnChain, 0b0010_1111);
pub const ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_2: u64 =
account_id(AccountType::FungibleFaucet, AccountStorageType::OnChain, 0b0011_1111);
pub const ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_3: u64 =
account_id(AccountType::FungibleFaucet, AccountStorageType::OnChain, 0b0100_1111);
// NON-FUNGIBLE TOKENS - OFF-CHAIN
pub const ACCOUNT_ID_INSUFFICIENT_ONES: u64 =
account_id(AccountType::NonFungibleFaucet, AccountStorageType::OffChain, 0b0000_0000); // invalid
pub const ACCOUNT_ID_NON_FUNGIBLE_FAUCET_OFF_CHAIN: u64 =
account_id(AccountType::NonFungibleFaucet, AccountStorageType::OffChain, 0b0001_1111);
// NON-FUNGIBLE TOKENS - ON-CHAIN
pub const ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN: u64 =
account_id(AccountType::NonFungibleFaucet, AccountStorageType::OnChain, 0b0010_1111);
pub const ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN_1: u64 =
account_id(AccountType::NonFungibleFaucet, AccountStorageType::OnChain, 0b0011_1111);
// UTILITIES
// --------------------------------------------------------------------------------------------
pub const fn account_id(
account_type: AccountType,
storage: AccountStorageType,
rest: u64,
) -> u64 {
let mut id = 0;
id ^= (storage as u64) << ACCOUNT_STORAGE_MASK_SHIFT;
id ^= (account_type as u64) << ACCOUNT_TYPE_MASK_SHIFT;
id ^= rest;
id
}
}
// TESTS
// ================================================================================================
#[cfg(test)]
mod tests {
use miden_crypto::utils::{Deserializable, Serializable};
use super::{
testing::*, AccountId, AccountStorageType, AccountType, ACCOUNT_ISFAUCET_MASK,
ACCOUNT_TYPE_MASK_SHIFT, FUNGIBLE_FAUCET, NON_FUNGIBLE_FAUCET,
REGULAR_ACCOUNT_IMMUTABLE_CODE, REGULAR_ACCOUNT_UPDATABLE_CODE,
};
#[test]
fn test_account_id() {
use crate::accounts::AccountId;
for account_type in [
AccountType::RegularAccountImmutableCode,
AccountType::RegularAccountUpdatableCode,
AccountType::NonFungibleFaucet,
AccountType::FungibleFaucet,
] {
for storage_type in [AccountStorageType::OnChain, AccountStorageType::OffChain] {
let acc = AccountId::try_from(account_id(account_type, storage_type, 0b1111_1111))
.unwrap();
assert_eq!(acc.account_type(), account_type);
assert_eq!(acc.storage_type(), storage_type);
}
}
}
#[test]
fn test_account_id_from_hex_and_back() {
for account_id in [
ACCOUNT_ID_REGULAR_ACCOUNT_IMMUTABLE_CODE_ON_CHAIN,
ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_OFF_CHAIN,
ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN,
] {
let acc = AccountId::try_from(account_id).expect("Valid account ID");
assert_eq!(acc, AccountId::from_hex(&acc.to_hex()).unwrap());
}
}
#[test]
fn test_account_id_serde() {
let account_id = AccountId::try_from(ACCOUNT_ID_REGULAR_ACCOUNT_IMMUTABLE_CODE_ON_CHAIN)
.expect("Valid account ID");
assert_eq!(account_id, AccountId::read_from_bytes(&account_id.to_bytes()).unwrap());
let account_id = AccountId::try_from(ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_OFF_CHAIN)
.expect("Valid account ID");
assert_eq!(account_id, AccountId::read_from_bytes(&account_id.to_bytes()).unwrap());
let account_id =
AccountId::try_from(ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN).expect("Valid account ID");
assert_eq!(account_id, AccountId::read_from_bytes(&account_id.to_bytes()).unwrap());
let account_id = AccountId::try_from(ACCOUNT_ID_NON_FUNGIBLE_FAUCET_OFF_CHAIN)
.expect("Valid account ID");
assert_eq!(account_id, AccountId::read_from_bytes(&account_id.to_bytes()).unwrap());
}
#[test]
fn test_account_id_account_type() {
let account_id = AccountId::try_from(ACCOUNT_ID_REGULAR_ACCOUNT_IMMUTABLE_CODE_ON_CHAIN)
.expect("Valid account ID");
let account_type: AccountType = ACCOUNT_ID_REGULAR_ACCOUNT_IMMUTABLE_CODE_ON_CHAIN.into();
assert_eq!(account_type, account_id.account_type());
let account_id = AccountId::try_from(ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_OFF_CHAIN)
.expect("Valid account ID");
let account_type: AccountType = ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_OFF_CHAIN.into();
assert_eq!(account_type, account_id.account_type());
let account_id =
AccountId::try_from(ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN).expect("Valid account ID");
let account_type: AccountType = ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN.into();
assert_eq!(account_type, account_id.account_type());
let account_id = AccountId::try_from(ACCOUNT_ID_NON_FUNGIBLE_FAUCET_OFF_CHAIN)
.expect("Valid account ID");
let account_type: AccountType = ACCOUNT_ID_NON_FUNGIBLE_FAUCET_OFF_CHAIN.into();
assert_eq!(account_type, account_id.account_type());
}
#[test]
fn test_account_id_tag_identifiers() {
let account_id = AccountId::try_from(ACCOUNT_ID_REGULAR_ACCOUNT_IMMUTABLE_CODE_ON_CHAIN)
.expect("Valid account ID");
assert!(account_id.is_regular_account());
assert_eq!(account_id.account_type(), AccountType::RegularAccountImmutableCode);
assert!(account_id.is_on_chain());
let account_id = AccountId::try_from(ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_OFF_CHAIN)
.expect("Valid account ID");
assert!(account_id.is_regular_account());
assert_eq!(account_id.account_type(), AccountType::RegularAccountUpdatableCode);
assert!(!account_id.is_on_chain());
let account_id =
AccountId::try_from(ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN).expect("Valid account ID");
assert!(account_id.is_faucet());
assert_eq!(account_id.account_type(), AccountType::FungibleFaucet);
assert!(account_id.is_on_chain());
let account_id = AccountId::try_from(ACCOUNT_ID_NON_FUNGIBLE_FAUCET_OFF_CHAIN)
.expect("Valid account ID");
assert!(account_id.is_faucet());
assert_eq!(account_id.account_type(), AccountType::NonFungibleFaucet);
assert!(!account_id.is_on_chain());
}
/// The following test ensure there is a bit available to identify an account as a faucet or
/// normal.
#[test]
fn test_account_id_faucet_bit() {
// faucets have a bit set
assert_ne!((FUNGIBLE_FAUCET << ACCOUNT_TYPE_MASK_SHIFT) & ACCOUNT_ISFAUCET_MASK, 0);
assert_ne!((NON_FUNGIBLE_FAUCET << ACCOUNT_TYPE_MASK_SHIFT) & ACCOUNT_ISFAUCET_MASK, 0);
// normal accounts do not have the faucet bit set
assert_eq!(
(REGULAR_ACCOUNT_IMMUTABLE_CODE << ACCOUNT_TYPE_MASK_SHIFT) & ACCOUNT_ISFAUCET_MASK,
0
);
assert_eq!(
(REGULAR_ACCOUNT_UPDATABLE_CODE << ACCOUNT_TYPE_MASK_SHIFT) & ACCOUNT_ISFAUCET_MASK,
0
);
}
}