Skip to content

Commit 5eefb0c

Browse files
authored
Merge pull request #825 from openmina/fix-remove
Fix map `token_to_accounts` in Mask
2 parents 50bc5df + dd34112 commit 5eefb0c

File tree

5 files changed

+1
-97
lines changed

5 files changed

+1
-97
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_to_account: 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 & 57 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_to_account: 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_to_account: 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_to_account,
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_to_account: token_to_account.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_to_account,
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_to_account: token_to_account.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_to_account,
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_to_account", &token_to_account.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_to_account,
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_to_account", &token_to_account.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_to_account,
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_to_account: take(token_to_account),
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_to_account,
382369
id_to_addr,
383370
hashes,
384371
..
385372
} => {
386373
assert_ne!(parent.get_uuid(), self_uuid);
387374

388375
let (accounts, hashes) = {
389-
token_to_account.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_to_account,
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_to_account = std::mem::take(token_to_account);
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_to_account,
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_to_account,
794776
id_to_addr,
795777
last_location,
796778
hashes,
797779
..
798780
}
799781
| Attached {
800782
owning_account,
801-
token_to_account,
802783
id_to_addr,
803784
last_location,
804785
hashes,
@@ -814,8 +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_to_account.remove(&account.token_id).unwrap();
798+
let _account = owning_account.remove(&account_index).unwrap();
819799

820800
if last_location
821801
.as_ref()
@@ -852,24 +832,20 @@ impl MaskImpl {
852832
Root { database, .. } => database.set(addr, account),
853833
Unattached {
854834
owning_account,
855-
token_to_account,
856835
id_to_addr,
857836
last_location,
858837
..
859838
}
860839
| Attached {
861840
owning_account,
862-
token_to_account,
863841
id_to_addr,
864842
last_location,
865843
..
866844
} => {
867845
let account_id = account.id();
868-
let token_id = account.token_id.clone();
869846

870847
owning_account.insert(account_index, *account);
871848
id_to_addr.insert(account_id.clone(), addr.clone());
872-
token_to_account.insert(token_id, account_id);
873849

874850
if last_location
875851
.as_ref()
@@ -1111,34 +1087,6 @@ impl BaseLedger for MaskImpl {
11111087
set
11121088
}
11131089

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

@@ -1195,15 +1143,13 @@ impl BaseLedger for MaskImpl {
11951143
Root { database, .. } => database.get_or_create_account(account_id, account)?,
11961144
Unattached {
11971145
owning_account,
1198-
token_to_account,
11991146
id_to_addr,
12001147
last_location,
12011148
depth,
12021149
..
12031150
}
12041151
| Attached {
12051152
owning_account,
1206-
token_to_account,
12071153
id_to_addr,
12081154
last_location,
12091155
depth,
@@ -1215,11 +1161,9 @@ impl BaseLedger for MaskImpl {
12151161
};
12161162

12171163
let account_index: AccountIndex = location.to_index();
1218-
let token_id = account.token_id.clone();
12191164

12201165
id_to_addr.insert(account_id.clone(), location.clone());
12211166
*last_location = Some(location.clone());
1222-
token_to_account.insert(token_id, account_id);
12231167
owning_account.insert(account_index, account);
12241168

12251169
self.invalidate_hashes(account_index);

0 commit comments

Comments
 (0)