# Besu Network Set

### **1. Overview**&#x20;

Hyperledger Besu is an open-source Ethereum client that supports both public and private networks. It provides different consensus algorithms, including QBFT for permissioned networks. In this guide, we'll use QBFT to set up a private blockchain network with 4 validator nodes and 1 RPC node.&#x20;

What You'll Set Up:&#x20;

* Validator Nodes: Validate transactions and blocks in the network.&#x20;
* RPC Node: Allows external applications to interact with the blockchain via JSON-RPC or WebSocket.&#x20;
* Prometheus & Grafana (Optional): Monitoring tools for visualizing metrics from the Besu nodes.&#x20;

&#x20;

### **2. Prerequisites**&#x20;

Before proceeding, ensure the following tools are installed:&#x20;

For Docker Setup:&#x20;

* Docker: Install Docker&#x20;
* Docker Compose: Install Docker Compose&#x20;

&#x20;

3\. Architecture&#x20;

We will set up the following:&#x20;

* 4 Validator Nodes: Each node will validate blocks and transactions using the QBFT consensus algorithm.&#x20;
* 1 RPC Node: Provides a JSON-RPC and WebSocket interface for interacting with the blockchain.&#x20;

Ports Used:&#x20;

* Validator Nodes: P2P ports (30303, 30304, 30305, 30306) and RPC ports (8545, 8547, 8548, 8549)&#x20;
* RPC Node: P2P port (30307), RPC port (8550), WebSocket port (8551)&#x20;

&#x20;

### **4. Step-by-Step Setup**&#x20;

#### **Step 1: Install Docker and Docker Compose**&#x20;

If you haven't already, install Docker and Docker Compose.&#x20;

* Docker Installation on Linux:&#x20;

```
sudo apt-get update  
sudo apt-get install docker.io  
sudo systemctl start docker  
sudo systemctl enable docker 
```

&#x20;

* **Docker Compose Installation:**&#x20;

```
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose 
```

Verify the installation by running:&#x20;

```
docker --version 
docker-compose --version 
```

#### **Step 2: Create a Directory Structure**&#x20;

1. Create a directory for your Besu network setup:&#x20;

```
mkdir besu-network 
cd besu-network 
```

1. Inside this directory, create subdirectories for each validator and the RPC node:&#x20;

```
mkdir -p data/validator1 data/validator2 data/validator3 data/validator4 data/rpcnode 
 
```

#### **Step 3: Generate Node Private Keys**&#x20;

To generate private keys for each validator node, you can use the besu CLI or generate them manually. The private keys will be required for each validator to participate in the consensus.&#x20;

Run the following command for each validator node:&#x20;

```
besu operator generate-blockchain-config --config-file=configFile --to=networkFiles --private-key-file-name=key 
```

&#x20;Place each validator's private key in its respective directory (e.g., data/validator1/key).&#x20;

#### **Step 4: Create the Genesis File**&#x20;

The genesis.json file defines the initial state and configuration of your blockchain. Create a file named **genesis.json** inside your besu-network directory:&#x20;

```
{ 
  "config": { 
    "chainId": 1337, 
    "isQuorum": true, 
    "qbft": { 
      "blockperiodseconds": 5, 
      "epochlength": 30000, 
      "requesttimeoutseconds": 10 
    } 
  }, 
  "nonce": "0x0", 
  "timestamp": "0x58ee40ba", 
  "gasLimit": "0x47b760", 
  "difficulty": "0x1", 
  "alloc": {}, 
  "extraData": "0x", 
  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", 
  "mixHash": "0x00000000000000000000000000000000000000647572616c65787365646c6578", 
  "coinbase": "0x0000000000000000000000000000000000000000", 
  "gasUsed": "0x0", 
  "number": "0x0" 
} 
```

The extraData field will include your validator addresses. You can generate this once the private keys are ready.&#x20;

#### **Step 5: Create the Docker Compose File**&#x20;

Now create a **docker-compose.yml** file to define your services (4 validators and 1 RPC node).&#x20;

