Skip to content

chore: Migrate to using factory methods #523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions test/compliance.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TypedEventEmitter } from '@libp2p/interface'
import tests from '@libp2p/interface-compliance-tests/pubsub'
import { defaultLogger } from '@libp2p/logger'
import { PersistentPeerStore } from '@libp2p/peer-store'
import { persistentPeerStore } from '@libp2p/peer-store'
import { MemoryDatastore } from 'datastore-core'
import { GossipSub } from '../src/index.js'
import type { Libp2pEvents } from '@libp2p/interface'
Expand All @@ -18,7 +18,7 @@ describe.skip('interface compliance', function () {
const pubsub = new GossipSub(
{
...args.components,
peerStore: new PersistentPeerStore({
peerStore: persistentPeerStore({
peerId: args.components.peerId,
datastore: new MemoryDatastore(),
events: new TypedEventEmitter<Libp2pEvents>(),
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/go-gossipsub.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FloodSub } from '@libp2p/floodsub'
import { floodsub } from '@libp2p/floodsub'
import { type Message, TopicValidatorResult, type Libp2pEvents } from '@libp2p/interface'
import { stop } from '@libp2p/interface'
import { mockNetwork } from '@libp2p/interface-compliance-tests/mocks'
Expand Down Expand Up @@ -693,7 +693,7 @@ describe('go-libp2p-pubsub gossipsub tests', function () {
})
const fsubs = await createComponentsArray({
number: 10,
pubsub: FloodSub
pubsub: floodsub
})
psubs = gsubs.concat(fsubs)

Expand Down
12 changes: 6 additions & 6 deletions test/floodsub.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FloodSub } from '@libp2p/floodsub'
import { floodsub } from '@libp2p/floodsub'
import { stop } from '@libp2p/interface'
import { mockNetwork } from '@libp2p/interface-compliance-tests/mocks'
import { expect } from 'aegir/chai'
Expand All @@ -23,7 +23,7 @@ describe('gossipsub fallbacks to floodsub', () => {
}
})
nodeFs = await createComponents({
pubsub: FloodSub
pubsub: floodsub
})
})

Expand Down Expand Up @@ -61,7 +61,7 @@ describe('gossipsub fallbacks to floodsub', () => {
}
})
nodeFs = await createComponents({
pubsub: FloodSub
pubsub: floodsub
})
})

Expand Down Expand Up @@ -97,7 +97,7 @@ describe('gossipsub fallbacks to floodsub', () => {
}
})
nodeFs = await createComponents({
pubsub: FloodSub
pubsub: floodsub
})

await connectPubsubNodes(nodeGs, nodeFs)
Expand Down Expand Up @@ -158,7 +158,7 @@ describe('gossipsub fallbacks to floodsub', () => {
}
})
nodeFs = await createComponents({
pubsub: FloodSub
pubsub: floodsub
})

await connectPubsubNodes(nodeGs, nodeFs)
Expand Down Expand Up @@ -227,7 +227,7 @@ describe('gossipsub fallbacks to floodsub', () => {
}
})
nodeFs = await createComponents({
pubsub: FloodSub
pubsub: floodsub
})

await connectPubsubNodes(nodeGs, nodeFs)
Expand Down
15 changes: 8 additions & 7 deletions test/utils/create-pubsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import { TypedEventEmitter, start } from '@libp2p/interface'
import { mockRegistrar, mockConnectionManager, mockNetwork } from '@libp2p/interface-compliance-tests/mocks'
import { defaultLogger } from '@libp2p/logger'
import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { PersistentPeerStore } from '@libp2p/peer-store'
import { persistentPeerStore } from '@libp2p/peer-store'
import { MemoryDatastore } from 'datastore-core'
import { stubInterface } from 'sinon-ts'
import { GossipSub, type GossipSubComponents, type GossipsubOpts } from '../../src/index.js'
import type { TypedEventTarget, Libp2pEvents, PubSub } from '@libp2p/interface'
import { gossipsub, GossipSub, type GossipSubComponents, type GossipsubOpts } from '../../src/index.js'
import type { TypedEventTarget, Libp2pEvents } from '@libp2p/interface'
import type { ConnectionManager } from '@libp2p/interface-internal'
import type { floodsub } from '@libp2p/floodsub'

export interface CreateComponentsOpts {
init?: Partial<GossipsubOpts>
pubsub?: { new (opts?: any): PubSub }
pubsub?: typeof floodsub
}

export interface GossipSubTestComponents extends GossipSubComponents {
Expand All @@ -26,7 +27,7 @@ export interface GossipSubAndComponents {
}

export const createComponents = async (opts: CreateComponentsOpts): Promise<GossipSubAndComponents> => {
const Ctor = opts.pubsub ?? GossipSub
const Ctor = opts.pubsub ?? gossipsub
const privateKey = await generateKeyPair('Ed25519')
const peerId = peerIdFromPrivateKey(privateKey)

Expand All @@ -38,7 +39,7 @@ export const createComponents = async (opts: CreateComponentsOpts): Promise<Goss
peerId,
registrar: mockRegistrar(),
connectionManager: stubInterface<ConnectionManager>(),
peerStore: new PersistentPeerStore({
peerStore: persistentPeerStore({
peerId,
datastore: new MemoryDatastore(),
events,
Expand All @@ -49,7 +50,7 @@ export const createComponents = async (opts: CreateComponentsOpts): Promise<Goss
}
components.connectionManager = mockConnectionManager(components)

const pubsub = new Ctor(components, opts.init) as GossipSub
const pubsub = Ctor(opts.init)(components) as GossipSub

await start(...Object.entries(components), pubsub)

Expand Down