/
index.js
322 lines (267 loc) · 9.95 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
'use strict';
var should = require('chai').should();
var expect = require('chai').expect;
var P2P = require('../../../');
var Messages = P2P.Messages;
var sinon = require('sinon');
var bitcore = require('bitcore-lib');
describe('Command Messages', function() {
var messages = new Messages();
describe('Addr', function() {
it('should error if arg is not an array of addrs', function() {
(function() {
var message = messages.Addresses(['not an addr']);
}).should.throw('First argument is expected to be an array of addrs');
});
it('should instantiate with an array of addrs', function() {
var message = messages.Addresses([{
ip: {
v4: 'localhost'
},
services: 1,
port: 1234
}]);
});
});
describe('Alert', function() {
it('should accept a transaction instance as an argument', function() {
var message = messages.Alert({
payload: new Buffer('abcdef', 'hex'),
signature: new Buffer('123456', 'hex')
});
message.payload.should.deep.equal(new Buffer('abcdef', 'hex'));
message.signature.should.deep.equal(new Buffer('123456', 'hex'));
});
});
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);
});
it('should create a transaction instance', function() {
var message = messages.Transaction();
message.transaction.should.be.instanceof(bitcore.Transaction);
});
it('version should remain the same', function() {
var tx = new bitcore.Transaction();
var version = Number(tx.version);
var message = messages.Transaction(tx);
message.transaction.version.should.equal(version);
});
});
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('Pong', function() {
it('should error if nonce is not a buffer', function() {
(function() {
var message = messages.Pong('not a buffer');
}).should.throw('First argument is expected to be an 8 byte buffer');
});
it('should error if nonce buffer has invalid length', function() {
(function() {
var message = messages.Pong(new Buffer(Array(9)));
}).should.throw('First argument is expected to be an 8 byte buffer');
});
it('should set a nonce if not included', function() {
var message = messages.Pong();
should.exist(message.nonce);
message.nonce.length.should.equal(8);
});
});
describe('Ping', function() {
it('should error if nonce is not a buffer', function() {
(function() {
var message = messages.Ping('not a buffer');
}).should.throw('First argument is expected to be an 8 byte buffer');
});
it('should error if nonce buffer has invalid length', function() {
(function() {
var message = messages.Ping(new Buffer(Array(9)));
}).should.throw('First argument is expected to be an 8 byte buffer');
});
it('should set a nonce if not included', function() {
var message = messages.Ping();
should.exist(message.nonce);
message.nonce.length.should.equal(8);
});
});
describe('FilterAdd', function() {
it('should error if arg is not a buffer', function() {
(function() {
var message = messages.FilterAdd('not a buffer');
}).should.throw('First argument is expected to be a Buffer or undefined');
});
});
describe('FilterLoad', function() {
it('should return a null payload', function() {
var message = messages.FilterLoad();
var payload = message.getPayload();
payload.length.should.equal(0);
payload.should.be.instanceof(Buffer);
});
it('should error if filter is not a bloom filter', function() {
(function() {
var message = messages.FilterLoad({filter: 'not a bloom filter'});
}).should.throw('An instance of BloomFilter');
});
});
describe('Inventory', function() {
it('should error if arg is not an array', function() {
(function() {
var message = messages.Inventory({});
}).should.throw('Argument is expected to be an array of inventory objects');
});
it('should not error if arg is an empty array', function() {
var message = messages.Inventory([]);
});
it('should error if arg is not an array of inventory objects', function() {
(function() {
var message = messages.Inventory([Number(0)]);
}).should.throw('Argument is expected to be an array of inventory objects');
});
});
describe('Transaction', function() {
it('should be able to pass a custom Transaction', function(done) {
var Transaction = function(){};
Transaction.prototype.fromBuffer = function() {
done();
};
var messagesCustom = new Messages({Transaction: Transaction});
var message = messagesCustom.Transaction.fromBuffer();
should.exist(message);
});
it('should work with Transaction.fromBuffer', function(done) {
var Transaction = sinon.stub();
Transaction.fromBuffer = function() {
done();
};
var messagesCustom = new Messages({Transaction: Transaction});
var message = messagesCustom.Transaction.fromBuffer();
should.exist(message);
});
});
describe('Block', function() {
it('should be able to pass a custom Block', function(done) {
var Block = sinon.stub();
Block.fromBuffer = function() {
done();
};
var messagesCustom = new Messages({Block: Block});
var message = messagesCustom.Block.fromBuffer();
should.exist(message);
});
});
describe('GetBlocks', function() {
it('should error with invalid stop', function() {
var invalidStop = '000000';
var starts = ['000000000000000013413cf2536b491bf0988f52e90c476ffeb701c8bfdb1db9'];
(function() {
var message = messages.GetBlocks({starts: starts, stop: invalidStop});
var buffer = message.toBuffer();
should.not.exist(buffer);
}).should.throw('Invalid hash length');
});
});
describe('GetHeaders', function() {
it('should error with invalid stop', function() {
var invalidStop = '000000';
var starts = ['000000000000000013413cf2536b491bf0988f52e90c476ffeb701c8bfdb1db9'];
(function() {
var message = messages.GetHeaders({starts: starts, stop: invalidStop});
var buffer = message.toBuffer();
should.not.exist(buffer);
}).should.throw('Invalid hash length');
});
});
describe('Headers', function() {
it('should error if arg is not an array', function() {
(function() {
var message = messages.Headers({});
}).should.throw('First argument is expected to be an array');
});
it('should error if arg is an empty array', function() {
(function() {
var message = messages.Headers([]);
}).should.throw('First argument is expected to be an array');
});
it('should error if arg is not an array of BlockHeaders', function() {
(function() {
var message = messages.Headers([Number(0)]);
}).should.throw('First argument is expected to be an array');
});
});
describe('MerkleBlock', function() {
it('should return null buffer for payload', function() {
var message = messages.MerkleBlock();
var payload = message.getPayload();
payload.length.should.equal(0);
});
it('should error if merkleBlock is not a MerkleBlock', function() {
(function() {
var message = messages.MerkleBlock({merkleBlock: 'not a merkle block'});
}).should.throw('An instance of MerkleBlock');
});
});
describe('Reject', function() {
it('should set properties from arg in constructor', function() {
var message = messages.Reject({
message: 'tx',
ccode: 0x01,
reason: 'transaction is malformed',
data: new Buffer('12345678901234567890123456789012', 'hex')
});
message.message.should.equal('tx');
message.ccode.should.equal(0x01);
message.reason.should.equal('transaction is malformed');
message.data.toString('hex').should.equal('12345678901234567890123456789012');
});
it('should let arg be optional in constructor', function() {
var message = messages.Reject();
expect(message.message).to.be.undefined;
expect(message.ccode).to.be.undefined;
expect(message.reason).to.be.undefined;
expect(message.data).to.be.undefined;
});
it('should write payload correctly', function() {
var message = messages.Reject({
message: 'tx',
ccode: 0x01,
reason: 'transaction is malformed',
data: new Buffer('12345678901234567890123456789012', 'hex')
});
var payload = message.getPayload();
message = messages.Reject();
message.setPayload(payload);
message.message.should.equal('tx');
message.ccode.should.equal(0x01);
message.reason.should.equal('transaction is malformed');
message.data.toString('hex').should.equal('12345678901234567890123456789012');
});
});
describe('Version', function() {
it('should set the default relay property as true', function() {
var message = messages.Version();
should.exist(message.relay);
message.relay.should.equal(true);
});
it('should set the relay as false', function() {
var message = messages.Version({relay: false});
should.exist(message.relay);
message.relay.should.equal(false);
});
it('should set the relay as true', function() {
var message = messages.Version({relay: true});
should.exist(message.relay);
message.relay.should.equal(true);
});
});
});