Fixes bugs on address service

This commit is contained in:
eordano 2015-03-19 09:10:26 -03:00
parent e70dbb4ce3
commit 492a325c18
5 changed files with 44 additions and 20 deletions

View File

@ -5,8 +5,8 @@ BitcoreNode:
port: 8333
LevelUp: ./db
RPC:
username: username
password: password
user: username
pass: password
protocol: http
host: 127.0.0.1
port: 8332

View File

@ -2,11 +2,12 @@
var Promise = require('bluebird');
var bitcore = require('bitcore');
var TransactionService = require('./transaction');
var _ = bitcore.deps._;
var NULLTXHASH = bitcore.util.buffer.emptyBuffer(32).toString('hex');
var LASTTXHASH = bitcore.util.buffer.fill(bitcore.util.buffer.emptyBuffer(32), -1).toString('hex');
var MAXOUTPUT = 1 << 31;
var MAXOUTPUT = 4294967295;
function AddressService(opts) {
opts = _.extend({}, opts);
@ -38,7 +39,7 @@ AddressService.prototype.getSummary = function(address, confirmations) {
}).then(function(spent) {
return self.buildAddressSummary(address, tip, allOutputs, spent);
return self.buildAddressSummary(address, tip, allOutputs, spent, confirmations);
});
};
@ -48,7 +49,7 @@ AddressService.prototype.getAllOutputs = function(address) {
var self = this;
return new Promise(function(resolve, reject) {
self.db.createReadStream({
self.database.createReadStream({
gte: TransactionService.Index.getOutputsForAddress(address, NULLTXHASH, 0),
lte: TransactionService.Index.getOutputsForAddress(address, LASTTXHASH, MAXOUTPUT)
}).on('data', function(element) {
@ -66,20 +67,20 @@ AddressService.prototype.getSpent = function(address) {
var self = this;
return new Promise(function(resolve, reject) {
self.db.createReadStream({
self.database.createReadStream({
gte: TransactionService.Index.getSpentOutputsForAddress(address, NULLTXHASH, 0),
lte: TransactionService.Index.getSpentOutputsForAddress(address, LASTTXHASH, MAXOUTPUT)
}).on('data', function(element) {
results.push(element.value);
}).on('close', function() {
reject();
}).on('error', function(err) {
return reject(err);
}).on('end', function() {
resolve(results);
return resolve(results);
});
});
};
AddressService.prototype.buildAddressSummary = function(address, tip, allOutputs, spent) {
AddressService.prototype.buildAddressSummary = function(address, tip, allOutputs, spent, confirmations) {
var result = {};
var transactionsAppended = {};

View File

@ -63,11 +63,20 @@ function BlockService (opts) {
BlockService.prototype.writeLock = function() {
var self = this;
return Promise.try(function() {
// TODO
return new Promise(function(resolve, reject) {
if (self.lock) {
return reject();
} else {
self.lock = true;
return resolve();
}
});
};
BlockService.prototype.unlock = function() {
this.lock = false;
};
/**
* Transforms data as received from an RPC result structure for `getblock`,
* plus a list of transactions, and build a block based on thosė.
@ -91,16 +100,17 @@ BlockService.blockRPCtoBitcore = function(blockData, transactions) {
return new bitcore.Block({
header: new bitcore.BlockHeader({
version: blockData.version,
prevHash: bitcore.util.buffer.reverse(
new bitcore.deps.Buffer(blockData.previousblockhash, 'hex')
),
prevHash: blockData.previousblockhash ?
bitcore.util.buffer.reverse(
new bitcore.deps.Buffer(blockData.previousblockhash, 'hex')
) : bitcore.util.buffer.emptyBuffer(32),
time: blockData.time,
nonce: blockData.nonce,
bits: new bitcore.deps.bnjs(
new bitcore.deps.Buffer(blockData.bits, 'hex')
),
merkleRoot: bitcore.util.buffer.reverse(
new bitcore.deps.Buffer(blockData.merkleRoot, 'hex')
new bitcore.deps.Buffer(blockData.merkleroot, 'hex')
)
}),
transactions: transactions
@ -210,7 +220,7 @@ BlockService.prototype._confirmBlock = function(block) {
var ops = [];
this.writeLock().then(function() {
return this.writeLock().then(function() {
return self._setNextBlock(ops, block.header.prevHash, block);
@ -236,11 +246,13 @@ BlockService.prototype._confirmBlock = function(block) {
return self.transactionService._confirmTransaction(ops, block, transaction);
}));
}).then(
}).then(function() {
self.database.batchAsync.bind(self, ops)
return self.database.batchAsync(ops)
).then(this.unlock);
}).then(function() {
return self.unlock();
});
};
BlockService.prototype._setNextBlock = function(ops, prevBlockHash, block) {

View File

@ -0,0 +1,6 @@
module.exports = ''
+ '01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff'
+ '4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72'
+ '206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff'
+ '0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f'
+ '61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000';

View File

@ -25,6 +25,7 @@ var _ = bitcore.deps._;
var $ = bitcore.util.preconditions;
var NULLTXHASH = bitcore.util.buffer.emptyBuffer(32).toString('hex');
var GENESISTX = '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b'
var helper = function(name) {
return function(txId, output) {
@ -93,6 +94,10 @@ TransactionService.prototype.getTransaction = function(transactionId) {
var self = this;
if (transactionId === GENESISTX) {
return new bitcore.Transaction(require('./data/genesistx'));
}
return Promise.try(function() {
return self.rpc.getRawTransactionAsync(transactionId);
}).then(function(rawTransaction) {