diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 4acb1a87..306d1fd6 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -79,3 +79,10 @@ jobs: - uses: actions/checkout@v2 - run: yarn - run: cd examples && yarn && npm run test -- libp2p-in-the-browser + test-discovery-mechanisms-example: + needs: check + runs-on: macos-latest + steps: + - uses: actions/checkout@v2 + - run: yarn + - run: cd examples && yarn && npm run test -- discovery-mechanisms diff --git a/examples/discovery-mechanisms/1.js b/examples/discovery-mechanisms/1.js index 8dbf31d1..d5187fcd 100644 --- a/examples/discovery-mechanisms/1.js +++ b/examples/discovery-mechanisms/1.js @@ -7,15 +7,7 @@ const Mplex = require('libp2p-mplex') const { NOISE } = require('libp2p-noise') const Bootstrap = require('libp2p-bootstrap') -// Find this list at: https://github.com/ipfs/js-ipfs/blob/master/src/core/runtime/config-nodejs.json -const bootstrapers = [ - '/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ', - '/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN', - '/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb', - '/dnsaddr/bootstrap.libp2p.io/p2p/QmZa1sAxajnQjVM8WjWXoMbmPd7NsWhfKsPkErzpm9wGkp', - '/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa', - '/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt' -] +const bootstrapers = require('./bootstrapers') ;(async () => { const node = await Libp2p.create({ diff --git a/examples/discovery-mechanisms/bootstrapers.js b/examples/discovery-mechanisms/bootstrapers.js new file mode 100644 index 00000000..50da9eb4 --- /dev/null +++ b/examples/discovery-mechanisms/bootstrapers.js @@ -0,0 +1,13 @@ +'use strict' + +// Find this list at: https://github.com/ipfs/js-ipfs/blob/master/src/core/runtime/config-nodejs.json +const bootstrapers = [ + '/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ', + '/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN', + '/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb', + '/dnsaddr/bootstrap.libp2p.io/p2p/QmZa1sAxajnQjVM8WjWXoMbmPd7NsWhfKsPkErzpm9wGkp', + '/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa', + '/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt' +] + +module.exports = bootstrapers diff --git a/examples/discovery-mechanisms/test-1.js b/examples/discovery-mechanisms/test-1.js new file mode 100644 index 00000000..d187953c --- /dev/null +++ b/examples/discovery-mechanisms/test-1.js @@ -0,0 +1,42 @@ +'use strict' + +const path = require('path') +const execa = require('execa') +const pWaitFor = require('p-wait-for') +const uint8ArrayToString = require('uint8arrays/to-string') +const bootstrapers = require('./bootstrapers') + +const discoveredCopy = 'Discovered:' +const connectedCopy = 'Connection established to:' + +async function test () { + const discoveredNodes = [] + const connectedNodes = [] + + process.stdout.write('1.js\n') + + const proc = execa('node', [path.join(__dirname, '1.js')], { + cwd: path.resolve(__dirname), + all: true + }) + + proc.all.on('data', async (data) => { + process.stdout.write(data) + const line = uint8ArrayToString(data) + + // Discovered or Connected + if (line.includes(discoveredCopy)) { + const id = line.trim().split(discoveredCopy)[1] + discoveredNodes.push(id) + } else if (line.includes(connectedCopy)) { + const id = line.trim().split(connectedCopy)[1] + connectedNodes.push(id) + } + }) + + await pWaitFor(() => discoveredNodes.length === bootstrapers.length && connectedNodes.length === bootstrapers.length) + + proc.kill() +} + +module.exports = test diff --git a/examples/discovery-mechanisms/test-2.js b/examples/discovery-mechanisms/test-2.js new file mode 100644 index 00000000..0aa5c848 --- /dev/null +++ b/examples/discovery-mechanisms/test-2.js @@ -0,0 +1,35 @@ +'use strict' + +const path = require('path') +const execa = require('execa') +const pWaitFor = require('p-wait-for') +const uint8ArrayToString = require('uint8arrays/to-string') + +const discoveredCopy = 'Discovered:' + +async function test() { + const discoveredNodes = [] + + process.stdout.write('2.js\n') + + const proc = execa('node', [path.join(__dirname, '2.js')], { + cwd: path.resolve(__dirname), + all: true + }) + + proc.all.on('data', async (data) => { + process.stdout.write(data) + const line = uint8ArrayToString(data) + + if (line.includes(discoveredCopy)) { + const id = line.trim().split(discoveredCopy)[1] + discoveredNodes.push(id) + } + }) + + await pWaitFor(() => discoveredNodes.length === 2) + + proc.kill() +} + +module.exports = test diff --git a/examples/discovery-mechanisms/test.js b/examples/discovery-mechanisms/test.js new file mode 100644 index 00000000..38b2a347 --- /dev/null +++ b/examples/discovery-mechanisms/test.js @@ -0,0 +1,11 @@ +'use strict' + +const test1 = require('./test-1') +const test2 = require('./test-2') + +async function test () { + await test1() + await test2() +} + +module.exports = test