Skip to content

Commit

Permalink
Merge a4b8404 into 1a086e4
Browse files Browse the repository at this point in the history
  • Loading branch information
pinheadmz committed Jan 19, 2021
2 parents 1a086e4 + a4b8404 commit 62bbbb2
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/wallet/nodeclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class NodeClient extends AsyncEmitter {
if (!this.opened)
return;

await this.emitAsync('block connect', entry, block.txs);
await this.emit('block connect', entry, block.txs);
});

this.node.chain.on('disconnect', async (entry, block) => {
Expand Down
5 changes: 5 additions & 0 deletions test/auction-rpc-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ describe('Auction RPCs', function() {
const address = (await wallet.createAddress(account)).address;
const hashes = await util.nrpc('generatetoaddress', [num, address]);
await util.confirmBlock(hashes.pop());
await common.forValue(
util.node.plugins.walletdb.wdb,
'height',
util.node.chain.height
);
};

// This function is a helper which:
Expand Down
2 changes: 2 additions & 0 deletions test/interactive-swap-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const rules = require('../lib/covenants/rules');
const {types} = rules;
const {Resource} = require('../lib/dns/resource');
const {WalletClient} = require('hs-client');
const {forValue} = require('./util/common');

const network = Network.get('regtest');

Expand Down Expand Up @@ -52,6 +53,7 @@ async function mineBlocks(n, addr) {
const block = await node.miner.mineBlock(null, addr);
await node.chain.add(block);
}
await forValue(wdb, 'height', node.chain.height);
}

describe('Interactive name swap', function() {
Expand Down
2 changes: 2 additions & 0 deletions test/wallet-accounts-auction-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const Address = require('../lib/primitives/address');
const rules = require('../lib/covenants/rules');
const Resource = require('../lib/dns/resource');
const {WalletClient} = require('hs-client');
const {forValue} = require('./util/common');

const network = Network.get('regtest');

Expand Down Expand Up @@ -39,6 +40,7 @@ async function mineBlocks(n, addr) {
const block = await node.miner.mineBlock(null, addr);
await node.chain.add(block);
}
await forValue(wdb, 'height', node.chain.height);
}

describe('Multiple accounts participating in same auction', function() {
Expand Down
2 changes: 2 additions & 0 deletions test/wallet-change-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {randomBytes} = require('bcrypto/lib/random');
const Path = require('path');
const layouts = require('../lib/wallet/layout');
const layout = layouts.wdb;
const {forValue} = require('./util/common');

const uniq = randomBytes(4).toString('hex');
const path = Path.join(tmpdir(), `hsd-test-${uniq}`);
Expand All @@ -35,6 +36,7 @@ async function mineBlocks(n, addr) {
const block = await node.miner.mineBlock(null, addr);
await node.chain.add(block);
}
await forValue(wdb, 'height', node.chain.height);
}

describe('Derive and save change addresses', function() {
Expand Down
2 changes: 2 additions & 0 deletions test/wallet-deepclean-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const Network = require('../lib/protocol/network');
const FullNode = require('../lib/node/fullnode');
const Address = require('../lib/primitives/address');
const Resource = require('../lib/dns/resource');
const {forValue} = require('./util/common');

const network = Network.get('regtest');

Expand Down Expand Up @@ -43,6 +44,7 @@ async function mineBlocks(n, addr) {
const block = await node.miner.mineBlock(null, addr);
await node.chain.add(block);
}
await forValue(wdb, 'height', node.chain.height);
}

describe('Wallet Deep Clean', function() {
Expand Down
5 changes: 5 additions & 0 deletions test/wallet-http-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,11 @@ async function mineBlocks(count, address) {
await nclient.execute('generatetoaddress', [1, address]);
await common.forValue(obj, 'complete', true);
}
await common.forValue(
node.plugins.walletdb.wdb,
'height',
node.chain.height
);
}

// create an OPEN output
Expand Down
154 changes: 154 additions & 0 deletions test/wallet-rescan-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/* eslint-env mocha */
/* eslint prefer-arrow-callback: "off" */

'use strict';

const assert = require('bsert');
const FullNode = require('../lib/node/fullnode');
const WalletNode = require('../lib/wallet/node');
const {forValue} = require('./util/common');

describe('Wallet Rescan (plugin)', function() {
const node = new FullNode({
memory: true,
network: 'regtest',
plugins: [require('../lib/wallet/plugin')]
});

const {wdb} = node.require('walletdb');
let wallet, account, address;

async function mineBlocks(n, addr) {
for (let i = 0; i < n; i++) {
const block = await node.miner.mineBlock(null, addr);
await node.chain.add(block);
}
}

before(async () => {
await node.open();
});

after(async () => {
await node.close();
});

it('should generate 100 blocks to wallet address', async () => {
wallet = await wdb.create();
account = await wallet.getAccount(0);
address = await account.receiveAddress();
await mineBlocks(100, address);
assert.strictEqual(node.chain.height, 100);
});

it('should manually rescan after rollback', async () => {
await forValue(wdb, 'height', node.chain.height);
const initialBalance = await wallet.getBalance(0);
assert.strictEqual(initialBalance.confirmed, 100 * 2000 * 1e6);

await wdb.rollback(0);
await forValue(wdb, 'height', 0);
const midBalance = await wallet.getBalance(0);
assert.strictEqual(midBalance.confirmed, 0);

await wdb.rescan(0);
await forValue(wdb, 'height', node.chain.height);
const finalBalance = await wallet.getBalance(0);
assert.deepStrictEqual(initialBalance, finalBalance);
});

it('should rescan after rollback on block connect', async () => {
// Rollback the wallet
await wdb.rollback(0);
await forValue(wdb, 'height', 0);
const midBalance = await wallet.getBalance(0);
assert.strictEqual(midBalance.confirmed, 0);

// Wallet state is way behind chain state
assert.strictEqual(node.chain.height, 100);
assert.strictEqual(wdb.state.height, 0);

// Adding a new block to the chain should trigger a wallet rescan
// if the wallet state is behind the chain height.
await mineBlocks(1, address);

await forValue(wdb, 'height', node.chain.height);
const finalBalance = await wallet.getBalance(0);
assert.strictEqual(finalBalance.confirmed, 101 * 2000 * 1e6);
});
});
describe('Wallet Rescan (node)', function() {
const node = new FullNode({
memory: true,
network: 'regtest'
});

const walletNode = new WalletNode({
memory: true,
network: 'regtest'
});

const {wdb} = walletNode;
let wallet, account, address;

async function mineBlocks(n, addr) {
for (let i = 0; i < n; i++) {
const block = await node.miner.mineBlock(null, addr);
await node.chain.add(block);
}
}

before(async () => {
await node.open();
await walletNode.open();
});

after(async () => {
await walletNode.close();
await node.close();
});

it('should generate 100 blocks to wallet address', async () => {
wallet = await wdb.create();
account = await wallet.getAccount(0);
address = await account.receiveAddress();
await mineBlocks(100, address);
assert.strictEqual(node.chain.height, 100);
});

it('should manually rescan after rollback', async () => {
await forValue(wdb, 'height', node.chain.height);
const initialBalance = await wallet.getBalance(0);
assert.strictEqual(initialBalance.confirmed, 100 * 2000 * 1e6);

await wdb.rollback(0);
await forValue(wdb, 'height', 0);
const midBalance = await wallet.getBalance(0);
assert.strictEqual(midBalance.confirmed, 0);

await wdb.rescan(0);
await forValue(wdb, 'height', node.chain.height);
const finalBalance = await wallet.getBalance(0);
assert.deepStrictEqual(initialBalance, finalBalance);
});

it('should rescan after rollback on block connect', async () => {
// Rollback the wallet
await wdb.rollback(0);
await forValue(wdb, 'height', 0);
const midBalance = await wallet.getBalance(0);
assert.strictEqual(midBalance.confirmed, 0);

// Wallet state is way behind chain state
assert.strictEqual(node.chain.height, 100);
assert.strictEqual(wdb.state.height, 0);

// Adding a new block to the chain should trigger a wallet rescan
// if the wallet state is behind the chain height.
await mineBlocks(1, address);

await forValue(wdb, 'height', node.chain.height);
const finalBalance = await wallet.getBalance(0);
assert.strictEqual(finalBalance.confirmed, 101 * 2000 * 1e6);
});
});
3 changes: 2 additions & 1 deletion test/wallet-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2619,7 +2619,8 @@ describe('Wallet', function() {
async function mineBlock(tip) {
const job = await miner.createJob(tip);
const block = await job.mineAsync();
return chain.add(block);
await chain.add(block);
await forValue(wdb, 'height', chain.height);
}

it('should not stack in-memory block queue (oom)', async () => {
Expand Down

0 comments on commit 62bbbb2

Please sign in to comment.