```
version: "3.7" 
 
services: 
  validator1: 
    image: hyperledger/besu:latest 
    container_name: validator1 
    volumes: 
      - ./data/validator1:/opt/besu/data 
      - ./genesis.json:/opt/besu/genesis.json 
    command: > 
      --genesis-file=/opt/besu/genesis.json 
      --data-path=/opt/besu/data 
      --node-private-key-file=/opt/besu/data/key 
      --p2p-port=30303 
      --rpc-http-enabled 
      --rpc-http-port=8545 
      --rpc-http-api=ETH,NET,WEB3,CLIQUE,ADMIN 
      --host-whitelist=* 
      --sync-mode=FULL 
      --miner-enabled 
      --min-gas-price=0 
      --miner-coinbase=0x0000000000000000000000000000000000000000 
    ports: 
      - "30303:30303" 
      - "8545:8545" 
 
  validator2: 
    image: hyperledger/besu:latest 
    container_name: validator2 
    volumes: 
      - ./data/validator2:/opt/besu/data 
      - ./genesis.json:/opt/besu/genesis.json 
    command: > 
      --genesis-file=/opt/besu/genesis.json 
      --data-path=/opt/besu/data 
      --node-private-key-file=/opt/besu/data/key 
      --p2p-port=30304 
      --rpc-http-enabled 
      --rpc-http-port=8547 
      --rpc-http-api=ETH,NET,WEB3,CLIQUE,ADMIN 
      --host-whitelist=* 
      --sync-mode=FULL 
      --miner-enabled 
      --min-gas-price=0 
      --miner-coinbase=0x0000000000000000000000000000000000000000 
    ports: 
      - "30304:30304" 
      - "8547:8547" 
 
  validator3: 
    image: hyperledger/besu:latest 
    container_name: validator3 
    volumes: 
      - ./data/validator3:/opt/besu/data 
      - ./genesis.json:/opt/besu/genesis.json 
    command: > 
      --genesis-file=/opt/besu/genesis.json 
      --data-path=/opt/besu/data 
      --node-private-key-file=/opt/besu/data/key 
      --p2p-port=30305 
      --rpc-http-enabled 
      --rpc-http-port=8548 
      --rpc-http-api=ETH,NET,WEB3,CLIQUE,ADMIN 
      --host-whitelist=* 
      --sync-mode=FULL 
      --miner-enabled 
      --min-gas-price=0 
      --miner-coinbase=0x0000000000000000000000000000000000000000 
    ports: 
      - "30305:30305" 
      - "8548:8548" 
 
  validator4: 
    image: hyperledger/besu:latest 
    container_name: validator4 
    volumes: 
      - ./data/validator4:/opt/besu/data 
      - ./genesis.json:/opt/besu/genesis.json 
    command: > 
      --genesis-file=/opt/besu/genesis.json 
      --data-path=/opt/besu/data 
      --node-private-key-file=/opt/besu/data/key 
      --p2p-port=30306 
      --rpc-http-enabled 
      --rpc-http-port=8549 
      --rpc-http-api=ETH,NET,WEB3,CLIQUE,ADMIN 
      --host-whitelist=* 
      --sync-mode=FULL 
      --miner-enabled 
      --min-gas-price=0 
      --miner-coinbase=0x0000000000000000000000000000000000000000 
    ports: 
      - "30306:30306" 
      - "8549:8549" 
 
  rpcnode: 
    image: hyperledger/besu:latest 
    container_name: rpcnode 
    volumes: 
      - ./data/rpcnode:/opt/besu/data 
      - ./genesis.json:/opt/besu/genesis.json 
    command: > 
      --genesis-file=/opt/besu/genesis.json 
      --data-path=/opt/besu/data 
      --p2p-port=30307 
      --rpc-http-enabled 
      --rpc-http-port=8550 
      --rpc-http-api=ETH,NET,WEB3,ADMIN,DEBUG,TXPOOL 
      --rpc-ws-enabled 
      --rpc-ws-port=8551 
      --rpc-ws-api=ETH,NET,WEB3,ADMIN,DEBUG,TXPOOL 
      --host-whitelist=* 
      --sync-mode=FAST 
    ports: 
      - "30307:30307" 
      - "8550:8550" 
      - "8551:8551" 
```

#### **Step 6: Launch the Network**&#x20;

With your docker-compose.yml, genesis file, and private keys in place, launch the Besu network: \
&#x20;

```
docker-compose up -d 
```

This will spin up 4 validator nodes and 1 RPC node.&#x20;

&#x20;

#### **Step 7: Verify and Interact with the Network**&#x20;

1. List Running Containers:&#x20;

```
docker ps
```

1. Check Node Logs:&#x20;

View logs for a specific validator to ensure it's working correctly:&#x20;

```
docker logs validator1 
```

3. Interact with the Network:&#x20;

Using curl, you can interact with the blockchain via the RPC node:&#x20;

1. Get the current block number:&#x20;

```
curl -X POST --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}' 
http://localhost:8550
```

2. Connect using web3.js or ethers.js to perform more advanced actions such as sending transactions and deploying smart contracts.&#x20;

## 5. Monitoring (Optional): Prometheus and Grafana&#x20;

Prometheus and Grafana can be used to monitor your Besu nodes in real time. First, create a **prometheus.yml** configuration file:&#x20;

```
scrape_configs: 
  - job_name: 'besu' 
    static_configs: 
      - targets: ['validator1:9545', 'validator2:9546', 'rpcnode:9547'] 
```

#### Add the services to your Docker Compose file:&#x20;

```
prometheus: 
  image: prom/prometheus 
  volumes: 
    - ./prometheus.yml:/etc/prometheus/prometheus.yml 
  ports: 
    - "9090:9090" 
 
grafana: 
  image: grafana/grafana 
  ports: 
    - "3000:3000" 
```

Start the services:&#x20;

```
docker-compose up -d 
```

You can access Grafana on [http://localhost:3000](http://localhost:3000/) and Prometheus on [http://localhost:9090](http://localhost:9090/).&#x20;

&#x20;

## 6. Troubleshooting&#x20;

1. Nodes Not Syncing:&#x20;
   1. Ensure all nodes use the same genesis.json.&#x20;
   2. Verify that all nodes can reach each other through their P2P ports.&#x20;
2. No Peer Connections:&#x20;
   1. Check firewall settings to ensure P2P ports are open.&#x20;
   2. Use docker network ls to verify that all containers are on the same network.
3. Node Crashes:&#x20;
   1. View the logs using docker logs \<container-name> to diagnose the issue.&#x20;
   2. Check if private keys are correctly placed.&#x20;

## 7. Conclusion&#x20;

By following this guide, you now have a fully functional Hyperledger Besu private blockchain network with 4 validator nodes and 1 RPC node. You can now expand this network, add new nodes, or deploy smart contracts. Monitoring tools such as Prometheus and Grafana can also be added for better visibility into node performance.&#x20;

&#x20;


---

# 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://abc.bharatblockchain.io/hyperledger-besu/besu-network-set.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.
