-
Notifications
You must be signed in to change notification settings - Fork 43
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
chore: move protocols CreateOptions
into interfaces and
#1145
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import type { | |
IEncoder, | ||
ILightPush, | ||
IMessage, | ||
ProtocolCreateOptions, | ||
ProtocolOptions, | ||
SendResult, | ||
} from "@waku/interfaces"; | ||
|
@@ -36,40 +37,31 @@ export interface LightPushComponents { | |
connectionManager: ConnectionManager; | ||
} | ||
|
||
export interface CreateOptions { | ||
/** | ||
* The PubSub Topic to use. Defaults to {@link DefaultPubSubTopic}. | ||
* | ||
* The usage of the default pubsub topic is recommended. | ||
* See [Waku v2 Topic Usage Recommendations](https://rfc.vac.dev/spec/23/) for details. | ||
* | ||
* @default {@link DefaultPubSubTopic} | ||
*/ | ||
pubSubTopic?: string; | ||
} | ||
|
||
/** | ||
* Implements the [Waku v2 Light Push protocol](https://rfc.vac.dev/spec/19/). | ||
*/ | ||
class LightPush implements ILightPush { | ||
multicodec: string; | ||
pubSubTopic: string; | ||
options: ProtocolCreateOptions; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. update changelog to highlight api breaking change |
||
|
||
constructor(public components: LightPushComponents, options?: CreateOptions) { | ||
constructor( | ||
public components: LightPushComponents, | ||
options?: ProtocolCreateOptions | ||
) { | ||
this.multicodec = LightPushCodec; | ||
this.pubSubTopic = options?.pubSubTopic ?? DefaultPubSubTopic; | ||
this.options = options || {}; | ||
} | ||
|
||
async push( | ||
encoder: IEncoder, | ||
message: IMessage, | ||
opts?: ProtocolOptions | ||
): Promise<SendResult> { | ||
const pubSubTopic = opts?.pubSubTopic ? opts.pubSubTopic : this.pubSubTopic; | ||
const { pubSubTopic = DefaultPubSubTopic } = this.options; | ||
|
||
const res = await selectPeerForProtocol( | ||
this.components.peerStore, | ||
[LightPushCodec], | ||
[this.multicodec], | ||
opts?.peerId | ||
); | ||
|
||
|
@@ -152,7 +144,7 @@ class LightPush implements ILightPush { | |
} | ||
|
||
export function wakuLightPush( | ||
init: Partial<CreateOptions> = {} | ||
init: Partial<ProtocolCreateOptions> = {} | ||
): (components: LightPushComponents) => ILightPush { | ||
return (components: LightPushComponents) => new LightPush(components, init); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ import type { | |
IEncoder, | ||
IMessage, | ||
IRelay, | ||
ProtocolCreateOptions, | ||
SendResult, | ||
} from "@waku/interfaces"; | ||
import { IDecodedMessage } from "@waku/interfaces"; | ||
|
@@ -30,22 +31,7 @@ export type Observer<T extends IDecodedMessage> = { | |
callback: Callback<T>; | ||
}; | ||
|
||
export interface RelayCreateOptions extends GossipsubOpts { | ||
/** | ||
* The PubSub Topic to use. Defaults to {@link DefaultPubSubTopic}. | ||
* | ||
* One and only one pubsub topic is used by Waku. This is used by: | ||
* - WakuRelay to receive, route and send messages, | ||
* - WakuLightPush to send messages, | ||
* - WakuStore to retrieve messages. | ||
* | ||
* The usage of the default pubsub topic is recommended. | ||
* See [Waku v2 Topic Usage Recommendations](https://rfc.vac.dev/spec/23/) for details. | ||
* | ||
* @default {@link DefaultPubSubTopic} | ||
*/ | ||
pubSubTopic?: string; | ||
} | ||
export type RelayCreateOptions = ProtocolCreateOptions & GossipsubOpts; | ||
|
||
/** | ||
* Implements the [Waku v2 Relay protocol](https://rfc.vac.dev/spec/11/). | ||
|
@@ -54,7 +40,7 @@ export interface RelayCreateOptions extends GossipsubOpts { | |
* @implements {require('libp2p-interfaces/src/pubsub')} | ||
*/ | ||
class Relay extends GossipSub implements IRelay { | ||
pubSubTopic: string; | ||
options: Partial<RelayCreateOptions>; | ||
defaultDecoder: IDecoder<IDecodedMessage>; | ||
public static multicodec: string = constants.RelayCodecs[0]; | ||
|
||
|
@@ -73,12 +59,13 @@ class Relay extends GossipSub implements IRelay { | |
globalSignaturePolicy: SignaturePolicy.StrictNoSign, | ||
fallbackToFloodsub: false, | ||
}); | ||
|
||
super(components, options); | ||
this.multicodecs = constants.RelayCodecs; | ||
|
||
this.observers = new Map(); | ||
|
||
this.pubSubTopic = options?.pubSubTopic ?? DefaultPubSubTopic; | ||
this.options = options ?? {}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you end up applyuing the default pubsub topic and every usage of Apply the default values in the constructor:
Or same logic in a getter Or create a class that handles the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TypeScript doesn't know if a particular value as been assigned to an object (``Object.assign |
||
|
||
// TODO: User might want to decide what decoder should be used (e.g. for RLN) | ||
this.defaultDecoder = new TopicOnlyDecoder(); | ||
|
@@ -92,20 +79,24 @@ class Relay extends GossipSub implements IRelay { | |
* @returns {void} | ||
*/ | ||
public async start(): Promise<void> { | ||
const { pubSubTopic = DefaultPubSubTopic } = this.options; | ||
await super.start(); | ||
this.subscribe(this.pubSubTopic); | ||
this.subscribe(pubSubTopic); | ||
} | ||
|
||
/** | ||
* Send Waku message. | ||
*/ | ||
public async send(encoder: IEncoder, message: IMessage): Promise<SendResult> { | ||
const { pubSubTopic = DefaultPubSubTopic } = this.options; | ||
|
||
const msg = await encoder.toWire(message); | ||
if (!msg) { | ||
log("Failed to encode message, aborting publish"); | ||
return { recipients: [] }; | ||
} | ||
return this.publish(this.pubSubTopic, msg); | ||
|
||
return this.publish(pubSubTopic, msg); | ||
} | ||
|
||
/** | ||
|
@@ -181,7 +172,8 @@ class Relay extends GossipSub implements IRelay { | |
} | ||
|
||
getMeshPeers(topic?: TopicStr): PeerIdStr[] { | ||
return super.getMeshPeers(topic ?? this.pubSubTopic); | ||
const { pubSubTopic = DefaultPubSubTopic } = this.options; | ||
return super.getMeshPeers(topic ?? pubSubTopic); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,7 +88,8 @@ export async function createLightNode( | |
const store = wakuStore(options); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have not removed the |
||
const lightPush = wakuLightPush(options); | ||
const filter = wakuFilter(options); | ||
const peerExchange = wakuPeerExchange(options); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. odd |
||
const peerExchange = wakuPeerExchange(); | ||
|
||
return new WakuNode( | ||
options ?? {}, | ||
|
@@ -155,7 +156,8 @@ export async function createFullNode( | |
const store = wakuStore(options); | ||
const lightPush = wakuLightPush(options); | ||
const filter = wakuFilter(options); | ||
const peerExchange = wakuPeerExchange(options); | ||
|
||
const peerExchange = wakuPeerExchange(); | ||
|
||
return new WakuNode( | ||
options ?? {}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,8 +17,26 @@ export interface PointToPointProtocol { | |
peers: () => Promise<Peer[]>; | ||
} | ||
|
||
export type ProtocolOptions = { | ||
export type ProtocolCreateOptions = { | ||
/** | ||
* The PubSub Topic to use. Defaults to {@link @waku/core/DefaultPubSubTopic }. | ||
* | ||
* One and only one pubsub topic is used by Waku. This is used by: | ||
* - WakuRelay to receive, route and send messages, | ||
* - WakuLightPush to send messages, | ||
* - WakuStore to retrieve messages. | ||
* | ||
* The usage of the default pubsub topic is recommended. | ||
* See [Waku v2 Topic Usage Recommendations](https://rfc.vac.dev/spec/23/) for details. | ||
* | ||
*/ | ||
pubSubTopic?: string; | ||
}; | ||
|
||
//TODO | ||
// we can probably move `peerId` into `ProtocolCreateOptions` and remove `ProtocolOptions` and pass it in the constructor | ||
// however, filter protocol can use multiple peers, so we need to think about this | ||
Comment on lines
+37
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All protocol can use multiple peers. I don't think Do note that it makes sense to have For now I'd remove this todo and leave it as it is and we can decide later once we ahve more information on sharding and Waku usage. |
||
export type ProtocolOptions = { | ||
/** | ||
* Optionally specify an PeerId for the protocol request. If not included, will use a random peer. | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changelog update needed to highlight breaking change in API.