Skip to content

Commit

Permalink
refactor!: change advertiser interface
Browse files Browse the repository at this point in the history
  • Loading branch information
tabcat committed Sep 10, 2023
1 parent 109b75f commit 7af6741
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 38 deletions.
21 changes: 14 additions & 7 deletions src/advertisers/dht.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import drain from 'it-all'
import type { Advertiser } from '../index.js'
import type { Ed25519PeerId } from '@libp2p/interface/peer-id'
import type { QueryEvent, KadDHT } from '@libp2p/kad-dht'
import type { PeerId } from '@libp2p/interface/peer-id'
import type { KadDHT } from '@libp2p/kad-dht'
import type { CID } from 'multiformats/cid'

export interface CreateEphemeralKadDHT {
(provider: Ed25519PeerId): Promise<{ dht: KadDHT, stop?: () => Promise<void> }>
(provider: PeerId): Promise<{ dht: KadDHT, stop?: () => Promise<void> }>
}

const collaborate = (createEphemeralKadDHT: CreateEphemeralKadDHT): Advertiser['collaborate'] =>
async function * (dcid: CID, provider: Ed25519PeerId): AsyncIterable<QueryEvent> {
async function (dcid: CID, provider: PeerId): Promise<void> {
const { dht, stop } = await createEphemeralKadDHT(provider)

try {
yield * dht.provide(dcid)
await drain(dht.provide(dcid))
} finally {
if (stop != null) {
await stop()
Expand All @@ -21,8 +22,14 @@ const collaborate = (createEphemeralKadDHT: CreateEphemeralKadDHT): Advertiser['
}

const findCollaborators = (dht: KadDHT): Advertiser['findCollaborators'] =>
function (dcid: CID): AsyncIterable<QueryEvent> {
return dht.findProviders(dcid)
async function * (dcid: CID): AsyncIterable<PeerId> {
for await (const event of dht.findProviders(dcid)) {
if (event.name === 'PROVIDER' || event.name === 'PEER_RESPONSE') {
for (const { id: peerId } of event.providers) {
yield peerId
}
}
}
}

export function dhtAdvertiser (dht: KadDHT, createEphemeralKadDHT: CreateEphemeralKadDHT): Advertiser {
Expand Down
7 changes: 3 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@
* ```
*/

import type { Ed25519PeerId } from '@libp2p/interface/peer-id'
import type { QueryEvent } from '@libp2p/kad-dht'
import type { Ed25519PeerId, PeerId } from '@libp2p/interface/peer-id'
import type { Blockstore } from 'interface-blockstore'
import type { CID } from 'multiformats/cid'

export { toDcid } from './dcid.js'

export interface Advertiser {
collaborate: (dcid: CID, provider: Ed25519PeerId) => AsyncIterable<QueryEvent>
findCollaborators: (dcid: CID) => AsyncIterable<QueryEvent>
collaborate: (dcid: CID, provider: PeerId) => Promise<void>
findCollaborators: (dcid: CID) => AsyncIterable<PeerId>
}

export interface Namer {
Expand Down
33 changes: 8 additions & 25 deletions test/advertisers/advertiser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { expect } from 'aegir/chai'
import type { Advertiser } from '../../src'
import type { Ed25519PeerId } from '@libp2p/interface/peer-id'
import type { PeerResponseEvent } from '@libp2p/kad-dht'
import type { Ed25519PeerId, PeerId } from '@libp2p/interface/peer-id'
import type { CID } from 'multiformats/cid'

interface AdvertiserOptions {
Expand All @@ -14,37 +13,21 @@ interface CollaborateOptions extends AdvertiserOptions {
collaborate: Advertiser['collaborate']
}

async function collaborate ({ collaborate, server, dcid, provider }: CollaborateOptions): Promise<void> {
let response: PeerResponseEvent | undefined
for await (const event of collaborate(dcid, provider)) {
if (event.name === 'PEER_RESPONSE' && event.messageName === 'ADD_PROVIDER') {
if (event.from.equals(server)) {
response = event
}
}
}

expect(response).to.not.equal(undefined)
async function collaborate ({ collaborate, dcid, provider }: CollaborateOptions): Promise<void> {
await collaborate(dcid, provider)
}

interface FindCollaboratorsOptions extends AdvertiserOptions {
findCollaborators: Advertiser['findCollaborators']
}

async function findCollaborators ({ findCollaborators, server, provider, dcid }: FindCollaboratorsOptions): Promise<void> {
let response: PeerResponseEvent | undefined
for await (const event of findCollaborators(dcid)) {
if (event.name === 'PEER_RESPONSE' && event.messageName === 'GET_PROVIDERS') {
if (event.from.equals(server)) {
response = event
break // findCollaborators never done
}
}
async function findCollaborators ({ findCollaborators, provider, dcid }: FindCollaboratorsOptions): Promise<void> {
const providers: PeerId[] = []
for await (const peerId of findCollaborators(dcid)) {
providers.push(peerId)
}

expect(response).to.not.equal(undefined)
expect(response?.providers.length).to.equal(1)
expect(response?.providers[0].id.equals(provider)).to.equal(true)
expect(providers[0].toString()).to.equal(provider.toString())
}

export const spec = {
Expand Down
4 changes: 2 additions & 2 deletions test/advertisers/dht.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { lanKadProtocol } from '../utils/protocols.js'
import { spec } from './advertiser.js'
import type { CreateEphemeralKadDHT } from '../../src/advertisers/dht.js'
import type { Advertiser } from '../../src/index.js'
import type { Ed25519PeerId } from '@libp2p/interface/peer-id'
import type { Ed25519PeerId, PeerId } from '@libp2p/interface/peer-id'
import type { DualKadDHT } from '@libp2p/kad-dht'
import type { Multiaddr } from '@multiformats/multiaddr'
import type { Libp2p } from 'libp2p'
Expand All @@ -22,7 +22,7 @@ describe('advertisers/dht.ts', () => {
provider: Ed25519PeerId,
addrs: Multiaddr[]

const createEphemeralKadDHT: CreateEphemeralKadDHT = async (peerId: Ed25519PeerId): ReturnType<CreateEphemeralKadDHT> => {
const createEphemeralKadDHT: CreateEphemeralKadDHT = async (peerId: PeerId): ReturnType<CreateEphemeralKadDHT> => {
const libp2p = await createLibp2pNode({ peerId })

await libp2p.dialProtocol(addrs, lanKadProtocol)
Expand Down

0 comments on commit 7af6741

Please # to comment.