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

View File

@ -1,6 +1,15 @@
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 { MessageCache } from 'libp2p-gossipsub/src/message-cache';
import {
PeerScoreParams,
PeerScoreThresholds,
} from 'libp2p-gossipsub/src/score';
import {
createGossipRpc,
messageIdToString,
@ -19,26 +28,50 @@ import { RelayHeartbeat } from './relay_heartbeat';
export * from './constants';
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 {
heartbeat: RelayHeartbeat;
/**
*
* @param libp2p: Libp2p
* @param {Libp2p} libp2p
* @param {Partial<GossipOptions>} [options]
*/
constructor(libp2p: Libp2p) {
super(libp2p, {
emitSelf: false,
// Ensure that no signature is expected in the messages.
constructor(libp2p: Libp2p, options?: Partial<GossipOptions>) {
super(
libp2p,
Object.assign(options, {
// Ensure that no signature is included nor expected in the messages.
globalSignaturePolicy: SignaturePolicy.StrictNoSign,
});
})
);
this.heartbeat = new RelayHeartbeat(this);
const multicodecs = [constants.RelayCodec];
// This is the downside of using `libp2p-gossipsub` instead of
// implementing WakuRelay from scratch.
Object.assign(this, { multicodecs });
}

View File

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