wip
This commit is contained in:
parent
0263aa14ae
commit
f47a3e3a3b
@ -348,7 +348,7 @@ AddressService.prototype._startSubscriptions = function() {
|
||||
this._bus.subscribe('block/block');
|
||||
};
|
||||
|
||||
AddressService.prototype._onReorg = function(oldBlockList, newBlockList, commonAncestor) {
|
||||
AddressService.prototype._onReorg = function(oldBlockList, commonAncestor) {
|
||||
|
||||
// if the common ancestor block height is greater than our own, then nothing to do for the reorg
|
||||
if (this._tip.height <= commonAncestor.height) {
|
||||
@ -407,8 +407,6 @@ AddressService.prototype._onReorg = function(oldBlockList, newBlockList, commonA
|
||||
|
||||
this._db.batch(removalOps);
|
||||
|
||||
//call onBlock for each of the new blocks
|
||||
newBlockList.forEach(this._onBlock.bind(this));
|
||||
};
|
||||
|
||||
AddressService.prototype._onBlock = function(block) {
|
||||
@ -416,7 +414,7 @@ AddressService.prototype._onBlock = function(block) {
|
||||
|
||||
var operations = [];
|
||||
|
||||
block.transactions.forEach(function(tx) {
|
||||
block.txs.forEach(function(tx) {
|
||||
operations.concat(self._processTransaction(tx, { block: block }));
|
||||
});
|
||||
|
||||
@ -427,26 +425,27 @@ AddressService.prototype._onBlock = function(block) {
|
||||
}
|
||||
};
|
||||
|
||||
AddressService.prototype._processInput = function(opts, input) {
|
||||
AddressService.prototype._processInput = function(tx, input, opts) {
|
||||
|
||||
var address = this._getAddress(opts, input);
|
||||
var address = utils.getAddressString({ item: input });
|
||||
|
||||
if (!address) {
|
||||
if(!address) {
|
||||
return;
|
||||
}
|
||||
|
||||
var txid = tx.txid();
|
||||
// address index
|
||||
var addressKey = this._encoding.encodeAddressIndexKey(address, opts.block.height, opts.tx.id);
|
||||
var addressKey = this._encoding.encodeAddressIndexKey(address, opts.block.height, txid);
|
||||
|
||||
var operations = [{
|
||||
type: opts.action,
|
||||
type: 'put',
|
||||
key: addressKey
|
||||
}];
|
||||
|
||||
// prev utxo
|
||||
var rec = {
|
||||
type: opts.action,
|
||||
key: this._encoding.encodeUtxoIndexKey(address, input.prevTxId.toString('hex'), input.outputIndex)
|
||||
type: 'del',
|
||||
key: this._encoding.encodeUtxoIndexKey(address, input.prevout.txid(), input.prevout.index)
|
||||
};
|
||||
|
||||
// In the event where we are reorg'ing,
|
||||
@ -461,24 +460,24 @@ AddressService.prototype._processInput = function(opts, input) {
|
||||
|
||||
AddressService.prototype._processOutput = function(tx, output, index, opts) {
|
||||
|
||||
var address = utils.getAddressString({ tx: tx, item: output, network: this._network });
|
||||
var address = utils.getAddressString({ item: output });
|
||||
|
||||
if(!address) {
|
||||
return;
|
||||
}
|
||||
|
||||
var txid = tx.id;
|
||||
var txid = tx.txid();
|
||||
var addressKey = this._encoding.encodeAddressIndexKey(address, opts.block.height, txid);
|
||||
var utxoKey = this._encoding.encodeUtxoIndexKey(address, txid, index);
|
||||
var utxoValue = this._encoding.encodeUtxoIndexValue(opts.block.height, output.satoshis, output._scriptBuffer);
|
||||
var utxoValue = this._encoding.encodeUtxoIndexValue(opts.block.height, Unit.fromBTC(output.value).toSatoshis(), output.script.toRaw());
|
||||
|
||||
var operations = [{
|
||||
type: opts.action,
|
||||
type: 'put',
|
||||
key: addressKey
|
||||
}];
|
||||
|
||||
operations.push({
|
||||
type: opts.action,
|
||||
type: 'put',
|
||||
key: utxoKey,
|
||||
value: utxoValue
|
||||
});
|
||||
@ -489,15 +488,7 @@ AddressService.prototype._processTransaction = function(opts, tx) {
|
||||
|
||||
var self = this;
|
||||
|
||||
var action = 'put';
|
||||
var reverseAction = 'del';
|
||||
|
||||
if (!opts.connect) {
|
||||
action = 'del';
|
||||
reverseAction = 'put';
|
||||
}
|
||||
|
||||
var _opts = { block: opts.block, action: action, reverseAction: reverseAction };
|
||||
var _opts = { block: opts.block };
|
||||
|
||||
var outputOperations = tx.outputs.map(function(output, index) {
|
||||
return self._processOutput(tx, output, index, _opts);
|
||||
|
||||
@ -121,10 +121,6 @@ TransactionService.prototype.stop = function(callback) {
|
||||
setImmediate(callback);
|
||||
};
|
||||
|
||||
TransactionService.prototype.syncPercentage = function(callback) {
|
||||
|
||||
};
|
||||
|
||||
// --- start private prototype functions
|
||||
TransactionService.prototype._cacheOutputValues = function(tx) {
|
||||
|
||||
@ -137,7 +133,7 @@ TransactionService.prototype._cacheOutputValues = function(tx) {
|
||||
};
|
||||
|
||||
TransactionService.prototype._getBlockTimestamp = function(hash) {
|
||||
return this._timestamp.getTimestamp(hash);
|
||||
return this._timestamp.getTimestampSync(hash);
|
||||
};
|
||||
|
||||
TransactionService.prototype._getInputValues = function(tx) {
|
||||
@ -212,7 +208,7 @@ TransactionService.prototype._processTransaction = function(tx, opts) {
|
||||
// input values
|
||||
tx.__inputValues = this._getInputValues(tx); //if there are any nulls here, this is a cache miss
|
||||
//timestamp
|
||||
tx.__timestamp = this._getBlockTimestamp(opts.block);
|
||||
tx.__timestamp = this._getBlockTimestamp(opts.block.rhash());
|
||||
//height
|
||||
tx.__height = opts.block.height;
|
||||
|
||||
|
||||
21
lib/utils.js
21
lib/utils.js
@ -64,29 +64,16 @@ utils.reverseBufferToString = function(buf) {
|
||||
|
||||
utils.getAddressString = function(opts) {
|
||||
|
||||
if (!opts.item || !opts.item.script) {
|
||||
if (!opts.item) {
|
||||
return;
|
||||
}
|
||||
|
||||
// is coinbase and is input, no address
|
||||
if (opts.tx && opts.tx.isCoinbase() && opts.item.prevTxId) {
|
||||
return;
|
||||
}
|
||||
|
||||
var address = opts.item.script.toAddress(opts.network || 'livenet');
|
||||
|
||||
if(address) {
|
||||
// TODO: this does not handle P2PK correctly, it uses the address and not the
|
||||
// pubkey
|
||||
if (opts.address) {
|
||||
return address.toString();
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
var pubkey = opts.item.script.getPublicKey();
|
||||
if(pubkey) {
|
||||
return pubkey.toString('hex');
|
||||
}
|
||||
} catch(e) {}
|
||||
|
||||
};
|
||||
|
||||
utils.sendError = function(err, res) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user