wip
This commit is contained in:
parent
b5d67135ae
commit
7afaa59862
103
lib/services/address/encoding.js
Normal file
103
lib/services/address/encoding.js
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
var bitcore = require('bitcore-lib');
|
||||||
|
var BufferReader = bitcore.encoding.BufferReader;
|
||||||
|
|
||||||
|
function Encoding(servicePrefix) {
|
||||||
|
this.servicePrefix = servicePrefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
Encoding.prototype.encodeAddressIndexKey = function(address, height, txid) {
|
||||||
|
var prefix = new Buffer('00', 'hex');
|
||||||
|
var buffers = [this.servicePrefix, prefix];
|
||||||
|
|
||||||
|
var addressSizeBuffer = new Buffer(1);
|
||||||
|
addressSizeBuffer.writeUInt8(address.length);
|
||||||
|
var addressBuffer = new Buffer(address, 'utf8');
|
||||||
|
|
||||||
|
buffers.push(addressSizeBuffer);
|
||||||
|
buffers.push(addressBuffer);
|
||||||
|
|
||||||
|
var heightBuffer = new Buffer(4);
|
||||||
|
heightBuffer.writeUInt32BE(height || 0);
|
||||||
|
buffers.push(heightBuffer);
|
||||||
|
|
||||||
|
var txidBuffer = new Buffer(txid || Array(65).join('0'), 'hex');
|
||||||
|
buffers.push(txidBuffer);
|
||||||
|
|
||||||
|
return Buffer.concat(buffers);
|
||||||
|
};
|
||||||
|
|
||||||
|
Encoding.prototype.decodeAddressIndexKey = function(buffer) {
|
||||||
|
var reader = new BufferReader(buffer);
|
||||||
|
reader.read(3);
|
||||||
|
|
||||||
|
var addressSize = reader.readUInt8();
|
||||||
|
var address = reader.read(addressSize).toString('utf8');
|
||||||
|
var height = reader.readUInt32BE();
|
||||||
|
var txid = reader.read(32).toString('hex');
|
||||||
|
return {
|
||||||
|
address: address,
|
||||||
|
height: height,
|
||||||
|
txid: txid,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
Encoding.prototype.encodeUtxoIndexKey = function(address, txid, outputIndex) {
|
||||||
|
var prefix = new Buffer('01', 'hex');
|
||||||
|
var buffers = [this.servicePrefix, prefix];
|
||||||
|
|
||||||
|
var addressSizeBuffer = new Buffer(1);
|
||||||
|
addressSizeBuffer.writeUInt8(address.length);
|
||||||
|
var addressBuffer = new Buffer(address, 'utf8');
|
||||||
|
|
||||||
|
buffers.push(addressSizeBuffer);
|
||||||
|
buffers.push(addressBuffer);
|
||||||
|
|
||||||
|
var txidBuffer = new Buffer(txid || new Array(65).join('0'), 'hex');
|
||||||
|
buffers.push(txidBuffer);
|
||||||
|
|
||||||
|
var outputIndexBuffer = new Buffer(4);
|
||||||
|
outputIndexBuffer.writeUInt32BE(outputIndex || 0);
|
||||||
|
buffers.push(outputIndexBuffer);
|
||||||
|
|
||||||
|
return Buffer.concat(buffers);
|
||||||
|
};
|
||||||
|
|
||||||
|
Encoding.prototype.decodeUtxoIndexKey = function(buffer) {
|
||||||
|
var reader = new BufferReader(buffer);
|
||||||
|
reader.read(3);
|
||||||
|
|
||||||
|
var addressSize = reader.readUInt8();
|
||||||
|
var address = reader.read(addressSize).toString('utf8');
|
||||||
|
var txid = reader.read(32).toString('hex');
|
||||||
|
var outputIndex = reader.readUInt32BE(4);
|
||||||
|
|
||||||
|
return {
|
||||||
|
address: address,
|
||||||
|
txid: txid,
|
||||||
|
outputIndex: outputIndex
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
Encoding.prototype.encodeUtxoIndexValue = function(height, satoshis, scriptBuffer) {
|
||||||
|
var heightBuffer = new Buffer(4);
|
||||||
|
heightBuffer.writeUInt32BE(height);
|
||||||
|
var satoshisBuffer = new Buffer(8);
|
||||||
|
satoshisBuffer.writeDoubleBE(satoshis);
|
||||||
|
return Buffer.concat([heightBuffer, satoshisBuffer, scriptBuffer]);
|
||||||
|
};
|
||||||
|
|
||||||
|
Encoding.prototype.decodeUtxoIndexValue = function(buffer) {
|
||||||
|
var height = buffer.readUInt32BE();
|
||||||
|
var satoshis = buffer.readDoubleBE(4);
|
||||||
|
var scriptBuffer = buffer.slice(12);
|
||||||
|
return {
|
||||||
|
height: height,
|
||||||
|
satoshis: satoshis,
|
||||||
|
script: scriptBuffer
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = Encoding;
|
||||||
|
|
||||||
1379
lib/services/address/index.js
Normal file
1379
lib/services/address/index.js
Normal file
File diff suppressed because it is too large
Load Diff
55
lib/services/transaction/encoding.js
Normal file
55
lib/services/transaction/encoding.js
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
var bitcore = require('bitcore-lib');
|
||||||
|
|
||||||
|
function Encoding(servicePrefix) {
|
||||||
|
//if you add any more encoding keys, be sure to add a subkey
|
||||||
|
this.servicePrefix = servicePrefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
Encoding.prototype.encodeTransactionKey = function(txid) {
|
||||||
|
return Buffer.concat([this.servicePrefix, new Buffer(txid, 'hex')]);
|
||||||
|
};
|
||||||
|
|
||||||
|
Encoding.prototype.decodeTransactionKey = function(buffer) {
|
||||||
|
return buffer.slice(2).toString('hex');
|
||||||
|
};
|
||||||
|
|
||||||
|
Encoding.prototype.encodeTransactionValue = function(transaction) {
|
||||||
|
var heightBuffer = new Buffer(4);
|
||||||
|
heightBuffer.writeUInt32BE(transaction.__height);
|
||||||
|
|
||||||
|
var timestampBuffer = new Buffer(8);
|
||||||
|
timestampBuffer.writeDoubleBE(transaction.__timestamp);
|
||||||
|
|
||||||
|
var inputValues = transaction.__inputValues;
|
||||||
|
var inputValuesBuffer = new Buffer(8 * inputValues.length);
|
||||||
|
for(var i = 0; i < inputValues.length; i++) {
|
||||||
|
inputValuesBuffer.writeDoubleBE(inputValues[i], i * 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
var inputValuesLengthBuffer = new Buffer(2);
|
||||||
|
inputValuesLengthBuffer.writeUInt16BE(inputValues.length);
|
||||||
|
|
||||||
|
return new Buffer.concat([heightBuffer, timestampBuffer,
|
||||||
|
inputValuesLengthBuffer, inputValuesBuffer, transaction.toBuffer()]);
|
||||||
|
};
|
||||||
|
|
||||||
|
Encoding.prototype.decodeTransactionValue = function(buffer) {
|
||||||
|
var height = buffer.readUInt32BE();
|
||||||
|
var timestamp = buffer.readDoubleBE(4);
|
||||||
|
|
||||||
|
var inputValuesLength = buffer.readUInt16BE(12);
|
||||||
|
var inputValues = [];
|
||||||
|
for(var i = 0; i < inputValuesLength; i++) {
|
||||||
|
inputValues.push(buffer.readDoubleBE(i * 8 + 14));
|
||||||
|
}
|
||||||
|
var transaction = new bitcore.Transaction(buffer.slice(inputValues.length * 8 + 14));
|
||||||
|
transaction.__height = height;
|
||||||
|
transaction.__inputValues = inputValues;
|
||||||
|
transaction.__timestamp = timestamp;
|
||||||
|
return transaction;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = Encoding;
|
||||||
|
|
||||||
198
lib/services/transaction/index.js
Normal file
198
lib/services/transaction/index.js
Normal file
@ -0,0 +1,198 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
var assert = require('assert');
|
||||||
|
var async = require('async');
|
||||||
|
var BaseService = require('../../service');
|
||||||
|
var inherits = require('util').inherits;
|
||||||
|
var Encoding = require('./encoding');
|
||||||
|
var levelup = require('levelup');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Transaction Service builds upon the Database Service and the Bitcoin Service to add additional
|
||||||
|
* functionality for getting information by bitcoin transaction hash/id. This includes the current
|
||||||
|
* bitcoin memory pool as validated by a trusted bitcoind instance.
|
||||||
|
* @param {Object} options
|
||||||
|
* @param {Node} options.node - An instance of the node
|
||||||
|
* @param {String} options.name - An optional name of the service
|
||||||
|
*/
|
||||||
|
function TransactionService(options) {
|
||||||
|
BaseService.call(this, options);
|
||||||
|
this.concurrency = options.concurrency || 20;
|
||||||
|
this.currentTransactions = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
inherits(TransactionService, BaseService);
|
||||||
|
|
||||||
|
TransactionService.dependencies = [
|
||||||
|
'db',
|
||||||
|
'timestamp',
|
||||||
|
'mempool'
|
||||||
|
];
|
||||||
|
|
||||||
|
TransactionService.prototype.start = function(callback) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
self.db = this.node.services.db;
|
||||||
|
|
||||||
|
self.node.services.db.getPrefix(self.name, function(err, prefix) {
|
||||||
|
if(err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
self.prefix = prefix;
|
||||||
|
self.encoding = new Encoding(self.prefix);
|
||||||
|
callback();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
TransactionService.prototype.stop = function(callback) {
|
||||||
|
setImmediate(callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
TransactionService.prototype.blockHandler = function(block, connectBlock, callback) {
|
||||||
|
var self = this;
|
||||||
|
var action = 'put';
|
||||||
|
var reverseAction = 'del';
|
||||||
|
if (!connectBlock) {
|
||||||
|
action = 'del';
|
||||||
|
reverseAction = 'put';
|
||||||
|
}
|
||||||
|
|
||||||
|
var operations = [];
|
||||||
|
|
||||||
|
this.currentTransactions = {};
|
||||||
|
|
||||||
|
async.series([
|
||||||
|
function(next) {
|
||||||
|
self.node.services.timestamp.getTimestamp(block.hash, function(err, timestamp) {
|
||||||
|
if(err) {
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
block.__timestamp = timestamp;
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}, function(next) {
|
||||||
|
async.eachSeries(block.transactions, function(tx, next) {
|
||||||
|
tx.__timestamp = block.__timestamp;
|
||||||
|
tx.__height = block.__height;
|
||||||
|
|
||||||
|
self._getInputValues(tx, function(err, inputValues) {
|
||||||
|
if(err) {
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
tx.__inputValues = inputValues;
|
||||||
|
self.currentTransactions[tx.id] = tx;
|
||||||
|
|
||||||
|
operations.push({
|
||||||
|
type: action,
|
||||||
|
key: self.encoding.encodeTransactionKey(tx.id),
|
||||||
|
value: self.encoding.encodeTransactionValue(tx)
|
||||||
|
});
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}, function(err) {
|
||||||
|
if(err) {
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}], function(err) {
|
||||||
|
if(err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
callback(null, operations);
|
||||||
|
});
|
||||||
|
|
||||||
|
};
|
||||||
|
TransactionService.prototype._getMissingInputValues = function(tx, callback) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
if (tx.isCoinbase()) {
|
||||||
|
return callback(null, []);
|
||||||
|
}
|
||||||
|
|
||||||
|
async.eachOf(tx.inputs, function(input, index, next) {
|
||||||
|
if (tx.__inputValues[index]) {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
self.getTransaction(input.prevTxId.toString('hex'), {}, function(err, prevTx) {
|
||||||
|
if(err) {
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
if (!prevTx) {
|
||||||
|
return next(new Error('previous Tx missing.'));
|
||||||
|
}
|
||||||
|
if (!prevTx.outputs[input.outputIndex]) {
|
||||||
|
return next(new Error('Input did not have utxo.'));
|
||||||
|
}
|
||||||
|
var satoshis = prevTx.outputs[input.outputIndex].satoshis;
|
||||||
|
tx.__inputValues[index] = satoshis;
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}, callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
TransactionService.prototype._getInputValues = function(tx, callback) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
if (tx.isCoinbase()) {
|
||||||
|
return callback(null, []);
|
||||||
|
}
|
||||||
|
|
||||||
|
async.mapLimit(tx.inputs, this.concurrency, function(input, next) {
|
||||||
|
self.getTransaction(input.prevTxId.toString('hex'), {}, function(err, prevTx) {
|
||||||
|
if(err) {
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
if (!prevTx.outputs[input.outputIndex]) {
|
||||||
|
return next(new Error('Input did not have utxo: ' + prevTx.id + ' for tx: ' + tx.id));
|
||||||
|
}
|
||||||
|
var satoshis = prevTx.outputs[input.outputIndex].satoshis;
|
||||||
|
next(null, satoshis);
|
||||||
|
});
|
||||||
|
}, callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
TransactionService.prototype.getTransaction = function(txid, options, callback) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
assert(txid.length === 64, 'Transaction, Txid: ' + txid + ' with length: ' + txid.length + ' does not resemble a txid.');
|
||||||
|
|
||||||
|
if(self.currentTransactions[txid]) {
|
||||||
|
return setImmediate(function() {
|
||||||
|
callback(null, self.currentTransactions[txid]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
var key = self.encoding.encodeTransactionKey(txid);
|
||||||
|
|
||||||
|
async.waterfall([
|
||||||
|
function(next) {
|
||||||
|
self.node.services.db.get(key, function(err, buffer) {
|
||||||
|
if (err instanceof levelup.errors.NotFoundError) {
|
||||||
|
return next(null, false);
|
||||||
|
} else if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
var tx = self.encoding.decodeTransactionValue(buffer);
|
||||||
|
next(null, tx);
|
||||||
|
});
|
||||||
|
}, function(tx, next) {
|
||||||
|
if (tx) {
|
||||||
|
return next(null, tx);
|
||||||
|
}
|
||||||
|
if (!options || !options.queryMempool) {
|
||||||
|
return next(new Error('Transaction: ' + txid + ' not found in index'));
|
||||||
|
}
|
||||||
|
self.node.services.mempool.getTransaction(txid, function(err, tx) {
|
||||||
|
if (err instanceof levelup.errors.NotFoundError) {
|
||||||
|
return callback(new Error('Transaction: ' + txid + ' not found in index or mempool'));
|
||||||
|
} else if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
self._getMissingInputValues(tx, next);
|
||||||
|
});
|
||||||
|
}], callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = TransactionService;
|
||||||
Loading…
Reference in New Issue
Block a user