This commit is contained in:
Chris Kleeschulte 2017-04-28 10:13:36 -04:00
parent ebe37222f5
commit 06b1f02e6a

View File

@ -240,84 +240,74 @@ WalletService.prototype.getAddressString = function(script, output) {
};
WalletService.prototype.concurrentBlockHandler = function(block, connectBlock, callback) {
var opts = {
block: block,
connectBlock: connectBlock,
fnEncodingKey: this._encoding.encodeWalletTransactionKey
};
this._blockHandler(opts, callback);
};
WalletService.prototype._blockHandler = function(opts, callback) {
var self = this;
var txs = block.transactions;
var action = 'put';
if (!connectBlock) {
action = 'del';
}
var txs = opts.block.transactions;
var operations = [];
for(var i = 0; i < txs.length; i++) {
var tx = txs[i];
var inputs = tx.inputs;
var outputs = tx.outputs;
for (var outputIndex = 0; outputIndex < outputs.length; outputIndex++) {
var output = outputs[outputIndex];
var script = output.script;
if(!script) {
log.debug('Invalid script');
continue;
}
var address = self.node.services.address.getAddressString(script);
if(!address || !self._addressMap[address]) {
continue;
}
var walletIds = self._addressMap[address];
for(var j = 0; j < walletIds.length; j++) {
var walletId = walletIds[j];
operations.push({
type: action,
key: self._encoding.encodeWalletTransactionKey(walletId, block.__height, tx.id)
});
}
txs.forEach(function(tx) {
var ops = self._processTransaction(opts, tx);
if (ops) {
operations.concat(ops);
}
});
if(tx.isCoinbase()) {
continue;
}
//function has a callback, but is synchronuous nonetheless
callback(null, operations);
};
//TODO deal with P2PK
for(var inputIndex = 0; inputIndex < inputs.length; inputIndex++) {
var input = inputs[inputIndex];
WalletService.prototype._processTransaction = function(opts, tx) {
var self = this;
if(!input.script) {
log.debug('Invalid script');
continue;
}
var inputAddress = self.getAddressString(input.script);
if(!inputAddress || !self._addressMap[inputAddress]) {
continue;
}
var inputWalletIds = self._addressMap[inputAddress];
for(var k = 0; k < inputWalletIds.length; k++) {
var inputWalletId = inputWalletIds[inputIndex];
operations.push({
type: action,
key: self._encoding.encodeWalletTransactionKey(inputWalletId, block.__height, tx.id)
});
}
}
if(tx.isCoinbase()) {
return;
}
setImmediate(function() {
callback(null, operations);
var ioData = tx.inputs.concat(tx.outputs);
var operations = [];
ioData.forEach(function(io) {
var ops = self._processIO(opts, tx, io);
if (ops) {
operations.concat(ops);
}
});
return operations;
};
WalletService.prototype._processIO = function(opts, tx, io) {
if(!io.script) {
log.debug('Invalid script');
return;
}
var address = this.getAddressString(io.script);
if(!address || !this._addressMap[address]) {
return;
}
var walletIds = this._addressMap[address];
var action = opts.connectBlock ? 'put' : 'del';
return walletIds.map(function(walletId) {
return {
type: action,
key: opts.fnEncoding.call(walletId, opts.block.__height, tx.id)
};
});
};
@ -418,8 +408,6 @@ WalletService.prototype._endpointGetBalance= function() {
var queryMempool = req.query.queryMempool !== false;
var byAddress = req.query.byAddress;
var height = null;
var options = {
queryMempool: queryMempool,
byAddress: byAddress
@ -537,13 +525,16 @@ WalletService.prototype._endpointGetWalletIds = function() {
lt: end
});
var walletIds = [];
var streamErr;
stream.on('error', function(err) {
var streamErr = err;
streamErr = err;
});
stream.on('data', function(data) {
walletIds.push(self._encoding.decodeWalletAddressesKey(data));
});
stream.on('end', function() {
if(streamErr) {
return utils.sendError(streamErr, res);
@ -618,7 +609,7 @@ WalletService.prototype._endpointGetTransactions = function() {
return utils.sendError(err, res);
}
var rs = new Readable;
var rs = new Readable();
transactions.forEach(function(transaction) {
rs.push(utils.toJSONL(self._formatTransaction(transaction)));
@ -675,7 +666,6 @@ WalletService.prototype._endpointPutAddresses = function() {
}
var addAddresses = _.without(newAddresses, oldAddresses);
var amountAdded = addAddresses.length;
var jobId = utils.generateJobId();
self._importAddresses(walletId, addAddresses, jobId, self._jobCompletionCallback.bind(self));
@ -783,10 +773,12 @@ WalletService.prototype._getTransactions = function(walletId, options, callback)
if (!self._cache.peek(key)) {
var start = self._encoding.encodeWalletTransactionKey(walletId, opts.start);
var end = self._encoding.encodeWalletTransactionKey(walletId, opts.end);
var end = Buffer.concat([
self._encoding.encodeWalletTransactionKey(walletId, opts.end)
.slice(0, -32), new Buffer('ff', 'hex') ]);
var stream = self.store.createKeyStream({
gte: start,
lt: end
lte: end
});
var streamErr;
@ -818,7 +810,10 @@ WalletService.prototype._removeWallet = function(walletId, callback) {
var keys = [];
var start = self._encoding.subKeyMap[prefix].fn.call(self._encoding, walletId);
var end = new Buffer.concat([self._encoding.subKeyMap[prefix].fn.call(self._encoding, walletId), new Buffer('ff', 'hex')]);
var end = new Buffer.concat([
self._encoding.subKeyMap[prefix]
.fn.call(self._encoding, walletId),
new Buffer('ff', 'hex')]);
var stream = self.store.createKeyStream({
gte: start,
@ -1137,7 +1132,10 @@ WalletService.prototype._processStartEndOptions = function(req, callback) {
}
done(null, heights.push(response.result.height));
});
}, function(err, obj) {
}, function(err) {
if(err) {
return callback(err);
}
if (heights.length > 1) {
callback(null, heights);
}
@ -1160,7 +1158,7 @@ WalletService.prototype._endpointJobs = function() {
return function(req, res) {
var count = 0;
self._jobs.rforEach(function(value, key) {
self._jobs.rforEach(function(value) {
if ((value.state === 'complete' || value.state === 'error') && value.reported) {
count++;
}