Skip to content

Commit

Permalink
Merge PR #520 from 'kyokan/remove-wid-from-map'
Browse files Browse the repository at this point in the history
  • Loading branch information
pinheadmz committed Feb 4, 2021
2 parents a800805 + b3c97a4 commit 4c7db7f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
10 changes: 10 additions & 0 deletions lib/wallet/walletdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,16 @@ class WalletDB extends EventEmitter {
return this.removePathMap(b, hash, wid);
});

const niter = this.db.iterator({
gte: layout.N.min(),
lte: layout.N.max()
});

await niter.each((key) => {
const [hash] = layout.N.decode(key);
return this.removeNameMap(b, hash, wid);
});

const removeRange = (opt) => {
return this.db.iterator(opt).each(key => b.del(key));
};
Expand Down
38 changes: 36 additions & 2 deletions test/wallet-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const policy = require('../lib/protocol/policy');
const HDPrivateKey = require('../lib/hd/private');
const Wallet = require('../lib/wallet/wallet');
const rules = require('../lib/covenants/rules');
const {types} = rules;
const {types, hashName} = rules;
const {forValue} = require('./util/common');

const KEY1 = 'xprv9s21ZrQH143K3Aj6xQBymM31Zb4BVc7wxqfUhMZrzewdDVCt'
Expand Down Expand Up @@ -1490,11 +1490,45 @@ describe('Wallet', function() {
});

it('should remove a wallet', async () => {
await wdb.create({
const wallet = await wdb.create({
id: 'alice100'
});
const addr1 = await wallet.receiveAddress();
const b = wdb.db.batch();
const wid = await wdb.getWID('alice100');
assert(await wdb.get('alice100'));

// Add one single, unconfirmed coin to wallet
const mtx = new MTX();
mtx.addInput(dummyInput());
mtx.addOutput(addr1, 10 * 1e8);
await wdb.addTXMap(b, mtx.hash(), wid);

// Add one name to NameMap
await wdb.addNameMap(b, hashName('test123'), wid);

await b.write();

// Should return tx from TX Map
let wids = await wdb.getWalletsByTX(mtx);
assert(wids.has(wid));

// Should have wid in NameMap
let map = await wdb.getNameMap(hashName('test123'));
assert(map.wids.has(wid));

// Remove wallet
await wdb.remove('alice100');

// Should not return tx from TX Map after wallet is removed
wids = await wdb.getWalletsByTX(mtx);
assert.strictEqual(wids, null);

// Should not return wid from NameMap after wid is removed
map = await wdb.getNameMap(hashName('test123'));
assert.strictEqual(map, null);

// Should not return wallet after it is removed
assert(!await wdb.get('alice100'));
});

Expand Down

0 comments on commit 4c7db7f

Please sign in to comment.