wip
This commit is contained in:
parent
ebe37222f5
commit
06b1f02e6a
@ -240,84 +240,74 @@ WalletService.prototype.getAddressString = function(script, output) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
WalletService.prototype.concurrentBlockHandler = function(block, connectBlock, callback) {
|
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 self = this;
|
||||||
|
|
||||||
var txs = block.transactions;
|
var txs = opts.block.transactions;
|
||||||
|
|
||||||
var action = 'put';
|
|
||||||
if (!connectBlock) {
|
|
||||||
action = 'del';
|
|
||||||
}
|
|
||||||
|
|
||||||
var operations = [];
|
var operations = [];
|
||||||
|
txs.forEach(function(tx) {
|
||||||
for(var i = 0; i < txs.length; i++) {
|
var ops = self._processTransaction(opts, tx);
|
||||||
|
if (ops) {
|
||||||
var tx = txs[i];
|
operations.concat(ops);
|
||||||
|
|
||||||
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)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
if(tx.isCoinbase()) {
|
//function has a callback, but is synchronuous nonetheless
|
||||||
continue;
|
callback(null, operations);
|
||||||
}
|
};
|
||||||
|
|
||||||
//TODO deal with P2PK
|
WalletService.prototype._processTransaction = function(opts, tx) {
|
||||||
for(var inputIndex = 0; inputIndex < inputs.length; inputIndex++) {
|
var self = this;
|
||||||
var input = inputs[inputIndex];
|
|
||||||
|
|
||||||
if(!input.script) {
|
if(tx.isCoinbase()) {
|
||||||
log.debug('Invalid script');
|
return;
|
||||||
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)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
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 queryMempool = req.query.queryMempool !== false;
|
||||||
var byAddress = req.query.byAddress;
|
var byAddress = req.query.byAddress;
|
||||||
|
|
||||||
var height = null;
|
|
||||||
|
|
||||||
var options = {
|
var options = {
|
||||||
queryMempool: queryMempool,
|
queryMempool: queryMempool,
|
||||||
byAddress: byAddress
|
byAddress: byAddress
|
||||||
@ -537,13 +525,16 @@ WalletService.prototype._endpointGetWalletIds = function() {
|
|||||||
lt: end
|
lt: end
|
||||||
});
|
});
|
||||||
var walletIds = [];
|
var walletIds = [];
|
||||||
|
|
||||||
var streamErr;
|
var streamErr;
|
||||||
stream.on('error', function(err) {
|
stream.on('error', function(err) {
|
||||||
var streamErr = err;
|
streamErr = err;
|
||||||
});
|
});
|
||||||
|
|
||||||
stream.on('data', function(data) {
|
stream.on('data', function(data) {
|
||||||
walletIds.push(self._encoding.decodeWalletAddressesKey(data));
|
walletIds.push(self._encoding.decodeWalletAddressesKey(data));
|
||||||
});
|
});
|
||||||
|
|
||||||
stream.on('end', function() {
|
stream.on('end', function() {
|
||||||
if(streamErr) {
|
if(streamErr) {
|
||||||
return utils.sendError(streamErr, res);
|
return utils.sendError(streamErr, res);
|
||||||
@ -618,7 +609,7 @@ WalletService.prototype._endpointGetTransactions = function() {
|
|||||||
return utils.sendError(err, res);
|
return utils.sendError(err, res);
|
||||||
}
|
}
|
||||||
|
|
||||||
var rs = new Readable;
|
var rs = new Readable();
|
||||||
|
|
||||||
transactions.forEach(function(transaction) {
|
transactions.forEach(function(transaction) {
|
||||||
rs.push(utils.toJSONL(self._formatTransaction(transaction)));
|
rs.push(utils.toJSONL(self._formatTransaction(transaction)));
|
||||||
@ -675,7 +666,6 @@ WalletService.prototype._endpointPutAddresses = function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var addAddresses = _.without(newAddresses, oldAddresses);
|
var addAddresses = _.without(newAddresses, oldAddresses);
|
||||||
var amountAdded = addAddresses.length;
|
|
||||||
|
|
||||||
var jobId = utils.generateJobId();
|
var jobId = utils.generateJobId();
|
||||||
self._importAddresses(walletId, addAddresses, jobId, self._jobCompletionCallback.bind(self));
|
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)) {
|
if (!self._cache.peek(key)) {
|
||||||
var start = self._encoding.encodeWalletTransactionKey(walletId, opts.start);
|
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({
|
var stream = self.store.createKeyStream({
|
||||||
gte: start,
|
gte: start,
|
||||||
lt: end
|
lte: end
|
||||||
});
|
});
|
||||||
|
|
||||||
var streamErr;
|
var streamErr;
|
||||||
@ -818,7 +810,10 @@ WalletService.prototype._removeWallet = function(walletId, callback) {
|
|||||||
var keys = [];
|
var keys = [];
|
||||||
|
|
||||||
var start = self._encoding.subKeyMap[prefix].fn.call(self._encoding, walletId);
|
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({
|
var stream = self.store.createKeyStream({
|
||||||
gte: start,
|
gte: start,
|
||||||
@ -1137,7 +1132,10 @@ WalletService.prototype._processStartEndOptions = function(req, callback) {
|
|||||||
}
|
}
|
||||||
done(null, heights.push(response.result.height));
|
done(null, heights.push(response.result.height));
|
||||||
});
|
});
|
||||||
}, function(err, obj) {
|
}, function(err) {
|
||||||
|
if(err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
if (heights.length > 1) {
|
if (heights.length > 1) {
|
||||||
callback(null, heights);
|
callback(null, heights);
|
||||||
}
|
}
|
||||||
@ -1160,7 +1158,7 @@ WalletService.prototype._endpointJobs = function() {
|
|||||||
return function(req, res) {
|
return function(req, res) {
|
||||||
|
|
||||||
var count = 0;
|
var count = 0;
|
||||||
self._jobs.rforEach(function(value, key) {
|
self._jobs.rforEach(function(value) {
|
||||||
if ((value.state === 'complete' || value.state === 'error') && value.reported) {
|
if ((value.state === 'complete' || value.state === 'error') && value.reported) {
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user