Skip to content

Commit dd34112

Browse files
committed
Remove token_owners map
Because it's expensive (because of `AccountId::derive_token_id`) and it's not used anywhere in the node
1 parent 30ea80b commit dd34112

File tree

5 files changed

+1
-95
lines changed

5 files changed

+1
-95
lines changed

ledger/src/base.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,6 @@ pub trait BaseLedger {
8383
/// set of account ids associated with accounts
8484
fn accounts(&self) -> HashSet<AccountId>;
8585

86-
/// Get the account id that owns a token.
87-
fn token_owner(&self, token_id: TokenId) -> Option<AccountId>;
88-
89-
/// Get the set of all accounts which own a token.
90-
fn token_owners(&self) -> HashSet<AccountId>;
91-
9286
/// Get all of the tokens for which a public key has accounts.
9387
fn tokens(&self, public_key: CompressedPubKey) -> HashSet<TokenId>;
9488

ledger/src/database/database.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,6 @@ impl BaseLedger for Database<V2> {
152152
self.with(|this| this.accounts())
153153
}
154154

155-
fn token_owner(&self, token_id: TokenId) -> Option<AccountId> {
156-
self.with(|this| this.token_owner(token_id))
157-
}
158-
159-
fn token_owners(&self) -> HashSet<AccountId> {
160-
self.with(|this| this.token_owners())
161-
}
162-
163155
fn tokens(&self, public_key: CompressedPubKey) -> HashSet<TokenId> {
164156
self.with(|this| this.tokens(public_key))
165157
}

ledger/src/database/database_impl.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ 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>,
2423
depth: u8,
2524
last_location: Option<Address>,
2625
naccounts: usize,
@@ -55,7 +54,6 @@ impl DatabaseImpl<V2> {
5554
// root: self.root.clone(),
5655
accounts: self.accounts.clone(),
5756
id_to_addr: self.id_to_addr.clone(),
58-
token_to_account: self.token_to_account.clone(),
5957
depth: self.depth,
6058
last_location: self.last_location.clone(),
6159
naccounts: self.naccounts,
@@ -90,7 +88,6 @@ impl DatabaseImpl<V2> {
9088
return Ok(GetOrCreated::Existed(addr));
9189
}
9290

93-
let token_id = account.token_id.clone();
9491
let location = match self.last_location.as_ref() {
9592
Some(last) => last.next().ok_or(DatabaseError::OutOfLeaves)?,
9693
None => Address::first(self.depth as usize),
@@ -105,7 +102,6 @@ impl DatabaseImpl<V2> {
105102
self.last_location = Some(location.clone());
106103
self.naccounts += 1;
107104

108-
self.token_to_account.insert(token_id, account_id.clone());
109105
self.id_to_addr.insert(account_id, location.clone());
110106

111107
// self.root_hash.borrow_mut().take();
@@ -340,7 +336,6 @@ impl DatabaseImpl<V2> {
340336
last_location: None,
341337
naccounts: 0,
342338
id_to_addr: HashMap::with_capacity(NACCOUNTS),
343-
token_to_account: HashMap::with_capacity(NACCOUNTS),
344339
uuid,
345340
directory: path,
346341
hashes_matrix: HashesMatrix::new(depth as usize),
@@ -521,14 +516,6 @@ impl BaseLedger for DatabaseImpl<V2> {
521516
self.id_to_addr.keys().cloned().collect()
522517
}
523518

524-
fn token_owner(&self, token_id: TokenId) -> Option<AccountId> {
525-
self.token_to_account.get(&token_id).cloned()
526-
}
527-
528-
fn token_owners(&self) -> HashSet<AccountId> {
529-
self.token_to_account.values().cloned().collect()
530-
}
531-
532519
fn tokens(&self, public_key: CompressedPubKey) -> HashSet<TokenId> {
533520
let mut set = HashSet::with_capacity(100);
534521

@@ -686,13 +673,10 @@ impl BaseLedger for DatabaseImpl<V2> {
686673
if let Some(account) = self.get(addr.clone()) {
687674
let id = account.id();
688675
self.id_to_addr.remove(&id);
689-
self.token_to_account.remove(&id.token_id);
690676
} else {
691677
self.naccounts += 1;
692678
}
693679

694-
self.token_to_account
695-
.insert(account.token_id.clone(), id.clone());
696680
self.id_to_addr.insert(id, addr.clone());
697681
self.accounts[index] = Some(*account);
698682
// root.add_account_on_path(account, addr.iter());
@@ -822,7 +806,6 @@ impl BaseLedger for DatabaseImpl<V2> {
822806

823807
let id = account.id();
824808
self.id_to_addr.remove(&id);
825-
self.token_to_account.remove(&id.token_id);
826809

827810
self.naccounts = self
828811
.naccounts

ledger/src/mask/mask.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ impl Mask {
100100
let mask = Self {
101101
inner: Arc::new(Mutex::new(MaskImpl::Unattached {
102102
owning_account: Default::default(),
103-
token_owners: Default::default(),
104103
id_to_addr: Default::default(),
105104
last_location: None,
106105
depth: depth as u8,
@@ -329,14 +328,6 @@ impl BaseLedger for Mask {
329328
self.with(|this| this.accounts())
330329
}
331330

332-
fn token_owner(&self, token_id: TokenId) -> Option<AccountId> {
333-
self.with(|this| this.token_owner(token_id))
334-
}
335-
336-
fn token_owners(&self) -> HashSet<AccountId> {
337-
self.with(|this| this.token_owners())
338-
}
339-
340331
fn tokens(&self, public_key: CompressedPubKey) -> HashSet<TokenId> {
341332
self.with(|this| this.tokens(public_key))
342333
}

ledger/src/mask/mask_impl.rs

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ pub enum MaskImpl {
2727
Attached {
2828
parent: Mask,
2929
owning_account: HashMap<AccountIndex, Account>,
30-
token_owners: HashMap<TokenId, AccountId>,
3130
id_to_addr: HashMap<AccountId, Address>,
3231
last_location: Option<Address>,
3332
depth: u8,
@@ -39,7 +38,6 @@ pub enum MaskImpl {
3938
depth: u8,
4039
childs: HashMap<Uuid, Mask>,
4140
owning_account: HashMap<AccountIndex, Account>,
42-
token_owners: HashMap<TokenId, AccountId>,
4341
id_to_addr: HashMap<AccountId, Address>,
4442
last_location: Option<Address>,
4543
hashes: HashesMatrix,
@@ -68,7 +66,6 @@ impl Clone for MaskImpl {
6866
Self::Attached {
6967
parent,
7068
owning_account,
71-
token_owners,
7269
id_to_addr,
7370
last_location,
7471
depth,
@@ -78,7 +75,6 @@ impl Clone for MaskImpl {
7875
} => Self::Attached {
7976
parent: parent.clone(),
8077
owning_account: owning_account.clone(),
81-
token_owners: token_owners.clone(),
8278
id_to_addr: id_to_addr.clone(),
8379
last_location: last_location.clone(),
8480
depth: *depth,
@@ -90,7 +86,6 @@ impl Clone for MaskImpl {
9086
depth,
9187
childs,
9288
owning_account,
93-
token_owners,
9489
id_to_addr,
9590
last_location,
9691
hashes,
@@ -99,7 +94,6 @@ impl Clone for MaskImpl {
9994
depth: *depth,
10095
childs: childs.clone(),
10196
owning_account: owning_account.clone(),
102-
token_owners: token_owners.clone(),
10397
id_to_addr: id_to_addr.clone(),
10498
last_location: last_location.clone(),
10599
hashes: hashes.clone(),
@@ -121,7 +115,6 @@ impl std::fmt::Debug for MaskImpl {
121115
Self::Attached {
122116
parent,
123117
owning_account,
124-
token_owners,
125118
id_to_addr,
126119
last_location,
127120
depth,
@@ -133,7 +126,6 @@ impl std::fmt::Debug for MaskImpl {
133126
.field("uuid", uuid)
134127
.field("parent", &parent.get_uuid())
135128
.field("owning_account", &owning_account.len())
136-
.field("token_owners", &token_owners.len())
137129
.field("id_to_addr", &id_to_addr.len())
138130
.field("last_location", last_location)
139131
.field("depth", depth)
@@ -145,7 +137,6 @@ impl std::fmt::Debug for MaskImpl {
145137
depth,
146138
childs,
147139
owning_account,
148-
token_owners,
149140
id_to_addr,
150141
last_location,
151142
hashes,
@@ -155,7 +146,6 @@ impl std::fmt::Debug for MaskImpl {
155146
.field("depth", depth)
156147
.field("childs", &childs.len())
157148
.field("owning_account", &owning_account.len())
158-
.field("token_owners", &token_owners.len())
159149
.field("id_to_addr", &id_to_addr.len())
160150
.field("last_location", last_location)
161151
.field("uuid", uuid)
@@ -298,7 +288,6 @@ impl MaskImpl {
298288
childs,
299289
uuid,
300290
owning_account,
301-
token_owners,
302291
id_to_addr,
303292
last_location,
304293
hashes,
@@ -308,7 +297,6 @@ impl MaskImpl {
308297
*self = Attached {
309298
parent,
310299
owning_account: take(owning_account),
311-
token_owners: take(token_owners),
312300
id_to_addr: take(id_to_addr),
313301
last_location: take(last_location),
314302
depth: *depth,
@@ -378,15 +366,13 @@ impl MaskImpl {
378366
Attached {
379367
parent,
380368
owning_account,
381-
token_owners,
382369
id_to_addr,
383370
hashes,
384371
..
385372
} => {
386373
assert_ne!(parent.get_uuid(), self_uuid);
387374

388375
let (accounts, hashes) = {
389-
token_owners.clear();
390376
id_to_addr.clear();
391377
(std::mem::take(owning_account), hashes.take())
392378
};
@@ -485,7 +471,6 @@ impl MaskImpl {
485471
let Self::Attached {
486472
parent,
487473
owning_account,
488-
token_owners,
489474
id_to_addr,
490475
last_location,
491476
depth,
@@ -502,15 +487,13 @@ impl MaskImpl {
502487
let owning_account = std::mem::take(owning_account);
503488
let depth = std::mem::take(depth);
504489
let childs = std::mem::take(childs);
505-
let token_owners = std::mem::take(token_owners);
506490
let id_to_addr = std::mem::take(id_to_addr);
507491
let last_location = std::mem::take(last_location);
508492
let hashes = std::mem::replace(hashes, HashesMatrix::new(depth as usize));
509493
let uuid = std::mem::replace(uuid, "temporary".to_string());
510494

511495
*self = Self::Unattached {
512496
owning_account,
513-
token_owners,
514497
id_to_addr,
515498
last_location,
516499
depth,
@@ -790,15 +773,13 @@ impl MaskImpl {
790773
Root { .. } => todo!(),
791774
Unattached {
792775
owning_account,
793-
token_owners,
794776
id_to_addr,
795777
last_location,
796778
hashes,
797779
..
798780
}
799781
| Attached {
800782
owning_account,
801-
token_owners,
802783
id_to_addr,
803784
last_location,
804785
hashes,
@@ -814,10 +795,7 @@ impl MaskImpl {
814795
let account_index = addr.to_index();
815796
hashes.invalidate_hashes(account_index);
816797

817-
let account = owning_account.remove(&account_index).unwrap();
818-
token_owners
819-
.remove(&account.id().derive_token_id())
820-
.unwrap();
798+
let _account = owning_account.remove(&account_index).unwrap();
821799

822800
if last_location
823801
.as_ref()
@@ -854,14 +832,12 @@ impl MaskImpl {
854832
Root { database, .. } => database.set(addr, account),
855833
Unattached {
856834
owning_account,
857-
token_owners,
858835
id_to_addr,
859836
last_location,
860837
..
861838
}
862839
| Attached {
863840
owning_account,
864-
token_owners,
865841
id_to_addr,
866842
last_location,
867843
..
@@ -870,7 +846,6 @@ impl MaskImpl {
870846

871847
owning_account.insert(account_index, *account);
872848
id_to_addr.insert(account_id.clone(), addr.clone());
873-
token_owners.insert(account_id.derive_token_id(), account_id);
874849

875850
if last_location
876851
.as_ref()
@@ -1112,32 +1087,6 @@ impl BaseLedger for MaskImpl {
11121087
set
11131088
}
11141089

1115-
fn token_owner(&self, token_id: TokenId) -> Option<AccountId> {
1116-
let (parent, token_owners) = match self {
1117-
Root { database, .. } => return database.token_owner(token_id),
1118-
Attached {
1119-
parent,
1120-
token_owners,
1121-
..
1122-
} => (Some(parent), token_owners),
1123-
Unattached { token_owners, .. } => (None, token_owners),
1124-
};
1125-
1126-
if let Some(account_id) = token_owners.get(&token_id).cloned() {
1127-
return Some(account_id);
1128-
};
1129-
1130-
parent.as_ref()?.token_owner(token_id)
1131-
}
1132-
1133-
fn token_owners(&self) -> HashSet<AccountId> {
1134-
// TODO: Not sure if it's the correct impl
1135-
self.to_list()
1136-
.into_iter()
1137-
.map(|account| account.id())
1138-
.collect()
1139-
}
1140-
11411090
fn tokens(&self, public_key: CompressedPubKey) -> HashSet<TokenId> {
11421091
let mut set = HashSet::with_capacity(1024);
11431092

@@ -1194,15 +1143,13 @@ impl BaseLedger for MaskImpl {
11941143
Root { database, .. } => database.get_or_create_account(account_id, account)?,
11951144
Unattached {
11961145
owning_account,
1197-
token_owners,
11981146
id_to_addr,
11991147
last_location,
12001148
depth,
12011149
..
12021150
}
12031151
| Attached {
12041152
owning_account,
1205-
token_owners,
12061153
id_to_addr,
12071154
last_location,
12081155
depth,
@@ -1217,7 +1164,6 @@ impl BaseLedger for MaskImpl {
12171164

12181165
id_to_addr.insert(account_id.clone(), location.clone());
12191166
*last_location = Some(location.clone());
1220-
token_owners.insert(account_id.derive_token_id(), account_id);
12211167
owning_account.insert(account_index, account);
12221168

12231169
self.invalidate_hashes(account_index);

0 commit comments

Comments
 (0)