wallet: refactor removal.

This commit is contained in:
Christopher Jeffrey 2017-11-28 10:30:15 -08:00
parent a43a2102e6
commit aa115c2e9d
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
5 changed files with 33 additions and 28 deletions

View File

@ -88,9 +88,7 @@ layouts.walletdb = {
},
Tt: function Tt(key) {
return [key.slice(1, 65)];
},
t: 't',
u: 'u'
}
};
layouts.txdb = {

View File

@ -176,9 +176,7 @@ layouts.walletdb = {
assert(Buffer.isBuffer(key));
assert(key.length === 33);
return key.toString('hex', 1, 33);
},
t: Buffer.from([0x74]),
u: Buffer.from([0x75])
}
};
/*

View File

@ -577,7 +577,7 @@ class MasterKey {
this.alg = br.readU8();
assert(MasterKey.algByVal[this.alg]);
assert(this.alg < MasterKey.algByVal.length);
this.n = br.readU32();
this.r = br.readU32();

View File

@ -356,6 +356,9 @@ class WalletDB extends EventEmitter {
async migrateState(state) {
const b = this.db.batch();
this.logger.info('Migrating to new sync state.');
const hashes = await this.client.getHashes(0, state.height);
for (let height = 0; height < hashes.length; height++) {
@ -816,12 +819,15 @@ class WalletDB extends EventEmitter {
if (!wid)
return false;
// Grab all locks.
const unlock1 = await this.readLock.lock(wid);
const unlock2 = await this.txLock.lock();
const unlock2 = await this.writeLock.lock();
const unlock3 = await this.txLock.lock();
try {
return await this._remove(wid);
} finally {
unlock3();
unlock2();
unlock1();
}
@ -835,6 +841,11 @@ class WalletDB extends EventEmitter {
*/
async _remove(wid) {
const tlayout = layouts.txdb;
if (wid === 1)
throw new Error('Cannot remove primary wallet.');
const data = await this.db.get(layout.w(wid));
if (!data)
@ -847,19 +858,18 @@ class WalletDB extends EventEmitter {
b.del(layout.w(wid));
b.del(layout.l(id));
const pathIter = this.db.iterator({
const piter = this.db.iterator({
gte: layout.P(wid, encoding.NULL_HASH),
lte: layout.P(wid, encoding.HIGH_HASH),
keys: true
lte: layout.P(wid, encoding.HIGH_HASH)
});
await pathIter.each(async (key, value) => {
await piter.each((key, value) => {
const hash = layout.Pp(key);
b.del(key);
return this.removePathMap(b, hash, wid);
});
const removeRange = async (opt) => {
const removeRange = (opt) => {
return this.db.iterator(opt).each(key => b.del(key));
};
@ -884,21 +894,18 @@ class WalletDB extends EventEmitter {
});
await removeRange({
gte: layout.t,
lt: layout.u
gte: tlayout.prefix(wid),
lt: tlayout.prefix(wid + 1)
});
const tlayout = layouts.txdb;
const prefix = tlayout.prefix(wid);
const bucket = this.db.bucket(prefix);
const bucket = this.db.bucket(tlayout.prefix(wid));
const biter = bucket.iterator({
gte: tlayout.b(0x00000000),
lte: tlayout.b(0xffffffff),
keys: true
lte: tlayout.b(0xffffffff)
});
await biter.each(async (key, value) => {
await biter.each((key, value) => {
const height = layout.bb(key);
return this.removeBlockMap(b, height, wid);
});
@ -909,25 +916,28 @@ class WalletDB extends EventEmitter {
keys: true
});
await siter.each(async (key, value) => {
await siter.each((key, value) => {
const [hash, index] = layout.ss(key);
return this.removeOutpointMap(b, hash, index, wid);
});
const piter = bucket.iterator({
const uiter = bucket.iterator({
gte: tlayout.p(encoding.NULL_HASH),
lte: tlayout.p(encoding.HIGH_HASH),
keys: true
});
await piter.each(async (key, value) => {
await uiter.each((key, value) => {
const hash = layout.pp(key);
return this.removeTXMap(b, hash, wid);
});
const w = await this._get(wid);
await w.destroy();
this.unregister(w);
const wallet = this.wallets.get(wid);
if (wallet) {
await wallet.destroy();
this.unregister(wallet);
}
await b.write();

View File

@ -456,7 +456,6 @@ async function updateAccount(wid, acct) {
};
const count = br.readU8();
const keys = [];
for (let i = 0; i < count; i++) {