Skip to content

Commit

Permalink
Merge 9ac2fcb into 1a086e4
Browse files Browse the repository at this point in the history
  • Loading branch information
pinheadmz committed Jan 19, 2021
2 parents 1a086e4 + 9ac2fcb commit f3d8ec5
Show file tree
Hide file tree
Showing 5 changed files with 161 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
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-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
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 f3d8ec5

Please sign in to comment.