# Running A Local Network

## Running a Local Network via Endless CLI <a href="#running-a-local-network-via-endless-cli" id="running-a-local-network-via-endless-cli"></a>

Local networks can be helpful when testing your code. They are not connected to any production Endless networks like mainnet, but they are useful for three main reasons:

1. **No rate limits:** You can interact with hosted services like the Node API, Indexer API, and faucet with no rate-limits to speed up testing.
2. **Reproducibility:** You can set up specific on-chain scenarios and restart the network from scratch at any point to return to a clean slate.
3. **High availability**: The Endless devnet and testnet networks are periodically upgraded, during which time they can be unavailable. Local development networks are also always available even if you have no internet access.

## Starting A Local Network <a href="#starting-a-local-network" id="starting-a-local-network"></a>

1. Ensure you have the Endless CLI installed.
2. Ensure you have [Docker](https://docs.docker.com/get-docker/) installed.
   1. This is exclusively needed for making a production-like environment by running the Indexer API. Many downstream tools such as the Endless SDK depend on the Indexer API.
   2. Docker recommends that you install via [Docker Desktop](https://www.docker.com/products/docker-desktop/) to get automatic updates.
3. Start Docker.
4. Run the following command in a new terminal to start the private network:

```shell
endless node run-local-testnet
```

{% hint style="info" %}
Note: Despite the name (`local-testnet`), this has nothing to do with the Endless testnet, it will run a network entirely local to your machine.
{% endhint %}

You should expect to see an output similar to this:

```shell
Identified node type (Validator) and chain ID (Some(testing)) from node config!


Readiness endpoint: http://0.0.0.0:8070/

Transaction stream is starting, please wait...
Node API is starting, please wait...

Completed generating configuration:
        Log file: "/Users/dport/.endless/testnet/validator.log"
        Test dir: "/Users/dport/.endless/testnet"
        Endless root key path: "/Users/dport/.endless/testnet/mint.key"
        Waypoint: 0:a7ee6363f24362a6ab7362558b7f0d2e63be78b19a73d3a8c1f5697f65aa1ac1
        ChainId: 223
        REST API endpoint: http://0.0.0.0:8080
        Metrics endpoint: http://0.0.0.0:9101/metrics
        Endlessnet fullnode network endpoint: /ip4/0.0.0.0/tcp/6181
        Indexer gRPC node stream endpoint: 0.0.0.0:50051

Endless is running, press ctrl-c to exit

Node API is ready. Endpoint: http://0.0.0.0:8080/
Transaction stream is ready. Endpoint: http://0.0.0.0:50051/

Applying post startup steps...

Setup is complete, you can now use the local testnet!
```

1. Wait for the final line `Setup is complete, you can now use the localnet!`

   :::caution If you ran into an error, jump to the Common Errors section below. :::

As you can see from the above example output, once the local network is running, you have access to the following services:

* Node API: This is a REST API that runs directly on the node. It enables core write functionality such as transaction submission and a limited set of read functionality, such as reading account resources or Move module information.
* Transaction Stream Service: This is a gRPC stream of transactions used by the Indexer API. This is only relevant to you if you are developing a custom processor.

If you do not want to run any of these sub-components of a network, there are flags to disable them.

If you are writing a script and would like to wait for the local network to come up with all services, you can make a GET request to `http://127.0.0.1:8070`. At first this will return http code [503](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/503). When it returns [200](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/200) it means all the services are ready.

For more information on different flags you can pass when starting your local network, or configuration settings such as changing which port certain services run on, run the help command:

```shell
endless node run-local-testnet --help
```

#### Common Errors On Network Startup <a href="#common-errors-on-network-startup" id="common-errors-on-network-startup"></a>

{% hint style="info" %}
If you successfully started the local network, skip to Using The Local Network.
{% endhint %}

**Address Already In Use**

```
panicked at 'error binding to 0.0.0.0:8080: error creating server listener: Address already in use (os error 48)
```

This means one of the ports needed by the local network is already in use by another process.

To fix this on Unix systems, you can:

1. Identify the name and PID of the process by running `lsof -i :8080`.
2. Run `kill <pid>` once you know the PID to free up that port.

**Too many open files error**

```
panicked at crates/endless/src/node/local_testnet/logging.rs:64:10:
called `Result::unwrap()` on an `Err` value: Os { code: 24, kind: Uncategorized, message: \"Too many open files\" }"""
```

This means there were too many open files on your system. On many Unix systems you can increase the maximum number of open files by adding something like this to your `.zshrc`:

```
ulimit -n 32768
```

**Docker is not available**

```
Unexpected error: Failed to apply pre-run steps for Postgres: Docker is not available, confirm it is installed and running. On Linux you may need to use sudo
```

To debug this, try the below fixes:

1. Make sure you have docker installed by running `docker --version`.
2. Ensure the Docker daemon is running by running `docker info` (if this errors saying `Cannot connect to the Docker daemon` Docker is NOT running)
3. Make sure the socket for connecting to Docker is present on your machine in the default location. For example, on Unix systems `/var/run/docker.sock` should exist.
   1. If that file does not exist, open Docker Desktop and enable `Settings -> Advanced -> Allow the default Docker socket to be used.`
   2. Or, you can find where the Docker socket is by running `docker context inspect | grep Host`, then symlink that location to the default location by running `sudo ln -s /Users/dport/.docker/run/docker.sock /var/run/docker.sock`

#### Using The Local Network <a href="#using-the-local-network" id="using-the-local-network"></a>

Now that the network is running, you can use it like you would any other network.

So, you can create a local profile like this:

```
endless init --profile <your-profile-name> --network local
```

You can then use that profile for any commands you want to use going forward. For example, if you wanted to publish a Move module like the [`hello_blockchain`](https://github.com/endless-labs/endless/tree/main/endless-move/move-examples/hello_blockchain) package to your local network you could run:

```
endless move publish --profile <your-profile-name> --package-dir /opt/git/endless-core/endless-move/move-examples/hello_blockchain --named-addresses HelloBlockchain=local
```

**Configuring the TypeScript SDK**

If you want to use the local network with the TypeScript SDK, you can use local network URLs when initializing the client object (Endless):

```
import { Endless, EndlessConfig, Network } from "@endless-labs/ts-sdk";

const network = Network.LOCAL;
const config = new EndlessConfig({ network });
const client = new Endless(config);
```

**Resetting the local network**

Sometimes while developing it is helpful to reset the local network back to its initial state, for example:

* You made backwards incompatible changes to a Move module, and you'd like to redeploy it without renaming it or using a new account.
* You are building a [custom indexer processor](https://endless.dev/indexer/custom-processors/) and would like to index using a fresh network.
* You want to clear all on chain state, e.g. accounts, objects, etc.

To start with a brand new local network, use the `--force-restart` flag:

```
endless node run-local-testnet --force-restart
```

It will then prompt you if you really want to restart the chain, to ensure that you do not delete your work by accident.

```
Are you sure you want to delete the existing chain? [yes/no]
> yes
```

If you do not want to be prompted, include `--assume-yes` as well:

```
endless node run-local-testnet --force-restart --assume-yes
```
