Skip to content

Commit 107a928

Browse files
author
Adrian Nagy
committed
Include everything archive realeted behind the --archive-address flag
1 parent 73b274e commit 107a928

File tree

21 files changed

+323
-234
lines changed

21 files changed

+323
-234
lines changed

ledger/src/bin/ledger.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ fn main() {
66

77
let now = redux::Instant::now();
88

9-
let mut db = Database::<V2>::create(20);
9+
let mut db = Database::<V2>::create(20, false);
1010

1111
let accounts = (0..naccounts).map(|_| Account::rand()).collect::<Vec<_>>();
1212

ledger/src/database/database.rs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@ impl Database<V2> {
4949
}
5050

5151
impl Database<V2> {
52-
pub fn create_with_dir(depth: u8, dir_name: Option<PathBuf>) -> Self {
53-
let db = DatabaseImpl::<V2>::create_with_dir(depth, dir_name);
52+
pub fn create_with_dir(depth: u8, dir_name: Option<PathBuf>, is_archive: bool) -> Self {
53+
let db = DatabaseImpl::<V2>::create_with_dir(depth, dir_name, is_archive);
5454

5555
Self {
5656
inner: Arc::new(Mutex::new(db)),
5757
}
5858
}
5959

60-
pub fn create(depth: u8) -> Self {
61-
Self::create_with_dir(depth, None)
60+
pub fn create(depth: u8, is_archive: bool) -> Self {
61+
Self::create_with_dir(depth, None, is_archive)
6262
}
6363

6464
pub fn root_hash(&mut self) -> Fp {
@@ -367,7 +367,7 @@ mod tests {
367367
let two: usize = 2;
368368

369369
for depth in 2..15 {
370-
let mut db = Database::<V2>::create(depth);
370+
let mut db = Database::<V2>::create(depth, false);
371371

372372
for _ in 0..two.pow(depth as u32) {
373373
let account = Account::rand();
@@ -480,7 +480,7 @@ export function performance_now() {
480480

481481
console::time_with_label("generate random accounts");
482482

483-
let mut db = Database::<V2>::create(20);
483+
let mut db = Database::<V2>::create(20, false);
484484

485485
console::log_1(&format!("{:?} accounts in nodejs", NACCOUNTS).into());
486486

@@ -507,7 +507,7 @@ export function performance_now() {
507507
const NACCOUNTS: u64 = 1_000;
508508

509509
let now = redux::Instant::now();
510-
let mut db = Database::<V2>::create(20);
510+
let mut db = Database::<V2>::create(20, false);
511511

512512
elog!("{:?} accounts natively", NACCOUNTS);
513513

@@ -627,7 +627,7 @@ export function performance_now() {
627627
/// Accounts inserted in a different order produce different root hash
628628
#[test]
629629
fn test_root_hash_different_orders() {
630-
let mut db = Database::<V2>::create(4);
630+
let mut db = Database::<V2>::create(4, false);
631631

632632
let accounts = (0..16).map(|_| Account::rand()).collect::<Vec<_>>();
633633

@@ -637,7 +637,7 @@ export function performance_now() {
637637
}
638638
let root_hash_1 = db.merkle_root();
639639

640-
let mut db = Database::<V2>::create(4);
640+
let mut db = Database::<V2>::create(4, false);
641641
for account in accounts.iter().rev() {
642642
db.get_or_create_account(account.id(), account.clone())
643643
.unwrap();
@@ -647,7 +647,7 @@ export function performance_now() {
647647
// Different orders, different root hash
648648
assert_ne!(root_hash_1, root_hash_2);
649649

650-
let mut db = Database::<V2>::create(4);
650+
let mut db = Database::<V2>::create(4, false);
651651
for account in accounts {
652652
db.get_or_create_account(account.id(), account).unwrap();
653653
}
@@ -710,7 +710,7 @@ mod tests_ocaml {
710710
// "add and retrieve an account"
711711
#[test]
712712
fn test_add_retrieve_account() {
713-
let mut db = Database::<V2>::create(4);
713+
let mut db = Database::<V2>::create(4, false);
714714

715715
let account = Account::rand();
716716
let location = db
@@ -724,7 +724,7 @@ mod tests_ocaml {
724724
// "accounts are atomic"
725725
#[test]
726726
fn test_accounts_are_atomic() {
727-
let mut db = Database::<V2>::create(4);
727+
let mut db = Database::<V2>::create(4, false);
728728

729729
let account = Box::new(Account::rand());
730730
let location: Address = db
@@ -743,7 +743,7 @@ mod tests_ocaml {
743743
#[test]
744744
fn test_lengths() {
745745
for naccounts in 50..100 {
746-
let mut db = Database::<V2>::create(10);
746+
let mut db = Database::<V2>::create(10, false);
747747
let mut unique = HashSet::with_capacity(naccounts);
748748

749749
for _ in 0..naccounts {
@@ -764,7 +764,7 @@ mod tests_ocaml {
764764
// "get_or_create_acount does not update an account if key already""
765765
#[test]
766766
fn test_no_update_if_exist() {
767-
let mut db = Database::<V2>::create(10);
767+
let mut db = Database::<V2>::create(10, false);
768768

769769
let mut account1 = Account::rand();
770770
account1.balance = Balance::from_u64(100);
@@ -792,7 +792,7 @@ mod tests_ocaml {
792792
#[test]
793793
fn test_location_of_account() {
794794
for naccounts in 50..100 {
795-
let mut db = Database::<V2>::create(10);
795+
let mut db = Database::<V2>::create(10, false);
796796

797797
for _ in 0..naccounts {
798798
let account = Account::rand();
@@ -816,7 +816,7 @@ mod tests_ocaml {
816816
}
817817

818818
fn create_full_db(depth: usize) -> Database<V2> {
819-
let mut db = Database::<V2>::create(depth as u8);
819+
let mut db = Database::<V2>::create(depth as u8, false);
820820

821821
for _ in 0..2u64.pow(depth as u32) {
822822
let account = Account::rand();
@@ -874,7 +874,7 @@ mod tests_ocaml {
874874
fn test_retrieve_account_after_set_batch() {
875875
const DEPTH: usize = 7;
876876

877-
let mut db = Database::<V2>::create(DEPTH as u8);
877+
let mut db = Database::<V2>::create(DEPTH as u8, false);
878878

879879
let mut addr = Address::root();
880880
for _ in 0..63 {
@@ -947,7 +947,7 @@ mod tests_ocaml {
947947
fn test_create_empty_doesnt_modify_hash() {
948948
const DEPTH: usize = 7;
949949

950-
let mut db = Database::<V2>::create(DEPTH as u8);
950+
let mut db = Database::<V2>::create(DEPTH as u8, false);
951951

952952
let start_hash = db.merkle_root();
953953

@@ -967,7 +967,7 @@ mod tests_ocaml {
967967
const DEPTH: usize = 7;
968968
const NACCOUNTS: usize = 2u64.pow(DEPTH as u32) as usize;
969969

970-
let mut db = Database::<V2>::create(DEPTH as u8);
970+
let mut db = Database::<V2>::create(DEPTH as u8, false);
971971
let mut accounts = Vec::with_capacity(NACCOUNTS);
972972

973973
for _ in 0..NACCOUNTS {
@@ -1010,7 +1010,7 @@ mod tests_ocaml {
10101010
const DEPTH: usize = 7;
10111011
const NACCOUNTS: usize = 2u64.pow(DEPTH as u32) as usize;
10121012

1013-
let mut db = Database::<V2>::create(DEPTH as u8);
1013+
let mut db = Database::<V2>::create(DEPTH as u8, false);
10141014
let mut accounts = Vec::with_capacity(NACCOUNTS);
10151015

10161016
for _ in 0..NACCOUNTS {
@@ -1028,7 +1028,7 @@ mod tests_ocaml {
10281028
const DEPTH: usize = 7;
10291029
const NACCOUNTS: usize = 2u64.pow(DEPTH as u32) as usize;
10301030

1031-
let mut db = Database::<V2>::create(DEPTH as u8);
1031+
let mut db = Database::<V2>::create(DEPTH as u8, false);
10321032
let mut accounts = Vec::with_capacity(NACCOUNTS);
10331033

10341034
for _ in 0..NACCOUNTS {
@@ -1053,7 +1053,7 @@ mod tests_ocaml {
10531053
const DEPTH: usize = 7;
10541054
const NACCOUNTS: usize = 2u64.pow(DEPTH as u32) as usize;
10551055

1056-
let mut db = Database::<V2>::create(DEPTH as u8);
1056+
let mut db = Database::<V2>::create(DEPTH as u8, false);
10571057

10581058
let root_hash = db.merkle_root();
10591059

@@ -1076,7 +1076,7 @@ mod tests_ocaml {
10761076
const DEPTH: usize = 7;
10771077
const NACCOUNTS: usize = 2u64.pow(DEPTH as u32) as usize;
10781078

1079-
let mut db = Database::<V2>::create(DEPTH as u8);
1079+
let mut db = Database::<V2>::create(DEPTH as u8, false);
10801080
let mut total_balance: u128 = 0;
10811081

10821082
for _ in 0..NACCOUNTS {
@@ -1095,7 +1095,7 @@ mod tests_ocaml {
10951095
const DEPTH: usize = 7;
10961096
const NACCOUNTS: usize = 2u64.pow(DEPTH as u32) as usize;
10971097

1098-
let mut db = Database::<V2>::create(DEPTH as u8);
1098+
let mut db = Database::<V2>::create(DEPTH as u8, false);
10991099
let mut total_balance: u128 = 0;
11001100
let mut last_id: AccountId = Account::empty().id();
11011101

@@ -1126,7 +1126,7 @@ mod tests_ocaml {
11261126
const DEPTH: usize = 4;
11271127
const NACCOUNTS: usize = 2u64.pow(DEPTH as u32) as usize;
11281128

1129-
let mut db = Database::<V2>::create(DEPTH as u8);
1129+
let mut db = Database::<V2>::create(DEPTH as u8, false);
11301130

11311131
for index in 0..NACCOUNTS / 2 {
11321132
let mut account = Account::empty();
@@ -1262,7 +1262,7 @@ mod tests_ocaml {
12621262
const DEPTH: usize = 20;
12631263
const NACCOUNTS: usize = 2u64.pow(DEPTH as u32) as usize;
12641264

1265-
let mut db = Database::<V2>::create(DEPTH as u8);
1265+
let mut db = Database::<V2>::create(DEPTH as u8, false);
12661266
db.merkle_path(Address::first(20));
12671267
}
12681268

@@ -1290,7 +1290,7 @@ mod tests_ocaml {
12901290
// db.get_or_create_account(account.id(), account).unwrap();
12911291
// db.merkle_root();
12921292

1293-
let mut db = Database::<V2>::create(DEPTH as u8);
1293+
let mut db = Database::<V2>::create(DEPTH as u8, false);
12941294

12951295
// for _ in 0..NACCOUNTS {
12961296
// let account = Account::rand();

ledger/src/database/database_impl.rs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub struct DatabaseImpl<T: TreeVersion> {
2020
accounts: Vec<Option<T::Account>>,
2121
pub hashes_matrix: HashesMatrix,
2222
id_to_addr: HashMap<AccountId, Address>,
23-
token_to_account: HashMap<T::TokenId, AccountId>,
23+
token_to_account: Option<HashMap<T::TokenId, AccountId>>,
2424
depth: u8,
2525
last_location: Option<Address>,
2626
naccounts: usize,
@@ -106,8 +106,9 @@ impl DatabaseImpl<V2> {
106106
self.naccounts += 1;
107107

108108
if !token_id.is_default() {
109-
self.token_to_account
110-
.insert(account_id.derive_token_id(), account_id.clone());
109+
if let Some(token_to_account) = self.token_to_account.as_mut() {
110+
token_to_account.insert(account_id.derive_token_id(), account_id.clone());
111+
}
111112
}
112113
self.id_to_addr.insert(account_id, location.clone());
113114

@@ -309,7 +310,7 @@ impl DatabaseImpl<V1> {
309310
}
310311

311312
impl DatabaseImpl<V2> {
312-
pub fn create_with_dir(depth: u8, dir_name: Option<PathBuf>) -> Self {
313+
pub fn create_with_dir(depth: u8, dir_name: Option<PathBuf>, is_archive: bool) -> Self {
313314
assert!((1..0xfe).contains(&depth));
314315

315316
const NACCOUNTS: usize = 10_000;
@@ -337,22 +338,28 @@ impl DatabaseImpl<V2> {
337338

338339
// std::fs::create_dir_all(&path).ok();
339340

341+
let token_to_account = if is_archive {
342+
None
343+
} else {
344+
Some(HashMap::with_capacity(NACCOUNTS))
345+
};
346+
340347
Self {
341348
depth,
342349
accounts: Vec::with_capacity(NACCOUNTS),
343350
last_location: None,
344351
naccounts: 0,
345352
id_to_addr: HashMap::with_capacity(NACCOUNTS),
346-
token_to_account: HashMap::with_capacity(NACCOUNTS),
353+
token_to_account,
347354
uuid,
348355
directory: path,
349356
hashes_matrix: HashesMatrix::new(depth as usize),
350357
// root_hash: Default::default(),
351358
}
352359
}
353360

354-
pub fn create(depth: u8) -> Self {
355-
Self::create_with_dir(depth, None)
361+
pub fn create(depth: u8, is_archive: bool) -> Self {
362+
Self::create_with_dir(depth, None, is_archive)
356363
}
357364

358365
pub fn root_hash(&mut self) -> Fp {
@@ -525,11 +532,17 @@ impl BaseLedger for DatabaseImpl<V2> {
525532
}
526533

527534
fn token_owner(&self, token_id: TokenId) -> Option<AccountId> {
528-
self.token_to_account.get(&token_id).cloned()
535+
self.token_to_account
536+
.as_ref()
537+
.and_then(|token_to_account| token_to_account.get(&token_id).cloned())
529538
}
530539

531540
fn token_owners(&self) -> HashSet<AccountId> {
532-
self.token_to_account.values().cloned().collect()
541+
if let Some(token_to_account) = self.token_to_account.as_ref() {
542+
token_to_account.values().cloned().collect()
543+
} else {
544+
HashSet::new()
545+
}
533546
}
534547

535548
fn tokens(&self, public_key: CompressedPubKey) -> HashSet<TokenId> {
@@ -690,15 +703,18 @@ impl BaseLedger for DatabaseImpl<V2> {
690703
let id = account.id();
691704
self.id_to_addr.remove(&id);
692705
if !id.token_id.is_default() {
693-
self.token_to_account.remove(&id.derive_token_id());
706+
if let Some(token_to_account) = self.token_to_account.as_mut() {
707+
token_to_account.remove(&id.derive_token_id());
708+
}
694709
}
695710
} else {
696711
self.naccounts += 1;
697712
}
698713

699714
if !account.token_id.is_default() {
700-
self.token_to_account
701-
.insert(account.id().derive_token_id(), id.clone());
715+
if let Some(token_to_account) = self.token_to_account.as_mut() {
716+
token_to_account.insert(account.id().derive_token_id(), id.clone());
717+
}
702718
}
703719
self.id_to_addr.insert(id, addr.clone());
704720
self.accounts[index] = Some(*account);
@@ -830,7 +846,9 @@ impl BaseLedger for DatabaseImpl<V2> {
830846
let id = account.id();
831847
self.id_to_addr.remove(&id);
832848
if !id.token_id.is_default() {
833-
self.token_to_account.remove(&id.derive_token_id());
849+
if let Some(token_to_account) = self.token_to_account.as_mut() {
850+
token_to_account.remove(&id.derive_token_id());
851+
}
834852
}
835853

836854
self.naccounts = self

ledger/src/mask/mask.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl Mask {
116116
}
117117

118118
pub fn create(depth: usize) -> Self {
119-
Self::new_root(Database::create(depth as u8))
119+
Self::new_root(Database::create(depth as u8, false))
120120
}
121121

122122
pub fn make_child(&self) -> Mask {
@@ -595,7 +595,7 @@ mod tests {
595595

596596
let mask_root_hash = mask.merkle_root();
597597

598-
let mut db = Database::create(DEPTH as u8);
598+
let mut db = Database::create(DEPTH as u8, false);
599599
for account in &accounts {
600600
db.get_or_create_account(account.id(), account.clone())
601601
.unwrap();
@@ -711,12 +711,12 @@ mod tests_mask_ocaml {
711711
pub const FIRST_LOC: Address = Address::first(DEPTH);
712712

713713
pub fn new_instances(depth: usize) -> (Mask, Mask) {
714-
let db = Database::<V2>::create(depth as u8);
714+
let db = Database::<V2>::create(depth as u8, false);
715715
(Mask::new_root(db), Mask::new_unattached(depth))
716716
}
717717

718718
pub fn new_chain(depth: usize) -> (Mask, Mask, Mask) {
719-
let db = Database::<V2>::create(depth as u8);
719+
let db = Database::<V2>::create(depth as u8, false);
720720
let layer1 = Mask::new_unattached(depth);
721721
let layer2 = Mask::new_unattached(depth);
722722

0 commit comments

Comments
 (0)