diff --git a/src/socket/socket_client.js b/src/socket/socket_client.js index a21be7b..a76a4a3 100644 --- a/src/socket/socket_client.js +++ b/src/socket/socket_client.js @@ -69,7 +69,13 @@ class SocketClient { } onMessage(body, n) { - const msg = JSON.parse(body) + let msg + try { + msg = JSON.parse(body) + } catch (e) { + console.error('failed to parse message body: [%s]', body) + return + } if (msg instanceof Array) { ; // don't support batch request } else { diff --git a/test/config.js b/test/config.js index 06866ff..e88bbd8 100644 --- a/test/config.js +++ b/test/config.js @@ -1,27 +1,68 @@ -const servers = { - tcp: { - protocol: 'tcp', port: '50001', host: 'electrum.bitaroo.net', +const TBTC_ELECTRUMX_HOST = 'electrumx-server.test.tbtc.network' +const BLOCKSTREAM_ELECTRS_HOST = 'electrum.blockstream.info' + +const servers = [ + // TODO: Enable all protocols test for test.tbtc.network servers once they are + // publicly exposed. + // { + // protocol: 'tcp', + // port: '80', + // host: TBTC_ELECTRUMX_HOST, + // }, + // { + // protocol: 'ssl', + // port: '443', + // host: TBTC_ELECTRUMX_HOST, + // }, + // { + // protocol: 'ws', + // port: '8080', + // host: TBTC_ELECTRUMX_HOST, + // }, + + { + protocol: 'wss', + port: '8443', + host: TBTC_ELECTRUMX_HOST, }, - ssl: { - protocol: 'ssl', port: '50002', host: 'electrum.bitaroo.net', + // electrumx tcp + { + host: 'electrum1.cipig.net', + port: '10068', + protocol: 'tcp', }, - ws: { - protocol: 'ws', port: '50003', host: 'electrumx-server.tbtc.svc.cluster.local', + // electrumx ssl + { + host: 'testnet.qtornado.com', + port: '51002', + protocol: 'ssl', }, - wss: { - protocol: 'wss', port: '50004', host: 'electrumx-server.tbtc.svc.cluster.local', + // electrs-esplora tcp + { + protocol: 'tcp', + port: '60001', + host: BLOCKSTREAM_ELECTRS_HOST, + }, + // electrs-esplora ssl + { + host: 'electrum.blockstream.info', + port: '60002', + protocol: 'ssl', + }, + // fulcrum tcp + { + host: 'testnet.aranguren.org', + port: 51001, + protocol: 'tcp', + }, + // fulcrum ssl + { + host: 'testnet.aranguren.org', + port: 51002, + protocol: 'ssl', }, -} - -const serversArray = [ - servers.tcp, - servers.ssl, - // FIXME: WebSocket is commented out for CI, until we find public servers for this protocol. - // electrumServers.ws, - // electrumServers.wss, ] module.exports = { servers, - serversArray, } diff --git a/test/integration_test.js b/test/integration_test.js index 9983596..f978f6c 100644 --- a/test/integration_test.js +++ b/test/integration_test.js @@ -5,76 +5,70 @@ const assert = chai.assert const fs = require('fs') -const config = require('./config') +const {servers} = require('./config') describe('ElectrumClient', async () => { let txData before(async () => { - txData = JSON.parse(await fs.readFileSync('./test/tx.json', 'utf8')) + txData = JSON.parse(fs.readFileSync('./test/tx.json', 'utf8')) }) - context('when connected', async () => { - config.serversArray.forEach((server) => { - describe(`for ${server.protocol} protocol`, async () => { - let client + describe('for all protocols', async () => { + servers.forEach((server) => { + describe(`${server.protocol}://${server.host}:${server.port}`, async () => { + describe('when connected', async () => { + let client - before(async () => { - client = new ElectrumClient( - server.host, - server.port, - server.protocol, - server.options - ) + before(async () => { + client = new ElectrumClient( + server.host, + server.port, + server.protocol, + server.options, + ) - await client - .connect('test_client' + server.protocol, '1.4.2') - .catch((err) => { - console.error( - `failed to connect with config [${JSON.stringify( - server - )}]: [${err}]` - ) - }) - }) + await client.connect('test_client' + server.protocol, '1.4.2') + }) - after(async () => { - await client.close() - }) + after(async () => { + await client.close() + }) - it('request returns result', async () => { - const expectedResult = txData.hex - const result = await client.blockchain_transaction_get(txData.hash) + it('request returns result', async () => { + const expectedResult = txData.hex + const result = await client.blockchain_transaction_get(txData.hash) - assert.equal(result, expectedResult, 'unexpected result') + assert.equal(result, expectedResult, 'unexpected result') + }) }) - }) - }) - }) - context('when not connected', async () => { - before(async () => { - const server = config.servers.tcp + describe('when not connected', async () => { + let client - client = new ElectrumClient( - server.host, - server.port, - server.protocol, - server.options - ) - }) + before(async () => { + client = new ElectrumClient( + server.host, + server.port, + server.protocol, + server.options, + ) + }) - it('request throws error', async () => { - await client.blockchain_transaction_get(txData.hash).then( - (value) => { - // onFulfilled - assert.fail('not failed as expected') - }, - (reason) => { - // onRejected - assert.include(reason.toString(), `connection not established`) - } - ) + it('request throws error', async () => { + await client.blockchain_transaction_get(txData.hash).then( + (value) => { + // onFulfilled + assert.fail('not failed as expected') + }, + (reason) => { + // onRejected + assert.include(reason.toString(), `connection not established`) + }, + ) + }) + }) + }) }) }) // TODO: Add tests diff --git a/test/tx.json b/test/tx.json index 6997a67..6f00fad 100644 --- a/test/tx.json +++ b/test/tx.json @@ -1,4 +1,4 @@ { - "hash": "d60033c5cf5c199208a9c656a29967810c4e428c22efb492fdd816e6a0a1e548", - "hex": "010000000001011746bd867400f3494b8f44c24b83e1aa58c4f0ff25b4a61cffeffd4bc0f9ba300000000000ffffffff024897070000000000220020a4333e5612ab1a1043b25755c89b16d55184a42f81799e623e6bc39db8539c180000000000000000166a14edb1b5c2f39af0fec151732585b1049b07895211024730440220276e0ec78028582054d86614c65bc4bf85ff5710b9d3a248ca28dd311eb2fa6802202ec950dd2a8c9435ff2d400cc45d7a4854ae085f49e05cc3f503834546d410de012103732783eef3af7e04d3af444430a629b16a9261e4025f52bf4d6d026299c37c7400000000" + "hash": "bfb696b574ef93041270db405fc9e692d25a68bb3c5011cbeb17d4e9e002f176", + "hex": "02000000000101f04ac24a78b061ffdb0fd19c6a18c01b0b2a690f728407646016269dae423b370000000000feffffff0270170000000000001600142167c4e195229053971e1c934d8ec018d0151dd41287410000000000160014521edf677ef1ff12d363d4204756bec1a9d7cd050247304402200b489a2c86e39aeb0b442665f1edeb4a46f92bdc6cef94f22c7961501f711d57022024a2e37049bf63e607c280af31200426df6e53410d963220b761ba860b7637b00121030d176fac399c01095d4648e77c47dd04c5c946d57779795fab6f6e4e473e025cb3d22400" }