Fixes bugs on address service
This commit is contained in:
parent
e70dbb4ce3
commit
492a325c18
@ -5,8 +5,8 @@ BitcoreNode:
|
|||||||
port: 8333
|
port: 8333
|
||||||
LevelUp: ./db
|
LevelUp: ./db
|
||||||
RPC:
|
RPC:
|
||||||
username: username
|
user: username
|
||||||
password: password
|
pass: password
|
||||||
protocol: http
|
protocol: http
|
||||||
host: 127.0.0.1
|
host: 127.0.0.1
|
||||||
port: 8332
|
port: 8332
|
||||||
|
|||||||
@ -2,11 +2,12 @@
|
|||||||
|
|
||||||
var Promise = require('bluebird');
|
var Promise = require('bluebird');
|
||||||
var bitcore = require('bitcore');
|
var bitcore = require('bitcore');
|
||||||
|
var TransactionService = require('./transaction');
|
||||||
var _ = bitcore.deps._;
|
var _ = bitcore.deps._;
|
||||||
|
|
||||||
var NULLTXHASH = bitcore.util.buffer.emptyBuffer(32).toString('hex');
|
var NULLTXHASH = bitcore.util.buffer.emptyBuffer(32).toString('hex');
|
||||||
var LASTTXHASH = bitcore.util.buffer.fill(bitcore.util.buffer.emptyBuffer(32), -1).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) {
|
function AddressService(opts) {
|
||||||
opts = _.extend({}, opts);
|
opts = _.extend({}, opts);
|
||||||
@ -38,7 +39,7 @@ AddressService.prototype.getSummary = function(address, confirmations) {
|
|||||||
|
|
||||||
}).then(function(spent) {
|
}).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;
|
var self = this;
|
||||||
|
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
self.db.createReadStream({
|
self.database.createReadStream({
|
||||||
gte: TransactionService.Index.getOutputsForAddress(address, NULLTXHASH, 0),
|
gte: TransactionService.Index.getOutputsForAddress(address, NULLTXHASH, 0),
|
||||||
lte: TransactionService.Index.getOutputsForAddress(address, LASTTXHASH, MAXOUTPUT)
|
lte: TransactionService.Index.getOutputsForAddress(address, LASTTXHASH, MAXOUTPUT)
|
||||||
}).on('data', function(element) {
|
}).on('data', function(element) {
|
||||||
@ -66,20 +67,20 @@ AddressService.prototype.getSpent = function(address) {
|
|||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
self.db.createReadStream({
|
self.database.createReadStream({
|
||||||
gte: TransactionService.Index.getSpentOutputsForAddress(address, NULLTXHASH, 0),
|
gte: TransactionService.Index.getSpentOutputsForAddress(address, NULLTXHASH, 0),
|
||||||
lte: TransactionService.Index.getSpentOutputsForAddress(address, LASTTXHASH, MAXOUTPUT)
|
lte: TransactionService.Index.getSpentOutputsForAddress(address, LASTTXHASH, MAXOUTPUT)
|
||||||
}).on('data', function(element) {
|
}).on('data', function(element) {
|
||||||
results.push(element.value);
|
results.push(element.value);
|
||||||
}).on('close', function() {
|
}).on('error', function(err) {
|
||||||
reject();
|
return reject(err);
|
||||||
}).on('end', function() {
|
}).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 result = {};
|
||||||
var transactionsAppended = {};
|
var transactionsAppended = {};
|
||||||
|
|||||||
@ -63,11 +63,20 @@ function BlockService (opts) {
|
|||||||
|
|
||||||
BlockService.prototype.writeLock = function() {
|
BlockService.prototype.writeLock = function() {
|
||||||
var self = this;
|
var self = this;
|
||||||
return Promise.try(function() {
|
return new Promise(function(resolve, reject) {
|
||||||
// TODO
|
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`,
|
* Transforms data as received from an RPC result structure for `getblock`,
|
||||||
* plus a list of transactions, and build a block based on thosė.
|
* 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({
|
return new bitcore.Block({
|
||||||
header: new bitcore.BlockHeader({
|
header: new bitcore.BlockHeader({
|
||||||
version: blockData.version,
|
version: blockData.version,
|
||||||
prevHash: bitcore.util.buffer.reverse(
|
prevHash: blockData.previousblockhash ?
|
||||||
new bitcore.deps.Buffer(blockData.previousblockhash, 'hex')
|
bitcore.util.buffer.reverse(
|
||||||
),
|
new bitcore.deps.Buffer(blockData.previousblockhash, 'hex')
|
||||||
|
) : bitcore.util.buffer.emptyBuffer(32),
|
||||||
time: blockData.time,
|
time: blockData.time,
|
||||||
nonce: blockData.nonce,
|
nonce: blockData.nonce,
|
||||||
bits: new bitcore.deps.bnjs(
|
bits: new bitcore.deps.bnjs(
|
||||||
new bitcore.deps.Buffer(blockData.bits, 'hex')
|
new bitcore.deps.Buffer(blockData.bits, 'hex')
|
||||||
),
|
),
|
||||||
merkleRoot: bitcore.util.buffer.reverse(
|
merkleRoot: bitcore.util.buffer.reverse(
|
||||||
new bitcore.deps.Buffer(blockData.merkleRoot, 'hex')
|
new bitcore.deps.Buffer(blockData.merkleroot, 'hex')
|
||||||
)
|
)
|
||||||
}),
|
}),
|
||||||
transactions: transactions
|
transactions: transactions
|
||||||
@ -210,7 +220,7 @@ BlockService.prototype._confirmBlock = function(block) {
|
|||||||
|
|
||||||
var ops = [];
|
var ops = [];
|
||||||
|
|
||||||
this.writeLock().then(function() {
|
return this.writeLock().then(function() {
|
||||||
|
|
||||||
return self._setNextBlock(ops, block.header.prevHash, block);
|
return self._setNextBlock(ops, block.header.prevHash, block);
|
||||||
|
|
||||||
@ -236,11 +246,13 @@ BlockService.prototype._confirmBlock = function(block) {
|
|||||||
return self.transactionService._confirmTransaction(ops, block, transaction);
|
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) {
|
BlockService.prototype._setNextBlock = function(ops, prevBlockHash, block) {
|
||||||
|
|||||||
6
lib/services/data/genesistx.js
Normal file
6
lib/services/data/genesistx.js
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
module.exports = ''
|
||||||
|
+ '01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff'
|
||||||
|
+ '4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72'
|
||||||
|
+ '206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff'
|
||||||
|
+ '0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f'
|
||||||
|
+ '61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000';
|
||||||
@ -25,6 +25,7 @@ var _ = bitcore.deps._;
|
|||||||
var $ = bitcore.util.preconditions;
|
var $ = bitcore.util.preconditions;
|
||||||
|
|
||||||
var NULLTXHASH = bitcore.util.buffer.emptyBuffer(32).toString('hex');
|
var NULLTXHASH = bitcore.util.buffer.emptyBuffer(32).toString('hex');
|
||||||
|
var GENESISTX = '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b'
|
||||||
|
|
||||||
var helper = function(name) {
|
var helper = function(name) {
|
||||||
return function(txId, output) {
|
return function(txId, output) {
|
||||||
@ -93,6 +94,10 @@ TransactionService.prototype.getTransaction = function(transactionId) {
|
|||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
|
if (transactionId === GENESISTX) {
|
||||||
|
return new bitcore.Transaction(require('./data/genesistx'));
|
||||||
|
}
|
||||||
|
|
||||||
return Promise.try(function() {
|
return Promise.try(function() {
|
||||||
return self.rpc.getRawTransactionAsync(transactionId);
|
return self.rpc.getRawTransactionAsync(transactionId);
|
||||||
}).then(function(rawTransaction) {
|
}).then(function(rawTransaction) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user