added tests for pool.listen and improved arguments for tx and block messages
This commit is contained in:
parent
8c9babc093
commit
3b53593288
@ -17,7 +17,15 @@ function BlockMessage(options) {
|
||||
Message.call(this, options);
|
||||
this.command = 'block';
|
||||
this.magicNumber = magicNumber;
|
||||
this.block = options.block;
|
||||
|
||||
var block;
|
||||
if (options instanceof Block) {
|
||||
block = options;
|
||||
} else {
|
||||
block = options.block;
|
||||
}
|
||||
|
||||
this.block = block;
|
||||
}
|
||||
inherits(BlockMessage, Message);
|
||||
|
||||
@ -35,7 +43,7 @@ BlockMessage.prototype.getPayload = function() {
|
||||
};
|
||||
|
||||
module.exports = function(options) {
|
||||
Block = options.Block;
|
||||
magicNumber = options.magicNumber;
|
||||
Block = options.Block || Block;
|
||||
magicNumber = options.magicNumber || magicNumber;
|
||||
return BlockMessage;
|
||||
};
|
||||
|
||||
@ -17,8 +17,16 @@ function TransactionMessage(options) {
|
||||
Message.call(this, options);
|
||||
this.command = 'tx';
|
||||
this.magicNumber = magicNumber;
|
||||
this.transaction = options.transaction;
|
||||
};
|
||||
|
||||
var transaction;
|
||||
if(options instanceof Transaction) {
|
||||
transaction = options;
|
||||
} else {
|
||||
transaction = options.transaction;
|
||||
}
|
||||
|
||||
this.transaction = transaction;
|
||||
}
|
||||
inherits(TransactionMessage, Message);
|
||||
|
||||
TransactionMessage.fromObject = function(options) {
|
||||
|
||||
@ -4,6 +4,7 @@ var should = require('chai').should();
|
||||
var P2P = require('../../../');
|
||||
var Messages = P2P.Messages;
|
||||
var sinon = require('sinon');
|
||||
var bitcore = require('bitcore');
|
||||
|
||||
describe('Command Messages', function() {
|
||||
|
||||
@ -13,7 +14,7 @@ describe('Command Messages', function() {
|
||||
describe('Inventory helpers for: ' + constructors.join(', '), function() {
|
||||
|
||||
var fakeHash = 'e2dfb8afe1575bfacae1a0b4afc49af7ddda69285857267bae0e22be15f74a3a';
|
||||
|
||||
|
||||
describe('#forTransaction', function() {
|
||||
constructors.forEach(function(name) {
|
||||
it(name, function() {
|
||||
@ -23,7 +24,7 @@ describe('Command Messages', function() {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('#forBlock', function() {
|
||||
constructors.forEach(function(name) {
|
||||
it(name, function() {
|
||||
@ -33,7 +34,7 @@ describe('Command Messages', function() {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('#forFilteredBlock', function() {
|
||||
constructors.forEach(function(name) {
|
||||
it(name, function() {
|
||||
@ -46,9 +47,31 @@ describe('Command Messages', function() {
|
||||
|
||||
});
|
||||
|
||||
describe('Transaction', function() {
|
||||
|
||||
it('should accept a transaction instance as an argument', function() {
|
||||
var tx = new bitcore.Transaction();
|
||||
var message = messages.Transaction(tx);
|
||||
message.transaction.should.be.instanceof(bitcore.Transaction);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('Block', function() {
|
||||
|
||||
it('should accept a block instance as an argument', function() {
|
||||
var block = new bitcore.Block({
|
||||
header: {},
|
||||
transactions: []
|
||||
});
|
||||
var message = messages.Block(block);
|
||||
message.block.should.be.instanceof(bitcore.Block);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('FilterLoad', function() {
|
||||
|
||||
|
||||
it('should return a null payload', function() {
|
||||
var message = messages.FilterLoad();
|
||||
var payload = message.getPayload();
|
||||
@ -69,7 +92,7 @@ describe('Command Messages', function() {
|
||||
var message = messagesCustom.Transaction.fromBuffer();
|
||||
should.exist(message);
|
||||
});
|
||||
|
||||
|
||||
it('should work with Transaction.fromBuffer', function(done) {
|
||||
var Transaction = sinon.stub();
|
||||
Transaction.fromBuffer = function() {
|
||||
@ -83,7 +106,7 @@ describe('Command Messages', function() {
|
||||
});
|
||||
|
||||
describe('Block', function() {
|
||||
|
||||
|
||||
it('should be able to pass a custom Block', function(done) {
|
||||
var Block = sinon.stub();
|
||||
Block.fromBuffer = function() {
|
||||
@ -97,7 +120,7 @@ describe('Command Messages', function() {
|
||||
});
|
||||
|
||||
describe('GetBlocks', function() {
|
||||
|
||||
|
||||
it('should error with invalid stop', function() {
|
||||
var invalidStop = '000000';
|
||||
var starts = ['000000000000000013413cf2536b491bf0988f52e90c476ffeb701c8bfdb1db9'];
|
||||
@ -111,7 +134,7 @@ describe('Command Messages', function() {
|
||||
});
|
||||
|
||||
describe('GetHeaders', function() {
|
||||
|
||||
|
||||
it('should error with invalid stop', function() {
|
||||
var invalidStop = '000000';
|
||||
var starts = ['000000000000000013413cf2536b491bf0988f52e90c476ffeb701c8bfdb1db9'];
|
||||
@ -125,7 +148,7 @@ describe('Command Messages', function() {
|
||||
});
|
||||
|
||||
describe('MerkleBlock', function() {
|
||||
|
||||
|
||||
it('should return null buffer for payload', function() {
|
||||
var message = messages.MerkleBlock();
|
||||
var payload = message.getPayload();
|
||||
|
||||
75
test/pool.js
75
test/pool.js
@ -17,6 +17,7 @@ var Networks = bitcore.Networks;
|
||||
|
||||
var dns = require('dns');
|
||||
var sinon = require('sinon');
|
||||
var net = require('net');
|
||||
|
||||
function getPayloadBuffer(messageBuffer) {
|
||||
return new Buffer(messageBuffer.slice(48), 'hex');
|
||||
@ -49,6 +50,11 @@ describe('Pool', function() {
|
||||
});
|
||||
|
||||
it('optionally connect without dns seeds', function() {
|
||||
sinon.stub(Peer.prototype, 'connect', function() {
|
||||
this.socket = {
|
||||
destroy: sinon.stub()
|
||||
};
|
||||
});
|
||||
var stub = sinon.stub(dns, 'resolve', function(seed, callback) {
|
||||
throw new Error('DNS should not be called');
|
||||
});
|
||||
@ -74,6 +80,7 @@ describe('Pool', function() {
|
||||
pool.disconnect();
|
||||
pool._addrs.length.should.equal(2);
|
||||
stub.restore();
|
||||
Peer.prototype.connect.restore();
|
||||
});
|
||||
|
||||
it('will add addrs via options argument', function() {
|
||||
@ -295,23 +302,81 @@ describe('Pool', function() {
|
||||
|
||||
it('send message to all peers', function(done) {
|
||||
var message = 'message';
|
||||
var peerConnectStub = sinon.stub(Peer.prototype, 'connect', function() {
|
||||
sinon.stub(Peer.prototype, 'connect', function() {
|
||||
this.socket = {
|
||||
destroy: sinon.stub()
|
||||
};
|
||||
var self = this;
|
||||
process.nextTick(function() {
|
||||
self.emit('ready');
|
||||
});
|
||||
});
|
||||
var peerMessageStub = sinon.stub(Peer.prototype, 'sendMessage', function(message) {
|
||||
sinon.stub(Peer.prototype, 'sendMessage', function(message) {
|
||||
message.should.equal(message);
|
||||
peerConnectStub.restore();
|
||||
peerMessageStub.restore();
|
||||
Peer.prototype.connect.restore();
|
||||
Peer.prototype.sendMessage.restore();
|
||||
pool.disconnect();
|
||||
done();
|
||||
});
|
||||
var pool = new Pool({network: Networks.livenet, maxSize: 1});
|
||||
var pool = new Pool({
|
||||
network: Networks.livenet,
|
||||
maxSize: 1,
|
||||
dnsSeed: false,
|
||||
addrs: [
|
||||
{
|
||||
ip:{
|
||||
v4: 'localhost'
|
||||
}
|
||||
}
|
||||
]
|
||||
});
|
||||
pool.on('peerready', function() {
|
||||
pool.sendMessage(message);
|
||||
});
|
||||
pool.connect();
|
||||
});
|
||||
|
||||
describe('#listen', function() {
|
||||
|
||||
it('create a server', function(done) {
|
||||
var netStub = sinon.stub(net, 'createServer', function() {
|
||||
return {
|
||||
listen: function() {
|
||||
netStub.restore();
|
||||
done();
|
||||
}
|
||||
};
|
||||
});
|
||||
var pool = new Pool({network: Networks.livenet, maxSize: 1});
|
||||
pool.listen();
|
||||
});
|
||||
|
||||
it('should handle an ipv6 connection', function(done) {
|
||||
var ipv6 = '2001:0db8:85a3:0042:1000:8a2e:0370:7334';
|
||||
sinon.stub(net, 'createServer', function(callback) {
|
||||
callback({
|
||||
remoteAddress: ipv6
|
||||
});
|
||||
return {
|
||||
listen: sinon.stub()
|
||||
};
|
||||
});
|
||||
sinon.stub(net, 'isIPv6', function() {
|
||||
return true;
|
||||
});
|
||||
var pool = new Pool({network: Networks.livenet, maxSize: 1});
|
||||
pool._addAddr = function(addr) {
|
||||
should.exist(addr.ip.v6);
|
||||
addr.ip.v6.should.equal(ipv6);
|
||||
net.isIPv6.restore();
|
||||
net.createServer.restore();
|
||||
done();
|
||||
};
|
||||
pool._addConnectedPeer = sinon.stub();
|
||||
pool.listen();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user