Module setup

Source
Expand description

Data structures and functions to setup a node and configure it for networking.

§Node setup

To set up a node, you’ll need to configure either a single peer or a group of peers that can kickstart the network. This involves:

  • Reading a .ini file with bootstrap configuration data
  • Or configuring parameters like bootstrap nodes, TCP/UDP ports, and cryptographic settings directly

These configurations takes place on a BootstrapConfig config object and they affect the node and the network at large. BootstrapConfig can be configured in two ways:

  • Using configuration methods:
    let mut bootnode = HashMap::new();  // Bootnodes
    let ports = (1509, 2710);  // TCP, UDP ports

    bootnode.insert(
        PeerId::random(),
        "/ip4/x.x.x.x/tcp/1509".to_string()
    );

    // Cryptographic keypair for message signing and identity generation    
    let mut ed25519_serialized_keypair =    
        Keypair::generate_ed25519().to_protobuf_encoding().unwrap();

    // Build config
    BootstrapConfig::new()
        .with_bootnodes(bootnode)
        .with_tcp(ports.0)
        .with_udp(ports.1)
        .generate_keypair_from_protobuf(key_type_str, &mut ed25519_serialized_keypair);
  • Reading the config values from a .ini config file:
    // Build config
    BootstrapConfig::from_file("bootstrap_config.ini")
        // You can combine methods that override the values in bootstrap_config
        .with_tcp(1509);     

When setting up a new network, you won’t need to specify any bootnodes initially since you’re the only one in the network. However, if you’re joining an existing network, you’ll need to obtain a peer’s peerId and multiaddress to configure it as your bootnode and connect to them.

An example .ini file could look like this:

# example .ini file
[ports]
; TCP/IP port to listen on
tcp=3000
; UDP port to listen on
udp=4000

[auth]
; Type of keypair to generate for node identity and message auth e.g RSA, EDSA, Ed25519
crypto=Ed25519
; The protobuf serialized format of the node's cryptographic keypair
protobuf_keypair=[]

[bootstrap]
; The boostrap nodes to connect to immediately after start up
boot_nodes=[12D3KooWGfbL6ZNGWqS11MoptH2A7DB1DG6u85FhXBUPXPVkVVRq:/ip4/x.x.x.x/tcp/1509, QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt:/x.x.x.x/tcp/1509]

[blacklist]
; The list of blacklisted peers we don't want to have anything to do with
blacklist=[]

§Fallback behaviour

Node setup will fallback to default behavior if:

  • The ini file doesn’t contain any [ports] section, the fallback ports for TCP is MIN_PORT and for UDP is MAX_PORT.
  • No key type is specified, it will default to Ed25519.

Structs§

BootstrapConfig
Configuration data required for node bootstrap.
Keypair
Identity keypair of a node.
PeerId
Identifier of a peer of the network.
RsaKeypair
An RSA keypair.

Enums§

KeyType
The type of key a KeyPair is holding.
SwarmNlError
Library error type containing all custom errors that could be encountered.
TcpConfig
TCP setup configuration.
TransportOpts
Supported transport protocols.

Constants§

MAX_PORT
Upper bound port range (u16::MAX).
MIN_PORT
Lower bound port range.

Statics§

DEFAULT_IP_ADDRESS
Default IP address when no address is specified.
DEFAULT_KEEP_ALIVE_DURATION
Default amount of time to keep a connection alive.
DEFAULT_NETWORK_ID
Default network ID.
MIN_NETWORK_ID_LENGTH
This constant sets the shortest acceptable length for a network ID. The network ID identifies a network and ensures it’s distinct from others.

Traits§

CustomFrom
An implementation of From<&str> for KeyType to read a key type from a bootstrap config file.

Type Aliases§

MultiaddrString
The stringified Multiaddr type.
Nodes
A collection of nodes described purely by their addresses.
PeerIdString
The stringified PeerId type.
Port
Port type.
Seconds
Seconds type.
SwarmNlResult
Generic SwarmNl result type.