Skip to content

Commit

Permalink
Merge pull request #44 from lidofinance/documentation
Browse files Browse the repository at this point in the history
feat: documentation
  • Loading branch information
sergeyWh1te authored Oct 23, 2024
2 parents 68c3339 + fb1e73d commit b2531b2
Show file tree
Hide file tree
Showing 7 changed files with 440 additions and 25 deletions.
167 changes: 142 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,149 @@
# Finding-forwarder
Service forwards findings from forta-local-node to telegram, opsGenie and discord

## How to develop
1. Install go1.23.1+
2. cd root repository
3. make tools
4. make vendor
5. copy `sample.env` to `.env`
6. clone https://github.com/lidofinance/alerting-forta/
1. cd ethereum-steth
2. make steth
7. clone https://github.com/forta-network/forta-node/
1. cd forta-node
2. make containers
8. Go back to current project then docker-compose up -d

### I dont need steth-bot. What I have to do?
1. comment service-ethereum-steth in docker-compose file
2. on your local host in your bot repo - ```yarn start```
3. Also, you have to provide the container name of your bot to FORTA-SOFTWARE by analogy with steth:
1. Container name for steth is **ethereum-steth** look docker-compose file
2. put bot's container name to the file forta-local-config.yml by analogy with steth
3. Consider that in app has programmed consumer for your team bot or use fallback consumer
(provider any name that not included in the list internal/utils/registry/registry.go)
# Finding-Forwarder

<p align="center">
<img src="./docs/Feeder.png" alt="Feeder" style="width:200px; height:200px; margin-right: 15px;" />
<img src="./docs/Forwarder.png" alt="Forwarder" style="width:200px; height:200px; margin-right: 15px;" />
</p>

**Finding-Forwarder** is a service suite designed to fetch blockchain data, process it, and forward important findings to various notification channels such as Telegram, OpsGenie, and Discord. The suite consists of two main components:

1. **Feeder**: Fetches the latest blockchain data at regular intervals and publishes it to a specific NATS topic.
2. **Forwarder**: Listens to findings from bots, applies quorum and filtering, and forwards critical information to the appropriate notification channels.

