From 1b6c2a486a6562a406965d8276dd64b7c8de9f9b Mon Sep 17 00:00:00 2001 From: Nabarun Gogoi Date: Wed, 28 Jun 2023 11:25:03 +0530 Subject: [PATCH] Add peer config options for `transports` and `peerDiscovery` (#387) * Add peer config options for transports and peerDiscovery * Add yamux muxer to peer libp2p --- packages/peer/package.json | 3 +- packages/peer/src/peer.ts | 23 +++++-- yarn.lock | 133 ++++++++++++++++++++++++++++++++++++- 3 files changed, 151 insertions(+), 8 deletions(-) diff --git a/packages/peer/package.json b/packages/peer/package.json index cdd765a8..e829e3f5 100644 --- a/packages/peer/package.json +++ b/packages/peer/package.json @@ -31,6 +31,7 @@ "@cerc-io/libp2p": "0.42.2-laconic-0.1.3", "@cerc-io/prometheus-metrics": "1.1.4", "@chainsafe/libp2p-noise": "^11.0.0", + "@chainsafe/libp2p-yamux": "^4.0.2", "@libp2p/floodsub": "^6.0.0", "@libp2p/mplex": "^7.1.1", "@libp2p/peer-id-factory": "^2.0.0", @@ -41,7 +42,6 @@ "buffer": "^6.0.3", "chai": "^4.3.4", "debug": "^4.3.1", - "dotenv": "^16.0.3", "it-length-prefixed": "^8.0.4", "it-map": "^2.0.0", "it-pipe": "^2.0.5", @@ -59,6 +59,7 @@ "@types/yargs": "^17.0.0", "@typescript-eslint/eslint-plugin": "^5.47.1", "@typescript-eslint/parser": "^5.47.1", + "dotenv": "^16.0.3", "eslint": "^8.35.0", "eslint-config-semistandard": "^15.0.1", "eslint-config-standard": "^16.0.3", diff --git a/packages/peer/src/peer.ts b/packages/peer/src/peer.ts index b1a4d102..86209219 100644 --- a/packages/peer/src/peer.ts +++ b/packages/peer/src/peer.ts @@ -12,10 +12,11 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'; import { toString as uint8ArrayToString } from 'uint8arrays/to-string'; import debug from 'debug'; -import { createLibp2p, Libp2p } from '@cerc-io/libp2p'; +import { createLibp2p, Libp2p, Libp2pInit } from '@cerc-io/libp2p'; import { webSockets } from '@libp2p/websockets'; import { noise } from '@chainsafe/libp2p-noise'; import { mplex } from '@libp2p/mplex'; +import { yamux } from '@chainsafe/libp2p-yamux'; import type { Stream as P2PStream, Connection } from '@libp2p/interface-connection'; import type { PeerInfo } from '@libp2p/interface-peer-info'; import type { Message } from '@libp2p/interface-pubsub'; @@ -67,6 +68,9 @@ export interface PeerInitConfig { minConnections?: number; dialTimeout?: number; enableDebugInfo?: boolean; + transports?: Libp2pInit['transports']; + listenMultiaddrs?: string[]; + peerDiscovery?: Libp2pInit['peerDiscovery']; } export class Peer { @@ -144,17 +148,24 @@ export class Peer { this._node = await createLibp2p({ peerId, - transports: [webSockets({ - filter: wsPeerFilter - })], + transports: [ + webSockets({ + filter: wsPeerFilter + }), + ...(initOptions.transports ?? []) + ], + addresses: { + listen: initOptions.listenMultiaddrs ?? [] + }, connectionEncryption: [noise()], - streamMuxers: [mplex()], + streamMuxers: [mplex(), yamux()], pubsub: floodsub({ globalSignaturePolicy: PUBSUB_SIGNATURE_POLICY }), peerDiscovery: [ // Use pubsub based discovery; relay server acts as a peer discovery source pubsubPeerDiscovery({ interval: PUBSUB_DISCOVERY_INTERVAL - }) + }), + ...(initOptions.peerDiscovery ?? []) ], relay: { enabled: true, diff --git a/yarn.lock b/yarn.lock index 7fd0f20e..c6c8ed9a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -392,6 +392,28 @@ uint8arraylist "^2.3.2" uint8arrays "^4.0.2" +"@chainsafe/libp2p-yamux@^4.0.2": + version "4.0.2" + resolved "https://registry.yarnpkg.com/@chainsafe/libp2p-yamux/-/libp2p-yamux-4.0.2.tgz#2e970da83cda5551c834fb786963007a71e772f1" + integrity sha512-p0m/4ab4JLaIQqUtxvm8bSqdt9sb0uXX8PFj1CQM1eJLeV1LxzzygaSOeLxN/5ckHCuK7q/9eb9xybvl6vz/JA== + dependencies: + "@libp2p/interface-connection" "^5.1.0" + "@libp2p/interface-stream-muxer" "^4.1.2" + "@libp2p/interfaces" "^3.3.2" + "@libp2p/logger" "^2.0.7" + abortable-iterator "^5.0.1" + any-signal "^4.1.1" + it-pipe "^3.0.1" + it-pushable "^3.1.3" + uint8arraylist "^2.4.3" + +"@chainsafe/netmask@^2.0.0": + version "2.0.0" + resolved "https://registry.yarnpkg.com/@chainsafe/netmask/-/netmask-2.0.0.tgz#0d4a75f47919f65011da4327a3845c9661f1038a" + integrity sha512-I3Z+6SWUoaljh3TBzCnCxjlUyN8tA+NAk5L6m9IxvCf1BENQTePzPMis97CoN/iMW1St3WN+AWCCRp+TTBRiDg== + dependencies: + "@chainsafe/is-ip" "^2.0.1" + "@cspotcode/source-map-support@^0.8.0": version "0.8.1" resolved "https://registry.yarnpkg.com/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz#00629c35a688e05a88b1cda684fb9d5e73f000a1" @@ -2331,6 +2353,17 @@ it-stream-types "^1.0.4" uint8arraylist "^2.1.2" +"@libp2p/interface-connection@^5.0.0", "@libp2p/interface-connection@^5.1.0": + version "5.1.1" + resolved "https://registry.yarnpkg.com/@libp2p/interface-connection/-/interface-connection-5.1.1.tgz#da0572c76da43629d52b8bec6cd092143fae421d" + integrity sha512-ytknMbuuNW72LYMmTP7wFGP5ZTaUSGBCmV9f+uQ55XPcFHtKXLtKWVU/HE8IqPmwtyU8AO7veGoJ/qStMHNRVA== + dependencies: + "@libp2p/interface-peer-id" "^2.0.0" + "@libp2p/interfaces" "^3.0.0" + "@multiformats/multiaddr" "^12.0.0" + it-stream-types "^2.0.1" + uint8arraylist "^2.4.3" + "@libp2p/interface-content-routing@^2.0.0": version "2.0.1" resolved "https://registry.yarnpkg.com/@libp2p/interface-content-routing/-/interface-content-routing-2.0.1.tgz#e050dc42adc3e9b4f1666eafa889c88f892ba1c4" @@ -2412,6 +2445,13 @@ dependencies: multiformats "^11.0.0" +"@libp2p/interface-peer-id@^2.0.2": + version "2.0.2" + resolved "https://registry.yarnpkg.com/@libp2p/interface-peer-id/-/interface-peer-id-2.0.2.tgz#6302e70b6fc17c451bc3daa11447d059357bcc32" + integrity sha512-9pZp9zhTDoVwzRmp0Wtxw0Yfa//Yc0GqBCJi3EznBDE6HGIAVvppR91wSh2knt/0eYg0AQj7Y35VSesUTzMCUg== + dependencies: + multiformats "^11.0.0" + "@libp2p/interface-peer-info@^1.0.0", "@libp2p/interface-peer-info@^1.0.2": version "1.0.9" resolved "https://registry.yarnpkg.com/@libp2p/interface-peer-info/-/interface-peer-info-1.0.9.tgz#4b1ff859b7861f4387a69711850b3ac31eaedc56" @@ -2503,6 +2543,20 @@ "@libp2p/interfaces" "^3.0.0" it-stream-types "^1.0.4" +"@libp2p/interface-stream-muxer@^4.1.2": + version "4.1.2" + resolved "https://registry.yarnpkg.com/@libp2p/interface-stream-muxer/-/interface-stream-muxer-4.1.2.tgz#f0a5edb906ec784d991b9421a024f0f21ebdaab4" + integrity sha512-dQJcn67UaAa8YQFRJDhbo4uT453z/2lCzD/ZwTk1YOqJxATXbXgVcB8dXDQFEUiUX3ZjVQ1IBu+NlQd+IZ++zw== + dependencies: + "@libp2p/interface-connection" "^5.0.0" + "@libp2p/interfaces" "^3.0.0" + "@libp2p/logger" "^2.0.7" + abortable-iterator "^5.0.1" + any-signal "^4.1.1" + it-pushable "^3.1.3" + it-stream-types "^2.0.1" + uint8arraylist "^2.4.3" + "@libp2p/interface-transport@^2.0.0", "@libp2p/interface-transport@^2.1.0": version "2.1.1" resolved "https://registry.yarnpkg.com/@libp2p/interface-transport/-/interface-transport-2.1.1.tgz#e463f30b272494c177d3a0bd494545616fd7b624" @@ -2519,6 +2573,11 @@ resolved "https://registry.yarnpkg.com/@libp2p/interfaces/-/interfaces-3.3.1.tgz#519c77c030b10d776250bbebf65990af53ccb2ee" integrity sha512-3N+goQt74SmaVOjwpwMPKLNgh1uDQGw8GD12c40Kc86WOq0qvpm3NfACW+H8Su2X6KmWjCSMzk9JWs9+8FtUfg== +"@libp2p/interfaces@^3.3.2": + version "3.3.2" + resolved "https://registry.yarnpkg.com/@libp2p/interfaces/-/interfaces-3.3.2.tgz#5d8079be845b0960939b5b18880e785a4714465a" + integrity sha512-p/M7plbrxLzuQchvNwww1Was7ZeGE2NaOFulMaZBYIihU8z3fhaV+a033OqnC/0NTX/yhfdNOG7znhYq3XoR/g== + "@libp2p/keychain@^1.0.0": version "1.0.1" resolved "https://registry.yarnpkg.com/@libp2p/keychain/-/keychain-1.0.1.tgz#f34d611ece45bd871f597bd14ce61e60459c6d5d" @@ -2555,6 +2614,17 @@ interface-datastore "^7.0.0" multiformats "^11.0.0" +"@libp2p/logger@^2.0.7": + version "2.1.1" + resolved "https://registry.yarnpkg.com/@libp2p/logger/-/logger-2.1.1.tgz#e12e6c320ea64252af954bcec996895098d1cd36" + integrity sha512-2UbzDPctg3cPupF6jrv6abQnAUTrbLybNOj0rmmrdGm1cN2HJ1o/hBu0sXuq4KF9P1h/eVRn1HIRbVIEKnEJrA== + dependencies: + "@libp2p/interface-peer-id" "^2.0.2" + "@multiformats/multiaddr" "^12.1.3" + debug "^4.3.4" + interface-datastore "^8.2.0" + multiformats "^11.0.2" + "@libp2p/mplex@^7.1.1": version "7.1.2" resolved "https://registry.yarnpkg.com/@libp2p/mplex/-/mplex-7.1.2.tgz#7bc4be3ff28848b51e07ce06dbd9737c108c7a13" @@ -2846,6 +2916,19 @@ uint8arrays "^4.0.2" varint "^6.0.0" +"@multiformats/multiaddr@^12.1.3": + version "12.1.3" + resolved "https://registry.yarnpkg.com/@multiformats/multiaddr/-/multiaddr-12.1.3.tgz#aff5aa61ec19c5320f0b756e88c3bbaac8d1c7af" + integrity sha512-rNcS3njkkSwuGF4x58L47jGH5kBXBfJPNsWnrt0gujhNYn6ReDt1je7vEU5/ddrVj0TStgxw+Hm+TkYDK0b60w== + dependencies: + "@chainsafe/is-ip" "^2.0.1" + "@chainsafe/netmask" "^2.0.0" + "@libp2p/interfaces" "^3.3.1" + dns-over-http-resolver "^2.1.0" + multiformats "^11.0.0" + uint8arrays "^4.0.2" + varint "^6.0.0" + "@noble/ed25519@^1.6.0": version "1.7.3" resolved "https://registry.yarnpkg.com/@noble/ed25519/-/ed25519-1.7.3.tgz#57e1677bf6885354b466c38e2b620c62f45a7123" @@ -4192,6 +4275,14 @@ abortable-iterator@^4.0.2: get-iterator "^2.0.0" it-stream-types "^1.0.3" +abortable-iterator@^5.0.1: + version "5.0.1" + resolved "https://registry.yarnpkg.com/abortable-iterator/-/abortable-iterator-5.0.1.tgz#5d93eba6fa8287a973a9ea090c64ca08b3777780" + integrity sha512-hlZ5Z8UwqrKsJcelVPEqDduZowJPBQJ9ZhBC2FXpja3lXy8X6MoI5uMzIgmrA8+3jcVnp8TF/tx+IBBqYJNUrg== + dependencies: + get-iterator "^2.0.0" + it-stream-types "^2.0.1" + abstract-level@^1.0.0, abstract-level@^1.0.2, abstract-level@^1.0.3: version "1.0.3" resolved "https://registry.yarnpkg.com/abstract-level/-/abstract-level-1.0.3.tgz#78a67d3d84da55ee15201486ab44c09560070741" @@ -4401,6 +4492,11 @@ any-signal@^3.0.0: resolved "https://registry.yarnpkg.com/any-signal/-/any-signal-3.0.1.tgz#49cae34368187a3472e31de28fb5cb1430caa9a6" integrity sha512-xgZgJtKEa9YmDqXodIgl7Fl1C8yNXr8w6gXjqK3LW4GcEiYT+6AQfJSE/8SPsEpLLmcvbv8YU+qet94UewHxqg== +any-signal@^4.1.1: + version "4.1.1" + resolved "https://registry.yarnpkg.com/any-signal/-/any-signal-4.1.1.tgz#928416c355c66899e6b2a91cad4488f0324bae03" + integrity sha512-iADenERppdC+A2YKbOXXB2WUeABLaM6qnpZ70kZbPZ1cZMMJ7eF+3CaYm+/PhBizgkzlvssC7QuHS30oOiQYWA== + anymatch@~3.1.1, anymatch@~3.1.2: version "3.1.3" resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-3.1.3.tgz#790c58b19ba1720a84205b57c618d5ad8524973e" @@ -9639,6 +9735,15 @@ interface-datastore@^8.0.0: nanoid "^4.0.0" uint8arrays "^4.0.2" +interface-datastore@^8.2.0: + version "8.2.3" + resolved "https://registry.yarnpkg.com/interface-datastore/-/interface-datastore-8.2.3.tgz#491f6e90da963dbbb23d44c97aaae38959a797de" + integrity sha512-5U6BfK7cR4oSngm4pqYZBBAX7tpLI4YCOa3Ifz/EJEaEV2mmlloH5qP8ADWCVFk6vP1MPT925JJkt3kFIi7iWQ== + dependencies: + interface-store "^5.0.0" + nanoid "^4.0.0" + uint8arrays "^4.0.2" + interface-store@^2.0.1: version "2.0.1" resolved "https://registry.npmjs.org/interface-store/-/interface-store-2.0.1.tgz" @@ -10384,6 +10489,13 @@ it-merge@^2.0.0: dependencies: it-pushable "^3.1.0" +it-merge@^3.0.0: + version "3.0.1" + resolved "https://registry.yarnpkg.com/it-merge/-/it-merge-3.0.1.tgz#20cc293593586e5afcbfed8ba88a94def5ccfcfa" + integrity sha512-I6hjU1ABO+k3xY1H6JtCSDXvUME88pxIXSgKeT4WI5rPYbQzpr98ldacVuG95WbjaJxKl6Qot6lUdxduLBQPHA== + dependencies: + it-pushable "^3.1.0" + it-pair@^2.0.2: version "2.0.4" resolved "https://registry.yarnpkg.com/it-pair/-/it-pair-2.0.4.tgz#3113fd3f0b573f823cfd6c4339432ea7ddebc7c8" @@ -10416,11 +10528,25 @@ it-pipe@^2.0.3, it-pipe@^2.0.4, it-pipe@^2.0.5: it-pushable "^3.1.0" it-stream-types "^1.0.3" +it-pipe@^3.0.1: + version "3.0.1" + resolved "https://registry.yarnpkg.com/it-pipe/-/it-pipe-3.0.1.tgz#b25720df82f4c558a8532602b5fbc37bbe4e7ba5" + integrity sha512-sIoNrQl1qSRg2seYSBH/3QxWhJFn9PKYvOf/bHdtCBF0bnghey44VyASsWzn5dAx0DCDDABq1hZIuzKmtBZmKA== + dependencies: + it-merge "^3.0.0" + it-pushable "^3.1.2" + it-stream-types "^2.0.1" + it-pushable@^3.0.0, it-pushable@^3.1.0, it-pushable@^3.1.2: version "3.1.2" resolved "https://registry.yarnpkg.com/it-pushable/-/it-pushable-3.1.2.tgz#6f2420fb192f637613c561720945a36b6d9160ae" integrity sha512-zU9FbeoGT0f+yobwm8agol2OTMXbq4ZSWLEi7hug6TEZx4qVhGhGyp31cayH04aBYsIoO2Nr5kgMjH/oWj2BJQ== +it-pushable@^3.1.3: + version "3.1.3" + resolved "https://registry.yarnpkg.com/it-pushable/-/it-pushable-3.1.3.tgz#b6f4a1e0236502f12b5661b40468b629799baf0e" + integrity sha512-f50iQ85HISS6DaWCyrqf9QJ6G/kQtKIMf9xZkgZgyOvxEQDfn8OfYcLXXquCqgoLboxQtAW1ZFZyFIAsLHDtJw== + it-reader@^6.0.1: version "6.0.2" resolved "https://registry.yarnpkg.com/it-reader/-/it-reader-6.0.2.tgz#2177afca42f0b41c6acc582cc6fc6869ae8d4dd4" @@ -10441,6 +10567,11 @@ it-stream-types@^1.0.2, it-stream-types@^1.0.3, it-stream-types@^1.0.4: resolved "https://registry.npmjs.org/it-stream-types/-/it-stream-types-1.0.5.tgz" integrity sha512-I88Ka1nHgfX62e5mi5LLL+oueqz7Ltg0bUdtsUKDe9SoUqbQPf2Mp5kxDTe9pNhHQGs4pvYPAINwuZ1HAt42TA== +it-stream-types@^2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/it-stream-types/-/it-stream-types-2.0.1.tgz#69cb4d7e79e707b8257a8997e02751ccb6c3af32" + integrity sha512-6DmOs5r7ERDbvS4q8yLKENcj6Yecr7QQTqWApbZdfAUTEC947d+PEha7PCqhm//9oxaLYL7TWRekwhoXl2s6fg== + it-take@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/it-take/-/it-take-2.0.0.tgz#e62bdf0f9bf1590b369a116b37de9f74b1f61f00" @@ -11958,7 +12089,7 @@ multiformats@^10.0.0: resolved "https://registry.yarnpkg.com/multiformats/-/multiformats-10.0.3.tgz#d4147d01f9a31271c6fb5d24adf9b01f9e656bba" integrity sha512-K2yGSmstS/oEmYiEIieHb53jJCaqp4ERPDQAYrm5sV3UUrVDZeshJQCK6GHAKyIGufU1vAcbS0PdAAZmC7Tzcw== -multiformats@^11.0.0: +multiformats@^11.0.0, multiformats@^11.0.2: version "11.0.2" resolved "https://registry.yarnpkg.com/multiformats/-/multiformats-11.0.2.tgz#b14735efc42cd8581e73895e66bebb9752151b60" integrity sha512-b5mYMkOkARIuVZCpvijFj9a6m5wMVLC7cf/jIPd5D/ARDOfLC5+IFkbgDXQgcU2goIsTD/O9NY4DI/Mt4OGvlg==