# Connecting to peers

The default behaviour for Etn-sc is to connect to Electroneum Mainnet. However, Etn-sc can also connect to public testnets, [private networks](/etn-sc-client/fundamentals/private-networks.md) and [local testnets](/etn-sc-client/developers/dapp-developers/dev-mode.md). For convenience, the the public testnet with long term support have their own command line flag. Etn-sc can connect to the testnet simply by passing:

* `--testnet`

**Note:** Network selection is not persisted from a config file. To connect to a pre-defined network you must always enable it explicitly, even when using the `--config` flag to load other configuration values. For example:

```sh
# Generate desired config file. You must specify testnet here.
etn-sc --testnet --syncmode "full" ... dumpconfig > testnet.toml

# Start etn-sc with given config file. Here too the testnet must be specified.
etn-sc --testnet --config testnet.toml
```

## Finding peers <a href="#finding-peers" id="finding-peers"></a>

Etn-sc continuously attempts to connect to other nodes on the network until it has enough peers. If UPnP (Universal Plug and Play) is enabled at the router or Electroneum is run on an Internet-facing server, it will also accept connections from other nodes. Etn-sc finds peers using the [discovery protocol](/advanced/networking-layer.md#discovery). In the discovery protocol, nodes exchange connectivity details and then establish sessions ([RLPx](https://github.com/ethereum/devp2p/blob/master/rlpx.md)). If the nodes support compatible sub-protocols they can start exchanging Electroneum data [on the wire](/advanced/networking-layer.md#wire-protocol).

A new node entering the network for the first time gets introduced to a set of peers by a bootstrap node ("bootnode") whose sole purpose is to connect new nodes to peers. The endpoints for these bootnodes are hardcoded into Etn-sc, but they can also be specified by providing the `--bootnode` flag along with comma-separated bootnode addresses in the form of [enodes](/advanced/networking-layer/network-addresses.md#enode) on startup. For example:

```sh
etn-sc --bootnodes enode://pubkey1@ip1:port1,enode://pubkey2@ip2:port2,enode://pubkey3@ip3:port3
```

There are scenarios where disabling the discovery process is useful, for example for running a local test node or an experimental test network with known, fixed nodes. This can be achieved by passing the `--nodiscover` flag to Etn-sc at startup.

## Connectivity problems <a href="#connectivity-problems" id="connectivity-problems"></a>

There are occasions when Etn-sc simply fails to connect to peers. The common reasons for this are:

* Local time might be incorrect. An accurate clock is required to participate in the Electroneum network. The local clock can be resynchronized using commands such as `sudo ntpdate -s time.nist.gov` (this will vary depending on operating system).
* Some firewall configurations can prohibit UDP traffic. The static nodes feature or `admin.addPeer()` on the console can be used to configure connections manually.
* Running Etn-sc in [light mode](/etn-sc-client/fundamentals/light-client.md) often leads to connectivity issues because there are few nodes running light servers. There is no easy fix for this except to switch Etn-sc out of light mode.
* The public test network Etn-sc is connecting to might be deprecated or have a low number of active nodes that are hard to find. In this case, the best action is to switch to an alternative test network.

## Checking Connectivity <a href="#checking-connectivity" id="checking-connectivity"></a>

The `net` module has two attributes that enable checking node connectivity from the [interactive Javascript console](/etn-sc-client/interacting-with-etn-sc/js-console.md). These are `net.listening` which reports whether the Etn-sc node is listening for inbound requests, and `peerCount` which returns the number of active peers the node is connected to.

```javascript
> net.listening
true

> net.peerCount
4
```

Functions in the `admin` module provide more information about the connected peers, including their IP address, port number, supported protocols etc. Calling `admin.peers` returns this information for all connected peers.

```
> admin.peers
[{
    caps: ["etn/66", "etn-istanbul/100", "etn-snap/1"],
    enode: "enode://619bf86ec25c3193b44f4ba2653f2d14edfef95157d4ab460e2896be2a43656236c7bf9b16b9d9c09d73400da487c3dd819a7a4a2629fc3bf634dddd2450f335@13.215.184.67:30303",
    id: "200f0c20d2b1d6ce11a6f7a74aa8ae5a37a4c52b345b82b816d380740992b10e",
    name: "etn-sc/v0.1.0-unstable-3bc1d078-20230105/linux-amd64/go1.20.4",
    network: {
      inbound: false,
      localAddress: "192.168.3.4:56831",
      remoteAddress: "13.215.184.67:30303",
      static: false,
      trusted: false
    },
    protocols: {
      etn: {
        difficulty: 2741015,
        head: "0xe22ca92c0724e9fa86f9d3b8d0aa07651d8dc24f194dc708271e965295d9b5cf",
        version: 66
      },
      etn-istanbul: "handshake",
      etn-snap: {
        version: 1
      }
    }
}, {
    caps: ["etn/66", "etn-istanbul/100", "etn-snap/1"],
    enode: "enode://50b9f7d46e4c84a1b76be778eadd69f7d969ce45772fcbaaf264be8f7f986d030fc26e113b138980727b8c134b5e08321917e07fa6245ae357e23d51db0678bf@13.250.53.55:30303",
    id: "2682531c8401443ad9ffe472c21ee98b5a90a6a45aba37fa5eddf90f5e8e1816",
    name: "etn-sc/v0.1.0-unstable-7af82b5d-20221209/linux-amd64/go1.19.2",
    network: {
      inbound: false,
      localAddress: "192.168.3.4:56843",
      remoteAddress: "13.250.53.55:30303",
      static: false,
      trusted: false
    },
    protocols: {
      etn: {
        difficulty: 2741016,
        head: "0xe1b396807e2d72dc605961c4e5da3d415fe67b301d652249784b487b8d9fa09c",
        version: 66
      },
      etn-istanbul: "handshake",
      etn-snap: {
        version: 1
      }
    }
}]
```

The `admin` module also includes functions for gathering information about the local node rather than its peers. For example, `admin.nodeInfo` returns the name and connectivity details for the local node.

```sh
> admin.nodeInfo
{
  enode: "enode://666e25ebd14912ab79dd9bdf547bdb17981ab328f79517b0f8191109daaeef7c54f4dead8fb7777726dd7a599cedc16cf006961716be181ee37efddb47ef0383@127.0.0.1:41300",
  enr: "enr:-Ke4QCFtS45WhyN1pDaOEevnapt1-vwQxMY41VFxe4CCJw_wdHZtQeNJTv4QicyHF2UkmVxtCHucKFQslwqn-VPRiDyGAYlqJvHyg2V0bsfGhDA3d5GAiGV0bi1zbmFwwIJpZIJ2NIJpcIR_AAABiXNlY3AyNTZrMaEDZm4l69FJEqt53ZvfVHvbF5gasyj3lRew-BkRCdqu73yDdGNwgqFUg3VkcIKhVA",
  id: "695cfb779e4be5821660f37aa71299ee4c91bf07d84a51620f54379de83a5ba5",
  ip: "127.0.0.1",
  listenAddr: "[::]:41300",
  name: "etn-sc/v1.0.0-stable-5be45479/darwin-arm64/go1.20.6",
  ports: {
    discovery: 41300,
    listener: 41300
  },
  protocols: {
    etn: {
      config: {
        berlinBlock: 0,
        byzantiumBlock: 0,
        chainId: 5201420,
        constantinopleBlock: 0,
        daoForkSupport: true,
        eip150Block: 0,
        eip150Hash: "0x0000000000000000000000000000000000000000000000000000000000000000",
        eip155Block: 0,
        eip158Block: 0,
        genesisETN: 2e+27,
        homesteadBlock: 0,
        ibft: {...},
        istanbulBlock: 0,
        londonBlock: 0,
        petersburgBlock: 0
      },
      difficulty: 2741028,
      genesis: "0xcf1b6615aa11a133442a21cc1bf9fbe935fd146f74123ba4f3f0e107362c3eb4",
      head: "0x8702ef01b6bb8ada23f193822246ba1f4f3a89c1e87027fda4df272e0f26452b",
      network: 5201420
    },
    etn-istanbul: {
      config: {
        berlinBlock: 0,
        byzantiumBlock: 0,
        chainId: 5201420,
        constantinopleBlock: 0,
        daoForkSupport: true,
        eip150Block: 0,
        eip150Hash: "0x0000000000000000000000000000000000000000000000000000000000000000",
        eip155Block: 0,
        eip158Block: 0,
        genesisETN: 2e+27,
        homesteadBlock: 0,
        ibft: {...},
        istanbulBlock: 0,
        londonBlock: 0,
        petersburgBlock: 0
      },
      consensus: "IBFT",
      difficulty: 2741028,
      genesis: "0xcf1b6615aa11a133442a21cc1bf9fbe935fd146f74123ba4f3f0e107362c3eb4",
      head: "0x8702ef01b6bb8ada23f193822246ba1f4f3a89c1e87027fda4df272e0f26452b",
      network: 5201420
    },
    etn-snap: {}
  }
}
```

## Custom Networks <a href="#custom-networks" id="custom-networks"></a>

It is often useful for developers to connect to private test networks rather than public testnets or Electroneum mainnet. These sandbox environments allow block creation without competing against other miners, easy minting of test ether and give freedom to break things without real-world consequences. A private network is started by providing a value to `--networkid` that is not used by any other existing public network ([Chainlist](https://chainlist.org/)) and creating a custom `genesis.json` file. Detailed instructions for this are available on the [Private Networks page](/etn-sc-client/fundamentals/private-networks.md).

## Static nodes <a href="#static-nodes" id="static-nodes"></a>

Etn-sc also supports static nodes. Static nodes are specific peers that are always connected to. Etn-sc reconnects to these peers automatically when it is restarted. Specific nodes are defined to be static nodes by adding their enode addresses to a config file. The easiest way to create this config file is to run:

```sh
etn-sc --datadir <datadir> dumpconfig > config.toml
```

This will create `config.toml` in the current directory. The enode addresses for static nodes can then be added as a list to the `StaticNodes` field of the `Node.P2P` section in `config.toml`. When Etn-sc is started, pass `--config config.toml`. The relevant line in `config.toml` looks as follows:

```toml
StaticNodes = ["enode://f4642fa65af50cfdea8fa7414a5def7bb7991478b768e296f5e4a54e8b995de102e0ceae2e826f293c481b5325f89be6d207b003382e18a8ecba66fbaf6416c0@33.4.2.1:30303"]
```

Ensure the other lines in `config.toml` are also set correctly before starting Etn-sc, as passing `--config` instructs Etn-sc to get its configuration values from this file. An example of a complete `config.toml` file can be found [here](https://gist.github.com/jmcook1186/16db2f0feddb4bd0581ebb9ba867a47a).

Static nodes can also be added at runtime in the Javascript console by passing an enode address to `admin.addPeer()`:

```javascript
admin.addPeer(
  'enode://f4642fa65af50cfdea8fa7414a5def7bb7991478b768e296f5e4a54e8b995de102e0ceae2e826f293c481b5325f89be6d207b003382e18a8ecba66fbaf6416c0@33.4.2.1:30303'
);
```

## Peer limit <a href="#peer-limit" id="peer-limit"></a>

It is sometimes desirable to cap the number of peers Etn-sc will connect to in order to limit on the computational and bandwidth cost associated with running a node. By default, the limit is 50 peers, however, this can be updated by passing a value to `--maxpeers`:

```sh
etn-sc <otherflags> --maxpeers 15
```

## Trusted nodes <a href="#trusted-nodes" id="trusted-nodes"></a>

Trusted nodes can be added to `config.toml` in the same way as for static nodes. Add the trusted node's enode address to the `TrustedNodes` field in `config.toml` before starting Etn-sc with `--config config.toml`.

Nodes can be added using the `admin.addTrustedPeer()` call in the Javascript console and removed using `admin.removeTrustedPeer()` call.

```javascript
admin.addTrustedPeer(
  'enode://f4642fa65af50cfdea8fa7414a5def7bb7991478b768e296f5e4a54e8b995de102e0ceae2e826f293c481b5325f89be6d207b003382e18a8ecba66fbaf6416c0@33.4.2.1:30303'
);
```

## Summary <a href="#summary" id="summary"></a>

Etn-sc connects to Electroneum Mainnet by default. However, this behaviour can be changed using combinations of command line flags and files. This page has described the various options available for connecting a Etn-sc node to the Electroneum Smart Chain, public testnet and private networks.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developer.electroneum.com/etn-sc-client/fundamentals/connecting-to-peers.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
