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) { Tt: function Tt(key) {
return [key.slice(1, 65)]; return [key.slice(1, 65)];
}, }
t: 't',
u: 'u'
}; };
layouts.txdb = { layouts.txdb = {

View File

@ -176,9 +176,7 @@ layouts.walletdb = {
assert(Buffer.isBuffer(key)); assert(Buffer.isBuffer(key));
assert(key.length === 33); assert(key.length === 33);
return key.toString('hex', 1, 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(); this.alg = br.readU8();
assert(MasterKey.algByVal[this.alg]); assert(this.alg < MasterKey.algByVal.length);
this.n = br.readU32(); this.n = br.readU32();
this.r = br.readU32(); this.r = br.readU32();

View File

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

View File

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