addBootstrap method

Future<void> addBootstrap({
  1. required String bsName,
  2. required String bsPeerId,
  3. int port = TransportUdp.defaultPort,
})
inherited

Adds a bootstrap node to the router.

This method adds a bootstrap node to the router, allowing it to join the network. It resolves the DNS name of the bootstrap node, adds its addresses to the routing table, and restarts the router to establish connections.

bsName The DNS name of the bootstrap node. bsPeerId The base64 encoded Peer ID of the bootstrap node. port The port to connect to. Defaults to TransportUdp.defaultPort.

Implementation

Future<void> addBootstrap({
  required String bsName,
  required String bsPeerId,
  int port = TransportUdp.defaultPort,
}) async {
  // Stop the router before adding a bootstrap node to prevent conflicts
  // during the address resolution and routing table updates.
  stop();

  // Resolve the DNS name to get the IP addresses of the bootstrap node.
  _addresses[bsPeerId] = await InternetAddress.lookup(
    bsName,
  ).timeout(messageTTL);

  // Set address properties for the bootstrap node, marking it as static.
  final addressProperties = AddressProperties(isStatic: true);

  // Add the bootstrap node's addresses to the peer addresses in the
  // routing table.
  for (final e in _addresses.entries) {
    final peerId = PeerId(value: base64Decode(e.key));
    for (final address in e.value) {
      addPeerAddress(
        canForward: true, // Bootstrap nodes can forward messages.
        peerId: peerId,
        address: FullAddress(address: address, port: port),
        properties: addressProperties,
      );
    }
  }

  // Restart the router after adding the bootstrap node to establish
  // connections.
  await start();
}