Skip to content

Commit

Permalink
WIP: Address Service: Fixed bugs from tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Braydon Fuller committed Jan 13, 2016
1 parent 188ff28 commit 7b0464d
Show file tree
Hide file tree
Showing 9 changed files with 1,517 additions and 781 deletions.
36 changes: 12 additions & 24 deletions integration/regtest-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var Transaction = index.Transaction;
var BitcoreNode = index.Node;
var AddressService = index.services.Address;
var BitcoinService = index.services.Bitcoin;
var encoding = require('../lib/services/address/encoding');
var DBService = index.services.DB;
var testWIF = 'cSdkPxkAjA4HDr5VHgsebAPDEh9Gyub4HK8UJr2DFGGqKKy4K5sG';
var testKey;
Expand All @@ -43,22 +44,6 @@ describe('Node Functionality', function() {
before(function(done) {
this.timeout(30000);

// Add the regtest network
bitcore.Networks.remove(bitcore.Networks.testnet);
bitcore.Networks.add({
name: 'regtest',
alias: 'regtest',
pubkeyhash: 0x6f,
privatekey: 0xef,
scripthash: 0xc4,
xpubkey: 0x043587cf,
xprivkey: 0x04358394,
networkMagic: 0xfabfb5da,
port: 18444,
dnsSeeds: [ ]
});
regtest = bitcore.Networks.get('regtest');

var datadir = __dirname + '/data';

testKey = bitcore.PrivateKey(testWIF);
Expand Down Expand Up @@ -93,6 +78,9 @@ describe('Node Functionality', function() {

node = new BitcoreNode(configuration);

regtest = bitcore.Networks.get('regtest');
should.exist(regtest);

node.on('error', function(err) {
log.error(err);
});
Expand Down Expand Up @@ -208,7 +196,7 @@ describe('Node Functionality', function() {

// We need to add a transaction to the mempool so that the next block will
// have a different hash as the hash has been invalidated.
client.sendToAddress(testKey.toAddress().toString(), 10, function(err) {
client.sendToAddress(testKey.toAddress(regtest).toString(), 10, function(err) {
if (err) {
throw err;
}
Expand Down Expand Up @@ -250,7 +238,7 @@ describe('Node Functionality', function() {
var address;
var unspentOutput;
before(function() {
address = testKey.toAddress().toString();
address = testKey.toAddress(regtest).toString();
});
it('should be able to get the balance of the test address', function(done) {
node.services.address.getBalance(address, false, function(err, balance) {
Expand Down Expand Up @@ -333,19 +321,19 @@ describe('Node Functionality', function() {
/* jshint maxstatements: 50 */

testKey2 = bitcore.PrivateKey.fromWIF('cNfF4jXiLHQnFRsxaJyr2YSGcmtNYvxQYSakNhuDGxpkSzAwn95x');
address2 = testKey2.toAddress().toString();
address2 = testKey2.toAddress(regtest).toString();

testKey3 = bitcore.PrivateKey.fromWIF('cVTYQbaFNetiZcvxzXcVMin89uMLC43pEBMy2etgZHbPPxH5obYt');
address3 = testKey3.toAddress().toString();
address3 = testKey3.toAddress(regtest).toString();

testKey4 = bitcore.PrivateKey.fromWIF('cPNQmfE31H2oCUFqaHpfSqjDibkt7XoT2vydLJLDHNTvcddCesGw');
address4 = testKey4.toAddress().toString();
address4 = testKey4.toAddress(regtest).toString();

testKey5 = bitcore.PrivateKey.fromWIF('cVrzm9gCmnzwEVMGeCxY6xLVPdG3XWW97kwkFH3H3v722nb99QBF');
address5 = testKey5.toAddress().toString();
address5 = testKey5.toAddress(regtest).toString();

testKey6 = bitcore.PrivateKey.fromWIF('cPfMesNR2gsQEK69a6xe7qE44CZEZavgMUak5hQ74XDgsRmmGBYF');
address6 = testKey6.toAddress().toString();
address6 = testKey6.toAddress(regtest).toString();

var tx = new Transaction();
tx.from(unspentOutput);
Expand Down Expand Up @@ -726,7 +714,7 @@ describe('Node Functionality', function() {
node.services.bitcoind.sendTransaction(tx.serialize());

setImmediate(function() {
var addrObj = node.services.address._getAddressInfo(address);
var addrObj = encoding.getAddressInfo(address);
node.services.address._getOutputsMempool(address, addrObj.hashBuffer,
addrObj.hashTypeBuffer, function(err, outs) {
if (err) {
Expand Down
2 changes: 2 additions & 0 deletions lib/services/address/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ exports.HASH_TYPES_MAP = {

exports.SPACER_MIN = new Buffer('00', 'hex');
exports.SPACER_MAX = new Buffer('ff', 'hex');
exports.SPACER_HEIGHT_MIN = new Buffer('0000000000', 'hex');
exports.SPACER_HEIGHT_MAX = new Buffer('ffffffffff', 'hex');
exports.TIMESTAMP_MIN = new Buffer('0000000000000000', 'hex');
exports.TIMESTAMP_MAX = new Buffer('ffffffffffffffff', 'hex');

Expand Down
32 changes: 26 additions & 6 deletions lib/services/address/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ exports.encodeOutputValue = function(satoshis, scriptBuffer) {
return Buffer.concat([satoshisBuffer, scriptBuffer]);
};

exports.encodeOutputMempoolValue = function(satoshis, timestampBuffer, scriptBuffer) {
var satoshisBuffer = new Buffer(8);
satoshisBuffer.writeDoubleBE(satoshis);
return Buffer.concat([satoshisBuffer, timestampBuffer, scriptBuffer]);
};

exports.decodeOutputValue = function(buffer) {
var satoshis = buffer.readDoubleBE(0);
var scriptBuffer = buffer.slice(8, buffer.length);
Expand All @@ -70,6 +76,17 @@ exports.decodeOutputValue = function(buffer) {
};
};

exports.decodeOutputMempoolValue = function(buffer) {
var satoshis = buffer.readDoubleBE(0);
var timestamp = buffer.readDoubleBE(8);
var scriptBuffer = buffer.slice(16, buffer.length);
return {
satoshis: satoshis,
timestamp: timestamp,
scriptBuffer: scriptBuffer
};
};

exports.encodeInputKey = function(hashBuffer, hashTypeBuffer, height, prevTxIdBuffer, outputIndex) {
var heightBuffer = new Buffer(4);
heightBuffer.writeUInt32BE(height);
Expand Down Expand Up @@ -175,7 +192,8 @@ exports.decodeSummaryCacheKey = function(buffer, network) {
return address;
};

exports.encodeSummaryCacheValue = function(cache, tipHeight) {
exports.encodeSummaryCacheValue = function(cache, tipHeight, tipHash) {
var tipHashBuffer = new Buffer(tipHash, 'hex');
var buffer = new Buffer(new Array(20));
buffer.writeUInt32BE(tipHeight);
buffer.writeDoubleBE(cache.result.totalReceived, 4);
Expand All @@ -189,21 +207,22 @@ exports.encodeSummaryCacheValue = function(cache, tipHeight) {
txidBuffers.push(buf);
}
var txidsBuffer = Buffer.concat(txidBuffers);
var value = Buffer.concat([buffer, txidsBuffer]);
var value = Buffer.concat([tipHashBuffer, buffer, txidsBuffer]);

return value;
};

exports.decodeSummaryCacheValue = function(buffer) {

var height = buffer.readUInt32BE();
var totalReceived = buffer.readDoubleBE(4);
var balance = buffer.readDoubleBE(12);
var hash = buffer.slice(0, 32).toString('hex');
var height = buffer.readUInt32BE(32);
var totalReceived = buffer.readDoubleBE(36);
var balance = buffer.readDoubleBE(44);

// read 32 byte chunks until exhausted
var appearanceIds = {};
var txids = [];
var pos = 20;
var pos = 52;
while(pos < buffer.length) {
var txid = buffer.slice(pos, pos + 32).toString('hex');
var txidHeight = buffer.readUInt32BE(pos + 32);
Expand All @@ -214,6 +233,7 @@ exports.decodeSummaryCacheValue = function(buffer) {

var cache = {
height: height,
hash: hash,
result: {
appearanceIds: appearanceIds,
txids: txids,
Expand Down
58 changes: 34 additions & 24 deletions lib/services/address/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ AddressHistory.prototype._mergeAndSortTxids = function(summaries) {
// Unconfirmed are sorted by timestamp
return unconfirmedAppearanceIds[a] - unconfirmedAppearanceIds[b];
});
var txids = confirmedTxids.concat(unconfirmedTxids);
return txids;
return confirmedTxids.concat(unconfirmedTxids);
};

/**
Expand All @@ -81,7 +80,7 @@ AddressHistory.prototype.get = function(callback) {
return callback(new Error('Maximum number of addresses (' + this.maxAddressQuery + ') exceeded'));
}

if (this.addresses.length === 0) {
if (this.addresses.length === 1) {
var address = this.addresses[0];
self.node.services.address.getAddressSummary(address, this.options, function(err, summary) {
if (err) {
Expand Down Expand Up @@ -111,9 +110,14 @@ AddressHistory.prototype.get = function(callback) {
totalCount = allTxids.length;

// Slice the page starting with the most recent
var fromOffset = totalCount - self.options.from;
var toOffset = totalCount - self.options.to;
var txids = allTxids.slice(toOffset, fromOffset);
var txids;
if (self.options.from >= 0 && self.options.to >= 0) {
var fromOffset = totalCount - self.options.from;
var toOffset = totalCount - self.options.to;
txids = allTxids.slice(toOffset, fromOffset);
} else {
txids = allTxids;
}

// Verify that this query isn't too long
if (txids.length > self.maxHistoryQueryLength) {
Expand Down Expand Up @@ -212,16 +216,19 @@ AddressHistory.prototype.getAddressDetailsForTransaction = function(transaction)
continue;
}
var inputAddress = input.script.toAddress(this.node.network);
if (inputAddress && this.addressStrings.indexOf(inputAddress.toString()) > 0) {
if (!result.addresses[inputAddress]) {
result.addresses[inputAddress] = {
inputIndexes: [],
outputIndexes: []
};
} else {
result.addresses[inputAddress].inputIndexes.push(inputIndex);
if (inputAddress) {
var inputAddressString = inputAddress.toString();
if (this.addressStrings.indexOf(inputAddressString) >= 0) {
if (!result.addresses[inputAddressString]) {
result.addresses[inputAddressString] = {
inputIndexes: [inputIndex],
outputIndexes: []
};
} else {
result.addresses[inputAddressString].inputIndexes.push(inputIndex);
}
result.satoshis -= input.output.satoshis;
}
result.satoshis -= input.output.satoshis;
}
}

Expand All @@ -231,16 +238,19 @@ AddressHistory.prototype.getAddressDetailsForTransaction = function(transaction)
continue;
}
var outputAddress = output.script.toAddress(this.node.network);
if (outputAddress && this.addressStrings.indexOf(outputAddress.toString()) > 0) {
if (!result.addresses[outputAddress]) {
result.addresses[outputAddress] = {
inputIndexes: [],
outputIndexes: []
};
} else {
result.addresses[outputAddress].inputIndexes.push(outputIndex);
if (outputAddress) {
var outputAddressString = outputAddress.toString();
if (this.addressStrings.indexOf(outputAddressString) >= 0) {
if (!result.addresses[outputAddressString]) {
result.addresses[outputAddressString] = {
inputIndexes: [],
outputIndexes: [outputIndex]
};
} else {
result.addresses[outputAddressString].outputIndexes.push(outputIndex);
}
result.satoshis += output.satoshis;
}
result.satoshis += output.satoshis;
}
}

Expand Down
Loading

0 comments on commit 7b0464d

Please sign in to comment.