Setting up an Ethereum Node (Execution Client)
Tags: blockchain, etherereum, tutorial
Published: 2022-09-10 17:55:33

## Caveat
It's still possible to run the execution client (layer 1) alone but after the Merge in the upcoming days, we need to run the execution client and consensus client to be able to access to the Ethereum network. In this article, I will cover the `execution layer` **only**. 
The execution client listens to new transactions and executes them in the EVM (state machine) and holds the database of Ethereum network.
- See the overview of execution client in [Etherenodes](https://ethernodes.org/)
- See the execution layer specification in [Ethereum Repo](https://github.com/ethereum/execution-specs/)

*This article covers the node running from the source code or from the package in Ubuntu*.

---

## <u>Table Of Contents</u>
  * [Abstract](#abstract)
  * [Prerequisites](#prerequisites)
  * [Node Types](#node-types)
  * [Why should I run a node](#run-node-why)
  * [Installation](#installation-windows)
  * [Create the chaindata folder](#chain-data)
  * [Run the node](#run-node)
  * [Interacting with Geth](#geth-attach)
  * [Run the node (dev mode / contributor)](#run-node-dev)
  * [Next Step](#next-step)
  * [Conclusion](#conclusion)

### <u>Abstract</u> <a name="abstract"></a>
Little abstract before jumping in our journey with the Ethereum node.
Ethereum is a distributed decentralised network of computers. Each computer in this network is running a software that contains the history of the blockchain, the ability to validate/mine blocks and transactions.
In order to be a node in the Ethereum network, a client needs to download this software that implements the specification of the yellow paper to be able to communicate within the network.
In this article, the client I'll be choosing is Geth. It's the most popular client and backed by the Ethereum Community.

---

### <u>Prerequisites </u><a name="prerequisites"></a>
You can run a node on any computers but it's worth to consider running your Ethereum node in a dedicated hardware to minimise the performance issues.
Make sure to have a stable internet connection. Even though you can go offline, your node goes inactive until you come back and download the latest changes from other nodes automatically. 
You can buy hardware from a vendor and have a plug and play node or you can build your own. The last option is cheaper and funnier.

#### <u>Hardware</u>
> <u>Minimum</u>:
> - 8GB RAM (16GB for a validator);
> - 2TB SSD for write speeds (12TB for Archive node);

> <u>Recommended</u>:
> - CPU with a benchmark score of 6667+
> - 16GB RAM (32GB for a validator);
> - Unlimited High speed internet bandwidth;

---

### <u>Node types</u> <a name="node-types"></a>
In order to run your node, you need to be aware of the node types. Each node type consumes data differently. There are 3 node type:
1. Light Node
2. Full Node
3. Archive Node

#### <u>Light Node</u>
Instead of downloading the entire blockchain, you can download a shallow copy of the data containing only the block headers. A light node relies on a full node to get more data. A light node can also verify the data and participate to the network. The drawback is that you cannot participate in a consensus, you cannot be a miner nor a validator.

#### <u>Full Node</u>
The full node stores all block data not since the `Genesis`. This is periodically pruned and get only the most recent 128 blocks. A full node can:
- Participate in a consensus;
- Validate blocks; 
- Verify the states;
- Serve the network and provide data on request;

The drawback is that you cannot query data at block #1,000,000 because you don't have the entire history. Only the 128 last blocks. 

#### <u>Archive Node</u>
The archive node stores the historical states since the `Genesis`. Basically, if you want the query data since the first block, you can with the archive node.

---

### <u>Why should I run a node?</u> <a name="run-node-why"></a>
If you really believe in Ethereum, and decentralised crypto currency, you should consider running a node.

#### <u>For me</u>
You can use Ethereum privately and verify the data yourself. "Don't trust, verify" is a popular Blockchain mantra.
- Don't rely on another node
- Use an Ethereum wallet within the node and use DApps more securely and privately without leaking your address and balance to another node.
- Build your custom RPC endpoint
- Stake your ETH and earn rewards (be a validator with 32ETH)
- Allow wallets provider like `Metamask` to use your node

#### <u>For the Network</u>
- Enforce the consensus rules
- Social recovery in case of massive attack
- More diverse and robust network and provide a censorship-resistant ecosystem

---

### <u>Installation</u> <a name="installation-windows"></a>
#### Golang
In order to download the source code, you need to install [git command](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git).
`geth` is a software coded in golang. You need to install [Go](https://go.dev/dl/) before. 

If you are on Windows and have `Chocolatey` installed, you can run (Admin mode) these commands:

```bash
choco install git
choco install golang
choco install mingw
```
(If you run these commands, you'll need to close your terminal and open up a new one)

Then you can follow one of these steps in order to have Geth installed: 
- Download Source code (Windows) or
- Download Source Code (Linux) or
- Download the Package (Ubuntu)

#### <u>Download Source code (Windows)</u>
In order to download the source code, you can copy and paste these commands: 
```bash
mkdir src\github.com\ethereum
git clone git@github.com:ethereum/go-ethereum.git src\github.com\ethereum\go-ethereum
mkdir src\github.com\ethereum\go-ethereum
```
You should have this output:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d07pli4qx04uo50qzd2a.png)

You can now build the project from the binaries
```bash
go get -u -v golang.org/x/net/context
go install -v ./cmd/...
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cnu40tn382nqwujwtrcc.png)

#### <u>Download Source Code (Linux)</u>
If you have Linux (or MacOS), it's slightly different. You can just run this command: 
```bash
go get -d github.com/ethereum/go-ethereum
```

If you have an existing Geth client, you can update with this command
```bash
go get -u github.com/ethereum/go-ethereum
```

#### <u>Download the Package (Ubuntu)</u>
You need to add the launchpad repository
```bash
sudo add-apt-repository -y ppa:ethereum/ethereum
```
Then you can download the stable version of go-ethereum
```bash
sudo apt-get update
sudo apt-get install ethereum
```

If you have an existing geth installation, you can get the latest version with the following commands:
```bash
sudo apt-get update
sudo apt-get install ethereum
sudo apt-get upgrade geth
``` 

---

### <u>Create the chaindata folder</u> <a name="chain-data"></a>
In order to store the chain data, you need to create a folder somewhere in your system. I usually create the chain data at the same level of the binary (in my case)

```bash
mkdir -p ./chain/data
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w12n3rhurok11ppyw2o9.png)

### <u>Run the node</u> <a name="run-node"></a>
Now you are ready for running the node.
```bash
geth console --syncmode full --networkid 1 --gcmode full --datadir ..\chain\data\
```
What I'm doing here:
- geth: command line interface for the client
- console: start geth environment
- syncmode full: blockchain sync mode to full (default to snap)
- networkid 1: run the mainnet (by default it runs the mainnet)
- gcmode full: Garbage collection (full or archive)
- datadir: chaindata folder path that I have created earlier on

You can run a testnet node if you want to. You need to remove `--networkid 1` and replace by `--ropsten` or `--goerli` or `kiln` or `sepolia` or `rinkeby`.

Run `geth --help` to see the full list of commands

Once you have executed this command, you should have this output

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/94s66trswwkrij5wzlvv.png)

You will see that it will be searching for peers and download the blockchain data since the genesis (7 years 2 month and 5 days in my case)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a7a8wqx41aaxqz7d0cky.png)

(It's running in foreground. As soon as you close the terminal or press Ctrl+C / Ctrl+D, it will shutdown the node). If you want to run a node in a foreground or using as a service in Ubuntu, there are a lot of documentation for you. This is not the purpose of this article.

---

### <u>Interacting with Geth</u> <a name="geth-attach"></a>
You can interact with your node by attaching it to the datadir
```bash
geth attach --datadir ..\chain\data\
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0na9q4bolglcnyk956zb.png)

You can see what is the current block being downloaded and the highest block by executing this command in the interactive console
`eth.syncing`
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zno5gmjvaiiygj7fl4yr.png)

You can also execute Javascript code. In this example, I execute this snippet to know every 10 seconds the number of blocks per seconds being downloaded, the percentage of data downloaded and the remaining time

```js
var lastPercentage = 0;
var lastBlocksToGo = 0;
var timeInterval = 10000;
setInterval(function(){
    var percentage = eth.syncing.currentBlock/eth.syncing.highestBlock*100;
     var percentagePerTime = percentage - lastPercentage;
     var blocksToGo = eth.syncing.highestBlock - eth.syncing.currentBlock;
     var bps = (lastBlocksToGo - blocksToGo) / (timeInterval / 1000)
     var etas = 100 / percentagePerTime * (timeInterval / 1000)

     var etaM = parseInt(etas/60,10);
     console.log(parseInt(percentage,10)+'% ETA: '+etaM+' minutes @ '+bps+'bps');

     lastPercentage = percentage;lastBlocksToGo = blocksToGo;
 },timeInterval);
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g0b2jnof76w0p4c6om9q.png)

The number of block will be decreasing because over the year, there are more and more transaction in a block and you will be downloading more data in a full node in the last 128 blocks.

---

### <u>Run the node (dev mode / contributor)</u> <a name="run-node-dev"></a>
*The purpose of this article is not to help you running in dev mode. So I won't detail too much.*
If you want to contribute to the Ethereum client, you can run the command geth in dev mode without putting your assets at risk. There is a `--dev` flag that run `geth` in dev mode. In this way you have a node with:
- Data directory with a testing genesis block
- Max peers to 0. That means that Geth won't search for peers (default to 50)
- The node won't be visible to other nodes
- Gas price is set at 0 wei
- PoA (Proof of Authority) in order to mine block on demand
- Generate Block on demand.

For example, you can run this command:
```bash
geth --dev --http --http.api eth,web3,personal,net --http.corsdomain "http://remix.ethereum.org"
```
(See the full help from the command `geth --help`)

---

### <u>Next Steps</u> <a name="next-step"></a>
The next step is to run the consensus layer in order to be able to participate in the network after the merge.
After that, we will be covering the setup of a private node.

---

### <u>Conclusion </u> <a name="conclusion"></a>
Here you have just an overview on how to setup an Ethereum node. There are more to cover like:
- Using Clef as ab account management for Geth that allows to sign transaction
- Create a specific user to setup the node and give privilege
- Port forwarding for your node
- Miner options from the command line (will be deprecated)
- Auto start on boot in Linux
And more.

Thank you for reading and have fun in the decentralised world :)