Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
haadcode committed Aug 1, 2024
1 parent 05138d2 commit 0c13ec6
Show file tree
Hide file tree
Showing 16 changed files with 469 additions and 145 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/run-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ jobs:
run: npm ci
- name: Run linter
run: npm run lint
- name: Run libp2p relay in the background
run: npm run start:relay:background
- name: Run an Orbiter 1 instance in the background
run: npm run start:orbiter1:background
- name: Run an Orbiter 2 instance in the background
Expand Down
3 changes: 3 additions & 0 deletions conf/webpack.tests.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export default (env, argv) => {
})
],
resolve: {
alias: {
'./test-config': path.resolve(__dirname, '../test/utils/test-config-browser')
},
modules: [
'node_modules',
path.resolve(__dirname, '../node_modules')
Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@
"lint:fix": "standard --fix",
"copy:fixtures1": "rm -rf ./orbiter1 && mkdir -p ./orbiter1/voyager/orbiter/keystore && cp -Rf test/fixtures/keystore1/** ./orbiter1/voyager/orbiter/keystore/",
"copy:fixtures2": "rm -rf ./orbiter2 && mkdir -p ./orbiter2/voyager/orbiter/keystore && cp -Rf test/fixtures/keystore2/** ./orbiter2/voyager/orbiter/keystore/",
"start:orbiter1": "npm run copy:fixtures1 && node ./src/bin/cli.js daemon -p 54321 --allow --directory ./orbiter1 -vv",
"start:orbiter2": "npm run copy:fixtures2 && node ./src/bin/cli.js daemon -p 54322 --allow --directory ./orbiter2 -vv",
"start:orbiter1:background": "npm run copy:fixtures1 && node ./src/bin/cli.js daemon -p 54321 --allow --directory ./orbiter1 &",
"start:orbiter2:background": "npm run copy:fixtures2 && node ./src/bin/cli.js daemon -p 54322 --allow --directory ./orbiter2 &",
"start:relay": "node ./test/utils/relay.js",
"start:relay:background": "node ./test/utils/relay.js &",
"start:orbiter1": "npm run copy:fixtures1 && node ./src/bin/cli.js daemon -p 54321 -w 55441 --allow --directory ./orbiter1 -vv",
"start:orbiter2": "npm run copy:fixtures2 && node ./src/bin/cli.js daemon -p 54322 -w 55442 --allow --directory ./orbiter2 -vv",
"start:orbiter1:background": "npm run copy:fixtures1 && node ./src/bin/cli.js daemon -p 54321 -w 55441 --allow --directory ./orbiter1 &",
"start:orbiter2:background": "npm run copy:fixtures2 && node ./src/bin/cli.js daemon -p 54322 -w 55442 --allow --directory ./orbiter2 &",
"test:browser": "npm run build:tests && ./node_modules/.bin/playwright-test test/browser/bundle.js --runner mocha"
},
"standard": {
Expand Down
5 changes: 5 additions & 0 deletions src/bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ yargs(hideBin(process.argv))
description: 'The port to listen on. Defaults to 0.',
type: 'number'
})
.option('wsport', {
alias: 'w',
description: 'The port to listen on for WebSockets. Defaults to 0.',
type: 'number'
})
},
async (argv) => {
await daemon({ options: argv })
Expand Down
5 changes: 2 additions & 3 deletions src/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ export default async ({ options }) => {
const defaultAccess = options.allow ? Access.ALLOW : Access.DENY

options.verbose = options.verbose || 0

options.silent = options.silent || false

options.port = options.port || 0
options.wsport = options.wsport || 0

const id = orbiterId

Expand All @@ -54,7 +53,7 @@ export default async ({ options }) => {
const peerId = await createFromPrivKey(await keystore.getKey(id))
await keystore.close()

const libp2p = await createLibp2p(await libp2pConfig({ peerId, port: options.port }))
const libp2p = await createLibp2p(await libp2pConfig({ peerId, port: options.port, websocketPort: options.wsport }))

log('peerid:', libp2p.peerId.toString())
for (const addr of libp2p.getMultiaddrs().map(e => e.toString())) {
Expand Down
9 changes: 6 additions & 3 deletions src/utils/libp2p-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import { webSockets } from '@libp2p/websockets'
import { gossipsub } from '@chainsafe/libp2p-gossipsub'
import { mdns } from '@libp2p/mdns'

export const config = ({ peerId, port } = {}) => {
export const config = ({ peerId, port, websocketPort } = {}) => {
const conf = {
addresses: {
listen: [
'/ip4/0.0.0.0/tcp/0',
`/ip4/0.0.0.0/tcp/${port || 0}/ws`
`/ip4/0.0.0.0/tcp/${port || 0}`,
`/ip4/0.0.0.0/tcp/${websocketPort || 0}/ws`
]
},
transports: [
Expand All @@ -24,6 +24,9 @@ export const config = ({ peerId, port } = {}) => {
streamMuxers: [
yamux()
],
connectionGater: {
denyDialMultiaddr: () => false // allow dialling of private addresses.
},
peerDiscovery: [
mdns()
],
Expand Down
249 changes: 169 additions & 80 deletions test/e2e-browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,116 +2,205 @@ import { multiaddr } from '@multiformats/multiaddr'
import { strictEqual, deepStrictEqual } from 'assert'
import { rimraf } from 'rimraf'
import { launchLander } from './utils/launch-lander.js'
import { launchOrbiter } from './utils/launch-orbiter.js'
import waitFor from './utils/wait-for.js'
import connectPeers from './utils/connect-nodes.js'

describe('End-to-End Browser Test', function () {
this.timeout(10000)
const isBrowser = () => typeof window !== 'undefined'

const orbiterAddress1 = multiaddr('/ip4/127.0.0.1/tcp/54321/ws/p2p/16Uiu2HAmBzKcgCfpJ4j4wJSLkKLbCVvnNBWPnhexrnJWJf1fDu5y')
const orbiterAddress2 = multiaddr('/ip4/127.0.0.1/tcp/54322/ws/p2p/16Uiu2HAmATMovCwY46yyJib7bGZF2f2XLRar7d7R3NJCSJtuyQLt')
describe('End-to-End Browser Tests', function () {
describe('Orbiter in Nodejs', function () {
this.timeout(10000)

let lander1
let lander2
let lander3
const orbiterAddress1 = isBrowser()
? multiaddr('/ip4/127.0.0.1/tcp/55441/ws/p2p/16Uiu2HAmBzKcgCfpJ4j4wJSLkKLbCVvnNBWPnhexrnJWJf1fDu5y')
: multiaddr('/ip4/127.0.0.1/tcp/54321/p2p/16Uiu2HAmBzKcgCfpJ4j4wJSLkKLbCVvnNBWPnhexrnJWJf1fDu5y')

beforeEach(async function () {
lander1 = await launchLander({ orbiterAddress: orbiterAddress1, directory: 'lander1' })
lander2 = await launchLander({ orbiterAddress: orbiterAddress1, directory: 'lander2' })
lander3 = await launchLander({ orbiterAddress: orbiterAddress2, directory: 'lander3' })
})
const orbiterAddress2 = isBrowser()
? multiaddr('/ip4/127.0.0.1/tcp/55442/ws/p2p/16Uiu2HAmATMovCwY46yyJib7bGZF2f2XLRar7d7R3NJCSJtuyQLt')
: multiaddr('/ip4/127.0.0.1/tcp/54322/p2p/16Uiu2HAmATMovCwY46yyJib7bGZF2f2XLRar7d7R3NJCSJtuyQLt')

afterEach(async function () {
if (lander1) {
await lander1.shutdown()
}
if (lander2) {
await lander2.shutdown()
}
if (lander3) {
await lander3.shutdown()
}
await rimraf('./lander1')
await rimraf('./lander2')
await rimraf('./lander3')
})
let lander1
let lander2
let lander3

beforeEach(async function () {
lander1 = await launchLander({ orbiterAddress: orbiterAddress1, directory: 'lander1' })
lander2 = await launchLander({ orbiterAddress: orbiterAddress1, directory: 'lander2' })
lander3 = await launchLander({ orbiterAddress: orbiterAddress2, directory: 'lander3' })
})

afterEach(async function () {
if (lander1) {
await lander1.shutdown()
}
if (lander2) {
await lander2.shutdown()
}
if (lander3) {
await lander3.shutdown()
}
await rimraf('./lander1')
await rimraf('./lander2')
await rimraf('./lander3')
})

it('pin and replicate a database - lander1->orbiter1->lander2', async function () {
const entryAmount = 100
let replicated = false

const db1 = await lander1.orbitdb.open('my-db')

for (let i = 0; i < entryAmount; i++) {
await db1.add('hello world ' + i)
}

const expected = await db1.all()

console.time('pin')
await lander1.pin(db1.address)
console.timeEnd('pin')
// await lander1.shutdown()

console.time('pin')
await lander2.pin(db1.address)
console.timeEnd('pin')

console.time('replicate')
const db2 = await lander2.orbitdb.open(db1.address)

const onConnected = (peerId, heads) => {
replicated = true
}

it('pin and replicate a database - lander1->orbiter1->lander2', async function () {
const entryAmount = 100
let replicated = false
db2.events.on('join', onConnected)

const db1 = await lander1.orbitdb.open('my-db')
await waitFor(() => replicated, () => true)
console.timeEnd('replicate')

for (let i = 0; i < entryAmount; i++) {
await db1.add('hello world ' + i)
}
const res = await db2.all()

const expected = await db1.all()
strictEqual(expected.length, entryAmount)
strictEqual(res.length, entryAmount)
deepStrictEqual(expected, res)
})

console.time('pin')
await lander1.pin(db1.address)
console.timeEnd('pin')
await lander1.shutdown()
it('pin and replicate a database - lander1->orbiter1->orbiter2->lander3', async function () {
const entryAmount = 100
let replicated = false

console.time('pin')
await lander2.pin(db1.address)
console.timeEnd('pin')
const db1 = await lander1.orbitdb.open('my-db2')

console.time('replicate')
const db2 = await lander2.orbitdb.open(db1.address)
for (let i = 0; i < entryAmount; i++) {
await db1.add('hello world ' + i)
}

const onConnected = (peerId, heads) => {
replicated = true
}
const expected = await db1.all()

db2.events.on('join', onConnected)
console.time('pin')
await lander1.pin(db1.address)
console.timeEnd('pin')
// await lander1.shutdown()

await waitFor(() => replicated, () => true)
console.timeEnd('replicate')
console.time('pin')
await lander3.pin(db1.address)
console.timeEnd('pin')

const res = await db2.all()
console.time('replicate')
const db2 = await lander3.orbitdb.open(db1.address)

strictEqual(expected.length, entryAmount)
strictEqual(res.length, entryAmount)
deepStrictEqual(expected, res)
const onConnected = (peerId, heads) => {
replicated = true
}

db2.events.on('join', onConnected)

await waitFor(() => replicated, () => true)
console.timeEnd('replicate')

const res = await db2.all()

strictEqual(expected.length, entryAmount)
strictEqual(res.length, entryAmount)
deepStrictEqual(expected, res)
})
})

it('pin and replicate a database - lander1->orbiter1->orbiter2->lander3', async function () {
const entryAmount = 100
let replicated = false
describe('Orbiter in the browser', function () {
this.timeout(10000)

const orbiterAddress1 = isBrowser()
? multiaddr('/ip4/127.0.0.1/tcp/55441/ws/p2p/16Uiu2HAmBzKcgCfpJ4j4wJSLkKLbCVvnNBWPnhexrnJWJf1fDu5y')
: multiaddr('/ip4/127.0.0.1/tcp/54321/p2p/16Uiu2HAmBzKcgCfpJ4j4wJSLkKLbCVvnNBWPnhexrnJWJf1fDu5y')

let orbiter
let lander1
let lander2

beforeEach(async function () {
orbiter = await launchOrbiter({ directory: 'orbiter3' })

await connectPeers(orbiter.orbitdb.ipfs, orbiterAddress1)

lander1 = await launchLander({ orbiterAddress: orbiterAddress1, directory: 'lander4' })
lander2 = await launchLander({ orbiterAddress: orbiter.orbitdb.ipfs.libp2p.getMultiaddrs().pop(), directory: 'lander5' })

await orbiter.auth.add(lander2.orbitdb.identity.id)
})

afterEach(async function () {
if (lander1) {
await lander1.shutdown()
}
if (lander2) {
await lander2.shutdown()
}
if (orbiter) {
await orbiter.shutdown()
}
await rimraf('./lander4')
await rimraf('./lander5')
await rimraf('./orbiter3')
})

it('pin and replicate a database - lander1->orbiter1(nodejs)->orbiter2(browser)->lander3', async function () {
const entryAmount = 100
let replicated = false

const db1 = await lander1.orbitdb.open('my-db2')
const db1 = await lander1.orbitdb.open('my-db3')

for (let i = 0; i < entryAmount; i++) {
await db1.add('hello world ' + i)
}
for (let i = 0; i < entryAmount; i++) {
await db1.add('hello world ' + i)
}

const expected = await db1.all()
const expected = await db1.all()

console.time('pin')
await lander1.pin(db1.address)
console.timeEnd('pin')
await lander1.shutdown()
console.time('pin')
await lander1.pin(db1.address)
console.timeEnd('pin')
// await lander1.shutdown()

console.time('pin')
await lander3.pin(db1.address)
console.timeEnd('pin')
console.time('pin')
await lander2.pin(db1.address)
console.timeEnd('pin')

console.time('replicate')
const db2 = await lander3.orbitdb.open(db1.address)
console.time('replicate')
const db2 = await lander2.orbitdb.open(db1.address)

const onConnected = (peerId, heads) => {
replicated = true
}
const onConnected = (peerId, heads) => {
replicated = true
}

db2.events.on('join', onConnected)
db2.events.on('join', onConnected)

await waitFor(() => replicated, () => true)
console.timeEnd('replicate')
await waitFor(() => replicated, () => true)
console.timeEnd('replicate')

const res = await db2.all()
const res = await db2.all()

strictEqual(expected.length, entryAmount)
strictEqual(res.length, entryAmount)
deepStrictEqual(expected, res)
strictEqual(expected.length, entryAmount)
strictEqual(res.length, entryAmount)
deepStrictEqual(expected, res)
})
})
})
Loading

0 comments on commit 0c13ec6

Please sign in to comment.