From d094524cf79a586962504c7b0c6e61439f411af2 Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Mon, 22 Jan 2024 21:05:46 +0530 Subject: [PATCH 01/23] initialise the new package --- package-lock.json | 37 +++++++++ package.json | 1 + .../local-storage-discovery/.eslintrc.cjs | 6 ++ .../.mocha.reporters.json | 6 ++ packages/local-storage-discovery/.mocharc.cjs | 26 +++++++ packages/local-storage-discovery/CHANGELOG.md | 0 .../local-storage-discovery/karma.conf.cjs | 3 + packages/local-storage-discovery/package.json | 78 +++++++++++++++++++ .../local-storage-discovery/rollup.config.js | 24 ++++++ packages/local-storage-discovery/src/index.ts | 3 + .../local-storage-discovery/tsconfig.dev.json | 3 + .../local-storage-discovery/tsconfig.json | 10 +++ .../tsconfig.karma.json | 7 ++ packages/local-storage-discovery/typedoc.json | 4 + 14 files changed, 208 insertions(+) create mode 100644 packages/local-storage-discovery/.eslintrc.cjs create mode 100644 packages/local-storage-discovery/.mocha.reporters.json create mode 100644 packages/local-storage-discovery/.mocharc.cjs create mode 100644 packages/local-storage-discovery/CHANGELOG.md create mode 100644 packages/local-storage-discovery/karma.conf.cjs create mode 100644 packages/local-storage-discovery/package.json create mode 100644 packages/local-storage-discovery/rollup.config.js create mode 100644 packages/local-storage-discovery/src/index.ts create mode 100644 packages/local-storage-discovery/tsconfig.dev.json create mode 100644 packages/local-storage-discovery/tsconfig.json create mode 100644 packages/local-storage-discovery/tsconfig.karma.json create mode 100644 packages/local-storage-discovery/typedoc.json diff --git a/package-lock.json b/package-lock.json index 30fa5c4078..523e523a62 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,7 @@ "packages/message-hash", "packages/peer-exchange", "packages/dns-discovery", + "packages/local-storage-discovery", "packages/message-encryption", "packages/sdk", "packages/tests", @@ -5516,6 +5517,10 @@ "resolved": "packages/interfaces", "link": true }, + "node_modules/@waku/local-storage-discovery": { + "resolved": "packages/local-storage-discovery", + "link": true + }, "node_modules/@waku/message-encryption": { "resolved": "packages/message-encryption", "link": true @@ -27761,6 +27766,24 @@ "node": ">=18" } }, + "packages/local-storage-discovery": { + "version": "0.0.1", + "license": "MIT OR Apache-2.0", + "devDependencies": { + "@rollup/plugin-commonjs": "^25.0.7", + "@rollup/plugin-json": "^6.0.0", + "@rollup/plugin-node-resolve": "^15.2.3", + "@types/chai": "^4.3.11", + "chai": "^4.3.10", + "cspell": "^7.3.2", + "mocha": "^10.2.0", + "npm-run-all": "^4.1.5", + "rollup": "^4.9.5" + }, + "engines": { + "node": ">=18" + } + }, "packages/message-encryption": { "name": "@waku/message-encryption", "version": "0.0.24", @@ -31940,6 +31963,20 @@ "npm-run-all": "^4.1.5" } }, + "@waku/local-storage-discovery": { + "version": "file:packages/local-storage-discovery", + "requires": { + "@rollup/plugin-commonjs": "^25.0.7", + "@rollup/plugin-json": "^6.0.0", + "@rollup/plugin-node-resolve": "^15.2.3", + "@types/chai": "^4.3.11", + "chai": "^4.3.10", + "cspell": "^7.3.2", + "mocha": "^10.2.0", + "npm-run-all": "^4.1.5", + "rollup": "^4.9.5" + } + }, "@waku/message-encryption": { "version": "file:packages/message-encryption", "requires": { diff --git a/package.json b/package.json index fb280910c7..5335816d7f 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "packages/message-hash", "packages/peer-exchange", "packages/dns-discovery", + "packages/local-storage-discovery", "packages/message-encryption", "packages/sdk", "packages/tests", diff --git a/packages/local-storage-discovery/.eslintrc.cjs b/packages/local-storage-discovery/.eslintrc.cjs new file mode 100644 index 0000000000..2299ed822e --- /dev/null +++ b/packages/local-storage-discovery/.eslintrc.cjs @@ -0,0 +1,6 @@ +module.exports = { + parserOptions: { + tsconfigRootDir: __dirname, + project: "./tsconfig.dev.json" + } +}; diff --git a/packages/local-storage-discovery/.mocha.reporters.json b/packages/local-storage-discovery/.mocha.reporters.json new file mode 100644 index 0000000000..8c00e441fc --- /dev/null +++ b/packages/local-storage-discovery/.mocha.reporters.json @@ -0,0 +1,6 @@ +{ + "reporterEnabled": "spec, allure-mocha", + "allureMochaReporter": { + "outputDir": "allure-results" + } +} diff --git a/packages/local-storage-discovery/.mocharc.cjs b/packages/local-storage-discovery/.mocharc.cjs new file mode 100644 index 0000000000..423c0517bd --- /dev/null +++ b/packages/local-storage-discovery/.mocharc.cjs @@ -0,0 +1,26 @@ +const config = { + extension: ['ts'], + spec: 'src/**/*.spec.ts', + require: ['ts-node/register', 'isomorphic-fetch'], + loader: 'ts-node/esm', + 'node-option': [ + 'experimental-specifier-resolution=node', + 'loader=ts-node/esm' + ], + exit: true +}; + +if (process.env.CI) { + console.log("Running tests in parallel"); + config.parallel = true; + config.jobs = 6; + console.log("Activating allure reporting"); + config.reporter = 'mocha-multi-reporters'; + config.reporterOptions = { + configFile: '.mocha.reporters.json' + }; +} else { + console.log("Running tests serially. To enable parallel execution update mocha config"); +} + +module.exports = config; diff --git a/packages/local-storage-discovery/CHANGELOG.md b/packages/local-storage-discovery/CHANGELOG.md new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/local-storage-discovery/karma.conf.cjs b/packages/local-storage-discovery/karma.conf.cjs new file mode 100644 index 0000000000..1acbc3dd2a --- /dev/null +++ b/packages/local-storage-discovery/karma.conf.cjs @@ -0,0 +1,3 @@ +const config = require("../../karma.conf.cjs"); + +module.exports = config; diff --git a/packages/local-storage-discovery/package.json b/packages/local-storage-discovery/package.json new file mode 100644 index 0000000000..56735dca51 --- /dev/null +++ b/packages/local-storage-discovery/package.json @@ -0,0 +1,78 @@ +{ + "name": "@waku/local-storage-discovery", + "version": "0.0.1", + "description": "Local Storage Discovery acts as a way to fetch (healthy) peers that you have previously successfully connected to, and act as a faster way to bootstrap your way into the network.", + "types": "./dist/index.d.ts", + "module": "./dist/index.js", + "exports": { + ".": { + "types": "./dist/index.d.ts", + "import": "./dist/index.js" + } + }, + "type": "module", + "author": "Waku Team", + "homepage": "https://github.com/waku-org/js-waku/tree/master/packages/local-storage#readme", + "repository": { + "type": "git", + "url": "https://github.com/waku-org/js-waku.git" + }, + "bugs": { + "url": "https://github.com/waku-org/js-waku/issues" + }, + "license": "MIT OR Apache-2.0", + "keywords": [ + "waku", + "decentralized", + "secure", + "communication", + "web3", + "ethereum", + "dapps", + "privacy" + ], + "scripts": { + "build": "run-s build:**", + "build:esm": "tsc", + "build:bundle": "rollup --config rollup.config.js", + "fix": "run-s fix:*", + "fix:lint": "eslint src *.js --fix", + "check": "run-s check:*", + "check:lint": "eslint src --ext .ts", + "check:spelling": "cspell \"{README.md,src/**/*.ts}\"", + "check:tsc": "tsc -p tsconfig.dev.json", + "prepublish": "npm run build", + "reset-hard": "git clean -dfx -e .idea && git reset --hard && npm i && npm run build", + "test": "NODE_ENV=test run-s test:*", + "test:node": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json mocha", + "test:browser": "NODE_ENV=test karma start karma.conf.cjs" + }, + "engines": { + "node": ">=18" + }, + "dependencies": { + + }, + "devDependencies": { + "@rollup/plugin-commonjs": "^25.0.7", + "@rollup/plugin-json": "^6.0.0", + "@rollup/plugin-node-resolve": "^15.2.3", + "@types/chai": "^4.3.11", + "chai": "^4.3.10", + "@waku/build-utils": "*", + "cspell": "^7.3.2", + "mocha": "^10.2.0", + "npm-run-all": "^4.1.5", + "rollup": "^4.9.5" + }, + "files": [ + "dist", + "bundle", + "src/**/*.ts", + "!**/*.spec.*", + "!**/*.json", + "CHANGELOG.md", + "LICENSE", + "README.md" + ] +} diff --git a/packages/local-storage-discovery/rollup.config.js b/packages/local-storage-discovery/rollup.config.js new file mode 100644 index 0000000000..4d0757b1c9 --- /dev/null +++ b/packages/local-storage-discovery/rollup.config.js @@ -0,0 +1,24 @@ +import commonjs from "@rollup/plugin-commonjs"; +import json from "@rollup/plugin-json"; +import { nodeResolve } from "@rollup/plugin-node-resolve"; +import { extractExports } from "@waku/build-utils"; + +import * as packageJson from "./package.json" assert { type: "json" }; + +const input = extractExports(packageJson); + +export default { + input, + output: { + dir: "bundle", + format: "esm" + }, + plugins: [ + commonjs(), + json(), + nodeResolve({ + browser: true, + preferBuiltins: false + }) + ] +}; diff --git a/packages/local-storage-discovery/src/index.ts b/packages/local-storage-discovery/src/index.ts new file mode 100644 index 0000000000..19c106ef3a --- /dev/null +++ b/packages/local-storage-discovery/src/index.ts @@ -0,0 +1,3 @@ +class LocalStorageDiscovery {} + +export default LocalStorageDiscovery; diff --git a/packages/local-storage-discovery/tsconfig.dev.json b/packages/local-storage-discovery/tsconfig.dev.json new file mode 100644 index 0000000000..4f7c34af3c --- /dev/null +++ b/packages/local-storage-discovery/tsconfig.dev.json @@ -0,0 +1,3 @@ +{ + "extends": "../../tsconfig.dev" +} diff --git a/packages/local-storage-discovery/tsconfig.json b/packages/local-storage-discovery/tsconfig.json new file mode 100644 index 0000000000..eebbc51585 --- /dev/null +++ b/packages/local-storage-discovery/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../../tsconfig", + "compilerOptions": { + "outDir": "dist/", + "rootDir": "src", + "tsBuildInfoFile": "dist/.tsbuildinfo" + }, + "include": ["src"], + "exclude": ["src/**/*.spec.ts", "src/test_utils"] +} diff --git a/packages/local-storage-discovery/tsconfig.karma.json b/packages/local-storage-discovery/tsconfig.karma.json new file mode 100644 index 0000000000..eb6e3bcd34 --- /dev/null +++ b/packages/local-storage-discovery/tsconfig.karma.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig", + "compilerOptions": { + "module": "esnext" + }, + "exclude": [] +} diff --git a/packages/local-storage-discovery/typedoc.json b/packages/local-storage-discovery/typedoc.json new file mode 100644 index 0000000000..00aa3dc064 --- /dev/null +++ b/packages/local-storage-discovery/typedoc.json @@ -0,0 +1,4 @@ +{ + "extends": ["../../typedoc.base.json"], + "entryPoints": ["src/index.ts"] +} \ No newline at end of file From ab5a2564c40334b9871b0329755cd6c8530f43b5 Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Wed, 24 Jan 2024 17:37:22 +0530 Subject: [PATCH 02/23] feat: implement LocalStorageDiscovery --- .vscode/settings.json | 2 +- package-lock.json | 11 ++ packages/interfaces/src/connection_manager.ts | 3 +- packages/local-storage-discovery/package.json | 6 +- packages/local-storage-discovery/src/index.ts | 155 +++++++++++++++++- 5 files changed, 171 insertions(+), 6 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index a93336e107..d2c3a0c90f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,7 +3,7 @@ "cSpell.enabled": true, "editor.defaultFormatter": "dbaeumer.vscode-eslint", "editor.codeActionsOnSave": { - "source.fixAll.eslint": true + "source.fixAll.eslint": "explicit" }, "editor.formatOnSave": false, // Disable general format on save "typescript.tsdk": "node_modules/typescript/lib", diff --git a/package-lock.json b/package-lock.json index 523e523a62..195e8dd66f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27767,13 +27767,20 @@ } }, "packages/local-storage-discovery": { + "name": "@waku/local-storage-discovery", "version": "0.0.1", "license": "MIT OR Apache-2.0", + "dependencies": { + "@libp2p/interface": "^1.1.2", + "@waku/interfaces": "^0.0.21", + "@waku/utils": "^0.0.14" + }, "devDependencies": { "@rollup/plugin-commonjs": "^25.0.7", "@rollup/plugin-json": "^6.0.0", "@rollup/plugin-node-resolve": "^15.2.3", "@types/chai": "^4.3.11", + "@waku/build-utils": "*", "chai": "^4.3.10", "cspell": "^7.3.2", "mocha": "^10.2.0", @@ -31966,10 +31973,14 @@ "@waku/local-storage-discovery": { "version": "file:packages/local-storage-discovery", "requires": { + "@libp2p/interface": "^1.1.2", "@rollup/plugin-commonjs": "^25.0.7", "@rollup/plugin-json": "^6.0.0", "@rollup/plugin-node-resolve": "^15.2.3", "@types/chai": "^4.3.11", + "@waku/build-utils": "*", + "@waku/interfaces": "^0.0.21", + "@waku/utils": "^0.0.14", "chai": "^4.3.10", "cspell": "^7.3.2", "mocha": "^10.2.0", diff --git a/packages/interfaces/src/connection_manager.ts b/packages/interfaces/src/connection_manager.ts index 46a3c4224d..0a62b8e0ef 100644 --- a/packages/interfaces/src/connection_manager.ts +++ b/packages/interfaces/src/connection_manager.ts @@ -2,7 +2,8 @@ import type { Peer, PeerId, TypedEventEmitter } from "@libp2p/interface"; export enum Tags { BOOTSTRAP = "bootstrap", - PEER_EXCHANGE = "peer-exchange" + PEER_EXCHANGE = "peer-exchange", + LOCAL_STORAGE = "local-storage" } export interface ConnectionManagerOptions { diff --git a/packages/local-storage-discovery/package.json b/packages/local-storage-discovery/package.json index 56735dca51..6448783513 100644 --- a/packages/local-storage-discovery/package.json +++ b/packages/local-storage-discovery/package.json @@ -51,15 +51,17 @@ "node": ">=18" }, "dependencies": { - + "@libp2p/interface": "^1.1.2", + "@waku/interfaces": "^0.0.21", + "@waku/utils": "^0.0.14" }, "devDependencies": { "@rollup/plugin-commonjs": "^25.0.7", "@rollup/plugin-json": "^6.0.0", "@rollup/plugin-node-resolve": "^15.2.3", "@types/chai": "^4.3.11", - "chai": "^4.3.10", "@waku/build-utils": "*", + "chai": "^4.3.10", "cspell": "^7.3.2", "mocha": "^10.2.0", "npm-run-all": "^4.1.5", diff --git a/packages/local-storage-discovery/src/index.ts b/packages/local-storage-discovery/src/index.ts index 19c106ef3a..033a318f87 100644 --- a/packages/local-storage-discovery/src/index.ts +++ b/packages/local-storage-discovery/src/index.ts @@ -1,3 +1,154 @@ -class LocalStorageDiscovery {} +import { TypedEventEmitter } from "@libp2p/interface"; +import type { + PeerDiscovery, + PeerDiscoveryEvents, + PeerInfo, + PeerUpdate, + Startable +} from "@libp2p/interface"; +import { createFromJSON } from "@libp2p/peer-id-factory"; +import { multiaddr } from "@multiformats/multiaddr"; +import { Libp2pComponents, Tags } from "@waku/interfaces"; +import { Logger } from "@waku/utils"; -export default LocalStorageDiscovery; +const log = new Logger("peer-exchange-discovery"); + +type LocalStoragePeerInfo = { + id: string; + address: string; +}; + +type LocalStorageDiscoveryOptions = { + tagValue?: number; + tagTTL?: number; +}; + +export const DEFAULT_PEER_EXCHANGE_TAG_NAME = Tags.PEER_EXCHANGE; +const DEFAULT_PEER_EXCHANGE_TAG_VALUE = 50; +const DEFAULT_PEER_EXCHANGE_TAG_TTL = 100_000_000; + +class LocalStorageDiscovery + extends TypedEventEmitter + implements PeerDiscovery, Startable +{ + private isStarted: boolean; + + constructor( + private readonly components: Libp2pComponents, + private readonly options?: LocalStorageDiscoveryOptions + ) { + super(); + this.isStarted = false; + } + get [Symbol.toStringTag](): string { + return "@waku/local-storage-discovery"; + } + + async start(): Promise { + if (this.isStarted) { + return; + } + + log.info("Starting Local Storage Discovery"); + + // only listen to healthy connected peers that have identified themselves + this.components.events.addEventListener("peer:update", this.handleNewPeers); + + // get all peers from local storage + const localStoragePeers = this.getPeersFromLocalStorage(); + + // update the tags of the peers in the peer store & dispatch them + for (const { id: idStr, address } of localStoragePeers) { + const peerId = await createFromJSON({ + id: idStr + }); + + await this.components.peerStore.save(peerId, { + tags: { + [DEFAULT_PEER_EXCHANGE_TAG_NAME]: { + value: this.options?.tagValue ?? DEFAULT_PEER_EXCHANGE_TAG_VALUE, + ttl: this.options?.tagTTL ?? DEFAULT_PEER_EXCHANGE_TAG_TTL + } + } + }); + + this.dispatchEvent( + new CustomEvent("peer", { + detail: { + id: peerId, + multiaddrs: [multiaddr(address)] + } + }) + ); + } + + log.info(`Discovered ${localStoragePeers.length} peers`); + } + + stop(): void | Promise { + if (!this.isStarted) return; + log.info("Stopping Local Storage Discovery"); + this.components.events.removeEventListener( + "peer:update", + this.handleNewPeers + ); + this.isStarted = false; + } + + handleNewPeers = (event: CustomEvent): void => { + const { peer } = event.detail; + + // Extract the websocket ma + const websocketMultiaddr = peer.addresses.find((addr) => + addr.toString().includes("ws" || "wss") + ); + if (!websocketMultiaddr) return; + + // Retrieve peers from local storage + const storedPeersData = localStorage.getItem("waku:peers"); + const localStoragePeers = ( + storedPeersData ? JSON.parse(storedPeersData) : [] + ) as LocalStoragePeerInfo[]; + + // Find if the peer already exists + const existingPeerIndex = localStoragePeers.findIndex( + (_peer) => _peer.id === peer.id.toString() + ); + + if (existingPeerIndex >= 0) { + // Update existing peer's address + localStoragePeers[existingPeerIndex].address = + websocketMultiaddr.toString(); + } else { + // Add new peer + localStoragePeers.push({ + id: peer.id.toString(), + address: websocketMultiaddr.toString() + }); + } + + this.setPeersInLocalStorage(localStoragePeers); + }; + + private getPeersFromLocalStorage(): LocalStoragePeerInfo[] { + const storedPeersData = localStorage.getItem("waku:peers"); + const localStoragePeers = ( + storedPeersData ? JSON.parse(storedPeersData) : [] + ) as LocalStoragePeerInfo[]; + return localStoragePeers; + } + + private setPeersInLocalStorage(peers: LocalStoragePeerInfo[]): void { + localStorage.setItem("waku:peers", JSON.stringify(peers)); + } +} + +export function wakuLocalStorageDiscovery(): ( + components: Libp2pComponents, + options?: LocalStorageDiscoveryOptions +) => LocalStorageDiscovery { + return ( + components: Libp2pComponents, + options?: LocalStorageDiscoveryOptions + ) => new LocalStorageDiscovery(components, options); +} From 80780d3fa1bb3ad80b0c144fa9e6606a7f2ae3a2 Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Wed, 24 Jan 2024 18:36:10 +0530 Subject: [PATCH 03/23] add: Startable to cspell --- .cspell.json | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/.cspell.json b/.cspell.json index a6c0a9724c..f5cdae8727 100644 --- a/.cspell.json +++ b/.cspell.json @@ -96,6 +96,7 @@ "secp", "sharded", "sscanf", + "Startable", "staticnode", "statusim", "statusteam", @@ -125,7 +126,15 @@ "Привет", "مرحبا" ], - "flagWords": ["pubSub: pubsub", "pubSubTopics: pubsubTopics", "pubSubTopic: pubsubTopic", "PubSub: Pubsub", "PubSubTopics: PubsubTopics", "PubSubTopic: PubsubTopic", "DefaultPubSubTopic: DefaultPubsubTopic"], + "flagWords": [ + "pubSub: pubsub", + "pubSubTopics: pubsubTopics", + "pubSubTopic: pubsubTopic", + "PubSub: Pubsub", + "PubSubTopics: PubsubTopics", + "PubSubTopic: PubsubTopic", + "DefaultPubSubTopic: DefaultPubsubTopic" + ], "ignorePaths": [ "package.json", "package-lock.json", From 1ad6ed2b0dcc433feec63ea3e180f29f4bac0dcf Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Tue, 30 Jan 2024 16:00:35 +0530 Subject: [PATCH 04/23] add compliance test --- .gitignore | 1 + package-lock.json | 84 +++++++++++++++++++ packages/interfaces/src/index.ts | 1 + packages/interfaces/src/local_storage.ts | 4 + packages/local-storage-discovery/src/index.ts | 16 ++-- packages/sdk/package.json | 1 + packages/sdk/src/create.ts | 4 +- packages/tests/package.json | 3 + .../tests/local_storage_discovery.spec.ts | 48 +++++++++++ 9 files changed, 153 insertions(+), 9 deletions(-) create mode 100644 packages/interfaces/src/local_storage.ts create mode 100644 packages/tests/tests/local_storage_discovery.spec.ts diff --git a/.gitignore b/.gitignore index f178fc9969..26e78e1fb7 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ test-results playwright-report example allure-results +tests/mock_local_storage \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 195e8dd66f..a477020234 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4860,6 +4860,15 @@ "version": "20.6.3", "license": "MIT" }, + "node_modules/@types/node-localstorage": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/@types/node-localstorage/-/node-localstorage-1.3.3.tgz", + "integrity": "sha512-Wkn5g4eM5x10UNV9Xvl9K6y6m0zorocuJy4WjB5muUdyMZuPbZpSJG3hlhjGHe1HGxbOQO7RcB+jlHcNwkh+Jw==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/normalize-package-data": { "version": "2.4.4", "resolved": "https://registry.npmjs.org/@types/normalize-package-data/-/normalize-package-data-2.4.4.tgz", @@ -16921,6 +16930,40 @@ "node": ">= 6.13.0" } }, + "node_modules/node-localstorage": { + "version": "3.0.5", + "resolved": "https://registry.npmjs.org/node-localstorage/-/node-localstorage-3.0.5.tgz", + "integrity": "sha512-GCwtK33iwVXboZWYcqQHu3aRvXEBwmPkAMRBLeaX86ufhqslyUkLGsi4aW3INEfdQYpUB5M9qtYf3eHvAk2VBg==", + "dependencies": { + "write-file-atomic": "^5.0.1" + }, + "engines": { + "node": ">=0.12" + } + }, + "node_modules/node-localstorage/node_modules/signal-exit": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-4.1.0.tgz", + "integrity": "sha512-bzyZ1e88w9O1iNJbKnOlvYTrWPDl46O1bG0D3XInv+9tkPrxrN8jUUTiFlDkkmKWgn1M6CfIA13SuGqOa9Korw==", + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/node-localstorage/node_modules/write-file-atomic": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/write-file-atomic/-/write-file-atomic-5.0.1.tgz", + "integrity": "sha512-+QU2zd6OTD8XWIJCbffaiQeH9U73qIqafo1x6V1snCWYGJf6cVE0cDR4D8xRzcEnfI21IFrUPzPGtcPf8AC+Rw==", + "dependencies": { + "imurmurhash": "^0.1.4", + "signal-exit": "^4.0.1" + }, + "engines": { + "node": "^14.17.0 || ^16.13.0 || >=18.0.0" + } + }, "node_modules/node-preload": { "version": "0.2.1", "resolved": "https://registry.npmjs.org/node-preload/-/node-preload-0.2.1.tgz", @@ -27955,6 +27998,7 @@ "@waku/core": "0.0.26", "@waku/dns-discovery": "0.0.20", "@waku/interfaces": "0.0.21", + "@waku/local-storage-discovery": "^0.0.1", "@waku/peer-exchange": "^0.0.19", "@waku/relay": "0.0.9", "@waku/utils": "0.0.14", @@ -27985,12 +28029,14 @@ "@waku/core": "*", "@waku/enr": "*", "@waku/interfaces": "*", + "@waku/local-storage-discovery": "^0.0.1", "@waku/utils": "*", "app-root-path": "^3.1.0", "chai-as-promised": "^7.1.1", "debug": "^4.3.4", "dockerode": "^3.3.5", "fast-check": "^3.15.0", + "node-localstorage": "^3.0.5", "p-retry": "^6.1.0", "p-timeout": "^6.1.0", "portfinder": "^1.0.32", @@ -28002,6 +28048,7 @@ "@types/chai": "^4.3.11", "@types/dockerode": "^3.3.19", "@types/mocha": "^10.0.1", + "@types/node-localstorage": "^1.3.3", "@types/sinon": "^17.0.2", "@types/tail": "^2.2.3", "@waku/dns-discovery": "*", @@ -31438,6 +31485,15 @@ "@types/node": { "version": "20.6.3" }, + "@types/node-localstorage": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/@types/node-localstorage/-/node-localstorage-1.3.3.tgz", + "integrity": "sha512-Wkn5g4eM5x10UNV9Xvl9K6y6m0zorocuJy4WjB5muUdyMZuPbZpSJG3hlhjGHe1HGxbOQO7RcB+jlHcNwkh+Jw==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, "@types/normalize-package-data": { "version": "2.4.4", "resolved": "https://registry.npmjs.org/@types/normalize-package-data/-/normalize-package-data-2.4.4.tgz", @@ -32122,6 +32178,7 @@ "@waku/core": "0.0.26", "@waku/dns-discovery": "0.0.20", "@waku/interfaces": "0.0.21", + "@waku/local-storage-discovery": "^0.0.1", "@waku/peer-exchange": "^0.0.19", "@waku/relay": "0.0.9", "@waku/utils": "0.0.14", @@ -32141,12 +32198,14 @@ "@types/chai": "^4.3.11", "@types/dockerode": "^3.3.19", "@types/mocha": "^10.0.1", + "@types/node-localstorage": "^1.3.3", "@types/sinon": "^17.0.2", "@types/tail": "^2.2.3", "@waku/core": "*", "@waku/dns-discovery": "*", "@waku/enr": "*", "@waku/interfaces": "*", + "@waku/local-storage-discovery": "^0.0.1", "@waku/message-encryption": "*", "@waku/peer-exchange": "*", "@waku/sdk": "*", @@ -32165,6 +32224,7 @@ "libp2p": "^1.1.2", "mocha": "^10.2.0", "mocha-multi-reporters": "^1.5.1", + "node-localstorage": "^3.0.5", "npm-run-all": "^4.1.5", "p-retry": "^6.1.0", "p-timeout": "^6.1.0", @@ -39681,6 +39741,30 @@ "resolved": "https://registry.npmjs.org/node-forge/-/node-forge-1.3.1.tgz", "integrity": "sha512-dPEtOeMvF9VMcYV/1Wb8CPoVAXtp6MKMlcbAt4ddqmGqUJ6fQZFXkNZNkNlfevtNkGtaSoXf/vNNNSvgrdXwtA==" }, + "node-localstorage": { + "version": "3.0.5", + "resolved": "https://registry.npmjs.org/node-localstorage/-/node-localstorage-3.0.5.tgz", + "integrity": "sha512-GCwtK33iwVXboZWYcqQHu3aRvXEBwmPkAMRBLeaX86ufhqslyUkLGsi4aW3INEfdQYpUB5M9qtYf3eHvAk2VBg==", + "requires": { + "write-file-atomic": "^5.0.1" + }, + "dependencies": { + "signal-exit": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-4.1.0.tgz", + "integrity": "sha512-bzyZ1e88w9O1iNJbKnOlvYTrWPDl46O1bG0D3XInv+9tkPrxrN8jUUTiFlDkkmKWgn1M6CfIA13SuGqOa9Korw==" + }, + "write-file-atomic": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/write-file-atomic/-/write-file-atomic-5.0.1.tgz", + "integrity": "sha512-+QU2zd6OTD8XWIJCbffaiQeH9U73qIqafo1x6V1snCWYGJf6cVE0cDR4D8xRzcEnfI21IFrUPzPGtcPf8AC+Rw==", + "requires": { + "imurmurhash": "^0.1.4", + "signal-exit": "^4.0.1" + } + } + } + }, "node-preload": { "version": "0.2.1", "resolved": "https://registry.npmjs.org/node-preload/-/node-preload-0.2.1.tgz", diff --git a/packages/interfaces/src/index.ts b/packages/interfaces/src/index.ts index 03ff627af8..1d98ed24d4 100644 --- a/packages/interfaces/src/index.ts +++ b/packages/interfaces/src/index.ts @@ -16,3 +16,4 @@ export * from "./keep_alive_manager.js"; export * from "./dns_discovery.js"; export * from "./metadata.js"; export * from "./constants.js"; +export * from "./local_storage.js"; diff --git a/packages/interfaces/src/local_storage.ts b/packages/interfaces/src/local_storage.ts new file mode 100644 index 0000000000..7790d917e7 --- /dev/null +++ b/packages/interfaces/src/local_storage.ts @@ -0,0 +1,4 @@ +export type LocalStoragePeerInfo = { + id: string; + address: string; +}; diff --git a/packages/local-storage-discovery/src/index.ts b/packages/local-storage-discovery/src/index.ts index 033a318f87..18cd23fc62 100644 --- a/packages/local-storage-discovery/src/index.ts +++ b/packages/local-storage-discovery/src/index.ts @@ -1,5 +1,6 @@ import { TypedEventEmitter } from "@libp2p/interface"; -import type { +import { + CustomEvent, PeerDiscovery, PeerDiscoveryEvents, PeerInfo, @@ -8,16 +9,15 @@ import type { } from "@libp2p/interface"; import { createFromJSON } from "@libp2p/peer-id-factory"; import { multiaddr } from "@multiformats/multiaddr"; -import { Libp2pComponents, Tags } from "@waku/interfaces"; +import { + type Libp2pComponents, + type LocalStoragePeerInfo, + Tags +} from "@waku/interfaces"; import { Logger } from "@waku/utils"; const log = new Logger("peer-exchange-discovery"); -type LocalStoragePeerInfo = { - id: string; - address: string; -}; - type LocalStorageDiscoveryOptions = { tagValue?: number; tagTTL?: number; @@ -27,7 +27,7 @@ export const DEFAULT_PEER_EXCHANGE_TAG_NAME = Tags.PEER_EXCHANGE; const DEFAULT_PEER_EXCHANGE_TAG_VALUE = 50; const DEFAULT_PEER_EXCHANGE_TAG_TTL = 100_000_000; -class LocalStorageDiscovery +export class LocalStorageDiscovery extends TypedEventEmitter implements PeerDiscovery, Startable { diff --git a/packages/sdk/package.json b/packages/sdk/package.json index c71c5c453f..23be3dcb38 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -69,6 +69,7 @@ "@waku/core": "0.0.26", "@waku/dns-discovery": "0.0.20", "@waku/interfaces": "0.0.21", + "@waku/local-storage-discovery": "^0.0.1", "@waku/peer-exchange": "^0.0.19", "@waku/relay": "0.0.9", "@waku/utils": "0.0.14", diff --git a/packages/sdk/src/create.ts b/packages/sdk/src/create.ts index 15bcf4e423..6753c6568a 100644 --- a/packages/sdk/src/create.ts +++ b/packages/sdk/src/create.ts @@ -26,6 +26,7 @@ import type { ProtocolCreateOptions, ShardingParams } from "@waku/interfaces"; +import { wakuLocalStorageDiscovery } from "@waku/local-storage-discovery"; import { wakuPeerExchangeDiscovery } from "@waku/peer-exchange"; import { RelayCreateOptions, wakuGossipSub, wakuRelay } from "@waku/relay"; import { createLibp2p } from "libp2p"; @@ -193,7 +194,8 @@ export function defaultPeerDiscoveries(): (( ) => PeerDiscovery)[] { const discoveries = [ wakuDnsDiscovery([enrTree["PROD"]], DEFAULT_NODE_REQUIREMENTS), - wakuPeerExchangeDiscovery() + wakuPeerExchangeDiscovery(), + wakuLocalStorageDiscovery() ]; return discoveries; } diff --git a/packages/tests/package.json b/packages/tests/package.json index dd8022767e..e68ad911ce 100644 --- a/packages/tests/package.json +++ b/packages/tests/package.json @@ -55,12 +55,14 @@ "@waku/core": "*", "@waku/enr": "*", "@waku/interfaces": "*", + "@waku/local-storage-discovery": "^0.0.1", "@waku/utils": "*", "app-root-path": "^3.1.0", "chai-as-promised": "^7.1.1", "debug": "^4.3.4", "dockerode": "^3.3.5", "fast-check": "^3.15.0", + "node-localstorage": "^3.0.5", "p-retry": "^6.1.0", "p-timeout": "^6.1.0", "portfinder": "^1.0.32", @@ -72,6 +74,7 @@ "@types/chai": "^4.3.11", "@types/dockerode": "^3.3.19", "@types/mocha": "^10.0.1", + "@types/node-localstorage": "^1.3.3", "@types/sinon": "^17.0.2", "@types/tail": "^2.2.3", "@waku/dns-discovery": "*", diff --git a/packages/tests/tests/local_storage_discovery.spec.ts b/packages/tests/tests/local_storage_discovery.spec.ts new file mode 100644 index 0000000000..06abcef91c --- /dev/null +++ b/packages/tests/tests/local_storage_discovery.spec.ts @@ -0,0 +1,48 @@ +import tests from "@libp2p/interface-compliance-tests/peer-discovery"; +import { LocalStoragePeerInfo } from "@waku/interfaces"; +import { LocalStorageDiscovery } from "@waku/local-storage-discovery"; +import { createLightNode, type LightNode } from "@waku/sdk"; +import { LocalStorage } from "node-localstorage"; + +import { makeLogFileName, ServiceNode } from "../src/index.js"; + +global.localStorage = new LocalStorage("./mock_local_storage"); + +describe("Local Storage Discovery: Compliance Test", function () { + this.timeout(10000); + let waku: LightNode; + let serviceNode1: ServiceNode; + let serviceNode2: ServiceNode; + this.beforeEach(async function () { + waku = await createLightNode(); + serviceNode1 = new ServiceNode(makeLogFileName(this) + "1"); + serviceNode2 = new ServiceNode(makeLogFileName(this) + "1"); + await serviceNode1.start(); + await serviceNode2.start(); + + await setPeersInLocalStorage([serviceNode1, serviceNode2]); + }); + tests({ + async setup() { + return new LocalStorageDiscovery(waku.libp2p.components); + }, + async teardown() {} + }); +}); + +async function setPeersInLocalStorage( + serviceNodes: ServiceNode[] +): Promise { + const peers: LocalStoragePeerInfo[] = []; + for (const node of serviceNodes) { + const nodePeerId = await node.getPeerId(); + const nodeMa = await node.getMultiaddrWithId(); + peers.push({ + id: nodePeerId.toString(), + address: nodeMa.toString() + }); + } + const localStorage = global.localStorage; + const peersStr = JSON.stringify(peers); + localStorage.setItem("waku:peers", peersStr); +} From 7a104d9581c2b15fcf52496dcb6be1ed5954fd7a Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Tue, 30 Jan 2024 17:56:44 +0530 Subject: [PATCH 05/23] add: discovery tests --- .gitignore | 2 +- .../tests/local_storage_discovery.spec.ts | 63 ++++++++++++++++--- 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 26e78e1fb7..babba11cfa 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,4 @@ test-results playwright-report example allure-results -tests/mock_local_storage \ No newline at end of file +packages/tests/mock_local_storage \ No newline at end of file diff --git a/packages/tests/tests/local_storage_discovery.spec.ts b/packages/tests/tests/local_storage_discovery.spec.ts index 06abcef91c..09ed432cf1 100644 --- a/packages/tests/tests/local_storage_discovery.spec.ts +++ b/packages/tests/tests/local_storage_discovery.spec.ts @@ -2,31 +2,76 @@ import tests from "@libp2p/interface-compliance-tests/peer-discovery"; import { LocalStoragePeerInfo } from "@waku/interfaces"; import { LocalStorageDiscovery } from "@waku/local-storage-discovery"; import { createLightNode, type LightNode } from "@waku/sdk"; +import { expect } from "chai"; import { LocalStorage } from "node-localstorage"; import { makeLogFileName, ServiceNode } from "../src/index.js"; global.localStorage = new LocalStorage("./mock_local_storage"); -describe("Local Storage Discovery: Compliance Test", function () { - this.timeout(10000); +describe("Local Storage Discovery", function () { + this.timeout(25_000); let waku: LightNode; let serviceNode1: ServiceNode; let serviceNode2: ServiceNode; this.beforeEach(async function () { - waku = await createLightNode(); serviceNode1 = new ServiceNode(makeLogFileName(this) + "1"); serviceNode2 = new ServiceNode(makeLogFileName(this) + "1"); await serviceNode1.start(); await serviceNode2.start(); - await setPeersInLocalStorage([serviceNode1, serviceNode2]); }); - tests({ - async setup() { - return new LocalStorageDiscovery(waku.libp2p.components); - }, - async teardown() {} + + it("Compliance Test", async function () { + waku = await createLightNode(); + await waku.start(); + tests({ + async setup() { + return new LocalStorageDiscovery(waku.libp2p.components); + }, + async teardown() {} + }); + }); + + it("Should discover peers from local storage", async function () { + waku = await createLightNode({ defaultBootstrap: true }); + await waku.start(); + + const serviceNode1PeerId = await serviceNode1.getPeerId(); + const serviceNode2PeerId = await serviceNode2.getPeerId(); + const serviceNode1Ma = await serviceNode1.getMultiaddrWithId(); + const serviceNode2Ma = await serviceNode2.getMultiaddrWithId(); + + let serviceNode1Received = false; + let serviceNode2Received = false; + + await new Promise((resolve) => { + waku.libp2p.addEventListener("peer:update", (event) => { + const { peer } = event.detail; + + if (peer.id.toString() === serviceNode1PeerId.toString()) { + serviceNode1Received = true; + + const hasValidAddress = peer.addresses.some( + (addr) => addr.toString() === serviceNode1Ma.toString() + ); + expect(hasValidAddress).to.eq(true); + } else if (peer.id.toString() === serviceNode2PeerId.toString()) { + serviceNode2Received = true; + const hasValidAddress = peer.addresses.some( + (addr) => addr.toString() === serviceNode2Ma.toString() + ); + expect(hasValidAddress).to.eq(true); + } + + if (serviceNode1Received && serviceNode2Received) { + resolve(true); + } + }); + }); + + expect(serviceNode1Received).to.eq(true); + expect(serviceNode2Received).to.eq(true); }); }); From d7b36d56c957f205f4a63e8dedc07dbd8ba1a068 Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Tue, 30 Jan 2024 19:38:05 +0530 Subject: [PATCH 06/23] rm: browser tests script --- .vscode/settings.json | 2 +- packages/local-storage-discovery/package.json | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index d2c3a0c90f..a93336e107 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,7 +3,7 @@ "cSpell.enabled": true, "editor.defaultFormatter": "dbaeumer.vscode-eslint", "editor.codeActionsOnSave": { - "source.fixAll.eslint": "explicit" + "source.fixAll.eslint": true }, "editor.formatOnSave": false, // Disable general format on save "typescript.tsdk": "node_modules/typescript/lib", diff --git a/packages/local-storage-discovery/package.json b/packages/local-storage-discovery/package.json index 6448783513..f98228fc8e 100644 --- a/packages/local-storage-discovery/package.json +++ b/packages/local-storage-discovery/package.json @@ -44,8 +44,7 @@ "prepublish": "npm run build", "reset-hard": "git clean -dfx -e .idea && git reset --hard && npm i && npm run build", "test": "NODE_ENV=test run-s test:*", - "test:node": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json mocha", - "test:browser": "NODE_ENV=test karma start karma.conf.cjs" + "test:node": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json mocha" }, "engines": { "node": ">=18" From 942e79ebfd20d241627b24be9adfdd8592568351 Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Wed, 7 Feb 2024 12:47:02 +0530 Subject: [PATCH 07/23] address comments --- package-lock.json | 4 ++-- package.json | 2 +- packages/core/src/lib/connection_manager.ts | 13 +++++++++++-- packages/interfaces/src/connection_manager.ts | 4 +++- packages/local-storage-discovery/package.json | 4 ++-- packages/local-storage-discovery/src/index.ts | 15 ++++++--------- 6 files changed, 25 insertions(+), 17 deletions(-) diff --git a/package-lock.json b/package-lock.json index f39aadd991..c2f50b4ee1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,7 +15,7 @@ "packages/relay", "packages/peer-exchange", "packages/dns-discovery", - "packages/local-storage-discovery", + "packages/local-discovery", "packages/message-encryption", "packages/sdk", "packages/tests", @@ -27753,7 +27753,7 @@ } }, "packages/local-storage-discovery": { - "name": "@waku/local-storage-discovery", + "name": "@waku/local-discovery", "version": "0.0.1", "license": "MIT OR Apache-2.0", "dependencies": { diff --git a/package.json b/package.json index 36a8189ec4..d2ad843b98 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "packages/relay", "packages/peer-exchange", "packages/dns-discovery", - "packages/local-storage-discovery", + "packages/local-discovery", "packages/message-encryption", "packages/sdk", "packages/tests", diff --git a/packages/core/src/lib/connection_manager.ts b/packages/core/src/lib/connection_manager.ts index 8a0ef17a26..1692154f5c 100644 --- a/packages/core/src/lib/connection_manager.ts +++ b/packages/core/src/lib/connection_manager.ts @@ -97,8 +97,11 @@ export class ConnectionManager const peersDiscoveredByBootstrap: Peer[] = []; const peersDiscoveredByPeerExchange: Peer[] = []; + const peersDiscoveredByLocal: Peer[] = []; + const peersConnectedByBootstrap: Peer[] = []; const peersConnectedByPeerExchange: Peer[] = []; + const peersConnectedByLocal: Peer[] = []; for (const peer of peersDiscovered) { const tags = await this.getTagNamesForPeer(peer.id); @@ -107,6 +110,8 @@ export class ConnectionManager peersDiscoveredByBootstrap.push(peer); } else if (tags.includes(Tags.PEER_EXCHANGE)) { peersDiscoveredByPeerExchange.push(peer); + } else if (tags.includes(Tags.LOCAL)) { + peersDiscoveredByLocal.push(peer); } } @@ -118,17 +123,21 @@ export class ConnectionManager peersConnectedByBootstrap.push(peer); } else if (tags.includes(Tags.PEER_EXCHANGE)) { peersConnectedByPeerExchange.push(peer); + } else if (tags.includes(Tags.LOCAL)) { + peersConnectedByLocal.push(peer); } } return { DISCOVERED: { [Tags.BOOTSTRAP]: peersDiscoveredByBootstrap, - [Tags.PEER_EXCHANGE]: peersDiscoveredByPeerExchange + [Tags.PEER_EXCHANGE]: peersDiscoveredByPeerExchange, + [Tags.LOCAL]: peersDiscoveredByLocal }, CONNECTED: { [Tags.BOOTSTRAP]: peersConnectedByBootstrap, - [Tags.PEER_EXCHANGE]: peersConnectedByPeerExchange + [Tags.PEER_EXCHANGE]: peersConnectedByPeerExchange, + [Tags.LOCAL]: peersConnectedByLocal } }; } diff --git a/packages/interfaces/src/connection_manager.ts b/packages/interfaces/src/connection_manager.ts index 0a62b8e0ef..486a48d3d4 100644 --- a/packages/interfaces/src/connection_manager.ts +++ b/packages/interfaces/src/connection_manager.ts @@ -3,7 +3,7 @@ import type { Peer, PeerId, TypedEventEmitter } from "@libp2p/interface"; export enum Tags { BOOTSTRAP = "bootstrap", PEER_EXCHANGE = "peer-exchange", - LOCAL_STORAGE = "local-storage" + LOCAL = "local" } export interface ConnectionManagerOptions { @@ -41,10 +41,12 @@ export interface PeersByDiscoveryResult { DISCOVERED: { [Tags.BOOTSTRAP]: Peer[]; [Tags.PEER_EXCHANGE]: Peer[]; + [Tags.LOCAL]: Peer[]; }; CONNECTED: { [Tags.BOOTSTRAP]: Peer[]; [Tags.PEER_EXCHANGE]: Peer[]; + [Tags.LOCAL]: Peer[]; }; } diff --git a/packages/local-storage-discovery/package.json b/packages/local-storage-discovery/package.json index f98228fc8e..7f6e61b8f1 100644 --- a/packages/local-storage-discovery/package.json +++ b/packages/local-storage-discovery/package.json @@ -1,7 +1,7 @@ { - "name": "@waku/local-storage-discovery", + "name": "@waku/local-discovery", "version": "0.0.1", - "description": "Local Storage Discovery acts as a way to fetch (healthy) peers that you have previously successfully connected to, and act as a faster way to bootstrap your way into the network.", + "description": "Local Discovery keeps records of healthy peers in the browser's local storage for quicker bootstrapping.", "types": "./dist/index.d.ts", "module": "./dist/index.js", "exports": { diff --git a/packages/local-storage-discovery/src/index.ts b/packages/local-storage-discovery/src/index.ts index 18cd23fc62..85e8c4e393 100644 --- a/packages/local-storage-discovery/src/index.ts +++ b/packages/local-storage-discovery/src/index.ts @@ -23,9 +23,9 @@ type LocalStorageDiscoveryOptions = { tagTTL?: number; }; -export const DEFAULT_PEER_EXCHANGE_TAG_NAME = Tags.PEER_EXCHANGE; -const DEFAULT_PEER_EXCHANGE_TAG_VALUE = 50; -const DEFAULT_PEER_EXCHANGE_TAG_TTL = 100_000_000; +export const DEFAULT_LOCAL_TAG_NAME = Tags.LOCAL; +const DEFAULT_LOCAL_TAG_VALUE = 50; +const DEFAULT_LOCAL_TAG_TTL = 100_000_000; export class LocalStorageDiscovery extends TypedEventEmitter @@ -51,13 +51,10 @@ export class LocalStorageDiscovery log.info("Starting Local Storage Discovery"); - // only listen to healthy connected peers that have identified themselves this.components.events.addEventListener("peer:update", this.handleNewPeers); - // get all peers from local storage const localStoragePeers = this.getPeersFromLocalStorage(); - // update the tags of the peers in the peer store & dispatch them for (const { id: idStr, address } of localStoragePeers) { const peerId = await createFromJSON({ id: idStr @@ -65,9 +62,9 @@ export class LocalStorageDiscovery await this.components.peerStore.save(peerId, { tags: { - [DEFAULT_PEER_EXCHANGE_TAG_NAME]: { - value: this.options?.tagValue ?? DEFAULT_PEER_EXCHANGE_TAG_VALUE, - ttl: this.options?.tagTTL ?? DEFAULT_PEER_EXCHANGE_TAG_TTL + [DEFAULT_LOCAL_TAG_NAME]: { + value: this.options?.tagValue ?? DEFAULT_LOCAL_TAG_VALUE, + ttl: this.options?.tagTTL ?? DEFAULT_LOCAL_TAG_TTL } } }); From 4162893aea97f39c44af94136160074fb1e3ad7c Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Wed, 7 Feb 2024 13:36:18 +0530 Subject: [PATCH 08/23] add type safety to getting peers from local --- packages/local-storage-discovery/src/index.ts | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/packages/local-storage-discovery/src/index.ts b/packages/local-storage-discovery/src/index.ts index 85e8c4e393..3b01c14da3 100644 --- a/packages/local-storage-discovery/src/index.ts +++ b/packages/local-storage-discovery/src/index.ts @@ -40,6 +40,7 @@ export class LocalStorageDiscovery super(); this.isStarted = false; } + get [Symbol.toStringTag](): string { return "@waku/local-storage-discovery"; } @@ -95,29 +96,21 @@ export class LocalStorageDiscovery handleNewPeers = (event: CustomEvent): void => { const { peer } = event.detail; - // Extract the websocket ma const websocketMultiaddr = peer.addresses.find((addr) => addr.toString().includes("ws" || "wss") ); if (!websocketMultiaddr) return; - // Retrieve peers from local storage - const storedPeersData = localStorage.getItem("waku:peers"); - const localStoragePeers = ( - storedPeersData ? JSON.parse(storedPeersData) : [] - ) as LocalStoragePeerInfo[]; + const localStoragePeers = this.getPeersFromLocalStorage(); - // Find if the peer already exists const existingPeerIndex = localStoragePeers.findIndex( (_peer) => _peer.id === peer.id.toString() ); if (existingPeerIndex >= 0) { - // Update existing peer's address localStoragePeers[existingPeerIndex].address = websocketMultiaddr.toString(); } else { - // Add new peer localStoragePeers.push({ id: peer.id.toString(), address: websocketMultiaddr.toString() @@ -128,11 +121,15 @@ export class LocalStorageDiscovery }; private getPeersFromLocalStorage(): LocalStoragePeerInfo[] { - const storedPeersData = localStorage.getItem("waku:peers"); - const localStoragePeers = ( - storedPeersData ? JSON.parse(storedPeersData) : [] - ) as LocalStoragePeerInfo[]; - return localStoragePeers; + try { + const storedPeersData = localStorage.getItem("waku:peers"); + if (!storedPeersData) return []; + const peers = JSON.parse(storedPeersData); + return peers.filter(isValidStoredPeer); + } catch (error) { + log.error("Error parsing peers from local storage:", error); + return []; + } } private setPeersInLocalStorage(peers: LocalStoragePeerInfo[]): void { @@ -140,6 +137,15 @@ export class LocalStorageDiscovery } } +function isValidStoredPeer(peer: any): peer is LocalStoragePeerInfo { + return ( + peer && + typeof peer === "object" && + typeof peer.id === "string" && + typeof peer.address === "string" + ); +} + export function wakuLocalStorageDiscovery(): ( components: Libp2pComponents, options?: LocalStorageDiscoveryOptions From 63fdf4972d7a7ee601df2923157dcbbca1941664 Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Wed, 7 Feb 2024 13:38:15 +0530 Subject: [PATCH 09/23] only dispatch peer if it does not exist --- packages/local-storage-discovery/src/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/local-storage-discovery/src/index.ts b/packages/local-storage-discovery/src/index.ts index 3b01c14da3..e33ca4b134 100644 --- a/packages/local-storage-discovery/src/index.ts +++ b/packages/local-storage-discovery/src/index.ts @@ -61,6 +61,8 @@ export class LocalStorageDiscovery id: idStr }); + if (await this.components.peerStore.has(peerId)) return; + await this.components.peerStore.save(peerId, { tags: { [DEFAULT_LOCAL_TAG_NAME]: { From 7b58ae9a3ee22826cb23125f2df5763dcee36ba4 Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Wed, 7 Feb 2024 16:45:30 +0530 Subject: [PATCH 10/23] move ws ma extraction to utils --- packages/local-storage-discovery/src/index.ts | 7 ++----- packages/utils/src/common/index.ts | 12 ++++++++++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/packages/local-storage-discovery/src/index.ts b/packages/local-storage-discovery/src/index.ts index e33ca4b134..7a982c13da 100644 --- a/packages/local-storage-discovery/src/index.ts +++ b/packages/local-storage-discovery/src/index.ts @@ -14,7 +14,7 @@ import { type LocalStoragePeerInfo, Tags } from "@waku/interfaces"; -import { Logger } from "@waku/utils"; +import { getWsMultiaddrFromMultiaddrs, Logger } from "@waku/utils"; const log = new Logger("peer-exchange-discovery"); @@ -98,10 +98,7 @@ export class LocalStorageDiscovery handleNewPeers = (event: CustomEvent): void => { const { peer } = event.detail; - const websocketMultiaddr = peer.addresses.find((addr) => - addr.toString().includes("ws" || "wss") - ); - if (!websocketMultiaddr) return; + const websocketMultiaddr = getWsMultiaddrFromMultiaddrs(peer.addresses); const localStoragePeers = this.getPeersFromLocalStorage(); diff --git a/packages/utils/src/common/index.ts b/packages/utils/src/common/index.ts index 5fbc976815..6485b9348d 100644 --- a/packages/utils/src/common/index.ts +++ b/packages/utils/src/common/index.ts @@ -1,3 +1,5 @@ +import type { Address } from "@libp2p/interface"; + export * from "./is_defined.js"; export * from "./random_subset.js"; export * from "./group_by.js"; @@ -14,3 +16,13 @@ export function removeItemFromArray(arr: unknown[], value: unknown): unknown[] { } return arr; } + +export function getWsMultiaddrFromMultiaddrs(multiaddrs: Address[]): Address { + const wsMultiaddr = multiaddrs.find((m) => + m.multiaddr.toString().includes("ws" || "wss") + ); + if (!wsMultiaddr) { + throw new Error("No ws multiaddr found in the given multiaddrs"); + } + return wsMultiaddr; +} From 17386c6ee675ffbd5cf3962c39bc8870fa2c1984 Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Thu, 8 Feb 2024 20:04:10 +0530 Subject: [PATCH 11/23] chore: update package name to local-discovery --- package-lock.json | 103 +++++++++++------- .../.eslintrc.cjs | 0 .../.mocha.reporters.json | 0 .../.mocharc.cjs | 0 .../CHANGELOG.md | 0 .../karma.conf.cjs | 0 .../package.json | 0 .../rollup.config.js | 0 .../src/index.ts | 2 +- .../tsconfig.dev.json | 0 .../tsconfig.json | 0 .../tsconfig.karma.json | 0 .../typedoc.json | 0 packages/sdk/package.json | 2 +- packages/sdk/src/create.ts | 2 +- packages/tests/package.json | 2 +- 16 files changed, 68 insertions(+), 43 deletions(-) rename packages/{local-storage-discovery => local-discovery}/.eslintrc.cjs (100%) rename packages/{local-storage-discovery => local-discovery}/.mocha.reporters.json (100%) rename packages/{local-storage-discovery => local-discovery}/.mocharc.cjs (100%) rename packages/{local-storage-discovery => local-discovery}/CHANGELOG.md (100%) rename packages/{local-storage-discovery => local-discovery}/karma.conf.cjs (100%) rename packages/{local-storage-discovery => local-discovery}/package.json (100%) rename packages/{local-storage-discovery => local-discovery}/rollup.config.js (100%) rename packages/{local-storage-discovery => local-discovery}/src/index.ts (98%) rename packages/{local-storage-discovery => local-discovery}/tsconfig.dev.json (100%) rename packages/{local-storage-discovery => local-discovery}/tsconfig.json (100%) rename packages/{local-storage-discovery => local-discovery}/tsconfig.karma.json (100%) rename packages/{local-storage-discovery => local-discovery}/typedoc.json (100%) diff --git a/package-lock.json b/package-lock.json index c2f50b4ee1..2953ffe426 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5466,8 +5466,8 @@ "resolved": "packages/interfaces", "link": true }, - "node_modules/@waku/local-storage-discovery": { - "resolved": "packages/local-storage-discovery", + "node_modules/@waku/local-discovery": { + "resolved": "packages/local-discovery", "link": true }, "node_modules/@waku/message-encryption": { @@ -27752,8 +27752,7 @@ "node": ">=18" } }, - "packages/local-storage-discovery": { - "name": "@waku/local-discovery", + "packages/local-discovery": { "version": "0.0.1", "license": "MIT OR Apache-2.0", "dependencies": { @@ -27777,7 +27776,7 @@ "node": ">=18" } }, - "packages/local-storage-discovery/node_modules/@cspell/cspell-bundled-dicts": { + "packages/local-discovery/node_modules/@cspell/cspell-bundled-dicts": { "version": "7.3.9", "resolved": "https://registry.npmjs.org/@cspell/cspell-bundled-dicts/-/cspell-bundled-dicts-7.3.9.tgz", "integrity": "sha512-ebfrf5Zaw33bcqT80Qrkv7IGT7GI/CDp15bSk2EUmdORzk1SCKZl6L4vUo3NLMmxVwYioS+OQmsW8E88sJNyGg==", @@ -27836,7 +27835,7 @@ "node": ">=16" } }, - "packages/local-storage-discovery/node_modules/@cspell/cspell-json-reporter": { + "packages/local-discovery/node_modules/@cspell/cspell-json-reporter": { "version": "7.3.9", "resolved": "https://registry.npmjs.org/@cspell/cspell-json-reporter/-/cspell-json-reporter-7.3.9.tgz", "integrity": "sha512-QHsem5OZXshFX+Wdlx3VpdPi9WS7KgoBMGGJ4zQZ3lp81Rb1tRj0Ij/98whq882QOmAVQfr+uOHANHLnyPr0LQ==", @@ -27848,7 +27847,7 @@ "node": ">=16" } }, - "packages/local-storage-discovery/node_modules/@cspell/cspell-pipe": { + "packages/local-discovery/node_modules/@cspell/cspell-pipe": { "version": "7.3.9", "resolved": "https://registry.npmjs.org/@cspell/cspell-pipe/-/cspell-pipe-7.3.9.tgz", "integrity": "sha512-gKYTHcryKOaTmr6t+M5h1sZnQ42eHeumBJejovphipXfdivedUnuYyQrrQGFAlUKzfEOWcOPME1nm17xsaX5Ww==", @@ -27857,7 +27856,7 @@ "node": ">=16" } }, - "packages/local-storage-discovery/node_modules/@cspell/cspell-resolver": { + "packages/local-discovery/node_modules/@cspell/cspell-resolver": { "version": "7.3.9", "resolved": "https://registry.npmjs.org/@cspell/cspell-resolver/-/cspell-resolver-7.3.9.tgz", "integrity": "sha512-2slYAGvi7EFLKyJ5hrYBNaFT2iyOEQM1pEIzm+PDuhNJE/9wuBY5pBVqIgFSPz53vsQvW9GJThNY8h1/2EH3ZA==", @@ -27869,7 +27868,7 @@ "node": ">=16" } }, - "packages/local-storage-discovery/node_modules/@cspell/cspell-service-bus": { + "packages/local-discovery/node_modules/@cspell/cspell-service-bus": { "version": "7.3.9", "resolved": "https://registry.npmjs.org/@cspell/cspell-service-bus/-/cspell-service-bus-7.3.9.tgz", "integrity": "sha512-VyfK3qWtJZag4Fe/x1Oh/tqCNVGKGlQ2ArX1fVdmTVGQtZcbXuMKdZI80t4b8SGtzGINHufAdakpu3xucX/FrQ==", @@ -27878,7 +27877,7 @@ "node": ">=16" } }, - "packages/local-storage-discovery/node_modules/@cspell/cspell-types": { + "packages/local-discovery/node_modules/@cspell/cspell-types": { "version": "7.3.9", "resolved": "https://registry.npmjs.org/@cspell/cspell-types/-/cspell-types-7.3.9.tgz", "integrity": "sha512-p7s8yEV6ASz0HjiArH11yjNj3vXzK2Ep94GrpdtYJxSxFC2w1mXAVUaJB/5+jC4+1YeYsmcBFTXmZ1rGMyTv3g==", @@ -27887,25 +27886,25 @@ "node": ">=16" } }, - "packages/local-storage-discovery/node_modules/@cspell/dict-cryptocurrencies": { + "packages/local-discovery/node_modules/@cspell/dict-cryptocurrencies": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/@cspell/dict-cryptocurrencies/-/dict-cryptocurrencies-4.0.0.tgz", "integrity": "sha512-EiZp91ATyRxTmauIQfOX9adLYCunKjHEh092rrM7o2eMXP9n7zpXAL9BK7LviL+LbB8VDOm21q+s83cKrrRrsg==", "dev": true }, - "packages/local-storage-discovery/node_modules/@cspell/dict-en-common-misspellings": { + "packages/local-discovery/node_modules/@cspell/dict-en-common-misspellings": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/@cspell/dict-en-common-misspellings/-/dict-en-common-misspellings-1.0.2.tgz", "integrity": "sha512-jg7ZQZpZH7+aAxNBlcAG4tGhYF6Ksy+QS5Df73Oo+XyckBjC9QS+PrRwLTeYoFIgXy5j3ICParK5r3MSSoL4gw==", "dev": true }, - "packages/local-storage-discovery/node_modules/@cspell/dict-git": { + "packages/local-discovery/node_modules/@cspell/dict-git": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/@cspell/dict-git/-/dict-git-2.0.0.tgz", "integrity": "sha512-n1AxyX5Kgxij/sZFkxFJlzn3K9y/sCcgVPg/vz4WNJ4K9YeTsUmyGLA2OQI7d10GJeiuAo2AP1iZf2A8j9aj2w==", "dev": true }, - "packages/local-storage-discovery/node_modules/@cspell/dynamic-import": { + "packages/local-discovery/node_modules/@cspell/dynamic-import": { "version": "7.3.9", "resolved": "https://registry.npmjs.org/@cspell/dynamic-import/-/dynamic-import-7.3.9.tgz", "integrity": "sha512-P6tAmDVhrW03hmhetxhBKlNTYwL2lk8ZehYQwSpXaLnaFrS3xrQvfUaJ3Mj9W2CIMzSYXlLmPO2FLRhXK2dnEw==", @@ -27917,7 +27916,7 @@ "node": ">=16" } }, - "packages/local-storage-discovery/node_modules/@cspell/strong-weak-map": { + "packages/local-discovery/node_modules/@cspell/strong-weak-map": { "version": "7.3.9", "resolved": "https://registry.npmjs.org/@cspell/strong-weak-map/-/strong-weak-map-7.3.9.tgz", "integrity": "sha512-XKpw/p3+EN+PWiFAWc45RJPI9zQRkPSVdUFeZb0YLseWF/CkogScgIe4CLfMLITiVbP0X/FKk90+aTPfAU38kg==", @@ -27926,7 +27925,7 @@ "node": ">=16" } }, - "packages/local-storage-discovery/node_modules/commander": { + "packages/local-discovery/node_modules/commander": { "version": "11.1.0", "resolved": "https://registry.npmjs.org/commander/-/commander-11.1.0.tgz", "integrity": "sha512-yPVavfyCcRhmorC7rWlkHn15b4wDVgVmBA7kV4QVBsF7kv/9TKJAbAXVTxvTnwP8HHKjRCJDClKbciiYS7p0DQ==", @@ -27935,7 +27934,7 @@ "node": ">=16" } }, - "packages/local-storage-discovery/node_modules/cspell": { + "packages/local-discovery/node_modules/cspell": { "version": "7.3.9", "resolved": "https://registry.npmjs.org/cspell/-/cspell-7.3.9.tgz", "integrity": "sha512-QzunjO9CmV5+98UfG4ONhvPtrcAC6Y2pEKeOrp5oPeyAI7HwgxmfsR3ybHRlMPAGcwKtDOurBKxM7jqXNwkzmA==", @@ -27971,7 +27970,7 @@ "url": "https://github.com/streetsidesoftware/cspell?sponsor=1" } }, - "packages/local-storage-discovery/node_modules/cspell-dictionary": { + "packages/local-discovery/node_modules/cspell-dictionary": { "version": "7.3.9", "resolved": "https://registry.npmjs.org/cspell-dictionary/-/cspell-dictionary-7.3.9.tgz", "integrity": "sha512-lkWfX5QNbs4yKqD9wa+G+NHRWmLgFdyposgJOyd/ojDbx99CDPMhMhg9pyMKdYl6Yt8kjMow58/i12EYvD8wnA==", @@ -27987,13 +27986,13 @@ "node": ">=16" } }, - "packages/local-storage-discovery/node_modules/cspell-dictionary/node_modules/fast-equals": { + "packages/local-discovery/node_modules/cspell-dictionary/node_modules/fast-equals": { "version": "4.0.3", "resolved": "https://registry.npmjs.org/fast-equals/-/fast-equals-4.0.3.tgz", "integrity": "sha512-G3BSX9cfKttjr+2o1O22tYMLq0DPluZnYtq1rXumE1SpL/F/SLIfHx08WYQoWSIpeMYf8sRbJ8++71+v6Pnxfg==", "dev": true }, - "packages/local-storage-discovery/node_modules/cspell-gitignore": { + "packages/local-discovery/node_modules/cspell-gitignore": { "version": "7.3.9", "resolved": "https://registry.npmjs.org/cspell-gitignore/-/cspell-gitignore-7.3.9.tgz", "integrity": "sha512-DLuu+K2q4xYNL4DpLyysUeiGU/NYYoObzfOYiISzOKYpi3aFLiUaiyfF6xWGsahmlijif+8bwSsIMmcvGa5dgA==", @@ -28009,7 +28008,7 @@ "node": ">=16" } }, - "packages/local-storage-discovery/node_modules/cspell-glob": { + "packages/local-discovery/node_modules/cspell-glob": { "version": "7.3.9", "resolved": "https://registry.npmjs.org/cspell-glob/-/cspell-glob-7.3.9.tgz", "integrity": "sha512-7PaTkCzJWjQex3men857v3ExF7Q10jbQkfD+wdln2te9iNFd+HEkstA173vb828D9yeib1q1of8oONr2SeGycg==", @@ -28021,7 +28020,7 @@ "node": ">=16" } }, - "packages/local-storage-discovery/node_modules/cspell-grammar": { + "packages/local-discovery/node_modules/cspell-grammar": { "version": "7.3.9", "resolved": "https://registry.npmjs.org/cspell-grammar/-/cspell-grammar-7.3.9.tgz", "integrity": "sha512-s1QOPg4AxWE8XBewDQLe14j0uDyWGjREfm4dZFTrslAZUrQ8/df5s152M5LtgOEza33FrkKKE2axbGvgS9O7sQ==", @@ -28037,7 +28036,7 @@ "node": ">=16" } }, - "packages/local-storage-discovery/node_modules/cspell-io": { + "packages/local-discovery/node_modules/cspell-io": { "version": "7.3.9", "resolved": "https://registry.npmjs.org/cspell-io/-/cspell-io-7.3.9.tgz", "integrity": "sha512-IbXOYaDxLg94uijv13kqb+6PQjEwGboQYtABuZs2+HuUVW89K2tE+fQcEhkAsrZ11sDj5lUqgEQj9omvknZSuA==", @@ -28050,7 +28049,7 @@ "node": ">=16" } }, - "packages/local-storage-discovery/node_modules/cspell-lib": { + "packages/local-discovery/node_modules/cspell-lib": { "version": "7.3.9", "resolved": "https://registry.npmjs.org/cspell-lib/-/cspell-lib-7.3.9.tgz", "integrity": "sha512-eFYYs8XoYmdu78UxrPisD+hAoXOLaLzcevKf9+oDPDgJmHpkGoFgbIBnHMRIsAM1e+QDS6OlWG/rybhZTqanCQ==", @@ -28083,7 +28082,7 @@ "node": ">=16" } }, - "packages/local-storage-discovery/node_modules/cspell-lib/node_modules/find-up": { + "packages/local-discovery/node_modules/cspell-lib/node_modules/find-up": { "version": "6.3.0", "resolved": "https://registry.npmjs.org/find-up/-/find-up-6.3.0.tgz", "integrity": "sha512-v2ZsoEuVHYy8ZIlYqwPe/39Cy+cFDzp4dXPaxNvkEuouymu+2Jbz0PxpKarJHYJTmv2HWT3O382qY8l4jMWthw==", @@ -28099,7 +28098,7 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "packages/local-storage-discovery/node_modules/cspell-trie-lib": { + "packages/local-discovery/node_modules/cspell-trie-lib": { "version": "7.3.9", "resolved": "https://registry.npmjs.org/cspell-trie-lib/-/cspell-trie-lib-7.3.9.tgz", "integrity": "sha512-aTWm2KYXjQ+MlM6kB37wmTV9RU8+fgZYkiFfMc48M0MhBc6XkHUibMGrFAS29gp+B70kWPxe+VHLmFIk9pRPyg==", @@ -28113,7 +28112,7 @@ "node": ">=16" } }, - "packages/local-storage-discovery/node_modules/file-entry-cache": { + "packages/local-discovery/node_modules/file-entry-cache": { "version": "7.0.2", "resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-7.0.2.tgz", "integrity": "sha512-TfW7/1iI4Cy7Y8L6iqNdZQVvdXn0f8B4QcIXmkIbtTIe/Okm/nSlHb4IwGzRVOd3WfSieCgvf5cMzEfySAIl0g==", @@ -28125,7 +28124,7 @@ "node": ">=12.0.0" } }, - "packages/local-storage-discovery/node_modules/import-meta-resolve": { + "packages/local-discovery/node_modules/import-meta-resolve": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/import-meta-resolve/-/import-meta-resolve-3.1.1.tgz", "integrity": "sha512-qeywsE/KC3w9Fd2ORrRDUw6nS/nLwZpXgfrOc2IILvZYnCaEMd+D56Vfg9k4G29gIeVi3XKql1RQatME8iYsiw==", @@ -28135,7 +28134,7 @@ "url": "https://github.com/sponsors/wooorm" } }, - "packages/local-storage-discovery/node_modules/locate-path": { + "packages/local-discovery/node_modules/locate-path": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-7.2.0.tgz", "integrity": "sha512-gvVijfZvn7R+2qyPX8mAuKcFGDf6Nc61GdvGafQsHL0sBIxfKzA+usWn4GFC/bk+QdwPUD4kWFJLhElipq+0VA==", @@ -28150,7 +28149,7 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "packages/local-storage-discovery/node_modules/p-limit": { + "packages/local-discovery/node_modules/p-limit": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-4.0.0.tgz", "integrity": "sha512-5b0R4txpzjPWVw/cXXUResoD4hb6U/x9BH08L7nw+GN1sezDzPdxeRvpc9c433fZhBan/wusjbCsqwqm4EIBIQ==", @@ -28165,7 +28164,7 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "packages/local-storage-discovery/node_modules/p-locate": { + "packages/local-discovery/node_modules/p-locate": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-6.0.0.tgz", "integrity": "sha512-wPrq66Llhl7/4AGC6I+cqxT07LhXvWL08LNXz1fENOw0Ap4sRZZ/gZpTTJ5jpurzzzfS2W/Ge9BY3LgLjCShcw==", @@ -28180,7 +28179,7 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "packages/local-storage-discovery/node_modules/path-exists": { + "packages/local-discovery/node_modules/path-exists": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-5.0.0.tgz", "integrity": "sha512-RjhtfwJOxzcFmNOi6ltcbcu4Iu+FL3zEj83dk4kAS+fVpTxXLO1b38RvJgT/0QwvV/L3aY9TAnyv0EOqW4GoMQ==", @@ -28189,7 +28188,7 @@ "node": "^12.20.0 || ^14.13.1 || >=16.0.0" } }, - "packages/local-storage-discovery/node_modules/yocto-queue": { + "packages/local-discovery/node_modules/yocto-queue": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-1.0.0.tgz", "integrity": "sha512-9bnSc/HEW2uRy67wc+T8UwauLuPJVn28jb+GtJY16iiKWyvmYJRXVT4UamsAEGQfPohgr2q4Tq0sQbQlxTfi1g==", @@ -28201,6 +28200,32 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "packages/local-storage-discovery": { + "name": "@waku/local-discovery", + "version": "0.0.1", + "extraneous": true, + "license": "MIT OR Apache-2.0", + "dependencies": { + "@libp2p/interface": "^1.1.2", + "@waku/interfaces": "^0.0.21", + "@waku/utils": "^0.0.14" + }, + "devDependencies": { + "@rollup/plugin-commonjs": "^25.0.7", + "@rollup/plugin-json": "^6.0.0", + "@rollup/plugin-node-resolve": "^15.2.3", + "@types/chai": "^4.3.11", + "@waku/build-utils": "*", + "chai": "^4.3.10", + "cspell": "^7.3.2", + "mocha": "^10.2.0", + "npm-run-all": "^4.1.5", + "rollup": "^4.9.5" + }, + "engines": { + "node": ">=18" + } + }, "packages/message-encryption": { "name": "@waku/message-encryption", "version": "0.0.24", @@ -28357,7 +28382,7 @@ "@waku/core": "0.0.26", "@waku/dns-discovery": "0.0.20", "@waku/interfaces": "0.0.21", - "@waku/local-storage-discovery": "^0.0.1", + "@waku/local-discovery": "^0.0.1", "@waku/peer-exchange": "^0.0.19", "@waku/relay": "0.0.9", "@waku/utils": "0.0.14", @@ -28388,7 +28413,7 @@ "@waku/core": "*", "@waku/enr": "*", "@waku/interfaces": "*", - "@waku/local-storage-discovery": "^0.0.1", + "@waku/local-discovery": "^0.0.1", "@waku/utils": "*", "app-root-path": "^3.1.0", "chai-as-promised": "^7.1.1", @@ -32375,8 +32400,8 @@ "npm-run-all": "^4.1.5" } }, - "@waku/local-storage-discovery": { - "version": "file:packages/local-storage-discovery", + "@waku/local-discovery": { + "version": "file:packages/local-discovery", "requires": { "@libp2p/interface": "^1.1.2", "@rollup/plugin-commonjs": "^25.0.7", @@ -32843,7 +32868,7 @@ "@waku/core": "0.0.26", "@waku/dns-discovery": "0.0.20", "@waku/interfaces": "0.0.21", - "@waku/local-storage-discovery": "^0.0.1", + "@waku/local-discovery": "^0.0.1", "@waku/peer-exchange": "^0.0.19", "@waku/relay": "0.0.9", "@waku/utils": "0.0.14", @@ -32870,7 +32895,7 @@ "@waku/dns-discovery": "*", "@waku/enr": "*", "@waku/interfaces": "*", - "@waku/local-storage-discovery": "^0.0.1", + "@waku/local-discovery": "^0.0.1", "@waku/message-encryption": "*", "@waku/peer-exchange": "*", "@waku/sdk": "*", diff --git a/packages/local-storage-discovery/.eslintrc.cjs b/packages/local-discovery/.eslintrc.cjs similarity index 100% rename from packages/local-storage-discovery/.eslintrc.cjs rename to packages/local-discovery/.eslintrc.cjs diff --git a/packages/local-storage-discovery/.mocha.reporters.json b/packages/local-discovery/.mocha.reporters.json similarity index 100% rename from packages/local-storage-discovery/.mocha.reporters.json rename to packages/local-discovery/.mocha.reporters.json diff --git a/packages/local-storage-discovery/.mocharc.cjs b/packages/local-discovery/.mocharc.cjs similarity index 100% rename from packages/local-storage-discovery/.mocharc.cjs rename to packages/local-discovery/.mocharc.cjs diff --git a/packages/local-storage-discovery/CHANGELOG.md b/packages/local-discovery/CHANGELOG.md similarity index 100% rename from packages/local-storage-discovery/CHANGELOG.md rename to packages/local-discovery/CHANGELOG.md diff --git a/packages/local-storage-discovery/karma.conf.cjs b/packages/local-discovery/karma.conf.cjs similarity index 100% rename from packages/local-storage-discovery/karma.conf.cjs rename to packages/local-discovery/karma.conf.cjs diff --git a/packages/local-storage-discovery/package.json b/packages/local-discovery/package.json similarity index 100% rename from packages/local-storage-discovery/package.json rename to packages/local-discovery/package.json diff --git a/packages/local-storage-discovery/rollup.config.js b/packages/local-discovery/rollup.config.js similarity index 100% rename from packages/local-storage-discovery/rollup.config.js rename to packages/local-discovery/rollup.config.js diff --git a/packages/local-storage-discovery/src/index.ts b/packages/local-discovery/src/index.ts similarity index 98% rename from packages/local-storage-discovery/src/index.ts rename to packages/local-discovery/src/index.ts index 7a982c13da..4aaf78af4a 100644 --- a/packages/local-storage-discovery/src/index.ts +++ b/packages/local-discovery/src/index.ts @@ -42,7 +42,7 @@ export class LocalStorageDiscovery } get [Symbol.toStringTag](): string { - return "@waku/local-storage-discovery"; + return "@waku/local-discovery"; } async start(): Promise { diff --git a/packages/local-storage-discovery/tsconfig.dev.json b/packages/local-discovery/tsconfig.dev.json similarity index 100% rename from packages/local-storage-discovery/tsconfig.dev.json rename to packages/local-discovery/tsconfig.dev.json diff --git a/packages/local-storage-discovery/tsconfig.json b/packages/local-discovery/tsconfig.json similarity index 100% rename from packages/local-storage-discovery/tsconfig.json rename to packages/local-discovery/tsconfig.json diff --git a/packages/local-storage-discovery/tsconfig.karma.json b/packages/local-discovery/tsconfig.karma.json similarity index 100% rename from packages/local-storage-discovery/tsconfig.karma.json rename to packages/local-discovery/tsconfig.karma.json diff --git a/packages/local-storage-discovery/typedoc.json b/packages/local-discovery/typedoc.json similarity index 100% rename from packages/local-storage-discovery/typedoc.json rename to packages/local-discovery/typedoc.json diff --git a/packages/sdk/package.json b/packages/sdk/package.json index a39a13d53e..5159960012 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -69,7 +69,7 @@ "@waku/core": "0.0.26", "@waku/dns-discovery": "0.0.20", "@waku/interfaces": "0.0.21", - "@waku/local-storage-discovery": "^0.0.1", + "@waku/local-discovery": "^0.0.1", "@waku/peer-exchange": "^0.0.19", "@waku/relay": "0.0.9", "@waku/utils": "0.0.14", diff --git a/packages/sdk/src/create.ts b/packages/sdk/src/create.ts index dca2fadeeb..bbdd3e8c80 100644 --- a/packages/sdk/src/create.ts +++ b/packages/sdk/src/create.ts @@ -26,7 +26,7 @@ import { type ProtocolCreateOptions, type ShardInfo } from "@waku/interfaces"; -import { wakuLocalStorageDiscovery } from "@waku/local-storage-discovery"; +import { wakuLocalStorageDiscovery } from "@waku/local-discovery"; import { wakuPeerExchangeDiscovery } from "@waku/peer-exchange"; import { RelayCreateOptions, wakuGossipSub, wakuRelay } from "@waku/relay"; import { ensureShardingConfigured } from "@waku/utils"; diff --git a/packages/tests/package.json b/packages/tests/package.json index 77076c35f3..b028d409d3 100644 --- a/packages/tests/package.json +++ b/packages/tests/package.json @@ -55,7 +55,7 @@ "@waku/core": "*", "@waku/enr": "*", "@waku/interfaces": "*", - "@waku/local-storage-discovery": "^0.0.1", + "@waku/local-discovery": "^0.0.1", "@waku/utils": "*", "app-root-path": "^3.1.0", "chai-as-promised": "^7.1.1", From 57c273025cbc198891de2a64717f69c0508de17c Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Tue, 13 Feb 2024 23:02:08 +0530 Subject: [PATCH 12/23] fix: add compliance test with no external deps on service node --- packages/local-discovery/src/index.ts | 5 +- .../tests/local_storage_discovery.spec.ts | 87 ++++--------------- packages/utils/src/common/index.ts | 10 ++- 3 files changed, 27 insertions(+), 75 deletions(-) diff --git a/packages/local-discovery/src/index.ts b/packages/local-discovery/src/index.ts index 4aaf78af4a..a10b8e98f5 100644 --- a/packages/local-discovery/src/index.ts +++ b/packages/local-discovery/src/index.ts @@ -64,6 +64,7 @@ export class LocalStorageDiscovery if (await this.components.peerStore.has(peerId)) return; await this.components.peerStore.save(peerId, { + multiaddrs: [multiaddr(address)], tags: { [DEFAULT_LOCAL_TAG_NAME]: { value: this.options?.tagValue ?? DEFAULT_LOCAL_TAG_VALUE, @@ -98,7 +99,9 @@ export class LocalStorageDiscovery handleNewPeers = (event: CustomEvent): void => { const { peer } = event.detail; - const websocketMultiaddr = getWsMultiaddrFromMultiaddrs(peer.addresses); + const { multiaddr: websocketMultiaddr } = getWsMultiaddrFromMultiaddrs( + peer.addresses + ); const localStoragePeers = this.getPeersFromLocalStorage(); diff --git a/packages/tests/tests/local_storage_discovery.spec.ts b/packages/tests/tests/local_storage_discovery.spec.ts index 09ed432cf1..1737e67f46 100644 --- a/packages/tests/tests/local_storage_discovery.spec.ts +++ b/packages/tests/tests/local_storage_discovery.spec.ts @@ -1,93 +1,40 @@ import tests from "@libp2p/interface-compliance-tests/peer-discovery"; import { LocalStoragePeerInfo } from "@waku/interfaces"; -import { LocalStorageDiscovery } from "@waku/local-storage-discovery"; +import { LocalStorageDiscovery } from "@waku/local-discovery"; import { createLightNode, type LightNode } from "@waku/sdk"; -import { expect } from "chai"; import { LocalStorage } from "node-localstorage"; -import { makeLogFileName, ServiceNode } from "../src/index.js"; - global.localStorage = new LocalStorage("./mock_local_storage"); describe("Local Storage Discovery", function () { this.timeout(25_000); let waku: LightNode; - let serviceNode1: ServiceNode; - let serviceNode2: ServiceNode; - this.beforeEach(async function () { - serviceNode1 = new ServiceNode(makeLogFileName(this) + "1"); - serviceNode2 = new ServiceNode(makeLogFileName(this) + "1"); - await serviceNode1.start(); - await serviceNode2.start(); - await setPeersInLocalStorage([serviceNode1, serviceNode2]); - }); - it("Compliance Test", async function () { - waku = await createLightNode(); - await waku.start(); + this.beforeEach(async function () {}); + + describe.only("Compliance Tests", function () { tests({ async setup() { + await setPeersInLocalStorage(); + waku = await createLightNode(); return new LocalStorageDiscovery(waku.libp2p.components); }, async teardown() {} }); }); - it("Should discover peers from local storage", async function () { - waku = await createLightNode({ defaultBootstrap: true }); - await waku.start(); - - const serviceNode1PeerId = await serviceNode1.getPeerId(); - const serviceNode2PeerId = await serviceNode2.getPeerId(); - const serviceNode1Ma = await serviceNode1.getMultiaddrWithId(); - const serviceNode2Ma = await serviceNode2.getMultiaddrWithId(); - - let serviceNode1Received = false; - let serviceNode2Received = false; - - await new Promise((resolve) => { - waku.libp2p.addEventListener("peer:update", (event) => { - const { peer } = event.detail; - - if (peer.id.toString() === serviceNode1PeerId.toString()) { - serviceNode1Received = true; + async function setPeersInLocalStorage(): Promise { + const MOCK_PEER_ID = + "16Uiu2HAm4v86W3bmT1BiH6oSPzcsSr24iDQpSN5Qa992BCjjwgrD"; + const MOCK_MA = `/ip4/127.0.0.1/tcp/8000/ws/p2p/${MOCK_PEER_ID}`; - const hasValidAddress = peer.addresses.some( - (addr) => addr.toString() === serviceNode1Ma.toString() - ); - expect(hasValidAddress).to.eq(true); - } else if (peer.id.toString() === serviceNode2PeerId.toString()) { - serviceNode2Received = true; - const hasValidAddress = peer.addresses.some( - (addr) => addr.toString() === serviceNode2Ma.toString() - ); - expect(hasValidAddress).to.eq(true); - } - - if (serviceNode1Received && serviceNode2Received) { - resolve(true); - } - }); - }); - - expect(serviceNode1Received).to.eq(true); - expect(serviceNode2Received).to.eq(true); - }); -}); - -async function setPeersInLocalStorage( - serviceNodes: ServiceNode[] -): Promise { - const peers: LocalStoragePeerInfo[] = []; - for (const node of serviceNodes) { - const nodePeerId = await node.getPeerId(); - const nodeMa = await node.getMultiaddrWithId(); + const peers: LocalStoragePeerInfo[] = []; peers.push({ - id: nodePeerId.toString(), - address: nodeMa.toString() + id: MOCK_PEER_ID.toString(), + address: MOCK_MA }); + const localStorage = global.localStorage; + const peersStr = JSON.stringify(peers); + localStorage.setItem("waku:peers", peersStr); } - const localStorage = global.localStorage; - const peersStr = JSON.stringify(peers); - localStorage.setItem("waku:peers", peersStr); -} +}); diff --git a/packages/utils/src/common/index.ts b/packages/utils/src/common/index.ts index 6485b9348d..d50c591c96 100644 --- a/packages/utils/src/common/index.ts +++ b/packages/utils/src/common/index.ts @@ -17,12 +17,14 @@ export function removeItemFromArray(arr: unknown[], value: unknown): unknown[] { return arr; } -export function getWsMultiaddrFromMultiaddrs(multiaddrs: Address[]): Address { - const wsMultiaddr = multiaddrs.find((m) => - m.multiaddr.toString().includes("ws" || "wss") +export function getWsMultiaddrFromMultiaddrs(addresses: Address[]): Address { + const wsMultiaddr = addresses.find( + (addr) => + addr.multiaddr.toString().includes("ws") || + addr.multiaddr.toString().includes("wss") ); if (!wsMultiaddr) { - throw new Error("No ws multiaddr found in the given multiaddrs"); + throw new Error("No ws multiaddr found in the given addresses"); } return wsMultiaddr; } From ceb97d13302e9c682165a6990c294a37aa0ba047 Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Tue, 13 Feb 2024 23:54:55 +0530 Subject: [PATCH 13/23] use peer:identify instead of peer:update --- packages/local-discovery/src/index.ts | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/packages/local-discovery/src/index.ts b/packages/local-discovery/src/index.ts index a10b8e98f5..9ad210be77 100644 --- a/packages/local-discovery/src/index.ts +++ b/packages/local-discovery/src/index.ts @@ -1,10 +1,10 @@ import { TypedEventEmitter } from "@libp2p/interface"; import { CustomEvent, + IdentifyResult, PeerDiscovery, PeerDiscoveryEvents, PeerInfo, - PeerUpdate, Startable } from "@libp2p/interface"; import { createFromJSON } from "@libp2p/peer-id-factory"; @@ -52,7 +52,10 @@ export class LocalStorageDiscovery log.info("Starting Local Storage Discovery"); - this.components.events.addEventListener("peer:update", this.handleNewPeers); + this.components.events.addEventListener( + "peer:identify", + this.handleNewPeers + ); const localStoragePeers = this.getPeersFromLocalStorage(); @@ -90,23 +93,21 @@ export class LocalStorageDiscovery if (!this.isStarted) return; log.info("Stopping Local Storage Discovery"); this.components.events.removeEventListener( - "peer:update", + "peer:identify", this.handleNewPeers ); this.isStarted = false; } - handleNewPeers = (event: CustomEvent): void => { - const { peer } = event.detail; + handleNewPeers = (event: CustomEvent): void => { + const peer = event.detail; - const { multiaddr: websocketMultiaddr } = getWsMultiaddrFromMultiaddrs( - peer.addresses - ); + const websocketMultiaddr = getWsMultiaddrFromMultiaddrs(peer.listenAddrs); const localStoragePeers = this.getPeersFromLocalStorage(); const existingPeerIndex = localStoragePeers.findIndex( - (_peer) => _peer.id === peer.id.toString() + (_peer) => _peer.id === peer.peerId.toString() ); if (existingPeerIndex >= 0) { @@ -114,7 +115,7 @@ export class LocalStorageDiscovery websocketMultiaddr.toString(); } else { localStoragePeers.push({ - id: peer.id.toString(), + id: peer.peerId.toString(), address: websocketMultiaddr.toString() }); } From bf1b4ab8dddfca70137f51a262b8431d741a36ff Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Tue, 13 Feb 2024 23:55:21 +0530 Subject: [PATCH 14/23] add: unit tests & remove sdk dependency --- packages/local-discovery/package.json | 2 +- .../tests/local_storage_discovery.spec.ts | 144 +++++++++++++++--- packages/utils/src/common/index.ts | 11 +- 3 files changed, 131 insertions(+), 26 deletions(-) diff --git a/packages/local-discovery/package.json b/packages/local-discovery/package.json index 7f6e61b8f1..b2dc5ee65b 100644 --- a/packages/local-discovery/package.json +++ b/packages/local-discovery/package.json @@ -12,7 +12,7 @@ }, "type": "module", "author": "Waku Team", - "homepage": "https://github.com/waku-org/js-waku/tree/master/packages/local-storage#readme", + "homepage": "https://github.com/waku-org/js-waku/tree/master/packages/local-discovery#readme", "repository": { "type": "git", "url": "https://github.com/waku-org/js-waku.git" diff --git a/packages/tests/tests/local_storage_discovery.spec.ts b/packages/tests/tests/local_storage_discovery.spec.ts index 1737e67f46..f837d76702 100644 --- a/packages/tests/tests/local_storage_discovery.spec.ts +++ b/packages/tests/tests/local_storage_discovery.spec.ts @@ -1,40 +1,146 @@ +import type { IdentifyResult } from "@libp2p/interface"; +import { TypedEventEmitter } from "@libp2p/interface"; import tests from "@libp2p/interface-compliance-tests/peer-discovery"; +import { prefixLogger } from "@libp2p/logger"; +import { createSecp256k1PeerId } from "@libp2p/peer-id-factory"; +import { createFromJSON } from "@libp2p/peer-id-factory"; +import { PersistentPeerStore } from "@libp2p/peer-store"; +import { multiaddr } from "@multiformats/multiaddr"; +import { Libp2pComponents } from "@waku/interfaces"; import { LocalStoragePeerInfo } from "@waku/interfaces"; import { LocalStorageDiscovery } from "@waku/local-discovery"; -import { createLightNode, type LightNode } from "@waku/sdk"; +import chai, { expect } from "chai"; +import chaiAsPromised from "chai-as-promised"; +import { MemoryDatastore } from "datastore-core/memory"; import { LocalStorage } from "node-localstorage"; +import sinon from "sinon"; + +chai.use(chaiAsPromised); global.localStorage = new LocalStorage("./mock_local_storage"); +const mockPeers = [ + { + id: "16Uiu2HAm4v86W3bmT1BiH6oSPzcsSr24iDQpSN5Qa992BCjjwgrD", + address: + "/ip4/127.0.0.1/tcp/8000/ws/p2p/16Uiu2HAm4v86W3bmT1BiH6oSPzcsSr24iDQpSN5Qa992BCjjwgrD" + }, + { + id: "16Uiu2HAm4v86W3bmT1BiH6oSPzcsSr24iDQpSN5Qa992BCjjwgrE", + address: + "/ip4/127.0.0.1/tcp/8001/ws/p2p/16Uiu2HAm4v86W3bmT1BiH6oSPzcsSr24iDQpSN5Qa992BCjjwgrE" + } +]; + +async function setPeersInLocalStorage( + peers: LocalStoragePeerInfo[] +): Promise { + localStorage.setItem("waku:peers", JSON.stringify(peers)); +} + describe("Local Storage Discovery", function () { this.timeout(25_000); - let waku: LightNode; + let components: Libp2pComponents; + + beforeEach(async function () { + localStorage.clear(); + components = { + peerStore: new PersistentPeerStore({ + events: new TypedEventEmitter(), + peerId: await createSecp256k1PeerId(), + datastore: new MemoryDatastore(), + logger: prefixLogger("local_discovery.spec.ts") + }), + events: new TypedEventEmitter() + } as unknown as Libp2pComponents; + }); - this.beforeEach(async function () {}); + describe("Compliance Tests", function () { + beforeEach(async function () { + await setPeersInLocalStorage([mockPeers[0]]); + }); - describe.only("Compliance Tests", function () { tests({ async setup() { - await setPeersInLocalStorage(); - waku = await createLightNode(); - return new LocalStorageDiscovery(waku.libp2p.components); + return new LocalStorageDiscovery(components); }, async teardown() {} }); }); - async function setPeersInLocalStorage(): Promise { - const MOCK_PEER_ID = - "16Uiu2HAm4v86W3bmT1BiH6oSPzcsSr24iDQpSN5Qa992BCjjwgrD"; - const MOCK_MA = `/ip4/127.0.0.1/tcp/8000/ws/p2p/${MOCK_PEER_ID}`; + describe("Unit Tests", function () { + let discovery: LocalStorageDiscovery; - const peers: LocalStoragePeerInfo[] = []; - peers.push({ - id: MOCK_PEER_ID.toString(), - address: MOCK_MA + beforeEach(async function () { + discovery = new LocalStorageDiscovery(components); + await setPeersInLocalStorage(mockPeers); }); - const localStorage = global.localStorage; - const peersStr = JSON.stringify(peers); - localStorage.setItem("waku:peers", peersStr); - } + + it("should load peers from local storage and dispatch events", async () => { + const dispatchEventSpy = sinon.spy(discovery, "dispatchEvent"); + + await discovery.start(); + + expect(dispatchEventSpy.calledWith(sinon.match.has("type", "peer"))).to.be + .true; + mockPeers.forEach((mockPeer) => { + expect( + dispatchEventSpy.calledWith( + sinon.match.hasNested("detail.id", mockPeer.id) + ) + ).to.be.true; + }); + }); + + it("should update peers in local storage on 'peer:identify' event", async () => { + const newPeerIdentifyEvent = { + detail: { + peerId: await createFromJSON({ + id: mockPeers[1].id + }), + listenAddrs: [multiaddr(mockPeers[1].address)] + } + } as CustomEvent; + + // Directly invoke handleNewPeers to simulate receiving an 'identify' event + discovery.handleNewPeers(newPeerIdentifyEvent); + + const updatedPeers = JSON.parse( + localStorage.getItem("waku:peers") || "[]" + ); + expect(updatedPeers).to.deep.include({ + id: newPeerIdentifyEvent.detail.peerId.toString(), + address: newPeerIdentifyEvent.detail.listenAddrs[0].toString() + }); + }); + + it("should handle corrupted local storage data gracefully", async () => { + localStorage.setItem("waku:peers", "not-a-valid-json"); + + try { + await discovery.start(); + } catch (error) { + expect.fail( + "start() should not have thrown an error for corrupted local storage data" + ); + } + }); + + it("should add and remove event listeners correctly", async () => { + const addEventListenerSpy = sinon.spy( + components.events, + "addEventListener" + ); + const removeEventListenerSpy = sinon.spy( + components.events, + "removeEventListener" + ); + + await discovery.start(); + expect(addEventListenerSpy.calledWith("peer:identify")).to.be.true; + + await discovery.stop(); + expect(removeEventListenerSpy.calledWith("peer:identify")).to.be.true; + }); + }); }); diff --git a/packages/utils/src/common/index.ts b/packages/utils/src/common/index.ts index d50c591c96..906bfb948d 100644 --- a/packages/utils/src/common/index.ts +++ b/packages/utils/src/common/index.ts @@ -1,5 +1,4 @@ -import type { Address } from "@libp2p/interface"; - +import type { Multiaddr } from "@multiformats/multiaddr"; export * from "./is_defined.js"; export * from "./random_subset.js"; export * from "./group_by.js"; @@ -17,11 +16,11 @@ export function removeItemFromArray(arr: unknown[], value: unknown): unknown[] { return arr; } -export function getWsMultiaddrFromMultiaddrs(addresses: Address[]): Address { +export function getWsMultiaddrFromMultiaddrs( + addresses: Multiaddr[] +): Multiaddr { const wsMultiaddr = addresses.find( - (addr) => - addr.multiaddr.toString().includes("ws") || - addr.multiaddr.toString().includes("wss") + (addr) => addr.toString().includes("ws") || addr.toString().includes("wss") ); if (!wsMultiaddr) { throw new Error("No ws multiaddr found in the given addresses"); From 6904f0bee5d86b9b296269802de39b37f2e02557 Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Tue, 13 Feb 2024 23:59:07 +0530 Subject: [PATCH 15/23] move tests to self package --- .gitignore | 2 +- package-lock.json | 15 ++++++++++++--- packages/local-discovery/package.json | 5 ++++- .../src/local_discovery.spec.ts} | 3 ++- 4 files changed, 19 insertions(+), 6 deletions(-) rename packages/{tests/tests/local_storage_discovery.spec.ts => local-discovery/src/local_discovery.spec.ts} (98%) diff --git a/.gitignore b/.gitignore index babba11cfa..1a3c2625c9 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,4 @@ test-results playwright-report example allure-results -packages/tests/mock_local_storage \ No newline at end of file +packages/local-discovery/mock_local_storage \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 8cd2d41d81..ade4e7f2c0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7416,7 +7416,8 @@ }, "node_modules/chai-as-promised": { "version": "7.1.1", - "license": "WTFPL", + "resolved": "https://registry.npmjs.org/chai-as-promised/-/chai-as-promised-7.1.1.tgz", + "integrity": "sha512-azL6xMoi+uxu6z4rhWQ1jbdUhOMhis2PvscD/xjLqNMkv3BPPp2JyyuTHOrf9BOosGpNQ11v6BKv/g57RXbiaA==", "dependencies": { "check-error": "^1.0.2" }, @@ -27942,10 +27943,13 @@ "@types/chai": "^4.3.11", "@waku/build-utils": "*", "chai": "^4.3.10", + "chai-as-promised": "^7.1.1", "cspell": "^7.3.2", "mocha": "^10.2.0", + "node-localstorage": "^3.0.5", "npm-run-all": "^4.1.5", - "rollup": "^4.9.5" + "rollup": "^4.9.5", + "sinon": "^17.0.1" }, "engines": { "node": ">=18" @@ -32606,10 +32610,13 @@ "@waku/interfaces": "^0.0.21", "@waku/utils": "^0.0.14", "chai": "^4.3.10", + "chai-as-promised": "^7.1.1", "cspell": "^7.3.2", "mocha": "^10.2.0", + "node-localstorage": "^3.0.5", "npm-run-all": "^4.1.5", - "rollup": "^4.9.5" + "rollup": "^4.9.5", + "sinon": "^17.0.1" }, "dependencies": { "@cspell/cspell-bundled-dicts": { @@ -34372,6 +34379,8 @@ }, "chai-as-promised": { "version": "7.1.1", + "resolved": "https://registry.npmjs.org/chai-as-promised/-/chai-as-promised-7.1.1.tgz", + "integrity": "sha512-azL6xMoi+uxu6z4rhWQ1jbdUhOMhis2PvscD/xjLqNMkv3BPPp2JyyuTHOrf9BOosGpNQ11v6BKv/g57RXbiaA==", "requires": { "check-error": "^1.0.2" } diff --git a/packages/local-discovery/package.json b/packages/local-discovery/package.json index b2dc5ee65b..98725573d0 100644 --- a/packages/local-discovery/package.json +++ b/packages/local-discovery/package.json @@ -61,10 +61,13 @@ "@types/chai": "^4.3.11", "@waku/build-utils": "*", "chai": "^4.3.10", + "chai-as-promised": "^7.1.1", "cspell": "^7.3.2", "mocha": "^10.2.0", + "node-localstorage": "^3.0.5", "npm-run-all": "^4.1.5", - "rollup": "^4.9.5" + "rollup": "^4.9.5", + "sinon": "^17.0.1" }, "files": [ "dist", diff --git a/packages/tests/tests/local_storage_discovery.spec.ts b/packages/local-discovery/src/local_discovery.spec.ts similarity index 98% rename from packages/tests/tests/local_storage_discovery.spec.ts rename to packages/local-discovery/src/local_discovery.spec.ts index f837d76702..23eba055cd 100644 --- a/packages/tests/tests/local_storage_discovery.spec.ts +++ b/packages/local-discovery/src/local_discovery.spec.ts @@ -8,13 +8,14 @@ import { PersistentPeerStore } from "@libp2p/peer-store"; import { multiaddr } from "@multiformats/multiaddr"; import { Libp2pComponents } from "@waku/interfaces"; import { LocalStoragePeerInfo } from "@waku/interfaces"; -import { LocalStorageDiscovery } from "@waku/local-discovery"; import chai, { expect } from "chai"; import chaiAsPromised from "chai-as-promised"; import { MemoryDatastore } from "datastore-core/memory"; import { LocalStorage } from "node-localstorage"; import sinon from "sinon"; +import { LocalStorageDiscovery } from "./index.js"; + chai.use(chaiAsPromised); global.localStorage = new LocalStorage("./mock_local_storage"); From ff3123196e284dd50c49ca15461a5e7141227874 Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Thu, 15 Feb 2024 16:23:56 +0530 Subject: [PATCH 16/23] update cspell + remove unrequired deps --- .cspell.json | 1 + package-lock.json | 30 +++++++----------------------- packages/tests/package.json | 2 -- 3 files changed, 8 insertions(+), 25 deletions(-) diff --git a/.cspell.json b/.cspell.json index f5cdae8727..4d79829a48 100644 --- a/.cspell.json +++ b/.cspell.json @@ -4,6 +4,7 @@ "language": "en", "words": [ "abortable", + "Addrs", "ahadns", "Alives", "asym", diff --git a/package-lock.json b/package-lock.json index ade4e7f2c0..5be6425dbd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4800,15 +4800,6 @@ "version": "20.6.3", "license": "MIT" }, - "node_modules/@types/node-localstorage": { - "version": "1.3.3", - "resolved": "https://registry.npmjs.org/@types/node-localstorage/-/node-localstorage-1.3.3.tgz", - "integrity": "sha512-Wkn5g4eM5x10UNV9Xvl9K6y6m0zorocuJy4WjB5muUdyMZuPbZpSJG3hlhjGHe1HGxbOQO7RcB+jlHcNwkh+Jw==", - "dev": true, - "dependencies": { - "@types/node": "*" - } - }, "node_modules/@types/normalize-package-data": { "version": "2.4.4", "resolved": "https://registry.npmjs.org/@types/normalize-package-data/-/normalize-package-data-2.4.4.tgz", @@ -17064,6 +17055,7 @@ "version": "3.0.5", "resolved": "https://registry.npmjs.org/node-localstorage/-/node-localstorage-3.0.5.tgz", "integrity": "sha512-GCwtK33iwVXboZWYcqQHu3aRvXEBwmPkAMRBLeaX86ufhqslyUkLGsi4aW3INEfdQYpUB5M9qtYf3eHvAk2VBg==", + "dev": true, "dependencies": { "write-file-atomic": "^5.0.1" }, @@ -17075,6 +17067,7 @@ "version": "4.1.0", "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-4.1.0.tgz", "integrity": "sha512-bzyZ1e88w9O1iNJbKnOlvYTrWPDl46O1bG0D3XInv+9tkPrxrN8jUUTiFlDkkmKWgn1M6CfIA13SuGqOa9Korw==", + "dev": true, "engines": { "node": ">=14" }, @@ -17086,6 +17079,7 @@ "version": "5.0.1", "resolved": "https://registry.npmjs.org/write-file-atomic/-/write-file-atomic-5.0.1.tgz", "integrity": "sha512-+QU2zd6OTD8XWIJCbffaiQeH9U73qIqafo1x6V1snCWYGJf6cVE0cDR4D8xRzcEnfI21IFrUPzPGtcPf8AC+Rw==", + "dev": true, "dependencies": { "imurmurhash": "^0.1.4", "signal-exit": "^4.0.1" @@ -28599,7 +28593,6 @@ "debug": "^4.3.4", "dockerode": "^3.3.5", "fast-check": "^3.15.1", - "node-localstorage": "^3.0.5", "p-retry": "^6.1.0", "p-timeout": "^6.1.0", "portfinder": "^1.0.32", @@ -28611,7 +28604,6 @@ "@types/chai": "^4.3.11", "@types/dockerode": "^3.3.19", "@types/mocha": "^10.0.6", - "@types/node-localstorage": "^1.3.3", "@types/sinon": "^17.0.3", "@types/tail": "^2.2.3", "@waku/dns-discovery": "*", @@ -32037,15 +32029,6 @@ "@types/node": { "version": "20.6.3" }, - "@types/node-localstorage": { - "version": "1.3.3", - "resolved": "https://registry.npmjs.org/@types/node-localstorage/-/node-localstorage-1.3.3.tgz", - "integrity": "sha512-Wkn5g4eM5x10UNV9Xvl9K6y6m0zorocuJy4WjB5muUdyMZuPbZpSJG3hlhjGHe1HGxbOQO7RcB+jlHcNwkh+Jw==", - "dev": true, - "requires": { - "@types/node": "*" - } - }, "@types/normalize-package-data": { "version": "2.4.4", "resolved": "https://registry.npmjs.org/@types/normalize-package-data/-/normalize-package-data-2.4.4.tgz", @@ -33089,7 +33072,6 @@ "@types/chai": "^4.3.11", "@types/dockerode": "^3.3.19", "@types/mocha": "^10.0.6", - "@types/node-localstorage": "^1.3.3", "@types/sinon": "^17.0.3", "@types/tail": "^2.2.3", "@waku/core": "*", @@ -33115,7 +33097,6 @@ "libp2p": "^1.1.2", "mocha": "^10.3.0", "mocha-multi-reporters": "^1.5.1", - "node-localstorage": "^3.0.5", "npm-run-all": "^4.1.5", "p-retry": "^6.1.0", "p-timeout": "^6.1.0", @@ -40789,6 +40770,7 @@ "version": "3.0.5", "resolved": "https://registry.npmjs.org/node-localstorage/-/node-localstorage-3.0.5.tgz", "integrity": "sha512-GCwtK33iwVXboZWYcqQHu3aRvXEBwmPkAMRBLeaX86ufhqslyUkLGsi4aW3INEfdQYpUB5M9qtYf3eHvAk2VBg==", + "dev": true, "requires": { "write-file-atomic": "^5.0.1" }, @@ -40796,12 +40778,14 @@ "signal-exit": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-4.1.0.tgz", - "integrity": "sha512-bzyZ1e88w9O1iNJbKnOlvYTrWPDl46O1bG0D3XInv+9tkPrxrN8jUUTiFlDkkmKWgn1M6CfIA13SuGqOa9Korw==" + "integrity": "sha512-bzyZ1e88w9O1iNJbKnOlvYTrWPDl46O1bG0D3XInv+9tkPrxrN8jUUTiFlDkkmKWgn1M6CfIA13SuGqOa9Korw==", + "dev": true }, "write-file-atomic": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/write-file-atomic/-/write-file-atomic-5.0.1.tgz", "integrity": "sha512-+QU2zd6OTD8XWIJCbffaiQeH9U73qIqafo1x6V1snCWYGJf6cVE0cDR4D8xRzcEnfI21IFrUPzPGtcPf8AC+Rw==", + "dev": true, "requires": { "imurmurhash": "^0.1.4", "signal-exit": "^4.0.1" diff --git a/packages/tests/package.json b/packages/tests/package.json index 486a3a1850..19a04f3895 100644 --- a/packages/tests/package.json +++ b/packages/tests/package.json @@ -61,7 +61,6 @@ "chai-as-promised": "^7.1.1", "debug": "^4.3.4", "dockerode": "^3.3.5", - "node-localstorage": "^3.0.5", "fast-check": "^3.15.1", "p-retry": "^6.1.0", "p-timeout": "^6.1.0", @@ -73,7 +72,6 @@ "@libp2p/bootstrap": "^10.0.11", "@types/chai": "^4.3.11", "@types/dockerode": "^3.3.19", - "@types/node-localstorage": "^1.3.3", "@types/mocha": "^10.0.6", "@types/sinon": "^17.0.3", "@types/tail": "^2.2.3", From db29bb8614065fd314839b2d2e5f910b02e8ded3 Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Thu, 15 Feb 2024 19:08:23 +0530 Subject: [PATCH 17/23] add types --- package-lock.json | 20 ++++++++++++++++++++ packages/local-discovery/package.json | 1 + 2 files changed, 21 insertions(+) diff --git a/package-lock.json b/package-lock.json index 5be6425dbd..b550f0ca26 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4800,6 +4800,15 @@ "version": "20.6.3", "license": "MIT" }, + "node_modules/@types/node-localstorage": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/@types/node-localstorage/-/node-localstorage-1.3.3.tgz", + "integrity": "sha512-Wkn5g4eM5x10UNV9Xvl9K6y6m0zorocuJy4WjB5muUdyMZuPbZpSJG3hlhjGHe1HGxbOQO7RcB+jlHcNwkh+Jw==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/normalize-package-data": { "version": "2.4.4", "resolved": "https://registry.npmjs.org/@types/normalize-package-data/-/normalize-package-data-2.4.4.tgz", @@ -27935,6 +27944,7 @@ "@rollup/plugin-json": "^6.0.0", "@rollup/plugin-node-resolve": "^15.2.3", "@types/chai": "^4.3.11", + "@types/node-localstorage": "^1.3.3", "@waku/build-utils": "*", "chai": "^4.3.10", "chai-as-promised": "^7.1.1", @@ -32029,6 +32039,15 @@ "@types/node": { "version": "20.6.3" }, + "@types/node-localstorage": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/@types/node-localstorage/-/node-localstorage-1.3.3.tgz", + "integrity": "sha512-Wkn5g4eM5x10UNV9Xvl9K6y6m0zorocuJy4WjB5muUdyMZuPbZpSJG3hlhjGHe1HGxbOQO7RcB+jlHcNwkh+Jw==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, "@types/normalize-package-data": { "version": "2.4.4", "resolved": "https://registry.npmjs.org/@types/normalize-package-data/-/normalize-package-data-2.4.4.tgz", @@ -32589,6 +32608,7 @@ "@rollup/plugin-json": "^6.0.0", "@rollup/plugin-node-resolve": "^15.2.3", "@types/chai": "^4.3.11", + "@types/node-localstorage": "^1.3.3", "@waku/build-utils": "*", "@waku/interfaces": "^0.0.21", "@waku/utils": "^0.0.14", diff --git a/packages/local-discovery/package.json b/packages/local-discovery/package.json index 98725573d0..5a45f72e27 100644 --- a/packages/local-discovery/package.json +++ b/packages/local-discovery/package.json @@ -59,6 +59,7 @@ "@rollup/plugin-json": "^6.0.0", "@rollup/plugin-node-resolve": "^15.2.3", "@types/chai": "^4.3.11", + "@types/node-localstorage": "^1.3.3", "@waku/build-utils": "*", "chai": "^4.3.10", "chai-as-promised": "^7.1.1", From 7cfb348579932c20510f5b0abb99db93fc62da0c Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Fri, 16 Feb 2024 18:15:23 +0530 Subject: [PATCH 18/23] maintain in-memory peers for localstorage --- packages/local-discovery/src/index.ts | 30 +++++++++++++-------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/packages/local-discovery/src/index.ts b/packages/local-discovery/src/index.ts index 9ad210be77..5b7d1d4c20 100644 --- a/packages/local-discovery/src/index.ts +++ b/packages/local-discovery/src/index.ts @@ -32,6 +32,7 @@ export class LocalStorageDiscovery implements PeerDiscovery, Startable { private isStarted: boolean; + private peers: LocalStoragePeerInfo[] = []; constructor( private readonly components: Libp2pComponents, @@ -39,6 +40,7 @@ export class LocalStorageDiscovery ) { super(); this.isStarted = false; + this.peers = this.getPeersFromLocalStorage(); } get [Symbol.toStringTag](): string { @@ -46,25 +48,17 @@ export class LocalStorageDiscovery } async start(): Promise { - if (this.isStarted) { - return; - } + if (this.isStarted) return; log.info("Starting Local Storage Discovery"); - this.components.events.addEventListener( "peer:identify", this.handleNewPeers ); - const localStoragePeers = this.getPeersFromLocalStorage(); - - for (const { id: idStr, address } of localStoragePeers) { - const peerId = await createFromJSON({ - id: idStr - }); - - if (await this.components.peerStore.has(peerId)) return; + for (const { id: idStr, address } of this.peers) { + const peerId = await createFromJSON({ id: idStr }); + if (await this.components.peerStore.has(peerId)) continue; await this.components.peerStore.save(peerId, { multiaddrs: [multiaddr(address)], @@ -86,7 +80,9 @@ export class LocalStorageDiscovery ); } - log.info(`Discovered ${localStoragePeers.length} peers`); + log.info(`Discovered ${this.peers.length} peers`); + + this.isStarted = true; } stop(): void | Promise { @@ -97,6 +93,8 @@ export class LocalStorageDiscovery this.handleNewPeers ); this.isStarted = false; + + this.setPeersInLocalStorage(); } handleNewPeers = (event: CustomEvent): void => { @@ -120,7 +118,7 @@ export class LocalStorageDiscovery }); } - this.setPeersInLocalStorage(localStoragePeers); + this.setPeersInLocalStorage(); }; private getPeersFromLocalStorage(): LocalStoragePeerInfo[] { @@ -135,8 +133,8 @@ export class LocalStorageDiscovery } } - private setPeersInLocalStorage(peers: LocalStoragePeerInfo[]): void { - localStorage.setItem("waku:peers", JSON.stringify(peers)); + private setPeersInLocalStorage(): void { + localStorage.setItem("waku:peers", JSON.stringify(this.peers)); } } From a39c4a9f701667c78b8df7b5a6032a217e886818 Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Fri, 16 Feb 2024 18:26:27 +0530 Subject: [PATCH 19/23] address comments --- package-lock.json | 2 -- packages/local-discovery/src/index.ts | 8 ++++---- packages/local-discovery/tsconfig.karma.json | 7 ------- packages/tests/package.json | 1 - 4 files changed, 4 insertions(+), 14 deletions(-) delete mode 100644 packages/local-discovery/tsconfig.karma.json diff --git a/package-lock.json b/package-lock.json index b550f0ca26..3d049260fb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -28596,7 +28596,6 @@ "@waku/core": "*", "@waku/enr": "*", "@waku/interfaces": "*", - "@waku/local-discovery": "^0.0.1", "@waku/utils": "*", "app-root-path": "^3.1.0", "chai-as-promised": "^7.1.1", @@ -33098,7 +33097,6 @@ "@waku/dns-discovery": "*", "@waku/enr": "*", "@waku/interfaces": "*", - "@waku/local-discovery": "^0.0.1", "@waku/message-encryption": "*", "@waku/peer-exchange": "*", "@waku/sdk": "*", diff --git a/packages/local-discovery/src/index.ts b/packages/local-discovery/src/index.ts index 5b7d1d4c20..d4f3b26835 100644 --- a/packages/local-discovery/src/index.ts +++ b/packages/local-discovery/src/index.ts @@ -98,14 +98,14 @@ export class LocalStorageDiscovery } handleNewPeers = (event: CustomEvent): void => { - const peer = event.detail; + const { peerId, listenAddrs } = event.detail; - const websocketMultiaddr = getWsMultiaddrFromMultiaddrs(peer.listenAddrs); + const websocketMultiaddr = getWsMultiaddrFromMultiaddrs(listenAddrs); const localStoragePeers = this.getPeersFromLocalStorage(); const existingPeerIndex = localStoragePeers.findIndex( - (_peer) => _peer.id === peer.peerId.toString() + (_peer) => _peer.id === peerId.toString() ); if (existingPeerIndex >= 0) { @@ -113,7 +113,7 @@ export class LocalStorageDiscovery websocketMultiaddr.toString(); } else { localStoragePeers.push({ - id: peer.peerId.toString(), + id: peerId.toString(), address: websocketMultiaddr.toString() }); } diff --git a/packages/local-discovery/tsconfig.karma.json b/packages/local-discovery/tsconfig.karma.json deleted file mode 100644 index eb6e3bcd34..0000000000 --- a/packages/local-discovery/tsconfig.karma.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "extends": "./tsconfig", - "compilerOptions": { - "module": "esnext" - }, - "exclude": [] -} diff --git a/packages/tests/package.json b/packages/tests/package.json index 19a04f3895..9fbb1ab196 100644 --- a/packages/tests/package.json +++ b/packages/tests/package.json @@ -55,7 +55,6 @@ "@waku/core": "*", "@waku/enr": "*", "@waku/interfaces": "*", - "@waku/local-discovery": "^0.0.1", "@waku/utils": "*", "app-root-path": "^3.1.0", "chai-as-promised": "^7.1.1", From c43ab3138a6802ac71852364ca63164573d32d1b Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Fri, 16 Feb 2024 18:28:51 +0530 Subject: [PATCH 20/23] chore: rename --- packages/local-discovery/src/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/local-discovery/src/index.ts b/packages/local-discovery/src/index.ts index d4f3b26835..100efbeb4e 100644 --- a/packages/local-discovery/src/index.ts +++ b/packages/local-discovery/src/index.ts @@ -94,7 +94,7 @@ export class LocalStorageDiscovery ); this.isStarted = false; - this.setPeersInLocalStorage(); + this.savePeersToLocalStorage(); } handleNewPeers = (event: CustomEvent): void => { @@ -118,7 +118,7 @@ export class LocalStorageDiscovery }); } - this.setPeersInLocalStorage(); + this.savePeersToLocalStorage(); }; private getPeersFromLocalStorage(): LocalStoragePeerInfo[] { @@ -133,7 +133,7 @@ export class LocalStorageDiscovery } } - private setPeersInLocalStorage(): void { + private savePeersToLocalStorage(): void { localStorage.setItem("waku:peers", JSON.stringify(this.peers)); } } From d904a050703e88641d10e6e4b60d2f1dc5ea9896 Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Fri, 16 Feb 2024 18:34:05 +0530 Subject: [PATCH 21/23] use name from options --- packages/local-discovery/src/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/local-discovery/src/index.ts b/packages/local-discovery/src/index.ts index 100efbeb4e..7fbd3122fd 100644 --- a/packages/local-discovery/src/index.ts +++ b/packages/local-discovery/src/index.ts @@ -19,6 +19,7 @@ import { getWsMultiaddrFromMultiaddrs, Logger } from "@waku/utils"; const log = new Logger("peer-exchange-discovery"); type LocalStorageDiscoveryOptions = { + tagName?: string; tagValue?: number; tagTTL?: number; }; @@ -63,7 +64,7 @@ export class LocalStorageDiscovery await this.components.peerStore.save(peerId, { multiaddrs: [multiaddr(address)], tags: { - [DEFAULT_LOCAL_TAG_NAME]: { + [this.options?.tagName ?? DEFAULT_LOCAL_TAG_NAME]: { value: this.options?.tagValue ?? DEFAULT_LOCAL_TAG_VALUE, ttl: this.options?.tagTTL ?? DEFAULT_LOCAL_TAG_TTL } From 5ceff8336a01449199ca7b4846778256cf436ccb Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Fri, 16 Feb 2024 19:11:22 +0530 Subject: [PATCH 22/23] fix: saving peers --- packages/local-discovery/src/index.ts | 1 + packages/local-discovery/src/local_discovery.spec.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/local-discovery/src/index.ts b/packages/local-discovery/src/index.ts index 7fbd3122fd..28549ac756 100644 --- a/packages/local-discovery/src/index.ts +++ b/packages/local-discovery/src/index.ts @@ -119,6 +119,7 @@ export class LocalStorageDiscovery }); } + this.peers = localStoragePeers; this.savePeersToLocalStorage(); }; diff --git a/packages/local-discovery/src/local_discovery.spec.ts b/packages/local-discovery/src/local_discovery.spec.ts index 23eba055cd..4a0ccd35dc 100644 --- a/packages/local-discovery/src/local_discovery.spec.ts +++ b/packages/local-discovery/src/local_discovery.spec.ts @@ -39,7 +39,7 @@ async function setPeersInLocalStorage( localStorage.setItem("waku:peers", JSON.stringify(peers)); } -describe("Local Storage Discovery", function () { +describe.only("Local Storage Discovery", function () { this.timeout(25_000); let components: Libp2pComponents; From dd5768ee9acf53d7e7b154ea89320d1cc6a3f414 Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Fri, 16 Feb 2024 19:47:41 +0530 Subject: [PATCH 23/23] rm: only --- packages/local-discovery/src/local_discovery.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/local-discovery/src/local_discovery.spec.ts b/packages/local-discovery/src/local_discovery.spec.ts index 4a0ccd35dc..23eba055cd 100644 --- a/packages/local-discovery/src/local_discovery.spec.ts +++ b/packages/local-discovery/src/local_discovery.spec.ts @@ -39,7 +39,7 @@ async function setPeersInLocalStorage( localStorage.setItem("waku:peers", JSON.stringify(peers)); } -describe.only("Local Storage Discovery", function () { +describe("Local Storage Discovery", function () { this.timeout(25_000); let components: Libp2pComponents;