itns-sidechain/test/protocol-test.js

130 lines
3.4 KiB
JavaScript
Raw Normal View History

2017-07-30 16:49:24 -07:00
/* eslint-env mocha */
2017-07-31 00:34:42 -07:00
/* eslint prefer-arrow-callback: "off" */
/* eslint indent: "off" */
2017-07-30 16:49:24 -07:00
2016-06-13 01:06:01 -07:00
'use strict';
2017-08-10 11:17:10 -07:00
const assert = require('./util/assert');
2017-06-29 20:54:07 -07:00
const Network = require('../lib/protocol/network');
const util = require('../lib/utils/util');
2017-11-17 21:22:23 -08:00
const NetAddress = require('../lib/net/netaddress');
2017-06-29 20:54:07 -07:00
const TX = require('../lib/primitives/tx');
const Framer = require('../lib/net/framer');
const Parser = require('../lib/net/parser');
const packets = require('../lib/net/packets');
const common = require('./util/common');
2017-06-29 20:54:07 -07:00
const network = Network.get('main');
2016-12-10 14:25:02 -08:00
const tx8 = common.readTX('tx8');
const tx9 = common.readTX('tx9');
2014-04-28 17:43:13 +04:00
describe('Protocol', function() {
2017-07-27 09:27:00 -07:00
const pkg = require('../lib/pkg');
const agent = `/bcoin:${pkg.version}/`;
let parser, framer;
2017-06-29 20:54:07 -07:00
beforeEach(() => {
2016-08-23 22:55:50 -07:00
parser = new Parser();
framer = new Framer();
2014-04-28 17:43:13 +04:00
});
function packetTest(cmd, payload, test) {
it(`should encode/decode ${cmd}`, (cb) => {
2017-06-29 20:54:07 -07:00
parser.once('packet', (packet) => {
try {
assert.strictEqual(packet.cmd, cmd);
test(packet);
} catch (e) {
cb(e);
return;
}
2014-04-30 12:44:59 +04:00
cb();
});
const raw = framer.packet(cmd, payload.toRaw());
parser.feed(raw);
2014-04-28 17:43:13 +04:00
});
2014-04-30 12:44:59 +04:00
}
const v1 = packets.VersionPacket.fromOptions({
2017-01-06 09:57:55 -08:00
version: 300,
services: 1,
2017-07-25 14:23:10 -07:00
time: network.now(),
2016-12-20 12:45:11 -08:00
remote: new NetAddress(),
local: new NetAddress(),
2017-10-26 04:07:36 -07:00
nonce: Buffer.allocUnsafe(8),
2017-01-06 09:57:55 -08:00
agent: agent,
2016-06-17 06:32:00 -07:00
height: 0,
2017-01-05 13:11:52 -08:00
noRelay: false
2016-06-22 11:44:43 -07:00
});
2016-06-17 06:32:00 -07:00
2017-06-29 20:54:07 -07:00
packetTest('version', v1, (payload) => {
2017-08-09 15:28:03 -07:00
assert.strictEqual(payload.version, 300);
assert.strictEqual(payload.agent, agent);
assert.strictEqual(payload.height, 0);
assert.strictEqual(payload.noRelay, false);
});
const v2 = packets.VersionPacket.fromOptions({
2017-01-06 09:57:55 -08:00
version: 300,
services: 1,
2017-07-25 14:23:10 -07:00
time: network.now(),
2016-12-20 12:45:11 -08:00
remote: new NetAddress(),
local: new NetAddress(),
2017-10-26 04:07:36 -07:00
nonce: Buffer.allocUnsafe(8),
2017-01-06 09:57:55 -08:00
agent: agent,
2016-06-17 06:32:00 -07:00
height: 10,
2017-01-05 13:11:52 -08:00
noRelay: true
2016-06-22 11:44:43 -07:00
});
2016-06-17 06:32:00 -07:00
2017-06-29 20:54:07 -07:00
packetTest('version', v2, (payload) => {
2017-08-09 15:28:03 -07:00
assert.strictEqual(payload.version, 300);
assert.strictEqual(payload.agent, agent);
assert.strictEqual(payload.height, 10);
assert.strictEqual(payload.noRelay, true);
2014-04-30 12:44:59 +04:00
});
2017-06-29 20:54:07 -07:00
packetTest('verack', new packets.VerackPacket(), (payload) => {
2014-04-28 17:43:13 +04:00
});
const hosts = [
2016-12-20 12:45:11 -08:00
new NetAddress({
2017-01-06 09:57:55 -08:00
services: 1,
host: '127.0.0.1',
port: 8333,
2017-07-25 14:23:10 -07:00
time: util.now()
2016-06-17 06:32:00 -07:00
}),
2016-12-20 12:45:11 -08:00
new NetAddress({
2017-01-06 09:57:55 -08:00
services: 1,
2016-05-26 17:59:43 -07:00
host: '::123:456:789a',
port: 18333,
2017-07-25 14:23:10 -07:00
time: util.now()
2016-06-17 06:32:00 -07:00
})
];
2017-06-29 20:54:07 -07:00
packetTest('addr', new packets.AddrPacket(hosts), (payload) => {
2017-08-11 18:25:54 -07:00
assert.typeOf(payload.items, 'array');
2017-08-09 15:28:03 -07:00
assert.strictEqual(payload.items.length, 2);
2017-08-11 18:25:54 -07:00
assert.typeOf(payload.items[0].time, 'number');
2017-08-09 15:28:03 -07:00
assert.strictEqual(payload.items[0].services, 1);
assert.strictEqual(payload.items[0].host, hosts[0].host);
assert.strictEqual(payload.items[0].port, hosts[0].port);
2017-08-11 18:25:54 -07:00
assert.typeOf(payload.items[1].time, 'number');
2017-08-09 15:28:03 -07:00
assert.strictEqual(payload.items[1].services, 1);
assert.strictEqual(payload.items[1].host, hosts[1].host);
assert.strictEqual(payload.items[1].port, hosts[1].port);
});
it('should include the raw data of only one transaction', () => {
const [tx1] = tx8.getTX();
const [tx2] = tx9.getTX();
const raw = Buffer.concat([tx1.toRaw(), tx2.toRaw()]);
const tx = TX.fromRaw(raw);
tx.refresh();
assert.bufferEqual(tx.toRaw(), tx1.toRaw());
});
2014-04-28 17:43:13 +04:00
});