wallet events.
This commit is contained in:
parent
1a32e66468
commit
e3f023142f
@ -281,6 +281,8 @@ Fullnode.prototype._open = function open(callback) {
|
||||
Fullnode.prototype._close = function close(callback) {
|
||||
var self = this;
|
||||
|
||||
this.wallet = null;
|
||||
|
||||
utils.serial([
|
||||
function(next) {
|
||||
if (!self.http)
|
||||
|
||||
@ -248,6 +248,8 @@ SPVNode.prototype._open = function open(callback) {
|
||||
SPVNode.prototype._close = function close(callback) {
|
||||
var self = this;
|
||||
|
||||
this.wallet = null;
|
||||
|
||||
utils.parallel([
|
||||
function(next) {
|
||||
if (!self.http)
|
||||
|
||||
@ -543,7 +543,7 @@ TXDB.prototype._add = function add(tx, map, callback, force) {
|
||||
if (err)
|
||||
return callback(err);
|
||||
|
||||
self.walletdb.syncOutputs(tx, map, function(err) {
|
||||
self.walletdb.handleTX(tx, map, function(err) {
|
||||
if (err)
|
||||
return callback(err);
|
||||
|
||||
|
||||
@ -49,7 +49,6 @@ function WalletDB(options) {
|
||||
|
||||
this.options = options;
|
||||
this.network = bcoin.network.get(options.network);
|
||||
this.watchers = {};
|
||||
|
||||
this.db = bcoin.ldb({
|
||||
network: this.network,
|
||||
@ -66,6 +65,8 @@ function WalletDB(options) {
|
||||
useFilter: true
|
||||
});
|
||||
|
||||
this.watchers = {};
|
||||
|
||||
this._init();
|
||||
}
|
||||
|
||||
@ -83,75 +84,35 @@ WalletDB.prototype._init = function _init() {
|
||||
self.emit('error', err);
|
||||
});
|
||||
|
||||
this.tx.on('tx', function(tx, map) {
|
||||
function handleEvent(event, tx, map) {
|
||||
var i, path;
|
||||
|
||||
self.emit('tx', tx, map);
|
||||
self.emit(event, tx, map);
|
||||
|
||||
for (i = 0; i < map.accounts.length; i++) {
|
||||
path = map.accounts[i];
|
||||
self.fire(path.id, 'tx', tx, path.name);
|
||||
self.fire(path.id, event, tx, path.name);
|
||||
}
|
||||
}
|
||||
|
||||
this.tx.on('tx', function(tx, map) {
|
||||
handleEvent('tx', tx, map);
|
||||
});
|
||||
|
||||
this.tx.on('conflict', function(tx, map) {
|
||||
var i, path;
|
||||
|
||||
self.emit('conflict', tx, map);
|
||||
|
||||
for (i = 0; i < map.accounts.length; i++) {
|
||||
path = map.accounts[i];
|
||||
self.fire(path.id, 'conflict', tx, path.name);
|
||||
}
|
||||
handleEvent('conflict', tx, map);
|
||||
});
|
||||
|
||||
this.tx.on('confirmed', function(tx, map) {
|
||||
var i, path;
|
||||
|
||||
self.emit('confirmed', tx, map);
|
||||
|
||||
for (i = 0; i < map.accounts.length; i++) {
|
||||
path = map.accounts[i];
|
||||
self.fire(path.id, 'confirmed', tx, path.name);
|
||||
}
|
||||
handleEvent('confirmed', tx, map);
|
||||
});
|
||||
|
||||
this.tx.on('unconfirmed', function(tx, map) {
|
||||
var i, path;
|
||||
|
||||
self.emit('unconfirmed', tx, map);
|
||||
|
||||
for (i = 0; i < map.accounts.length; i++) {
|
||||
path = map.accounts[i];
|
||||
self.fire(path.id, 'unconfirmed', tx, path.name);
|
||||
}
|
||||
handleEvent('unconfirmed', tx, map);
|
||||
});
|
||||
|
||||
this.tx.on('updated', function(tx, map) {
|
||||
var i, path, keys, id;
|
||||
|
||||
self.emit('updated', tx, map);
|
||||
|
||||
for (i = 0; i < map.accounts.length; i++) {
|
||||
path = map.accounts[i];
|
||||
self.fire(path.id, 'updated', tx, path.name);
|
||||
}
|
||||
|
||||
self.updateBalances(tx, map, function(err, balances) {
|
||||
if (err) {
|
||||
self.emit('error', err);
|
||||
return;
|
||||
}
|
||||
|
||||
keys = Object.keys(balances);
|
||||
|
||||
for (i = 0; i < keys.length; i++) {
|
||||
id = keys[i];
|
||||
self.fire(id, 'balance', balances[id]);
|
||||
}
|
||||
|
||||
self.emit('balances', balances, map);
|
||||
});
|
||||
handleEvent('updated', tx, map);
|
||||
});
|
||||
};
|
||||
|
||||
@ -206,7 +167,7 @@ WalletDB.prototype._close = function close(callback) {
|
||||
WalletDB.prototype.updateBalances = function updateBalances(tx, map, callback) {
|
||||
var self = this;
|
||||
var balances = {};
|
||||
var id;
|
||||
var i, id, keys;
|
||||
|
||||
utils.forEachSerial(map.outputs, function(output, next) {
|
||||
id = output.id;
|
||||
@ -231,6 +192,15 @@ WalletDB.prototype.updateBalances = function updateBalances(tx, map, callback) {
|
||||
if (err)
|
||||
return callback(err);
|
||||
|
||||
keys = Object.keys(balances);
|
||||
|
||||
for (i = 0; i < keys.length; i++) {
|
||||
id = keys[i];
|
||||
self.fire(id, 'balance', balances[id]);
|
||||
}
|
||||
|
||||
self.emit('balances', balances, map);
|
||||
|
||||
return callback(null, balances);
|
||||
});
|
||||
};
|
||||
@ -260,6 +230,24 @@ WalletDB.prototype.syncOutputs = function syncOutputs(tx, map, callback) {
|
||||
}, callback);
|
||||
};
|
||||
|
||||
/**
|
||||
* Derive new addresses and emit balance.
|
||||
* @private
|
||||
* @param {TX} tx
|
||||
* @param {WalletMap} map
|
||||
* @param {Function} callback
|
||||
*/
|
||||
|
||||
WalletDB.prototype.handleTX = function handleTX(tx, map, callback) {
|
||||
var self = this;
|
||||
this.syncOutputs(tx, map, function(err) {
|
||||
if (err)
|
||||
return callback(err);
|
||||
|
||||
self.updateBalances(tx, map, callback);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Dump database (for debugging).
|
||||
* @param {Function} callback - Returns [Error, Object].
|
||||
|
||||
@ -433,10 +433,9 @@ describe('Wallet', function() {
|
||||
var t3 = bcoin.mtx().addOutput(w2, 15000);
|
||||
w1.fill(t3, { rate: 10000 }, function(err) {
|
||||
assert(err);
|
||||
setTimeout(function() {
|
||||
assert(balance.total === 5460);
|
||||
cb();
|
||||
}, 100);
|
||||
assert(balance);
|
||||
assert(balance.total === 5460);
|
||||
cb();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user