Skip to content

Commit

Permalink
Pin command arg are now addresses, launchLander takes an orbiter address
Browse files Browse the repository at this point in the history
  • Loading branch information
haadcode committed Aug 1, 2024
1 parent 3526bc3 commit f3ad586
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 88 deletions.
4 changes: 2 additions & 2 deletions src/lib/handlers/pin.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ const waitForReplication = (db) => {
export default async ({ orbitdb, pins, dbs, pubkey, addresses }) => {
for (const address of addresses) {
log('pin ', address)

let pubkeys = await pins.get(address)
let hasDb = false
const hasDb = pubkeys !== undefined

if (pubkeys) {
hasDb = true
pubkeys.push(pubkey)
} else {
pubkeys = [pubkey]
Expand Down
8 changes: 4 additions & 4 deletions src/lib/lander.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { Requests, Responses, createRequestMessage, parseMessage } from './messa
import { voyagerProtocol } from './protocol.js'

export default async ({ orbitdb, orbiterAddressOrId }) => {
const pin = async (dbs) => {
const pin = async (addresses) => {
let pinned = false
const pinDBs = source => {
return (async function * () {
const addresses = dbs.map(p => p.address)
addresses = Array.isArray(addresses) ? addresses : [addresses]
const message = await createRequestMessage(Requests.PIN, addresses, orbitdb.identity)
yield message
})()
Expand All @@ -28,12 +28,12 @@ export default async ({ orbitdb, orbiterAddressOrId }) => {
return pinned
}

const unpin = async (dbs) => {
const unpin = async (addresses) => {
let unpinned = false

const unpinDBs = source => {
return (async function * () {
const addresses = dbs.map(p => p.address)
addresses = Array.isArray(addresses) ? addresses : [addresses]
const message = await createRequestMessage(Requests.UNPIN, addresses, orbitdb.identity)
yield message
})()
Expand Down
52 changes: 9 additions & 43 deletions test/e2e-browser.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { multiaddr } from '@multiformats/multiaddr'
import { peerIdFromString } from '@libp2p/peer-id'
import { strictEqual, deepStrictEqual } from 'assert'
import { rimraf } from 'rimraf'
import { launchLander } from './utils/launch-lander.js'
Expand All @@ -8,50 +7,17 @@ import waitFor from './utils/wait-for.js'
describe('End-to-End Browser Test', function () {
this.timeout(10000)

const peerId1 = peerIdFromString('16Uiu2HAmBzKcgCfpJ4j4wJSLkKLbCVvnNBWPnhexrnJWJf1fDu5y')
const peerId2 = peerIdFromString('16Uiu2HAmATMovCwY46yyJib7bGZF2f2XLRar7d7R3NJCSJtuyQLt')
const peerAddress1 = multiaddr('/ip4/127.0.0.1/tcp/54321/ws/p2p/16Uiu2HAmBzKcgCfpJ4j4wJSLkKLbCVvnNBWPnhexrnJWJf1fDu5y')
const peerAddress2 = multiaddr('/ip4/127.0.0.1/tcp/54322/ws/p2p/16Uiu2HAmATMovCwY46yyJib7bGZF2f2XLRar7d7R3NJCSJtuyQLt')

let orbiter1
let orbiter2
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')

let lander1
let lander2
let lander3

beforeEach(async function () {
orbiter1 = {
orbitdb: {
ipfs: {
libp2p: {
peerId: peerId1
}
}
}
}

orbiter2 = {
orbitdb: {
ipfs: {
libp2p: {
peerId: peerId2
}
}
}
}

orbiter1.orbitdb.ipfs.libp2p.getMultiaddrs = () => {
return [peerAddress1]
}

orbiter2.orbitdb.ipfs.libp2p.getMultiaddrs = () => {
return [peerAddress2]
}

lander1 = await launchLander({ orbiter: orbiter1, directory: 'lander1' })
lander2 = await launchLander({ orbiter: orbiter1, directory: 'lander2' })
lander3 = await launchLander({ orbiter: orbiter2, directory: 'lander3' })
lander1 = await launchLander({ orbiterAddress: orbiterAddress1, directory: 'lander1' })
lander2 = await launchLander({ orbiterAddress: orbiterAddress1, directory: 'lander2' })
lander3 = await launchLander({ orbiterAddress: orbiterAddress2, directory: 'lander3' })
})

afterEach(async function () {
Expand Down Expand Up @@ -82,12 +48,12 @@ describe('End-to-End Browser Test', function () {
const expected = await db1.all()

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

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

console.time('replicate')
Expand Down Expand Up @@ -122,12 +88,12 @@ describe('End-to-End Browser Test', function () {
const expected = await db1.all()

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

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

console.time('replicate')
Expand Down
2 changes: 1 addition & 1 deletion test/messages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('Messages', function () {

beforeEach(async function () {
orbiter = await launchOrbiter()
lander = await launchLander({ orbiter })
lander = await launchLander({ orbiterAddress: orbiter.orbitdb.ipfs.libp2p.getMultiaddrs().pop() })
db = await lander.orbitdb.open('db')
})

Expand Down
6 changes: 3 additions & 3 deletions test/orbiter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('Orbiter', function () {

beforeEach(async function () {
orbiter = await launchOrbiter()
lander = await launchLander({ orbiter })
lander = await launchLander({ orbiterAddress: orbiter.orbitdb.ipfs.libp2p.getMultiaddrs().pop() })
await orbiter.auth.add(lander.orbitdb.identity.publicKey)
})

Expand All @@ -24,12 +24,12 @@ describe('Orbiter', function () {
})

it('loads a pinned database', async function () {
const { dbs } = await createPins(1, lander)
const { addresses } = await createPins(1, lander)

await orbiter.shutdown()

orbiter = await launchOrbiter()

strictEqual(Object.values(orbiter.dbs).pop().address, dbs.pop().address)
strictEqual(Object.values(orbiter.dbs).pop().address, addresses.pop())
})
})
28 changes: 14 additions & 14 deletions test/pin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('Pin', function () {
let lander

beforeEach(async function () {
lander = await launchLander({ orbiter })
lander = await launchLander({ orbiterAddress: orbiter.orbitdb.ipfs.libp2p.getMultiaddrs().pop() })
await orbiter.auth.add(lander.orbitdb.identity.publicKey)
})

Expand All @@ -32,24 +32,24 @@ describe('Pin', function () {
})

it('pins a database', async function () {
const { pinned, dbs } = await createPins(1, lander)
const { pinned, addresses } = await createPins(1, lander)

strictEqual(pinned, true)
strictEqual(Object.values(orbiter.dbs).pop().address, dbs.pop().address)
strictEqual(Object.values(orbiter.dbs).pop().address, addresses.pop())
})

it('pins multiple databases', async function () {
const { pinned, dbs } = await createPins(2, lander)
const { pinned, addresses } = await createPins(2, lander)

strictEqual(pinned, true)
strictEqual(Object.values(orbiter.dbs)[0].address, dbs[0].address)
strictEqual(Object.values(orbiter.dbs)[1].address, dbs[1].address)
strictEqual(Object.values(orbiter.dbs)[0].address, addresses[0])
strictEqual(Object.values(orbiter.dbs)[1].address, addresses[1])
})

it('tries to pin a database when not authorized', async function () {
await orbiter.auth.del(lander.orbitdb.identity.publicKey)
const dbs = [await lander.orbitdb.open('db')]
const pinned = await lander.pin(dbs)
const db = await lander.orbitdb.open('db')
const pinned = await lander.pin(db.address)

strictEqual(pinned, false)
})
Expand All @@ -59,10 +59,10 @@ describe('Pin', function () {
let lander1, lander2

beforeEach(async function () {
lander1 = await launchLander({ directory: './lander1', orbiter })
lander1 = await launchLander({ orbiterAddress: orbiter.orbitdb.ipfs.libp2p.getMultiaddrs().pop(), directory: './lander1' })
await orbiter.auth.add(lander1.orbitdb.identity.publicKey)

lander2 = await launchLander({ directory: './lander2', orbiter })
lander2 = await launchLander({ orbiterAddress: orbiter.orbitdb.ipfs.libp2p.getMultiaddrs().pop(), directory: './lander2' })
await orbiter.auth.add(lander2.orbitdb.identity.publicKey)
})

Expand All @@ -74,11 +74,11 @@ describe('Pin', function () {
})

it('pins a database', async function () {
const { dbs: dbs1 } = await createPins(1, lander1)
const { dbs: dbs2 } = await createPins(1, lander2)
const { addresses: addresses1 } = await createPins(1, lander1)
const { addresses: addresses2 } = await createPins(1, lander2)

strictEqual(Object.values(orbiter.dbs)[0].address, dbs1.pop().address)
strictEqual(Object.values(orbiter.dbs)[1].address, dbs2.pop().address)
strictEqual(Object.values(orbiter.dbs)[0].address, addresses1.pop())
strictEqual(Object.values(orbiter.dbs)[1].address, addresses2.pop())
})
})
})
16 changes: 8 additions & 8 deletions test/unpin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('Unpin', function () {
let lander

beforeEach(async function () {
lander = await launchLander({ orbiter })
lander = await launchLander({ orbiterAddress: orbiter.orbitdb.ipfs.libp2p.getMultiaddrs().pop() })
await orbiter.auth.add(lander.orbitdb.identity.publicKey)
})

Expand All @@ -32,33 +32,33 @@ describe('Unpin', function () {
})

it('unpins a database', async function () {
const { dbs } = await createPins(1, lander)
const { addresses } = await createPins(1, lander)

const unpinned = await lander.unpin(dbs)
const unpinned = await lander.unpin(addresses)

strictEqual(unpinned, true)
strictEqual((await orbiter.pins.all()).length, 0)
strictEqual(Object.values(orbiter.dbs).length, 0)
})

it('unpins multiple databases', async function () {
const { dbs } = await createPins(2, lander)
const { addresses } = await createPins(2, lander)

const unpinned = await lander.unpin(dbs)
const unpinned = await lander.unpin(addresses)

strictEqual(unpinned, true)
strictEqual((await orbiter.pins.all()).length, 0)
strictEqual(Object.values(orbiter.dbs).length, 0)
})

it('unpins a database when multiple databases have been pinned', async function () {
const { dbs } = await createPins(2, lander)
const { addresses } = await createPins(2, lander)

const unpinned = await lander.unpin(dbs.slice(0, 1))
const unpinned = await lander.unpin(addresses.slice(0, 1))

strictEqual(unpinned, true)
strictEqual((await orbiter.pins.all()).length, 1)
strictEqual(Object.values(orbiter.dbs).pop().address, dbs[1].address)
strictEqual(Object.values(orbiter.dbs).pop().address, addresses[1])
})
})
})
11 changes: 5 additions & 6 deletions test/utils/connect-nodes.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const defaultFilter = () => true
import { peerIdFromString } from '@libp2p/peer-id'

const connectIpfsNodes = async (ipfs1, ipfs2, options = {
filter: defaultFilter
}) => {
await ipfs2.libp2p.peerStore.save(ipfs1.libp2p.peerId, { multiaddrs: ipfs1.libp2p.getMultiaddrs().filter(options.filter) })
await ipfs2.libp2p.dial(ipfs1.libp2p.peerId)
const connectIpfsNodes = async (ipfs, peerAddress) => {
const peerId = peerIdFromString(peerAddress.getPeerId())
await ipfs.libp2p.peerStore.save(peerId, { multiaddrs: [peerAddress] })
await ipfs.libp2p.dial(peerId)
}

export default connectIpfsNodes
9 changes: 5 additions & 4 deletions test/utils/create-pins.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
export const createPins = async (howMany, lander) => {
const dbs = []
const addresses = []

for (let i = 1; i <= howMany; i++) {
dbs.push(await lander.orbitdb.open(`db${i}`))
const db = await lander.orbitdb.open(`db${i}`)
addresses.push(db.address)
}

const pinned = await lander.pin(dbs)
const pinned = await lander.pin(addresses)

return { pinned, dbs }
return { pinned, addresses }
}
7 changes: 4 additions & 3 deletions test/utils/launch-lander.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,18 @@ const heliaOptions = {
routers: [
]
}
export const launchLander = async ({ directory, orbiter } = {}) => {

export const launchLander = async ({ directory, orbiterAddress } = {}) => {
const libp2p = await createLibp2p({ ...options })
const ipfs = await createHelia({ libp2p, ...heliaOptions })

directory = directory || './lander'

const orbitdb = await createOrbitDB({ ipfs, directory })

await connectPeers(orbiter.orbitdb.ipfs, ipfs)
await connectPeers(ipfs, orbiterAddress)

const lander = await Lander({ orbitdb, orbiterAddressOrId: orbiter.orbitdb.ipfs.libp2p.peerId })
const lander = await Lander({ orbitdb, orbiterAddressOrId: orbiterAddress })

// Helper function for tests
lander.shutdown = async () => {
Expand Down

0 comments on commit f3ad586

Please sign in to comment.