Skip to content

Commit

Permalink
Merge pull request #412 from pinheadmz/wallet2
Browse files Browse the repository at this point in the history
wallet: port various fixes and improvements from bcoin
  • Loading branch information
boymanjor authored Apr 2, 2020
2 parents 24a9d23 + 3f65375 commit 9d21a04
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 110 deletions.
87 changes: 2 additions & 85 deletions lib/node/spvnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
'use strict';

const assert = require('bsert');
const {Lock} = require('bmutex');
const NameState = require('../covenants/namestate');
const Chain = require('../blockchain/chain');
const Pool = require('../net/pool');
Expand Down Expand Up @@ -113,10 +112,6 @@ class SPVNode extends Node {
noUnbound: this.config.bool('rs-no-unbound')
});

this.rescanJob = null;
this.scanLock = new Lock();
this.watchLock = new Lock();

this.init();
}

Expand All @@ -134,9 +129,6 @@ class SPVNode extends Node {
this.http.on('error', err => this.error(err));

this.pool.on('tx', (tx) => {
if (this.rescanJob)
return;

this.emit('tx', tx);
});

Expand All @@ -145,15 +137,6 @@ class SPVNode extends Node {
});

this.chain.on('connect', async (entry, block) => {
if (this.rescanJob) {
try {
await this.watchBlock(entry, block);
} catch (e) {
this.error(e);
}
return;
}

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

Expand Down Expand Up @@ -221,64 +204,11 @@ class SPVNode extends Node {
* Scan for any missed transactions.
* Note that this will replay the blockchain sync.
* @param {Number|Hash} start - Start block.
* @param {Bloom} filter
* @param {Function} iter - Iterator.
* @returns {Promise}
*/

async scan(start, filter, iter) {
const unlock = await this.scanLock.lock();
const height = this.chain.height;

try {
await this.chain.replay(start);

if (this.chain.height < height) {
// We need to somehow defer this.
// await this.connect();
// this.startSync();
// await this.watchUntil(height, iter);
}
} finally {
unlock();
}
}

/**
* Watch the blockchain until a certain height.
* @param {Number} height
* @param {Function} iter
* @returns {Promise}
*/

watchUntil(height, iter) {
return new Promise((resolve, reject) => {
this.rescanJob = new RescanJob(resolve, reject, height, iter);
});
}

/**
* Handled watched block.
* @param {ChainEntry} entry
* @param {MerkleBlock} block
* @returns {Promise}
*/

async watchBlock(entry, block) {
const unlock = await this.watchLock.lock();
try {
if (entry.height < this.rescanJob.height) {
await this.rescanJob.iter(entry, block.txs);
return;
}
this.rescanJob.resolve();
this.rescanJob = null;
} catch (e) {
this.rescanJob.reject(e);
this.rescanJob = null;
} finally {
unlock();
}
async scan(start) {
throw new Error('Not implemented.');
}

/**
Expand Down Expand Up @@ -428,19 +358,6 @@ class SPVNode extends Node {
}
}

/*
* Helpers
*/

class RescanJob {
constructor(resolve, reject, height, iter) {
this.resolve = resolve;
this.reject = reject;
this.height = height;
this.iter = iter;
}
}

/*
* Expose
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/wallet/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -1297,7 +1297,7 @@ class HTTP extends Server {
if (!this.channel(name) && !this.channel('w:*'))
return;

const json = details.getJSON(this.network, this.wdb.height);
const json = details.getJSON(this.network, this.wdb.liveHeight());

if (this.channel(name))
this.to(name, event, wallet.id, json);
Expand Down
1 change: 0 additions & 1 deletion lib/wallet/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class WalletNode extends Node {
memory: this.config.bool('memory'),
maxFiles: this.config.uint('max-files'),
cacheSize: this.config.mb('cache-size'),
checkpoints: this.config.bool('checkpoints'),
wipeNoReally: this.config.bool('wipe-no-really'),
spv: this.config.bool('spv')
});
Expand Down
11 changes: 7 additions & 4 deletions lib/wallet/nodeclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ class NodeClient extends AsyncEmitter {
*/

init() {
this.node.on('connect', (entry, block) => {
this.node.chain.on('connect', async (entry, block) => {
if (!this.opened)
return;

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

this.node.on('disconnect', (entry, block) => {
this.node.chain.on('disconnect', async (entry, block) => {
if (!this.opened)
return;

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

this.node.on('tx', (tx) => {
Expand Down Expand Up @@ -217,6 +217,9 @@ class NodeClient extends AsyncEmitter {
*/

async rescan(start) {
if (this.node.spv)
return this.node.chain.reset(start);

return this.node.chain.scan(start, this.filter, (entry, txs) => {
return this.emitAsync('block rescan', entry, txs);
});
Expand Down
2 changes: 0 additions & 2 deletions lib/wallet/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ class Plugin extends EventEmitter {
memory: this.config.bool('memory', node.memory),
maxFiles: this.config.uint('max-files'),
cacheSize: this.config.mb('cache-size'),
witness: this.config.bool('witness'),
checkpoints: this.config.bool('checkpoints'),
wipeNoReally: this.config.bool('wipe-no-really'),
spv: node.spv
});
Expand Down
49 changes: 32 additions & 17 deletions lib/wallet/walletdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class WalletDB extends EventEmitter {

this.primary = null;
this.state = new ChainState();
this.confirming = false;
this.height = 0;
this.wallets = new Map();
this.depth = 0;
Expand Down Expand Up @@ -1657,6 +1658,22 @@ class WalletDB extends EventEmitter {
this.height = state.height;
}

/**
* Will return the current height and will increment
* to the current height of a block currently being
* added to the wallet.
* @returns {Number}
*/

liveHeight() {
let height = this.height;

if (this.confirming)
height += 1;

return height;
}

/**
* Mark current state.
* @param {BlockMeta} block
Expand Down Expand Up @@ -2062,19 +2079,23 @@ class WalletDB extends EventEmitter {
return 0;
}

// Sync the state to the new tip.
await this.setTip(tip);

if (this.options.checkpoints && !this.state.marked) {
if (tip.height <= this.network.lastCheckpoint)
return 0;
}

let total = 0;

for (const tx of txs) {
if (await this._addTX(tx, tip))
total += 1;
try {
// We set the state as confirming so that
// anything that uses the current height can
// increment by one until the block is fully
// added and the height is updated.
this.confirming = true;
for (const tx of txs) {
if (await this._addTX(tx, tip))
total += 1;
}

// Sync the state to the new tip.
await this.setTip(tip);
} finally {
this.confirming = false;
}

if (total > 0) {
Expand Down Expand Up @@ -2299,7 +2320,6 @@ class WalletOptions {
this.compression = true;

this.spv = false;
this.checkpoints = false;
this.wipeNoReally = false;

if (options)
Expand Down Expand Up @@ -2373,11 +2393,6 @@ class WalletOptions {
this.spv = options.spv;
}

if (options.checkpoints != null) {
assert(typeof options.checkpoints === 'boolean');
this.checkpoints = options.checkpoints;
}

if (options.wipeNoReally != null) {
assert(typeof options.wipeNoReally === 'boolean');
this.wipeNoReally = options.wipeNoReally;
Expand Down
Loading

0 comments on commit 9d21a04

Please sign in to comment.