Skip to content

Commit

Permalink
Merge pull request #16 from lidofinance/local-dev-desc
Browse files Browse the repository at this point in the history
Local dev desc
  • Loading branch information
sergeyWh1te authored Sep 6, 2024
2 parents 26108f3 + 9eed29d commit 9d0c1d7
Show file tree
Hide file tree
Showing 34 changed files with 2,369 additions and 89 deletions.
13 changes: 12 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ outdated-deps:
go list -u -m -json -mod=readonly all
.PHONY: outdated-deps


.PHONY: swagger-gen
swagger-gen:
rm -rf generated/forta && \
Expand All @@ -45,3 +44,15 @@ swagger-gen:
--exclude-main \
--skip-support \
--skip-operations

generate-proto:
@mkdir -p ./generated/proto
protoc --go_out=./generated/proto \
--go-grpc_out=./generated/proto \
brief/proto/*.proto

generate-databus-objects:
bin/jsonschema -p databus -o generated/databaus/block.dto.go ./brief/databus/block.dto.json
.PHONY: generate-databus-objects

.PHONY: generate-proto
86 changes: 86 additions & 0 deletions brief/databus/block.dto.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"$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"]
}
97 changes: 97 additions & 0 deletions brief/proto/finding.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
syntax = "proto3";

option go_package = "./;proto";

message Finding {
enum Severity {
UNKNOWN = 0;
INFO = 1;
LOW = 2;
MEDIUM = 3;
HIGH = 4;
CRITICAL = 5;
}

enum FindingType {
UNKNOWN_TYPE = 0;
EXPLOIT = 1;
SUSPICIOUS = 2;
DEGRADED = 3;
INFORMATION = 4;
SCAM = 5;
}

string protocol = 1;
Severity severity = 2;
map<string, string> metadata = 3;
FindingType type = 4;
string alertId = 5;
string name = 6;
string description = 7;
reserved 8;
bool private = 9;
repeated string addresses = 10;
map<string, double> indicators = 11;
repeated Label labels = 12;
repeated string relatedAlerts = 13;
string uniqueKey = 14;
Source source = 15;
string timestamp = 16;
}

message Label {
enum EntityType {
UNKNOWN_ENTITY_TYPE = 0;
ADDRESS = 1;
TRANSACTION = 2;
BLOCK = 3;
URL = 4;
}

EntityType entityType = 1;
string entity = 2;
reserved 3;
float confidence = 4;
reserved 5;
bool remove = 6;
string label = 7;
repeated string metadata = 8;
string uniqueKey = 9;
}

message Source {
message TransactionSource {
uint64 chainId = 1;
string hash = 2;
}

message BlockSource {
uint64 chainId = 1;
string hash = 2;
uint64 number = 3;
}

message URLSource {
string url = 1;
}

message ChainSource {
uint64 chainId = 1;
}

message AlertSource {
string id = 1;
}

message CustomSource {
string name = 1;
string value = 2;
}

repeated TransactionSource transactions = 1;
repeated BlockSource blocks = 2;
repeated URLSource urls = 3;
repeated ChainSource chains = 4;
repeated AlertSource alerts = 5;
repeated CustomSource customSources = 6;
}
87 changes: 60 additions & 27 deletions cmd/worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"github.com/lidofinance/finding-forwarder/internal/utils/registry/teams"
"os"
"os/signal"
"syscall"
Expand All @@ -15,14 +16,14 @@ import (
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/sync/errgroup"

"github.com/lidofinance/finding-forwarder/internal/app/feeder"
"github.com/lidofinance/finding-forwarder/internal/app/server"
"github.com/lidofinance/finding-forwarder/internal/app/worker"
"github.com/lidofinance/finding-forwarder/internal/connectors/logger"
"github.com/lidofinance/finding-forwarder/internal/connectors/metrics"
nc "github.com/lidofinance/finding-forwarder/internal/connectors/nats"
"github.com/lidofinance/finding-forwarder/internal/env"
"github.com/lidofinance/finding-forwarder/internal/utils/registry"
"github.com/lidofinance/finding-forwarder/internal/utils/registry/teams"
)

func main() {
Expand Down Expand Up @@ -62,24 +63,35 @@ func main() {
app.Metrics.BuildInfo.Inc()
app.RegisterWorkerRoutes(r)

protocolSubject := fmt.Sprintf(`%s.%s`, cfg.AppConfig.NatsStreamName, teams.Protocol)
fallbackSubject := fmt.Sprintf(`%s.%s`, cfg.AppConfig.NatsStreamName, registry.FallBackTeam)
protocolNatsSubject := fmt.Sprintf(`%s.%s`, cfg.AppConfig.FindingTopic, teams.Protocol)

protocolFortaSubject := fmt.Sprintf(`%s.%s`, cfg.AppConfig.FortaAlertsTopic, teams.Protocol)
fallbackFortaSubject := fmt.Sprintf(`%s.%s`, cfg.AppConfig.FortaAlertsTopic, registry.FallBackTeam)

natsStreamName := `NatsStream`
fortaStreamName := `FortaStream`

commonStreamName := fmt.Sprintf(`%s_STREAM`, cfg.AppConfig.NatsStreamName)
natStream, err := js.CreateOrUpdateStream(ctx, jetstream.StreamConfig{
Name: natsStreamName,
Discard: jetstream.DiscardOld,
MaxAge: 10 * time.Minute,
Subjects: []string{
protocolNatsSubject,
},
})

stream, createStreamErr := js.CreateOrUpdateStream(ctx, jetstream.StreamConfig{
Name: commonStreamName,
fortaStream, err := js.CreateOrUpdateStream(ctx, jetstream.StreamConfig{
Name: fortaStreamName,
Discard: jetstream.DiscardOld,
MaxAge: 10 * time.Minute,
Subjects: []string{
protocolSubject,
fallbackSubject,
// When you want to set up notifications for your team, you have to add your subject
protocolFortaSubject,
fallbackFortaSubject,
},
})

if createStreamErr != nil && !errors.Is(createStreamErr, nats.ErrStreamNameAlreadyInUse) {
fmt.Printf("could not create %s stream error: %v\n", commonStreamName, createStreamErr)
if err != nil && !errors.Is(err, nats.ErrStreamNameAlreadyInUse) {
fmt.Printf("could not create %s stream error: %v\n", natsStreamName, err)
return
}

Expand All @@ -89,32 +101,53 @@ func main() {
OpsGenie = `OpsGenie`
)

protocolWorker := worker.NewWorker(
protocolSubject,
stream, log, metricsStore,
worker.WithConsumer(services.OnChainAlertsTelegram, `OnChainAlerts_Telegram_Consumer`, registry.OnChainAlerts, Telegram),
worker.WithConsumer(services.OnChainUpdatesTelegram, `OnChainUpdates_Telegram_Consumer`, registry.OnChainUpdates, Telegram),
worker.WithConsumer(services.ErrorsTelegram, `Protocol_Errors_Telegram_Consumer`, registry.OnChainErrors, Telegram),
worker.WithConsumer(services.Discord, `Protocol_Discord_Consumer`, registry.FallBackAlerts, Discord),
worker.WithConsumer(services.OpsGenia, `Protocol_OpGenia_Consumer`, registry.OnChainAlerts, OpsGenie),
protocolWorker := worker.NewFindingWorker(
protocolNatsSubject,
natStream, log, metricsStore,
worker.WithFindingConsumer(services.OnChainAlertsTelegram, `OnChainAlerts_Telegram_Consumer`, registry.OnChainAlerts, Telegram),
worker.WithFindingConsumer(services.OnChainUpdatesTelegram, `OnChainUpdates_Telegram_Consumer`, registry.OnChainUpdates, Telegram),
worker.WithFindingConsumer(services.ErrorsTelegram, `Protocol_Errors_Telegram_Consumer`, registry.OnChainErrors, Telegram),
worker.WithFindingConsumer(services.Discord, `Protocol_Discord_Consumer`, registry.FallBackAlerts, Discord),
worker.WithFindingConsumer(services.OpsGenia, `Protocol_OpGenia_Consumer`, registry.OnChainAlerts, OpsGenie),
)

protocolFortaWorker := worker.NewAlertWorker(
protocolFortaSubject,
fortaStream, log, metricsStore,
worker.WithAlertConsumer(services.OnChainAlertsTelegram, `Forta_OnChainAlerts_Telegram_Consumer`, registry.AlertOnChainAlerts, Telegram),
worker.WithAlertConsumer(services.OnChainUpdatesTelegram, `Forta_OnChainUpdates_Telegram_Consumer`, registry.AlertOnChainUpdates, Telegram),
worker.WithAlertConsumer(services.ErrorsTelegram, `Forta_Protocol_Errors_Telegram_Consumer`, registry.AlertOnChainErrors, Telegram),
worker.WithAlertConsumer(services.Discord, `Forta_Protocol_Discord_Consumer`, registry.AlertFallBackAlerts, Discord),
worker.WithAlertConsumer(services.OpsGenia, `Forta_Protocol_OpGenia_Consumer`, registry.AlertOnChainAlerts, OpsGenie),
)

fallbackWorker := worker.NewWorker(
fallbackSubject,
stream, log, metricsStore,
worker.WithConsumer(services.Discord, `Fallback_Discord_Consumer`, registry.FallBackAlerts, Discord),
fallbackFortaWorker := worker.NewAlertWorker(
fallbackFortaSubject,
fortaStream, log, metricsStore,
worker.WithAlertConsumer(services.Discord, `FortaFallback_Discord_Consumer`, registry.AlertFallBackAlerts, Discord),
)

if wrkErr := protocolWorker.Run(gCtx, g); wrkErr != nil {
fmt.Println("Could not start protocolWorker error:", wrkErr.Error())
feederWrk := feeder.New(log, services.ChainSrv, js, metricsStore, cfg.AppConfig.BlockTopic)

// Listen findings from Nats
if err := protocolWorker.Run(gCtx, g); err != nil {
fmt.Println("Could not start protocolWorker error:", err.Error())
return
}

if wrkErr := fallbackWorker.Run(gCtx, g); wrkErr != nil {
fmt.Println("Could not start fallbackWorker error:", wrkErr.Error())
// Listen alerts from Forta group software
if err := protocolFortaWorker.Run(gCtx, g); err != nil {
fmt.Println("Could not start protocolFortaWorker error:", err.Error())
return
}

if wrkErr := fallbackFortaWorker.Run(gCtx, g); wrkErr != nil {
fmt.Println("Could not start fallbackFortaWorker error:", wrkErr.Error())
return
}

feederWrk.Run(gCtx, g)

app.RunHTTPServer(gCtx, g, cfg.AppConfig.Port, r)

if err := g.Wait(); err != nil {
Expand Down
23 changes: 21 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ x-logging: &default-logging

services:
service-forta-nats:
image: nats:2.10.16-alpine3.19
image: nats:2.10.20-alpine3.20
container_name: forta-nats
ports:
- "4222:4222"
Expand Down Expand Up @@ -101,7 +101,26 @@ services:
- ETHEREUM_RPC_URL=http://forta-json-rpc:8545
- USE_FORTA_RPC_URL=false
- NODE_ENV=production
- NATS_SERVER_URL=http://forta-nats:4222
depends_on:
- service-forta-json-rpc
- service-forta-nats
ports:
- '3001:3000'

service-l2-bridge-arbitrum:
container_name: l2-bridge-arbitrum
logging: *default-logging
image: monitoring/l2-bridge-arbitrum:latest
restart: unless-stopped
environment:
- APP_NAME=l2-bridge-arbitrum
- INSTANCE=forta-local-host
- ETHEREUM_RPC_URL=http://forta-json-rpc:8545
- ARBITRUM_RPC_URL=https://arbitrum-one.blastapi.io/<you_api_ley>
- USE_FORTA_RPC_URL=false
- NODE_ENV=production
depends_on:
- service-forta-json-rpc
ports:
- '3000:3000'
- '3002:3000'
3 changes: 2 additions & 1 deletion forta-local-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ localMode:
enable: true
botContainers:
- host.docker.internal
- ethereum-steth
- ethereum-steth
- l2-bridge-arbitrum
Loading

0 comments on commit 9d0c1d7

Please sign in to comment.