Merge pull request #49 from maraoz/fix/testnet

Fix testnet and optimize performance
This commit is contained in:
Manuel Aráoz 2015-04-27 10:48:19 -03:00
commit 6cf224b0a7
9 changed files with 155 additions and 160 deletions

16
config/testnet.yml Normal file
View File

@ -0,0 +1,16 @@
BitcoreNode:
LevelUp: ./testnet-db
network: testnet
NetworkMonitor:
host: localhost
port: 18333
Reporter: none # none, simple, matrix
BitcoreHTTP:
host: localhost
port: 8080
RPC:
user: user
pass: password
protocol: http
host: 127.0.0.1
port: 18332

View File

@ -9,5 +9,14 @@ module.exports = {
'000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a6' +
'7962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b' +
'8d578a4c702b6bf11d5fac00000000', 'hex'),
testnet: new Buffer('')
}
testnet: new Buffer('010000000000000000000000000000000000000000000000000000000000' +
'0000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a5132' +
'3a9fb8aa4b1e5e4adae5494dffff001d1aa4ae1801010000000100000000' +
'00000000000000000000000000000000000000000000000000000000ffff' +
'ffff4d04ffff001d0104455468652054696d65732030332f4a616e2f3230' +
'3039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f' +
'6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01' +
'000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a6' +
'7962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b' +
'8d578a4c702b6bf11d5fac00000000', 'hex')
};

View File