This solution serves as an alternative to **[OpenZeppelin Defender](https://defender.openzeppelin.com/)** and **[Forta](https://app.forta.network/)**, providing features such as guaranteed message delivery and findings processing based on quorum.

## Components

- **[Feeder](./feeder.md)**: Fetches blockchain data and publishes it to a NATS topic.
- **[Forwarder](./forwarder.md)**: Receives findings from various bots, processes them, and forwards them to notification channels.
- **[Configuration](./config.md)**: Contains details on how to set up and configure the **Finding-Forwarder** system.
- **[notification.prod.sample.yaml](./notification.prod.sample.yaml)**: Dynamic notification config

## How It Works - Simplified Overview

> The following graphic represents how the infrastructure is set up on a single virtual machine. In practice, there are three such machines, and quorum is collected 2 out of 3 based on Redis.
```plaintext
+-----------------------------+
| Blockchain |
| (Source of block data) |
+--------------+--------------+
|
v
+-----------------------------+
| Feeder |
| Fetches blockchain data and |
| publishes to NATS topic |
| (e.g., blocks.mainnet.l1) |
+--------------+--------------+
|
v
+-----------------------------+
| NATS Server |
| Manages data communication |
| between components |
+--------------+--------------+
|
v
+-----------------------------+
| Bots |
| Subscribed to block data, |
| process findings and send |
| them to findings.<team>.<bot>|
+--------------+--------------+
|
v
+-----------------------------+
| Forwarder |
| Listens to findings topics, |
| applies quorum and filters, |
| and forwards notifications |
| to configured channels like |
| Telegram, Discord, OpsGenie.|
+--------------+--------------+
|
v
+-----------------------------+
| Redis (Quorum Storage) |
| Ensures that findings are |
| processed only after quorum |
| is reached (e.g., 2 out of 3|
| forwarders must agree). |
| Prevents duplicate sending |
| and ensures consistency. |
+-----------------------------+
```
### Explanation:
1. **Feeder** continuously fetches the latest blockchain data and publishes it to a specific NATS topic.
2. **Bots** subscribe to this topic, process the block data, and send their findings to topics like `findings.<team_name>.<bot_name>`.
3. **Forwarder Instances**: Forwarders listen to findings topics, process the data, and check for quorum.
- Forwarders use **Redis** to store quorum-related data, ensuring that findings are only processed after the quorum (e.g., 2 out of 3 forwarders) is reached.
- This mechanism also prevents duplicate sending, as only one instance will proceed once the quorum condition is satisfied.
4. **Redis** helps maintain state consistency, ensuring reliable and fault-tolerant processing of findings across the distributed setup.

## How to Develop

To set up a local development environment for **Finding-Forwarder**, follow these steps:

1. **Prerequisites**:
- Install `go1.23.1+`
- Clone the repository: `git clone <your-repo-url>`
- Navigate to the root of the repository: `cd finding-forwarder`

2. **Install Tools and Dependencies**:
```bash
make tools
make vendor
```

3. **Environment Setup**:
- Copy the `sample.env` file to `.env`:
```bash
cp sample.env .env
```
- Configure your environment variables as needed. Below is an explanation of the available environment variables:

| Variable | Description | Default Value |
|------------------------|---------------------------------------------------------------------------------------------------|------------------------------------|
| `READ_ENV_FROM_SHELL` | Whether to read environment variables from the shell (useful for container setups). | `false` |
| `SOURCE` | Source name for identifying where the forwarder is running. | `local` |
| `ENV` | The environment mode of the application. | `local` |
| `APP_NAME` | Name of the application. | `finding_forwarder` |
| `PORT` | Port on which the application will run. | `8080` |
| `LOG_FORMAT` | Log format (`simple` or `json`). | `simple` |
| `LOG_LEVEL` | Log level (e.g., `debug`, `info`, `warn`, `error`). | `debug` |
| `BLOCK_TOPIC` | NATS topic for the Feeder to publish blockchain data. | `blocks.mainnet.l1` |
| `NATS_DEFAULT_URL` | URL for connecting to the NATS server. | `http://service-forta-nats:4222` |
| `REDIS_ADDRESS` | Address for connecting to the Redis instance. | `redis:6379` |
| `REDIS_DB` | Redis database index to use. | `0` |
| `QUORUM_SIZE` | The number of confirmations required before forwarding a message. | `1` |
| `JSON_RPC_URL` | URL for connecting to the Ethereum JSON-RPC endpoint. | `https://eth.drpc.org` |

4. **Building and Running Bots**:
- Clone the **Testing Forta Bots** repository:
```bash
git clone https://github.com/lidofinance/testing-forta-bots/
```
- Navigate to the `bots` directory and then into the specific bot you want to build, for example:
```bash
cd testing-forta-bots/bots/ethereum-steth-v2
```
- Build the Docker image for the bot:
```bash
make generate-docker
```
- After building the bot, return to the **Finding-Forwarder** project directory:
- You can now add environment variables for the bot either directly in the `.env` file or pass them through the `docker-compose.yml` file.

5. **Start Services Using Docker Compose**:
```bash
docker-compose up -d
```

### I want to develop finding-forwarder server or worker
1. comment forwarder-server or forwarder-worker in docker-compose file
2. provide env variable for application that it could connect to nats in docker
3. provide some bot for your purposes or run bot on local machine not in docker container

## Docs and rules
1. [App structure layout](./docs/structure.md)
Expand Down
File renamed without changes.
Binary file added docs/Feeder.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/Forwarder.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/grafana.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
160 changes: 160 additions & 0 deletions feeder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@

# Feeder
<div style="display: flex; align-items: flex-start;">
<img src="./docs/Feeder.png" alt="Feeder" style="width:200px; height:200px; margin-right: 15px;" />
<div>
<p>
<strong>Feeder</strong> is a component of the <strong>finding-forwarder</strong> software suite. The Feeder serves as an alternative group of software developed by Forta (forta-rpc-scanner, forta-json-rpc),
Its primary function is to fetch the latest blockchain data at regular intervals and publish this data to a NATS topic. This allows other components, particularly bots, to subscribe to the topic and process the blockchain information.
</p>
<h3>How Feeder Works</h3>
<ul>
<li><strong>Feeder</strong> runs continuously, fetching the latest block data from the blockchain every 6 seconds.</li>
<li>It retrieves transaction receipts and other relevant data, organizes this information, and publishes it to a specific topic defined in the environment variables: <code>BLOCK_TOPIC="blocks.mainnet.l1"</code>.</li>
<li>Bots within the system subscribe to this topic (<code>blocks.mainnet.l1</code>) to listen for new block data. After processing, they send their findings to different NATS topics following the naming pattern: <code>findings.&lt;team_name&gt;.&lt;bot_name&gt;</code>.</li>
<li>The processed findings are then managed and sent to notification channels by the <strong>Forwarder</strong> component, ensuring critical information reaches the appropriate channels.</li>
</ul>
</div>
</div>

### BlockDto Format
The data published by **Feeder** follows a structured JSON format defined in [block.dto.json](./brief/databus/block.dto.json). Below is the schema:

```json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "BlockDto",
"type": "object",
"properties": {
"number": {
"type": "integer"
},
"timestamp": {
"type": "integer"
},
"parentHash": {
"type": "string"
},
"hash": {
"type": "string"
},
"receipts": {
"title": "Receipts",
"type": "array",
"items": {
"title": "Receipt",
"type": "object",
"properties": {
"to": {
"type": "string"
},
"logs": {
"title": "Logs",
"type": "array",
"items": {
"title": "Log",
"type": "object",
"properties": {
"address": {
"type": "string"
},
"topics": {
"title": "Topics",
"type": "array",
"items": {
"type": "string"
}
},
"data": {
"type": "string"
},
"blockNumber": {
"type": "integer"
},
"transactionHash": {
"type": "string"
},
"transactionIndex": {
"type": "integer"
},
"blockHash": {
"type": "string"
},
"logIndex": {
"type": "integer"
},
"removed": {
"type": "boolean"
}
},
"required": [
"address",
"topics",
"data",
"blockNumber",
"transactionHash",
"transactionIndex",
"blockHash",
"logIndex",
"removed"
]
}
}
},
"required": ["logs"]
}
}
},
"required": ["number", "timestamp", "hash", "parentHash", "receipts"]
}
```

### Key Functionality
1. **Regular Data Fetching:**
- **Feeder** retrieves the latest blockchain block data every 6 seconds, ensuring that the system is always up-to-date with the latest information.
2. **Publishing to NATS JetStream:**
- The fetched data is published to a defined NATS topic, which other bots can subscribe to for further processing.
3. **Integration with Finding-Forwarder Workflow:**
- After bots process the data, the results are published to findings topics, which are then picked up by **Forwarder** for further handling and delivery to notification channels.

### Example Workflow:
1. **Feeder** fetches a new block from the blockchain and publishes it to `blocks.mainnet.l1`.
2. Bots subscribed to `blocks.mainnet.l1` process the block data, extracting relevant findings.
3. The bots send their findings to `findings.<team_name>.<bot_name>`.
4. **Forwarder** listens to these findings topics, applies quorum and filtering, and forwards the important findings to configured notification channels like Telegram, Discord, or OpsGenie.

### Process Flow Diagram

```plaintext
+--------------------------+
| Feeder |
| Fetches latest block data|
+-----------+--------------+
|
v
+--------------------------+
| Publishes block data to |
| blocks.mainnet.l1 topic |
+-----------+--------------+
|
v
+-------------------------------+
| Bots Subscribed |
| Listen to blocks.mainnet.l1 |
+-----------+-------------------+
|
v
+-----------------------------------+
| Process block data and send |
| findings to findings.<team>.<bot> |
+-----------+-----------------------+
|
v
+------------------------------------+
| Forwarder |
| Listens to findings topics |
| Applies quorum and filtering |
| Forwards to notification channels |
| (Telegram, Discord, OpsGenie) |
+------------------------------------+
```
Loading

0 comments on commit b2531b2

Please sign in to comment.