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