@ -9,13 +9,15 @@ var $ = bitcore.util.preconditions;
var _ = bitcore.deps._;
var p2p = require('bitcore-p2p');
var Peer = p2p.Peer;
var messages = new p2p.Messages();
function NetworkMonitor(eventBus, peer) {
$.checkArgument(eventBus);
$.checkArgument(peer);
this.bus = eventBus;
this.peer = peer;
this.messages = new p2p.Messages({
network: this.peer.network,
});
this.setupPeer(peer);
}
util.inherits(NetworkMonitor, EventEmitter);
@ -41,7 +43,7 @@ NetworkMonitor.prototype.setupPeer = function(peer) {
peer.on('inv', function(m) {
self.emit('inv', m.inventory);
// TODO only ask for data if tx or block is unknown
peer.sendMessage(messages.GetData(m.inventory));
peer.sendMessage(self.messages.GetData(m.inventory));
});
peer.on('tx', function(m) {
self.bus.process(m.transaction)
@ -69,7 +71,7 @@ NetworkMonitor.prototype.requestBlocks = function(locator) {
$.checkArgument(_.isArray(locator) &&
_.isUndefined(locator[0]) ||
_.isString(locator[0]), 'start must be a block hash string array');
this.peer.sendMessage(messages.GetBlocks({
this.peer.sendMessage(this.messages.GetBlocks({
starts: locator,
//stop: '000000002c05cc2e78923c34df87fd108b22221ac6076c18f3ade378a4d915e9' // TODO: remove this!!!
}));

View File

@ -53,7 +53,7 @@ BitcoreNode.create = function(opts) {
);
var rpc = opts.rpc || Promise.promisifyAll(new RPC(opts.RPC));
var transactionService = opts.transactionService || new TransactionService({
var transactionService = opts.transactionService || new TransactionService({
rpc: rpc,
database: database
});
@ -75,6 +75,9 @@ BitcoreNode.create = function(opts) {
BitcoreNode.prototype.initialize = function() {
var self = this;
var prevHeight = 0;
var statTimer = 5 * 1000;
setInterval(function() {
if (!self.blockchain) {
// not ready yet
@ -82,8 +85,10 @@ BitcoreNode.prototype.initialize = function() {
}
var tipHash = self.blockchain.tip;
var block = self.blockCache[tipHash];
console.log('block', block.id, 'height', block.height);
}, 5 * 1000);
var delta = block.height - prevHeight;
prevHeight = block.height;
console.log(block.id, block.height, 'vel', delta * 1000 / statTimer, 'b/s');
}, statTimer);
this.bus.register(bitcore.Block, function(block) {
@ -100,6 +105,8 @@ BitcoreNode.prototype.initialize = function() {
block.height = self.blockchain.height[block.id];
block.work = self.blockchain.work[block.id];
//console.log('block', block.id, block.height);
return Promise.each(blockchainChanges.unconfirmed, function(hash) {
return self.blockService.unconfirm(self.blockCache[hash]);
})
@ -144,7 +151,6 @@ BitcoreNode.prototype.start = function() {
this.blockService.getBlockchain().then(function(blockchain) {
if (!blockchain) {
console.log('nothing');
self.blockchain = new BlockChain();
self.bus.process(genesis);
} else {

View File

@ -200,7 +200,9 @@ BlockService.prototype.getLatest = function() {
return self.getBlock(blockHash);
}).catch(LevelUp.errors.NotFoundError, function() {
return null;
});
};
@ -251,6 +253,7 @@ BlockService.prototype.confirm = function(block, ops) {
//console.log(4);
self._setBlockByTs(ops, block);
//console.log(4.1);
self._setTip(ops, block);
//console.log(5);
@ -471,23 +474,23 @@ BlockService.prototype.getBlockchain = function() {
};
return self.database.getAsync(Index.tip)
.catch(function(err) {
if (err.notFound) {
return undefined;
}
throw err;
})
.then(function(tip) {
if (!tip) {
console.log('No tip found');
return;
}
console.log('Tip is', tip);
blockchain.tip = tip;
return fetchUnlessGenesis(tip).then(function() {
return blockchain;
.catch(function(err) {
if (err.notFound) {
return undefined;
}
throw err;
})
.then(function(tip) {
if (!tip) {
console.log('No tip found');
return;
}
console.log('Tip is', tip);
blockchain.tip = tip;
return fetchUnlessGenesis(tip).then(function() {
return blockchain;
});
});
});
};
module.exports = BlockService;

View File

@ -25,7 +25,7 @@ var _ = bitcore.deps._;
var $ = bitcore.util.preconditions;
var NULLTXHASH = bitcore.util.buffer.emptyBuffer(32).toString('hex');
var GENESISTX = '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b'
var GENESISTX = '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b';
var helper = function(name) {
return function(txId, output) {
@ -50,15 +50,16 @@ var helperAddress = function(index) {
};
var Index = {
output: 'txo-', // txo-<txid>-<n> -> serialized Output
spent: 'txs-', // txo-<txid>-<n>-<spend txid>-<m> -> block height of confirmation for spend
address: 'txa-', // txa-<address>-<txid>-<n> -> Output
addressSpent: 'txas-', // txa-<address>-<txid>-<n> -> {
// heightSpent: number, (may be -1 for unconfirmed tx)
// spentTx: string, spentTxInputIndex: number, spendInput: Input
// }
transaction: 'btx-' // btx-<txid> -> block in main chain that confirmed the tx
}
output: 'txo-', // txo-<txid>-<n> -> serialized Output
spent: 'txs-', // txo-<txid>-<n>-<spend txid>-<m> -> block height of confirmation for spend
address: 'txa-', // txa-<address>-<txid>-<n> -> Output
addressSpent: 'txas-',
// txa-<address>-<txid>-<n> -> {
// heightSpent: number, (may be -1 for unconfirmed tx)
// spentTx: string, spentTxInputIndex: number, spendInput: Input
// }
transaction: 'btx-' // btx-<txid> -> block in main chain that confirmed the tx
};
_.extend(Index, {
getOutput: helper(Index.output),
@ -76,7 +77,7 @@ _.extend(Index, {
}
});
function TransactionService (opts) {
function TransactionService(opts) {
opts = _.extend({}, opts);
this.database = opts.database || Promise.promisifyAll(new LevelUp(config.get('LevelUp')));
this.rpc = opts.rpc || Promise.promisifyAll(new RPC(config.get('RPC')));
@ -91,7 +92,6 @@ TransactionService.transactionRPCtoBitcore = function(rpcResponse) {
};
TransactionService.prototype.getTransaction = function(transactionId) {
var self = this;
if (transactionId === GENESISTX) {
@ -106,27 +106,24 @@ TransactionService.prototype.getTransaction = function(transactionId) {
};
TransactionService.prototype._confirmOutput = function(ops, block, transaction) {
var txid = transaction.id;
return function(output, index) {
ops.push({
type: 'put',
key: Index.getOutput(transaction.id, index),
key: Index.getOutput(txid, index),
value: output.toJSON()
});
var address;
// TODO: Move this logic to bitcore
if (output.script.isPublicKeyOut()) {
var hash = bitcore.crypto.Hash.sha256ripemd160(output.script.chunks[0].buf);
address = new bitcore.Address(hash, bitcore.Networks.defaultNetwork, bitcore.Address.PayToPublicKeyHash);
} else if (output.script.isPublicKeyHashOut() || output.script.isScriptHashOut()) {
if (output.script.isPublicKeyHashOut() || output.script.isScriptHashOut()) {
address = output.script.toAddress();
}
if (address) {
var out4addr = output.toObject();
out4addr.heightConfirmed = block.height;
ops.push({
type: 'put',
key: Index.getOutputsForAddress(address, transaction.id, index),
value: JSON.stringify(_.extend(output.toObject(), {
heightConfirmed: block.height
}))
key: Index.getOutputsForAddress(address, txid, index),
value: JSON.stringify(out4addr)
});
}
};
@ -134,13 +131,14 @@ TransactionService.prototype._confirmOutput = function(ops, block, transaction)
TransactionService.prototype._confirmInput = function(ops, block, transaction) {
var self = this;
var txid = transaction.id;
return function(input, index) {
if (input.prevTxId.toString('hex') === NULLTXHASH) {
return Promise.resolve();
}
ops.push({
type: 'put',
key: Index.getOutput(transaction.id, index),
key: Index.getOutput(txid, index),
value: JSON.stringify(_.extend(input.toObject(), {
heightConfirmed: block.height
}))
@ -151,15 +149,14 @@ TransactionService.prototype._confirmInput = function(ops, block, transaction) {
}
return Promise.try(function() {
return self._getAddressForInput(input)
}).then(function(address) {
var address = self._getAddressForInput(input);
if (address) {
ops.push({
type: 'put',
key: Index.getSpentOutputsForAddress(address, transaction.id, index),
key: Index.getSpentOutputsForAddress(address, txid, index),
value: JSON.stringify({
heightSpent: block.height,
spentTx: transaction.id,
spentTx: txid,
spentTxInputIndex: index,
spendInput: input.toObject()
})
@ -171,29 +168,8 @@ TransactionService.prototype._confirmInput = function(ops, block, transaction) {
TransactionService.prototype._getAddressForInput = function(input) {
var script = input.script;
var self = this;
if (script.isPublicKeyHashIn()) {
var hash = bitcore.crypto.Hash.sha256ripemd160(script.chunks[0].buf);
return new bitcore.Address(
hash, bitcore.Networks.defaultNetwork, bitcore.Address.PayToPublicKeyHash
);
} else if (script.isPublicKeyIn()) {
/*
return self.getTransaction(input.prevTxId.toString('hex')).then(function(transaction) {
var outputScript = transaction.outputs[input.outputIndex].script;
if (outputScript.isPublicKeyOut()) {
return new bitcore.Address(
bitcore.crypto.Hash.sha256ripemd160(outputScript.chunks[0].buf),
bitcore.Networks.defaultNetwork, bitcore.Address.PayToPublicKeyHash
);
}
return;
});
*/
} else {
return new bitcore.Script(script.chunks[script.chunks.length - 1]).toAddress();
}
// TODO: move this to bitcore
return new bitcore.Script(script.chunks[script.chunks.length - 1]).toAddress();
};
TransactionService.prototype._confirmTransaction = function(ops, block, transaction) {
@ -203,11 +179,12 @@ TransactionService.prototype._confirmTransaction = function(ops, block, transact
key: Index.getBlockForTransaction(transaction),
value: block.id
});
return Promise.all(
var confirmFunctions =
_.map(transaction.outputs, self._confirmOutput(ops, block, transaction))
.concat(
_.map(transaction.inputs, self._confirmInput(ops, block, transaction))
));
.concat(
_.map(transaction.inputs, self._confirmInput(ops, block, transaction))
);
return Promise.all(confirmFunctions);
};
TransactionService.prototype._unconfirmTransaction = function(ops, block, transaction) {
@ -219,9 +196,9 @@ TransactionService.prototype._unconfirmTransaction = function(ops, block, transa
});
return Promise.all(
_.map(transaction.outputs, self._unconfirmOutput(ops, block, transaction))
.concat(
_.map(transaction.inputs, self._unconfirmInput(ops, block, transaction))
));
.concat(
_.map(transaction.inputs, self._unconfirmInput(ops, block, transaction))
));
};
module.exports = TransactionService;

View File

@ -5,29 +5,17 @@
"author": "BitPay <dev@bitpay.com>",
"repository": "git://github.com/bitpay/bitcore-node.git",
"contributors": [
{
"name": "Matias Alejo Garcia",
"email": "ematiu@gmail.com"
},
{
"name": "Manuel Araoz",
"email": "manuelaraoz@gmail.com"
},
{
"name": "Mario Colque",
"email": "colquemario@gmail.com"
"name": "Yemel Jardi",
"email": "angel.jardi@gmail.com"
},
{
"name": "Gustavo Cortez",
"email": "cmgustavo83@gmail.com"
},
{
"name": "Juan Ignacio Sosa Lopez",
"email": "bechilandia@gmail.com"
},
{
"name": "Ivan Socolsky",
"email": "jungans@gmail.com"
"name": "Esteban Ordano",
"email": "eordano@gmail.com"
}
],
"bugs": {
@ -47,7 +35,7 @@
"async": "0.9.0",
"bitcoind-rpc": "^0.2.1",
"bitcore": "bitpay/bitcore",
"bitcore-p2p": "^0.11.0",
"bitcore-p2p": "^0.13.0",
"bluebird": "^2.9.12",
"body-parser": "^1.12.0",
"bufferput": "bitpay/node-bufferput",

View File

@ -25,6 +25,7 @@ describe('NetworkMonitor', function() {
mockBlock.id = 'asd';
busMock = new EventBus();
peerMock = new EventEmitter();
peerMock.network = Networks.testnet;
peerMock.sendMessage = sinon.spy();
peerMock.connect = function() {
this.emit('ready');

View File

@ -71,27 +71,26 @@ describe('TransactionService', function() {
it('confirms correctly the first transaction on genesis block', function(callback) {
var ops = [];
service._confirmTransaction(ops, genesisBlock, genesisTx).then(function() {
ops.map(function(k) {
var opsObj = ops.map(function(k) {
if (bitcore.util.js.isValidJSON(k.value)) {
k.value = JSON.parse(k.value);
}
return k;
}).should.deep.equal([
{ type: 'put',
key: 'btx-4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b',
value: '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f' },
{ type: 'put',
key: 'txo-4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b-0',
value:
{ satoshis: 5000000000,
script: '65 0x04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f OP_CHECKSIG' } },
{ type: 'put',
key: 'txa-1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa-4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b-0',
value:
{ satoshis: 5000000000,
script: '65 0x04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f OP_CHECKSIG',
heightConfirmed: 0} }
]);
});
opsObj.should.deep.equal([{
type: 'put',
key: 'btx-4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b',
value: '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'
}, {
type: 'put',
key: 'txo-4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b-0',
value: {
satoshis: 5000000000,
script: '65 0x04678afdb0fe5548271967f1a67130b7105cd6a828e03909' +
'a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7b' +
'a0b8d578a4c702b6bf11d5f OP_CHECKSIG'
}
}]);
callback();
});
});
@ -113,52 +112,46 @@ describe('TransactionService', function() {
k.value = JSON.parse(k.value);
}
return k;
}).should.deep.equal([
{ type: 'put',
key: 'btx-f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
value: '00000000d1145790a8694403d4063f323d499e655c83426834d4ce2f8dd4a2ee' },
{ type: 'put',
key: 'txo-f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16-0',
value:
{ satoshis: 1000000000,
script: '65 0x04ae1a62fe09c5f51b13905f07f06b99a2f7159b2225f374cd378d71302fa28414e7aab37397f554a7df5f142c21c1b7303b8a0626f1baded5c72a704f7e6cd84c OP_CHECKSIG' } },
{ type: 'put',
key: 'txa-1Q2TWHE3GMdB6BZKafqwxXtWAWgFt5Jvm3-f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16-0',
value:
{ satoshis: 1000000000,
script: '65 0x04ae1a62fe09c5f51b13905f07f06b99a2f7159b2225f374cd378d71302fa28414e7aab37397f554a7df5f142c21c1b7303b8a0626f1baded5c72a704f7e6cd84c OP_CHECKSIG',
heightConfirmed: 170 } },
{ type: 'put',
key: 'txo-f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16-1',
value:
{ satoshis: 4000000000,
script: '65 0x0411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3 OP_CHECKSIG' } },
{ type: 'put',
key: 'txa-12cbQLTFMXRnSzktFkuoG3eHoMeFtpTu3S-f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16-1',
value:
{ satoshis: 4000000000,
script: '65 0x0411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3 OP_CHECKSIG',
heightConfirmed: 170 } },
{ type: 'put',
key: 'txo-f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16-0',
value:
{ prevTxId: '0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9',
}).should.deep.equal([{
type: 'put',
key: 'btx-f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
value: '00000000d1145790a8694403d4063f323d499e655c83426834d4ce2f8dd4a2ee'
}, {
type: 'put',
key: 'txo-f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16-0',
value: {
satoshis: 1000000000,
script: '65 0x04ae1a62fe09c5f51b13905f07f06b99a2f7159b2225f374cd378d71302fa28414e7aab37397f554a7df5f142c21c1b7303b8a0626f1baded5c72a704f7e6cd84c OP_CHECKSIG'
}
}, {
type: 'put',
key: 'txo-f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16-1',
value: {
satoshis: 4000000000,
script: '65 0x0411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3 OP_CHECKSIG'
}
}, {
type: 'put',
key: 'txo-f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16-0',
value: {
prevTxId: '0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9',
outputIndex: 0,
sequenceNumber: 4294967295,
script: '71 0x304402204e45e16932b8af514961a1d3a1a25fdf3f4f7732e9d624c6c61548ab5fb8cd410220181522ec8eca07de4860a4acdd12909d831cc56cbbac4622082221a8768d1d0901',
heightConfirmed: 170
}
}, ]);
/* TODO: This should work if address spent is accepted for public key. Add test for P2PKH if not accepted
* { type: 'put',
key: 'txas-12cbQLTFMXRnSzktFkuoG3eHoMeFtpTu3S-f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16-0',
value:
{ heightSpent: 170,
spentTx: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
spentTxInputIndex: 0,
spendInput: { prevTxId: '0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9',
outputIndex: 0,
sequenceNumber: 4294967295,
script: '71 0x304402204e45e16932b8af514961a1d3a1a25fdf3f4f7732e9d624c6c61548ab5fb8cd410220181522ec8eca07de4860a4acdd12909d831cc56cbbac4622082221a8768d1d0901',
heightConfirmed: 170 } },
]);
/* TODO: This should work if address spent is accepted for public key. Add test for P2PKH if not accepted
* { type: 'put',
key: 'txas-12cbQLTFMXRnSzktFkuoG3eHoMeFtpTu3S-f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16-0',
value:
{ heightSpent: 170,
spentTx: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
spentTxInputIndex: 0,
spendInput: { prevTxId: '0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9',
outputIndex: 0,
sequenceNumber: 4294967295,
script: '71 0x304402204e45e16932b8af514961a1d3a1a25fdf3f4f7732e9d624c6c61548ab5fb8cd410220181522ec8eca07de4860a4acdd12909d831cc56cbbac4622082221a8768d1d0901' }}}]);*/
script: '71 0x304402204e45e16932b8af514961a1d3a1a25fdf3f4f7732e9d624c6c61548ab5fb8cd410220181522ec8eca07de4860a4acdd12909d831cc56cbbac4622082221a8768d1d0901' }}}]);*/
callback();
});
});