Skip to content

Commit

Permalink
Wallet cleanup should use an iter to load records (#5098)
Browse files Browse the repository at this point in the history
In the case that the records to cleanup is very high, we shouldn't load
all of them in memory before cleaning up. We should only load 1 leveldb
memory block at most. This will ensure if there is a bad feedback loop
this list won't permanently grow and continue to use more memory as it
tries to clean up accounts.

This also ensures that the account record deletion is considered a
record so deleting thousands of empty account cleanup records doesn't
run forever.
  • Loading branch information
NullSoldier authored Jul 2, 2024
1 parent 8e6dfda commit e22b3b8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
1 change: 1 addition & 0 deletions ironfish/src/wallet/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,7 @@ export class Wallet {

async removeAccount(account: Account, tx?: IDatabaseTransaction): Promise<void> {
this.accounts.delete(account.id)

await this.walletDb.db.withTransaction(tx, async (tx) => {
if (account.id === this.defaultAccount) {
await this.walletDb.setDefaultAccount(null, tx)
Expand Down
5 changes: 3 additions & 2 deletions ironfish/src/wallet/walletdb/walletdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1129,13 +1129,13 @@ export class WalletDB {
}

async cleanupDeletedAccounts(recordsToCleanup: number, signal?: AbortSignal): Promise<void> {
for (const [accountId] of await this.accountIdsToCleanup.getAll()) {
for await (const [accountId] of this.accountIdsToCleanup.getAllIter()) {
const prefix = calculateAccountPrefix(accountId)
const range = StorageUtils.getPrefixKeyRange(prefix)

for (const store of this.cacheStores) {
for await (const key of store.getAllKeysIter(undefined, range)) {
if (signal?.aborted === true || recordsToCleanup === 0) {
if (signal?.aborted === true || recordsToCleanup <= 0) {
return
}

Expand All @@ -1145,6 +1145,7 @@ export class WalletDB {
}

await this.accountIdsToCleanup.del(accountId)
recordsToCleanup--
}
}

Expand Down

0 comments on commit e22b3b8

Please sign in to comment.