configure-discovery.mdx
1 --- 2 title: Bootstrap Nodes and Discover Peers 3 hide_table_of_contents: true 4 --- 5 6 import Tabs from '@theme/Tabs'; 7 import TabItem from '@theme/TabItem'; 8 9 This guide provides detailed steps to bootstrap your your node using [Static Peers](/learn/concepts/static-peers) and discover peers in the Waku Network using [DNS Discovery](/learn/concepts/dns-discovery). 10 11 :::info 12 If you do not set up a bootstrap node or discovery mechanism, your node will not connect to any remote peer. 13 ::: 14 15 :::tip 16 Until [node incentivisation](/learn/research#prevention-of-denial-of-service-dos-and-node-incentivisation) is in place, you should [operate extra nodes](/run-node) alongside the ones provided by the Waku Network. When running a node, we recommend using the [DNS Discovery and Static Peers](#configure-dns-discovery-and-static-peers) configuration to connect to both the Waku Network and your node. 17 ::: 18 19 ## Default bootstrap method 20 21 The `@waku/sdk` package provides a built-in bootstrapping method that uses [DNS Discovery](/learn/concepts/dns-discovery) to locate peers from the `waku v2.prod` `ENR` tree. 22 23 ```js 24 import { createLightNode } from "@waku/sdk"; 25 26 // Bootstrap node using the default bootstrap method 27 const node = await createLightNode({ defaultBootstrap: true }); 28 ``` 29 30 ## Configure static peers 31 32 To set [static peers](/learn/concepts/static-peers), a list of `multiaddr` to bootstrap the node should be passed to the `bootstrapPeers` parameter of the `createLightNode()` function: 33 34 ```js 35 import { createLightNode } from "@waku/sdk"; 36 37 // Bootstrap node using static peers 38 const node = await createLightNode({ 39 bootstrapPeers: ["[PEER MULTIADDR]"], 40 }); 41 ``` 42 43 For example, consider a node that connects to two static peers on the same local host (IP: `0.0.0.0`) using TCP ports `60002` and `60003` with WebSocket enabled: 44 45 ```js 46 // Define the list of static peers to bootstrap 47 const peers = [ 48 "/ip4/0.0.0.0/tcp/60002/ws/p2p/16Uiu2HAkzjwwgEAXfeGNMKFPSpc6vGBRqCdTLG5q3Gmk2v4pQw7H", 49 "/ip4/0.0.0.0/tcp/60003/ws/p2p/16Uiu2HAmFBA7LGtwY5WVVikdmXVo3cKLqkmvVtuDu63fe8safeQJ", 50 ]; 51 52 // Bootstrap node using the static peers 53 const node = await createLightNode({ 54 bootstrapPeers: peers, 55 }); 56 ``` 57 58 Alternatively, you can dial a particular node like this: 59 60 ```js 61 // Define the list of static peers to bootstrap 62 const peers = [ 63 "/ip4/0.0.0.0/tcp/60002/ws/p2p/16Uiu2HAkzjwwgEAXfeGNMKFPSpc6vGBRqCdTLG5q3Gmk2v4pQw7H", 64 "/ip4/0.0.0.0/tcp/60003/ws/p2p/16Uiu2HAmFBA7LGtwY5WVVikdmXVo3cKLqkmvVtuDu63fe8safeQJ", 65 ]; 66 67 const node = await createLightNode(); 68 69 // In case nodes are using IP address and / or `ws` protocol - additional configuration is needed: 70 /* 71 const node = await createLightNode({ 72 libp2p: { 73 filterMultiaddrs: false, 74 }, 75 }); 76 */ 77 78 const promises = peers.map((multiaddr) => node.dial(multiaddr)); 79 80 await Promise.all(promises); 81 ``` 82 83 :::tip 84 For local development using a `nwaku` node, use a `ws` address instead of `wss`. Remember that this setup is functional only when your web server is running locally. You can check how to get multi address of your locally run node in [Find node address](/run-node/find-node-address). 85 ::: 86 87 ## Configure DNS discovery 88 89 To bootstrap a node using [DNS Discovery](/learn/concepts/dns-discovery), first install the `@waku/dns-discovery` package: 90 91 <Tabs groupId="package-manager"> 92 <TabItem value="npm" label="NPM"> 93 94 ```shell 95 npm install @waku/dns-discovery 96 ``` 97 98 </TabItem> 99 <TabItem value="yarn" label="Yarn"> 100 101 ```shell 102 yarn add @waku/dns-discovery 103 ``` 104 105 </TabItem> 106 </Tabs> 107 108 Then, use the `wakuDnsDiscovery()` function to provide a list of URLs for DNS node list in the format `enrtree://<key>@<fqdn>`: 109 110 ```js 111 import { createLightNode } from "@waku/sdk"; 112 import { wakuDnsDiscovery } from "@waku/dns-discovery"; 113 114 // Define DNS node list 115 const enrTree = "enrtree://[PUBLIC KEY]@[DOMAIN NAME]"; 116 117 // Define node requirements 118 const NODE_REQUIREMENTS = { 119 store: 3, 120 lightPush: 3, 121 filter: 3, 122 }; 123 124 // Bootstrap node using DNS Discovery 125 const node = await createLightNode({ 126 libp2p: { 127 peerDiscovery: [wakuDnsDiscovery([enrTree], NODE_REQUIREMENTS)], 128 }, 129 }); 130 ``` 131 132 For example, consider a node that uses the `waku v2.prod` and `waku v2.test` `ENR` trees for `DNS Discovery`: 133 134 ```js 135 import { enrTree } from "@waku/dns-discovery"; 136 137 // Bootstrap node using DNS Discovery 138 const node = await createLightNode({ 139 libp2p: { 140 peerDiscovery: [ 141 wakuDnsDiscovery([enrTree["PROD"], enrTree["TEST"]], NODE_REQUIREMENTS), 142 ], 143 }, 144 }); 145 ``` 146 147 ## Configure DNS discovery and static peers 148 149 You can also bootstrap your node using [DNS Discovery](/learn/concepts/dns-discovery) and [Static Peers](/learn/concepts/static-peers) simultaneously: 150 151 ```js 152 import { createLightNode } from "@waku/sdk"; 153 import { bootstrap } from "@libp2p/bootstrap"; 154 import { enrTree, wakuDnsDiscovery } from "@waku/dns-discovery"; 155 156 // Define the list of static peers to bootstrap 157 const peers = [ 158 "/ip4/0.0.0.0/tcp/60002/ws/p2p/16Uiu2HAkzjwwgEAXfeGNMKFPSpc6vGBRqCdTLG5q3Gmk2v4pQw7H", 159 "/ip4/0.0.0.0/tcp/60003/ws/p2p/16Uiu2HAmFBA7LGtwY5WVVikdmXVo3cKLqkmvVtuDu63fe8safeQJ", 160 ]; 161 162 // Define node requirements 163 const NODE_REQUIREMENTS = { 164 store: 3, 165 lightPush: 3, 166 filter: 3, 167 }; 168 169 // Bootstrap node using DNS Discovery and static peers 170 const node = await createLightNode({ 171 libp2p: { 172 bootstrapPeers: peers, 173 peerDiscovery: [wakuDnsDiscovery([enrTree["PROD"]], NODE_REQUIREMENTS)], 174 }, 175 }); 176 ``` 177 178 ## Retrieving connected peers 179 180 You can retrieve the array of peers connected to a node using the `libp2p.getPeers()` function within the `@waku/sdk` package: 181 182 ```js 183 import { createLightNode } from "@waku/sdk"; 184 185 const node = await createLightNode({ defaultBootstrap: true }); 186 await node.waitForPeers(); 187 188 // Retrieve array of peers connected to the node 189 console.log(node.libp2p.getPeers()); 190 ```