Add a method to send messages on the network using pubsub (#293)

* Add a method to send messages using pubsub on a topic

* Update version to 0.2.22
This commit is contained in:
prathamesh0 2023-01-11 17:19:21 +05:30 committed by GitHub
parent 55884a8ecd
commit 38daa31029
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 134 additions and 68 deletions

View File

@ -2,7 +2,7 @@
"packages": [
"packages/*"
],
"version": "0.2.21",
"version": "0.2.22",
"npmClient": "yarn",
"useWorkspaces": true,
"command": {

View File

@ -1,6 +1,6 @@
{
"name": "@cerc-io/address-watcher",
"version": "0.2.21",
"version": "0.2.22",
"description": "Address Watcher",
"private": true,
"scripts": {
@ -26,11 +26,11 @@
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
"dependencies": {
"@apollo/client": "^3.3.19",
"@cerc-io/cache": "^0.2.21",
"@cerc-io/ipld-eth-client": "^0.2.21",
"@cerc-io/solidity-mapper": "^0.2.21",
"@cerc-io/tracing-client": "^0.2.21",
"@cerc-io/util": "^0.2.21",
"@cerc-io/cache": "^0.2.22",
"@cerc-io/ipld-eth-client": "^0.2.22",
"@cerc-io/solidity-mapper": "^0.2.22",
"@cerc-io/tracing-client": "^0.2.22",
"@cerc-io/util": "^0.2.22",
"@types/lodash": "^4.14.168",
"debug": "^4.3.1",
"ethers": "^5.4.4",

View File

@ -1,6 +1,6 @@
{
"name": "@cerc-io/cache",
"version": "0.2.21",
"version": "0.2.22",
"description": "Generic object cache",
"main": "dist/index.js",
"scripts": {

View File

@ -1,6 +1,6 @@
{
"name": "@cerc-io/cli",
"version": "0.2.21",
"version": "0.2.22",
"main": "dist/index.js",
"license": "AGPL-3.0",
"scripts": {
@ -11,8 +11,8 @@
"chat": "node dist/chat.js"
},
"dependencies": {
"@cerc-io/peer": "^0.2.21",
"@cerc-io/util": "^0.2.21",
"@cerc-io/peer": "^0.2.22",
"@cerc-io/util": "^0.2.22",
"@ethersproject/providers": "^5.4.4",
"@graphql-tools/utils": "^9.1.1",
"@ipld/dag-cbor": "^8.0.0",

View File

@ -1,6 +1,6 @@
{
"name": "@cerc-io/codegen",
"version": "0.2.21",
"version": "0.2.22",
"description": "Code generator",
"private": true,
"main": "index.js",
@ -20,7 +20,7 @@
},
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
"dependencies": {
"@cerc-io/util": "^0.2.21",
"@cerc-io/util": "^0.2.22",
"@graphql-tools/load-files": "^6.5.2",
"@poanet/solidity-flattener": "https://github.com/vulcanize/solidity-flattener.git",
"@solidity-parser/parser": "^0.13.2",

View File

@ -41,12 +41,12 @@
"dependencies": {
"@apollo/client": "^3.3.19",
"@ethersproject/providers": "^5.4.4",
"@cerc-io/cli": "^0.2.21",
"@cerc-io/ipld-eth-client": "^0.2.21",
"@cerc-io/solidity-mapper": "^0.2.21",
"@cerc-io/util": "^0.2.21",
"@cerc-io/cli": "^0.2.22",
"@cerc-io/ipld-eth-client": "^0.2.22",
"@cerc-io/solidity-mapper": "^0.2.22",
"@cerc-io/util": "^0.2.22",
{{#if (subgraphPath)}}
"@cerc-io/graph-node": "^0.2.21",
"@cerc-io/graph-node": "^0.2.22",
{{/if}}
"apollo-type-bigint": "^0.1.3",
"debug": "^4.3.1",

View File

@ -1,6 +1,6 @@
{
"name": "@cerc-io/eden-watcher",
"version": "0.2.21",
"version": "0.2.22",
"description": "eden-watcher",
"private": true,
"main": "dist/index.js",
@ -38,11 +38,11 @@
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
"dependencies": {
"@apollo/client": "^3.3.19",
"@cerc-io/cli": "^0.2.21",
"@cerc-io/graph-node": "^0.2.21",
"@cerc-io/ipld-eth-client": "^0.2.21",
"@cerc-io/solidity-mapper": "^0.2.21",
"@cerc-io/util": "^0.2.21",
"@cerc-io/cli": "^0.2.22",
"@cerc-io/graph-node": "^0.2.22",
"@cerc-io/ipld-eth-client": "^0.2.22",
"@cerc-io/solidity-mapper": "^0.2.22",
"@cerc-io/util": "^0.2.22",
"@ethersproject/providers": "^5.4.4",
"apollo-type-bigint": "^0.1.3",
"debug": "^4.3.1",

View File

@ -1,6 +1,6 @@
{
"name": "@cerc-io/erc20-watcher",
"version": "0.2.21",
"version": "0.2.22",
"description": "ERC20 Watcher",
"private": true,
"main": "dist/index.js",
@ -42,10 +42,10 @@
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
"dependencies": {
"@apollo/client": "^3.3.19",
"@cerc-io/cli": "^0.2.21",
"@cerc-io/ipld-eth-client": "^0.2.21",
"@cerc-io/solidity-mapper": "^0.2.21",
"@cerc-io/util": "^0.2.21",
"@cerc-io/cli": "^0.2.22",
"@cerc-io/ipld-eth-client": "^0.2.22",
"@cerc-io/solidity-mapper": "^0.2.22",
"@cerc-io/util": "^0.2.22",
"@ethersproject/providers": "^5.4.4",
"apollo-type-bigint": "^0.1.3",
"debug": "^4.3.1",

View File

@ -1,6 +1,6 @@
{
"name": "@cerc-io/erc721-watcher",
"version": "0.2.21",
"version": "0.2.22",
"description": "erc721-watcher",
"private": true,
"main": "dist/index.js",
@ -47,10 +47,10 @@
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
"dependencies": {
"@apollo/client": "^3.3.19",
"@cerc-io/cli": "^0.2.21",
"@cerc-io/ipld-eth-client": "^0.2.21",
"@cerc-io/solidity-mapper": "^0.2.21",
"@cerc-io/util": "^0.2.21",
"@cerc-io/cli": "^0.2.22",
"@cerc-io/ipld-eth-client": "^0.2.22",
"@cerc-io/solidity-mapper": "^0.2.22",
"@cerc-io/util": "^0.2.22",
"@ethersproject/providers": "^5.4.4",
"apollo-type-bigint": "^0.1.3",
"debug": "^4.3.1",

View File

@ -1,10 +1,10 @@
{
"name": "@cerc-io/graph-node",
"version": "0.2.21",
"version": "0.2.22",
"main": "dist/index.js",
"license": "AGPL-3.0",
"devDependencies": {
"@cerc-io/solidity-mapper": "^0.2.21",
"@cerc-io/solidity-mapper": "^0.2.22",
"@ethersproject/providers": "^5.4.4",
"@graphprotocol/graph-ts": "^0.22.0",
"@nomiclabs/hardhat-ethers": "^2.0.2",
@ -50,9 +50,9 @@
"dependencies": {
"@apollo/client": "^3.3.19",
"@cerc-io/assemblyscript": "0.19.10-watcher-ts-0.1.2",
"@cerc-io/cache": "^0.2.21",
"@cerc-io/ipld-eth-client": "^0.2.21",
"@cerc-io/util": "^0.2.21",
"@cerc-io/cache": "^0.2.22",
"@cerc-io/ipld-eth-client": "^0.2.22",
"@cerc-io/util": "^0.2.22",
"@types/json-diff": "^0.5.2",
"bn.js": "^4.11.9",
"debug": "^4.3.1",

View File

@ -1,6 +1,6 @@
{
"name": "@cerc-io/graph-test-watcher",
"version": "0.2.21",
"version": "0.2.22",
"description": "graph-test-watcher",
"private": true,
"main": "dist/index.js",
@ -38,11 +38,11 @@
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
"dependencies": {
"@apollo/client": "^3.3.19",
"@cerc-io/cli": "^0.2.21",
"@cerc-io/graph-node": "^0.2.21",
"@cerc-io/ipld-eth-client": "^0.2.21",
"@cerc-io/solidity-mapper": "^0.2.21",
"@cerc-io/util": "^0.2.21",
"@cerc-io/cli": "^0.2.22",
"@cerc-io/graph-node": "^0.2.22",
"@cerc-io/ipld-eth-client": "^0.2.22",
"@cerc-io/solidity-mapper": "^0.2.22",
"@cerc-io/util": "^0.2.22",
"@ethersproject/providers": "^5.4.4",
"apollo-type-bigint": "^0.1.3",
"debug": "^4.3.1",

View File

@ -1,6 +1,6 @@
{
"name": "@cerc-io/ipld-eth-client",
"version": "0.2.21",
"version": "0.2.22",
"description": "IPLD ETH Client",
"main": "dist/index.js",
"scripts": {
@ -20,7 +20,7 @@
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
"dependencies": {
"@apollo/client": "^3.7.1",
"@cerc-io/cache": "^0.2.21",
"@cerc-io/cache": "^0.2.22",
"cross-fetch": "^3.1.4",
"debug": "^4.3.1",
"ethers": "^5.4.4",

View File

@ -1,6 +1,6 @@
{
"name": "@cerc-io/mobymask-watcher",
"version": "0.2.21",
"version": "0.2.22",
"description": "mobymask-watcher",
"private": true,
"main": "dist/index.js",
@ -37,10 +37,10 @@
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
"dependencies": {
"@apollo/client": "^3.3.19",
"@cerc-io/cli": "^0.2.21",
"@cerc-io/ipld-eth-client": "^0.2.21",
"@cerc-io/solidity-mapper": "^0.2.21",
"@cerc-io/util": "^0.2.21",
"@cerc-io/cli": "^0.2.22",
"@cerc-io/ipld-eth-client": "^0.2.22",
"@cerc-io/solidity-mapper": "^0.2.22",
"@cerc-io/util": "^0.2.22",
"@ethersproject/providers": "^5.4.4",
"apollo-type-bigint": "^0.1.3",
"debug": "^4.3.1",

View File

@ -1,10 +1,10 @@
{
"name": "@cerc-io/peer-test-app",
"version": "0.2.21",
"version": "0.2.22",
"private": true,
"dependencies": {
"@cerc-io/peer": "^0.2.21",
"@cerc-io/react-peer": "^0.2.21",
"@cerc-io/peer": "^0.2.22",
"@cerc-io/react-peer": "^0.2.22",
"@emotion/react": "^11.10.5",
"@emotion/styled": "^11.10.5",
"@mui/material": "^5.11.3",

View File

@ -8,8 +8,13 @@ import { AppBar, Box, CssBaseline, Paper, Table, TableBody, TableCell, TableCont
import './App.css';
import { useForceUpdate } from './hooks/forceUpdate';
const TEST_TOPIC = 'test';
declare global {
interface Window { broadcast: (message: string) => void; }
interface Window {
broadcast: (message: string) => void;
flood: (message: string) => void;
}
}
const theme = createTheme();
@ -33,6 +38,14 @@ function App() {
peer.broadcastMessage(message)
}
const unsubscribeTopic = peer.subscribeTopic(TEST_TOPIC, (data) => {
console.log(`> ${data}`)
})
window.flood = (message: string) => {
peer.floodMessage(TEST_TOPIC, message)
}
peer.node.peerStore.addEventListener('change:multiaddrs', forceUpdate)
peer.node.connectionManager.addEventListener('peer:connect', forceUpdate)
@ -50,6 +63,7 @@ function App() {
return () => {
unsubscribeMessage()
unsubscribeTopic()
peer.node?.peerStore.removeEventListener('change:multiaddrs', forceUpdate)
peer.node?.connectionManager.removeEventListener('peer:connect', forceUpdate)
peer.node?.connectionManager.removeEventListener('peer:disconnect', disconnectHandler)
@ -64,7 +78,7 @@ function App() {
<Typography variant="h6" color="inherit" noWrap>
Peer Test App
</Typography>
</Toolbar>
</Toolbar>
</AppBar>
<main>
<Box

View File

@ -1,6 +1,6 @@
{
"name": "@cerc-io/peer",
"version": "0.2.21",
"version": "0.2.22",
"description": "libp2p module",
"main": "dist/index.js",
"exports": "./dist/index.js",

View File

@ -18,6 +18,7 @@ import { noise } from '@chainsafe/libp2p-noise';
import { mplex } from '@libp2p/mplex';
import type { Stream as P2PStream, Connection } from '@libp2p/interface-connection';
import type { PeerInfo } from '@libp2p/interface-peer-info';
import type { Message } from '@libp2p/interface-pubsub';
import { PeerId } from '@libp2p/interface-peer-id';
import { multiaddr, Multiaddr } from '@multiformats/multiaddr';
import { floodsub } from '@libp2p/floodsub';
@ -36,6 +37,7 @@ export class Peer {
_remotePeerIds: PeerId[] = []
_peerStreamMap: Map<string, Pushable<any>> = new Map()
_messageHandlers: Array<(peerId: PeerId, message: any) => void> = []
_topicHandlers: Map<string, Array<(data: any) => void>> = new Map()
constructor (nodejs?: boolean) {
// Instantiation in nodejs.
@ -145,6 +147,11 @@ export class Peer {
await this._node.handle(CHAT_PROTOCOL, async ({ stream, connection }) => {
this._handleStream(connection.remotePeer, stream);
});
// Listen for pubsub messages
this._node.pubsub.addEventListener('message', (evt) => {
this._handleMessage(evt.detail);
});
}
async close (): Promise<void> {
@ -153,6 +160,7 @@ export class Peer {
this._node.removeEventListener('peer:discovery');
this._node.connectionManager.removeEventListener('peer:connect');
this._node.connectionManager.removeEventListener('peer:disconnect');
this._node.pubsub.removeEventListener('message');
await this._node.unhandle(CHAT_PROTOCOL);
const hangUpPromises = this._remotePeerIds.map(async peerId => this._node?.hangUp(peerId));
@ -165,6 +173,11 @@ export class Peer {
}
}
floodMessage (topic: string, msg: any): void {
assert(this._node);
this._node.pubsub.publish(topic, uint8ArrayFromString(JSON.stringify(msg)));
}
subscribeMessage (handler: (peerId: PeerId, message: any) => void) : () => void {
this._messageHandlers.push(handler);
@ -176,6 +189,37 @@ export class Peer {
return unsubscribe;
}
subscribeTopic (topic: string, handler: (data: any) => void): () => void {
assert(this._node);
// Subscribe node to the topic
this._node.pubsub.subscribe(topic);
// Register provided handler for the topic
if (!this._topicHandlers.has(topic)) {
this._topicHandlers.set(topic, [handler]);
} else {
this._topicHandlers.get(topic)?.push(handler);
}
// Create a unsubscribe callback
const unsubscribe = () => {
// Remove handler from registered handlers for the topic
const filteredTopicHandlers = this._topicHandlers.get(topic)
?.filter(registeredHandler => registeredHandler !== handler);
if (filteredTopicHandlers?.length) {
this._topicHandlers.set(topic, filteredTopicHandlers);
} else {
// Remove topic from map and unsubscribe node from the topic if no handlers left
this._topicHandlers.delete(topic);
this._node?.pubsub.unsubscribe(topic);
}
};
return unsubscribe;
}
_handleDiscovery (peer: PeerInfo): void {
// Check connected peers as they are discovered repeatedly.
if (!this._remotePeerIds.some(remotePeerId => remotePeerId.toString() === peer.id.toString())) {
@ -273,4 +317,12 @@ export class Peer {
// TODO: Check if stream already exists for peer id
this._peerStreamMap.set(peerId.toString(), messageStream);
}
_handleMessage (msg: Message): void {
// Send msg data to registered topic handlers
this._topicHandlers.get(msg.topic)?.forEach(handler => {
const dataObj = JSON.parse(uint8ArrayToString(msg.data));
handler(dataObj);
});
}
}

View File

@ -1,6 +1,6 @@
{
"name": "@cerc-io/react-peer",
"version": "0.2.21",
"version": "0.2.22",
"description": "react-peer React component",
"main": "lib/index.js",
"files": [
@ -18,7 +18,7 @@
"test:watch": "nwb test-react --server"
},
"dependencies": {
"@cerc-io/peer": "^0.2.21"
"@cerc-io/peer": "^0.2.22"
},
"peerDependencies": {
"react": "^18.2.0"

View File

@ -1,10 +1,10 @@
{
"name": "@cerc-io/solidity-mapper",
"version": "0.2.21",
"version": "0.2.22",
"main": "dist/index.js",
"license": "AGPL-3.0",
"devDependencies": {
"@cerc-io/ipld-eth-client": "^0.2.21",
"@cerc-io/ipld-eth-client": "^0.2.22",
"@ethersproject/abi": "^5.3.0",
"@nomiclabs/hardhat-ethers": "^2.0.2",
"@nomiclabs/hardhat-waffle": "^2.0.1",

View File

@ -1,6 +1,6 @@
{
"name": "@cerc-io/test",
"version": "0.2.21",
"version": "0.2.22",
"main": "dist/index.js",
"license": "AGPL-3.0",
"private": true,

View File

@ -1,6 +1,6 @@
{
"name": "@cerc-io/tracing-client",
"version": "0.2.21",
"version": "0.2.22",
"description": "ETH VM tracing client",
"private": true,
"main": "dist/index.js",

View File

@ -1,11 +1,11 @@
{
"name": "@cerc-io/util",
"version": "0.2.21",
"version": "0.2.22",
"main": "dist/index.js",
"license": "AGPL-3.0",
"dependencies": {
"@apollo/utils.keyvaluecache": "^1.0.1",
"@cerc-io/solidity-mapper": "^0.2.21",
"@cerc-io/solidity-mapper": "^0.2.22",
"@ethersproject/providers": "^5.4.4",
"@graphql-tools/schema": "^9.0.10",
"@graphql-tools/utils": "^9.1.1",
@ -37,8 +37,8 @@
"yargs": "^17.0.1"
},
"devDependencies": {
"@cerc-io/cache": "^0.2.21",
"@cerc-io/ipld-eth-client": "^0.2.21",
"@cerc-io/cache": "^0.2.22",
"@cerc-io/ipld-eth-client": "^0.2.22",
"@nomiclabs/hardhat-waffle": "^2.0.1",
"@types/express": "^4.17.14",
"@types/fs-extra": "^9.0.11",