From 7af674178640527e36ea7a37a2163c0d902dcb34 Mon Sep 17 00:00:00 2001 From: tabcat Date: Sun, 10 Sep 2023 12:37:07 +0100 Subject: [PATCH] refactor!: change advertiser interface --- src/advertisers/dht.ts | 21 ++++++++++++++------- src/index.ts | 7 +++---- test/advertisers/advertiser.ts | 33 ++++++++------------------------- test/advertisers/dht.spec.ts | 4 ++-- 4 files changed, 27 insertions(+), 38 deletions(-) diff --git a/src/advertisers/dht.ts b/src/advertisers/dht.ts index f794344..e030897 100644 --- a/src/advertisers/dht.ts +++ b/src/advertisers/dht.ts @@ -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 }> + (provider: PeerId): Promise<{ dht: KadDHT, stop?: () => Promise }> } const collaborate = (createEphemeralKadDHT: CreateEphemeralKadDHT): Advertiser['collaborate'] => - async function * (dcid: CID, provider: Ed25519PeerId): AsyncIterable { + async function (dcid: CID, provider: PeerId): Promise { const { dht, stop } = await createEphemeralKadDHT(provider) try { - yield * dht.provide(dcid) + await drain(dht.provide(dcid)) } finally { if (stop != null) { await stop() @@ -21,8 +22,14 @@ const collaborate = (createEphemeralKadDHT: CreateEphemeralKadDHT): Advertiser[' } const findCollaborators = (dht: KadDHT): Advertiser['findCollaborators'] => - function (dcid: CID): AsyncIterable { - return dht.findProviders(dcid) + async function * (dcid: CID): AsyncIterable { + 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 { diff --git a/src/index.ts b/src/index.ts index 2805bc8..d32c725 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 - findCollaborators: (dcid: CID) => AsyncIterable + collaborate: (dcid: CID, provider: PeerId) => Promise + findCollaborators: (dcid: CID) => AsyncIterable } export interface Namer { diff --git a/test/advertisers/advertiser.ts b/test/advertisers/advertiser.ts index 48f75a9..e05d158 100644 --- a/test/advertisers/advertiser.ts +++ b/test/advertisers/advertiser.ts @@ -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 { @@ -14,37 +13,21 @@ interface CollaborateOptions extends AdvertiserOptions { collaborate: Advertiser['collaborate'] } -async function collaborate ({ collaborate, server, dcid, provider }: CollaborateOptions): Promise { - 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 { + await collaborate(dcid, provider) } interface FindCollaboratorsOptions extends AdvertiserOptions { findCollaborators: Advertiser['findCollaborators'] } -async function findCollaborators ({ findCollaborators, server, provider, dcid }: FindCollaboratorsOptions): Promise { - 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 { + 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 = { diff --git a/test/advertisers/dht.spec.ts b/test/advertisers/dht.spec.ts index 7339efe..b89493c 100644 --- a/test/advertisers/dht.spec.ts +++ b/test/advertisers/dht.spec.ts @@ -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' @@ -22,7 +22,7 @@ describe('advertisers/dht.ts', () => { provider: Ed25519PeerId, addrs: Multiaddr[] - const createEphemeralKadDHT: CreateEphemeralKadDHT = async (peerId: Ed25519PeerId): ReturnType => { + const createEphemeralKadDHT: CreateEphemeralKadDHT = async (peerId: PeerId): ReturnType => { const libp2p = await createLibp2pNode({ peerId }) await libp2p.dialProtocol(addrs, lanKadProtocol)