-
Notifications
You must be signed in to change notification settings - Fork 301
wallet: deep clean #511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
wallet: deep clean #511
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -503,6 +503,93 @@ class WalletDB extends EventEmitter { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Deep Clean: | ||
| * Keep all keys, account data, wallet maps (name and path). | ||
| * Dump all TX history and balance state. | ||
| * A rescan will be required but is not initiated automatically. | ||
| * @returns {Promise} | ||
| */ | ||
|
|
||
| async deepClean() { | ||
| const unlock1 = await this.txLock.lock(); | ||
| const unlock2 = await this.writeLock.lock(); | ||
| const unlock3 = await this.readLock.lock(); | ||
| try { | ||
| return await this._deepClean(); | ||
| } finally { | ||
| unlock3(); | ||
| unlock2(); | ||
| unlock1(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Deep Clean (without locks): | ||
| * Keep all keys, account data, wallet maps (name and path). | ||
| * Dump all TX history and balance state. | ||
| * A rescan will be required but is not initiated automatically. | ||
| * @returns {Promise} | ||
| */ | ||
|
|
||
| async _deepClean() { | ||
| this.logger.warning('Initiating Deep Clean...'); | ||
|
|
||
| const b = this.db.batch(); | ||
| const removeRange = (opt) => { | ||
| return this.db.iterator(opt).each(key => b.del(key)); | ||
| }; | ||
|
|
||
| this.logger.warning('Clearing block map, tx map and outpoint map...'); | ||
| // b[height] -> block->wid map | ||
| await removeRange({ | ||
| gte: layout.b.min(), | ||
| lte: layout.b.max() | ||
| }); | ||
|
|
||
| // o[hash][index] -> outpoint->wid map | ||
| await removeRange({ | ||
| gte: layout.o.min(), | ||
| lte: layout.o.max() | ||
| }); | ||
|
|
||
| // T[hash] -> tx->wid map | ||
| await removeRange({ | ||
| gte: layout.T.min(), | ||
| lte: layout.T.max() | ||
| }); | ||
|
|
||
| const wids = await this.db.keys({ | ||
| gte: layout.W.min(), | ||
| lte: layout.W.max(), | ||
| parse: key => layout.W.decode(key)[0] | ||
| }); | ||
|
|
||
| for (const wid of wids) { | ||
| const wallet = await this.get(wid); | ||
| this.logger.warning( | ||
| 'Clearing all tx history for wallet: %s (%d)', | ||
| wallet.id, wid | ||
| ); | ||
|
|
||
| // remove all txdb data *except* blinds ('v') | ||
| const key = 'v'.charCodeAt(); | ||
| const prefix = layout.t.encode(wid); | ||
| await removeRange({ | ||
| gte: Buffer.concat([prefix, Buffer.alloc(1)]), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this supposed to be a
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It "should" be a single Removed: |
||
| lt: Buffer.concat([prefix, Buffer.from([key])]) | ||
| }); | ||
| await removeRange({ | ||
| gt: Buffer.concat([prefix, Buffer.from([key + 1])]), | ||
| lte: Buffer.concat([prefix, Buffer.from([0xff])]) | ||
| }); | ||
| } | ||
|
|
||
| await b.write(); | ||
|
|
||
| this.logger.warning('Deep Clean complete. A rescan is now required.'); | ||
| } | ||
|
|
||
| /** | ||
| * Force a rescan. | ||
| * @param {Number} height | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.