forked from bcoin-org/bcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotocol-test.js
129 lines (111 loc) · 3.41 KB
/
protocol-test.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
/* eslint-env mocha */
/* eslint prefer-arrow-callback: "off" */
/* eslint indent: "off" */
'use strict';
const assert = require('./util/assert');
const Network = require('../lib/protocol/network');
const util = require('../lib/utils/util');
const NetAddress = require('../lib/net/netaddress');
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');
const network = Network.get('main');
const tx8 = common.readTX('tx8');
const tx9 = common.readTX('tx9');
describe('Protocol', function() {
const pkg = require('../lib/pkg');
const agent = `/bcoin:${pkg.version}/`;
let parser, framer;
beforeEach(() => {
parser = new Parser();
framer = new Framer();
});
function packetTest(cmd, payload, test) {
it(`should encode/decode ${cmd}`, (cb) => {
parser.once('packet', (packet) => {
try {
assert.strictEqual(packet.cmd, cmd);
test(packet);
} catch (e) {
cb(e);
return;
}
cb();
});
const raw = framer.packet(cmd, payload.toRaw());
parser.feed(raw);
});
}
const v1 = packets.VersionPacket.fromOptions({
version: 300,
services: 1,
time: network.now(),
remote: new NetAddress(),
local: new NetAddress(),
nonce: Buffer.allocUnsafe(8),
agent: agent,
height: 0,
noRelay: false
});
packetTest('version', v1, (payload) => {
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({
version: 300,
services: 1,
time: network.now(),
remote: new NetAddress(),
local: new NetAddress(),
nonce: Buffer.allocUnsafe(8),
agent: agent,
height: 10,
noRelay: true
});
packetTest('version', v2, (payload) => {
assert.strictEqual(payload.version, 300);
assert.strictEqual(payload.agent, agent);
assert.strictEqual(payload.height, 10);
assert.strictEqual(payload.noRelay, true);
});
packetTest('verack', new packets.VerackPacket(), (payload) => {
});
const hosts = [
new NetAddress({
services: 1,
host: '127.0.0.1',
port: 8333,
time: util.now()
}),
new NetAddress({
services: 1,
host: '::123:456:789a',
port: 18333,
time: util.now()
})
];
packetTest('addr', new packets.AddrPacket(hosts), (payload) => {
assert.typeOf(payload.items, 'array');
assert.strictEqual(payload.items.length, 2);
assert.typeOf(payload.items[0].time, 'number');
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);
assert.typeOf(payload.items[1].time, 'number');
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());
});
});