From 38daa31029d972efa582c71c901de654e1ec7367 Mon Sep 17 00:00:00 2001 From: prathamesh0 <42446521+prathamesh0@users.noreply.github.com> Date: Wed, 11 Jan 2023 17:19:21 +0530 Subject: [PATCH] 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 --- lerna.json | 2 +- packages/address-watcher/package.json | 12 ++--- packages/cache/package.json | 2 +- packages/cli/package.json | 6 +-- packages/codegen/package.json | 4 +- .../src/templates/package-template.handlebars | 10 ++-- packages/eden-watcher/package.json | 12 ++--- packages/erc20-watcher/package.json | 10 ++-- packages/erc721-watcher/package.json | 10 ++-- packages/graph-node/package.json | 10 ++-- packages/graph-test-watcher/package.json | 12 ++--- packages/ipld-eth-client/package.json | 4 +- packages/mobymask-watcher/package.json | 10 ++-- packages/peer-test-app/package.json | 6 +-- packages/peer-test-app/src/App.tsx | 18 ++++++- packages/peer/package.json | 2 +- packages/peer/src/index.ts | 52 +++++++++++++++++++ packages/react-peer/package.json | 4 +- packages/solidity-mapper/package.json | 4 +- packages/test/package.json | 2 +- packages/tracing-client/package.json | 2 +- packages/util/package.json | 8 +-- 22 files changed, 134 insertions(+), 68 deletions(-) diff --git a/lerna.json b/lerna.json index ab3f2940..79f3cde1 100644 --- a/lerna.json +++ b/lerna.json @@ -2,7 +2,7 @@ "packages": [ "packages/*" ], - "version": "0.2.21", + "version": "0.2.22", "npmClient": "yarn", "useWorkspaces": true, "command": { diff --git a/packages/address-watcher/package.json b/packages/address-watcher/package.json index 9331ef36..b0ef0f23 100644 --- a/packages/address-watcher/package.json +++ b/packages/address-watcher/package.json @@ -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", diff --git a/packages/cache/package.json b/packages/cache/package.json index 64745d46..02d07c2a 100644 --- a/packages/cache/package.json +++ b/packages/cache/package.json @@ -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": { diff --git a/packages/cli/package.json b/packages/cli/package.json index bb6c5d7e..974ac523 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -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", diff --git a/packages/codegen/package.json b/packages/codegen/package.json index 094ab04b..2756b2ae 100644 --- a/packages/codegen/package.json +++ b/packages/codegen/package.json @@ -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", diff --git a/packages/codegen/src/templates/package-template.handlebars b/packages/codegen/src/templates/package-template.handlebars index 30a108be..21ca3701 100644 --- a/packages/codegen/src/templates/package-template.handlebars +++ b/packages/codegen/src/templates/package-template.handlebars @@ -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", diff --git a/packages/eden-watcher/package.json b/packages/eden-watcher/package.json index c54c6598..9db88d67 100644 --- a/packages/eden-watcher/package.json +++ b/packages/eden-watcher/package.json @@ -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", diff --git a/packages/erc20-watcher/package.json b/packages/erc20-watcher/package.json index 27aa84dc..bc9dba5a 100644 --- a/packages/erc20-watcher/package.json +++ b/packages/erc20-watcher/package.json @@ -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", diff --git a/packages/erc721-watcher/package.json b/packages/erc721-watcher/package.json index 7d7e2221..59e2dda8 100644 --- a/packages/erc721-watcher/package.json +++ b/packages/erc721-watcher/package.json @@ -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", diff --git a/packages/graph-node/package.json b/packages/graph-node/package.json index 5a0f9dde..22e672d6 100644 --- a/packages/graph-node/package.json +++ b/packages/graph-node/package.json @@ -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", diff --git a/packages/graph-test-watcher/package.json b/packages/graph-test-watcher/package.json index 0ae9cac5..eb6aa901 100644 --- a/packages/graph-test-watcher/package.json +++ b/packages/graph-test-watcher/package.json @@ -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", diff --git a/packages/ipld-eth-client/package.json b/packages/ipld-eth-client/package.json index ddb6f099..dac669d2 100644 --- a/packages/ipld-eth-client/package.json +++ b/packages/ipld-eth-client/package.json @@ -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", diff --git a/packages/mobymask-watcher/package.json b/packages/mobymask-watcher/package.json index d40e0f46..47d1e8fe 100644 --- a/packages/mobymask-watcher/package.json +++ b/packages/mobymask-watcher/package.json @@ -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", diff --git a/packages/peer-test-app/package.json b/packages/peer-test-app/package.json index 51e4b1f4..a119b335 100644 --- a/packages/peer-test-app/package.json +++ b/packages/peer-test-app/package.json @@ -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", diff --git a/packages/peer-test-app/src/App.tsx b/packages/peer-test-app/src/App.tsx index d8460c39..45ce91f8 100644 --- a/packages/peer-test-app/src/App.tsx +++ b/packages/peer-test-app/src/App.tsx @@ -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() { Peer Test App - +
> = new Map() _messageHandlers: Array<(peerId: PeerId, message: any) => void> = [] + _topicHandlers: Map 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 { @@ -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); + }); + } } diff --git a/packages/react-peer/package.json b/packages/react-peer/package.json index 8aa7c158..31b86c82 100644 --- a/packages/react-peer/package.json +++ b/packages/react-peer/package.json @@ -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" diff --git a/packages/solidity-mapper/package.json b/packages/solidity-mapper/package.json index 76a2b8e6..675eab0b 100644 --- a/packages/solidity-mapper/package.json +++ b/packages/solidity-mapper/package.json @@ -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", diff --git a/packages/test/package.json b/packages/test/package.json index f0fdfa63..3ddb2a98 100644 --- a/packages/test/package.json +++ b/packages/test/package.json @@ -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, diff --git a/packages/tracing-client/package.json b/packages/tracing-client/package.json index c71f2cd7..39f891e7 100644 --- a/packages/tracing-client/package.json +++ b/packages/tracing-client/package.json @@ -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", diff --git a/packages/util/package.json b/packages/util/package.json index c1d65c3b..39497efc 100644 --- a/packages/util/package.json +++ b/packages/util/package.json @@ -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",