wallet: refactor.

This commit is contained in:
Christopher Jeffrey 2017-12-08 23:14:11 -08:00
parent aa3988aa2f
commit 30597b83f6
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 37 additions and 24 deletions

View File

@ -420,7 +420,7 @@ class WalletDB extends EventEmitter {
height -= 1; height -= 1;
} }
await this.scan(height); return this.scan(height);
} }
/** /**
@ -800,7 +800,11 @@ class WalletDB extends EventEmitter {
this.save(b, wallet); this.save(b, wallet);
wallet.id = old;
await b.write(); await b.write();
wallet.id = id;
} }
/** /**
@ -1067,7 +1071,7 @@ class WalletDB extends EventEmitter {
* @returns {Promise} - Returns Array. * @returns {Promise} - Returns Array.
*/ */
getAccounts(wid) { async getAccounts(wid) {
return this.db.values({ return this.db.values({
gte: layout.n.min(wid), gte: layout.n.min(wid),
lte: layout.n.max(wid), lte: layout.n.max(wid),
@ -1228,7 +1232,7 @@ class WalletDB extends EventEmitter {
* @returns {Promise} * @returns {Promise}
*/ */
getHashes() { async getHashes() {
return this.db.keys({ return this.db.keys({
gte: layout.p.min(), gte: layout.p.min(),
lte: layout.p.max(), lte: layout.p.max(),
@ -1241,7 +1245,7 @@ class WalletDB extends EventEmitter {
* @returns {Promise} * @returns {Promise}
*/ */
getOutpoints() { async getOutpoints() {
return this.db.keys({ return this.db.keys({
gte: layout.o.min(), gte: layout.o.min(),
lte: layout.o.max(), lte: layout.o.max(),
@ -1258,7 +1262,7 @@ class WalletDB extends EventEmitter {
* @returns {Promise} * @returns {Promise}
*/ */
getWalletHashes(wid) { async getWalletHashes(wid) {
return this.db.keys({ return this.db.keys({
gte: layout.P.min(wid), gte: layout.P.min(wid),
lte: layout.P.max(wid), lte: layout.P.max(wid),
@ -1273,7 +1277,7 @@ class WalletDB extends EventEmitter {
* @returns {Promise} * @returns {Promise}
*/ */
getAccountHashes(wid, account) { async getAccountHashes(wid, account) {
return this.db.keys({ return this.db.keys({
gte: layout.r.min(wid, account), gte: layout.r.min(wid, account),
lte: layout.r.max(wid, account), lte: layout.r.max(wid, account),
@ -1295,9 +1299,9 @@ class WalletDB extends EventEmitter {
const paths = []; const paths = [];
for (const item of items) { for (const {key, value} of items) {
const [, hash] = layout.P.parse(item.key); const [, hash] = layout.P.parse(key);
const path = Path.fromRaw(item.value); const path = Path.fromRaw(value);
path.hash = hash; path.hash = hash;
path.name = await this.getAccountName(wid, path.account); path.name = await this.getAccountName(wid, path.account);
@ -1314,7 +1318,7 @@ class WalletDB extends EventEmitter {
* @returns {Promise} * @returns {Promise}
*/ */
getWallets() { async getWallets() {
return this.db.keys({ return this.db.keys({
gte: layout.l.min(), gte: layout.l.min(),
lte: layout.l.max(), lte: layout.l.max(),
@ -1458,7 +1462,7 @@ class WalletDB extends EventEmitter {
*/ */
async getWalletsByTX(tx) { async getWalletsByTX(tx) {
const result = new Set(); const wids = new Set();
if (!tx.isCoinbase()) { if (!tx.isCoinbase()) {
for (const {prevout} of tx.inputs) { for (const {prevout} of tx.inputs) {
@ -1473,7 +1477,7 @@ class WalletDB extends EventEmitter {
continue; continue;
for (const wid of map.wids) for (const wid of map.wids)
result.add(wid); wids.add(wid);
} }
} }
@ -1489,13 +1493,13 @@ class WalletDB extends EventEmitter {
continue; continue;
for (const wid of map.wids) for (const wid of map.wids)
result.add(wid); wids.add(wid);
} }
if (result.size === 0) if (wids.size === 0)
return null; return null;
return result; return wids;
} }
/** /**
@ -1644,7 +1648,7 @@ class WalletDB extends EventEmitter {
* @returns {Promise} * @returns {Promise}
*/ */
getPathMap(hash) { async getPathMap(hash) {
return this.getMap(layout.p.build(hash)); return this.getMap(layout.p.build(hash));
} }
@ -1667,7 +1671,7 @@ class WalletDB extends EventEmitter {
* @param {Number} wid * @param {Number} wid
*/ */
removePathMap(b, hash, wid) { async removePathMap(b, hash, wid) {
return this.removeMap(b, layout.p.build(hash), wid); return this.removeMap(b, layout.p.build(hash), wid);
} }
@ -1688,7 +1692,7 @@ class WalletDB extends EventEmitter {
* @param {Number} wid * @param {Number} wid
*/ */
addBlockMap(b, height, wid) { async addBlockMap(b, height, wid) {
return this.addMap(b, layout.b.build(height), wid); return this.addMap(b, layout.b.build(height), wid);
} }
@ -1699,7 +1703,7 @@ class WalletDB extends EventEmitter {
* @param {Number} wid * @param {Number} wid
*/ */
removeBlockMap(b, height, wid) { async removeBlockMap(b, height, wid) {
return this.removeMap(b, layout.b.build(height), wid); return this.removeMap(b, layout.b.build(height), wid);
} }
@ -1709,7 +1713,7 @@ class WalletDB extends EventEmitter {
* @returns {Promise} * @returns {Promise}
*/ */
getTXMap(hash) { async getTXMap(hash) {
return this.getMap(layout.T.build(hash)); return this.getMap(layout.T.build(hash));
} }
@ -1720,7 +1724,7 @@ class WalletDB extends EventEmitter {
* @param {Number} wid * @param {Number} wid
*/ */
addTXMap(b, hash, wid) { async addTXMap(b, hash, wid) {
return this.addMap(b, layout.T.build(hash), wid); return this.addMap(b, layout.T.build(hash), wid);
} }
@ -1731,7 +1735,7 @@ class WalletDB extends EventEmitter {
* @param {Number} wid * @param {Number} wid
*/ */
removeTXMap(b, hash, wid) { async removeTXMap(b, hash, wid) {
return this.removeMap(b, layout.T.build(hash), wid); return this.removeMap(b, layout.T.build(hash), wid);
} }
@ -1741,7 +1745,7 @@ class WalletDB extends EventEmitter {
* @returns {Promise} * @returns {Promise}
*/ */
getOutpointMap(hash, index) { async getOutpointMap(hash, index) {
return this.getMap(layout.o.build(hash, index)); return this.getMap(layout.o.build(hash, index));
} }
@ -1764,7 +1768,7 @@ class WalletDB extends EventEmitter {
* @param {Number} wid * @param {Number} wid
*/ */
removeOutpointMap(b, hash, index, wid) { async removeOutpointMap(b, hash, index, wid) {
return this.removeMap(b, layout.o.build(hash, index), wid); return this.removeMap(b, layout.o.build(hash, index), wid);
} }

View File

@ -1498,6 +1498,15 @@ describe('Wallet', function() {
assert.strictEqual((await bob.getBalance()).unconfirmed, 30000); assert.strictEqual((await bob.getBalance()).unconfirmed, 30000);
}); });
it('should remove a wallet', async () => {
const wallet = await wdb.create({
id: 'alice100'
});
assert(await wdb.get('alice100'));
await wdb.remove('alice100');
assert(!await wdb.get('alice100'));
});
it('should cleanup', () => { it('should cleanup', () => {
consensus.COINBASE_MATURITY = 100; consensus.COINBASE_MATURITY = 100;
}); });