Self emit to see own messages

This commit is contained in:
Franck Royer 2021-04-22 14:47:43 +10:00
parent 9937652105
commit 69a3f73ee6
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
3 changed files with 67 additions and 29 deletions

View File

@ -1,4 +1,4 @@
import Libp2p from 'libp2p'; import Libp2p, { Libp2pConfig, Libp2pModules, Libp2pOptions } from 'libp2p';
import Mplex from 'libp2p-mplex'; import Mplex from 'libp2p-mplex';
import { bytes } from 'libp2p-noise/dist/src/@types/basic'; import { bytes } from 'libp2p-noise/dist/src/@types/basic';
import { Noise } from 'libp2p-noise/dist/src/noise'; import { Noise } from 'libp2p-noise/dist/src/noise';
@ -12,16 +12,14 @@ import { StoreCodec, WakuStore } from './waku_store';
const transportKey = Websockets.prototype[Symbol.toStringTag]; const transportKey = Websockets.prototype[Symbol.toStringTag];
export interface CreateOptions { export type CreateOptions =
listenAddresses: string[]; | {
listenAddresses: string[] | undefined;
staticNoiseKey: bytes | undefined; staticNoiseKey: bytes | undefined;
modules: { modules: Partial<Libp2pModules>;
transport: import('libp2p-interfaces/src/transport/types').TransportFactory< config: Partial<Libp2pConfig>;
any, }
any | (Libp2pOptions & import('libp2p').CreateOptions);
>[];
};
}
export default class Waku { export default class Waku {
public libp2p: Libp2p; public libp2p: Libp2p;
@ -52,6 +50,19 @@ export default class Waku {
options options
); );
opts.config = Object.assign(
{
transport: {
[transportKey]: {
filter: filters.all,
},
},
},
options.config
);
opts.modules = Object.assign({}, options.modules);
let transport = [Websockets]; let transport = [Websockets];
if (opts.modules?.transport) { if (opts.modules?.transport) {
transport = transport.concat(opts.modules?.transport); transport = transport.concat(opts.modules?.transport);
@ -73,13 +84,7 @@ export default class Waku {
// @ts-ignore: Type needs update // @ts-ignore: Type needs update
pubsub: WakuRelay, pubsub: WakuRelay,
}, },
config: { config: opts.config,
transport: {
[transportKey]: {
filter: filters.all,
},
},
},
}); });
const wakuStore = new WakuStore(libp2p); const wakuStore = new WakuStore(libp2p);

View File

@ -1,6 +1,15 @@
import Gossipsub from 'libp2p-gossipsub'; import Gossipsub from 'libp2p-gossipsub';
import { Libp2p } from 'libp2p-gossipsub/src/interfaces'; import {
AddrInfo,
Libp2p,
MessageIdFunction,
} from 'libp2p-gossipsub/src/interfaces';
import { ControlPrune, PeerInfo } from 'libp2p-gossipsub/src/message'; import { ControlPrune, PeerInfo } from 'libp2p-gossipsub/src/message';
import { MessageCache } from 'libp2p-gossipsub/src/message-cache';
import {
PeerScoreParams,
PeerScoreThresholds,
} from 'libp2p-gossipsub/src/score';
import { import {
createGossipRpc, createGossipRpc,
messageIdToString, messageIdToString,
@ -19,26 +28,50 @@ import { RelayHeartbeat } from './relay_heartbeat';
export * from './constants'; export * from './constants';
export * from './relay_heartbeat'; export * from './relay_heartbeat';
/**
* See GossipOptions from libp2p-gossipsub
*/
interface GossipOptions {
emitSelf: boolean;
gossipIncoming: boolean;
fallbackToFloodsub: boolean;
floodPublish: boolean;
doPX: boolean;
msgIdFn: MessageIdFunction;
messageCache: MessageCache;
globalSignaturePolicy: string;
scoreParams: Partial<PeerScoreParams>;
scoreThresholds: Partial<PeerScoreThresholds>;
directPeers: AddrInfo[];
D: number;
Dlo: number;
Dhi: number;
Dscore: number;
Dout: number;
Dlazy: number;
}
export class WakuRelay extends Gossipsub { export class WakuRelay extends Gossipsub {
heartbeat: RelayHeartbeat; heartbeat: RelayHeartbeat;
/** /**
* *
* @param libp2p: Libp2p * @param {Libp2p} libp2p
* @param {Partial<GossipOptions>} [options]
*/ */
constructor(libp2p: Libp2p) { constructor(libp2p: Libp2p, options?: Partial<GossipOptions>) {
super(libp2p, { super(
emitSelf: false, libp2p,
// Ensure that no signature is expected in the messages. Object.assign(options, {
// Ensure that no signature is included nor expected in the messages.
globalSignaturePolicy: SignaturePolicy.StrictNoSign, globalSignaturePolicy: SignaturePolicy.StrictNoSign,
}); })
);
this.heartbeat = new RelayHeartbeat(this); this.heartbeat = new RelayHeartbeat(this);
const multicodecs = [constants.RelayCodec]; const multicodecs = [constants.RelayCodec];
// This is the downside of using `libp2p-gossipsub` instead of
// implementing WakuRelay from scratch.
Object.assign(this, { multicodecs }); Object.assign(this, { multicodecs });
} }

View File

@ -24,7 +24,7 @@ export default function App() {
useEffect(() => { useEffect(() => {
async function initWaku() { async function initWaku() {
try { try {
const waku = await Waku.create({}); const waku = await Waku.create({ config: { pubsub: { enabled: true, emitSelf: true } } });
setState(({ messages }) => { setState(({ messages }) => {
return { waku, messages }; return { waku, messages };