2022-10-09 11:41:15 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-10-22 16:38:29 +00:00
|
|
|
"fmt"
|
2022-10-09 11:41:15 +00:00
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
2022-11-28 23:00:40 +00:00
|
|
|
sentinelrpc "github.com/ledgerwatch/erigon-lib/gointerfaces/sentinel"
|
2022-11-15 19:39:18 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
2022-12-05 00:25:12 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cl/cltypes"
|
2022-10-29 19:51:32 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/sentinel/sentinel"
|
2022-10-09 11:41:15 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
2023-05-11 16:38:56 +00:00
|
|
|
rcmgrObs "github.com/libp2p/go-libp2p/p2p/host/resource-manager/obs"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2022-10-09 11:41:15 +00:00
|
|
|
"google.golang.org/grpc"
|
2022-11-11 22:22:47 +00:00
|
|
|
"google.golang.org/grpc/credentials"
|
2022-10-09 15:49:27 +00:00
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
2022-10-09 11:41:15 +00:00
|
|
|
)
|
|
|
|
|
2022-12-22 18:59:24 +00:00
|
|
|
const maxMessageSize = 437323800
|
|
|
|
|
2022-10-09 11:41:15 +00:00
|
|
|
type ServerConfig struct {
|
|
|
|
Network string
|
|
|
|
Addr string
|
|
|
|
}
|
|
|
|
|
2023-05-19 17:41:53 +00:00
|
|
|
func createSentinel(cfg *sentinel.SentinelConfig, db kv.RoDB, logger log.Logger) (*sentinel.Sentinel, error) {
|
|
|
|
sent, err := sentinel.New(context.Background(), cfg, db, logger)
|
2022-10-09 11:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := sent.Start(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-06 23:37:53 +00:00
|
|
|
gossipTopics := []sentinel.GossipTopic{
|
2022-10-09 11:41:15 +00:00
|
|
|
sentinel.BeaconBlockSsz,
|
2022-12-03 02:16:26 +00:00
|
|
|
//sentinel.BeaconAggregateAndProofSsz,
|
2023-03-17 21:51:19 +00:00
|
|
|
//sentinel.VoluntaryExitSsz,
|
|
|
|
//sentinel.ProposerSlashingSsz,
|
|
|
|
//sentinel.AttesterSlashingSsz,
|
2022-10-09 11:41:15 +00:00
|
|
|
}
|
2023-07-11 15:53:00 +00:00
|
|
|
// gossipTopics = append(gossipTopics, sentinel.GossipSidecarTopics(chain.MaxBlobsPerBlock)...)
|
2023-04-17 18:06:50 +00:00
|
|
|
|
2023-05-06 23:37:53 +00:00
|
|
|
for _, v := range gossipTopics {
|
2023-04-17 18:06:50 +00:00
|
|
|
if err := sent.Unsubscribe(v); err != nil {
|
2023-05-19 17:41:53 +00:00
|
|
|
logger.Error("[Sentinel] failed to start sentinel", "err", err)
|
2023-04-17 18:06:50 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-10-09 11:41:15 +00:00
|
|
|
// now lets separately connect to the gossip topics. this joins the room
|
|
|
|
subscriber, err := sent.SubscribeGossip(v)
|
|
|
|
if err != nil {
|
2023-05-19 17:41:53 +00:00
|
|
|
logger.Error("[Sentinel] failed to start sentinel", "err", err)
|
2022-10-09 11:41:15 +00:00
|
|
|
}
|
2022-10-16 17:54:12 +00:00
|
|
|
// actually start the subscription, aka listening and sending packets to the sentinel recv channel
|
2022-10-09 11:41:15 +00:00
|
|
|
err = subscriber.Listen()
|
|
|
|
if err != nil {
|
2023-05-19 17:41:53 +00:00
|
|
|
logger.Error("[Sentinel] failed to start sentinel", "err", err)
|
2022-10-09 11:41:15 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-26 13:33:21 +00:00
|
|
|
return sent, nil
|
|
|
|
}
|
2023-04-17 18:06:50 +00:00
|
|
|
|
2023-05-19 17:41:53 +00:00
|
|
|
func StartSentinelService(cfg *sentinel.SentinelConfig, db kv.RoDB, srvCfg *ServerConfig, creds credentials.TransportCredentials, initialStatus *cltypes.Status, logger log.Logger) (sentinelrpc.SentinelClient, error) {
|
2023-04-26 13:33:21 +00:00
|
|
|
ctx := context.Background()
|
2023-05-19 17:41:53 +00:00
|
|
|
sent, err := createSentinel(cfg, db, logger)
|
2023-04-26 13:33:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-11 16:38:56 +00:00
|
|
|
rcmgrObs.MustRegisterWith(prometheus.DefaultRegisterer)
|
2023-05-19 17:41:53 +00:00
|
|
|
logger.Info("[Sentinel] Sentinel started", "enr", sent.String())
|
2022-12-05 00:25:12 +00:00
|
|
|
if initialStatus != nil {
|
|
|
|
sent.SetStatus(initialStatus)
|
|
|
|
}
|
2023-05-19 17:41:53 +00:00
|
|
|
server := NewSentinelServer(ctx, sent, logger)
|
2022-11-11 22:22:47 +00:00
|
|
|
if creds == nil {
|
|
|
|
creds = insecure.NewCredentials()
|
|
|
|
}
|
|
|
|
|
|
|
|
go StartServe(server, srvCfg, creds)
|
2022-10-22 16:38:29 +00:00
|
|
|
timeOutTimer := time.NewTimer(5 * time.Second)
|
|
|
|
WaitingLoop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timeOutTimer.C:
|
|
|
|
return nil, fmt.Errorf("[Server] timeout beginning server")
|
|
|
|
default:
|
2022-12-05 00:25:12 +00:00
|
|
|
if _, err := server.GetPeers(ctx, &sentinelrpc.EmptyMessage{}); err == nil {
|
2022-10-22 16:38:29 +00:00
|
|
|
break WaitingLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-11 22:22:47 +00:00
|
|
|
|
2023-05-11 16:38:56 +00:00
|
|
|
conn, err := grpc.DialContext(ctx,
|
|
|
|
srvCfg.Addr,
|
|
|
|
grpc.WithTransportCredentials(creds),
|
|
|
|
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMessageSize)),
|
|
|
|
)
|
2022-10-09 11:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-28 23:00:40 +00:00
|
|
|
return sentinelrpc.NewSentinelClient(conn), nil
|
2022-10-09 11:41:15 +00:00
|
|
|
}
|
|
|
|
|
2022-11-11 22:22:47 +00:00
|
|
|
func StartServe(server *SentinelServer, srvCfg *ServerConfig, creds credentials.TransportCredentials) {
|
2022-10-09 11:41:15 +00:00
|
|
|
lis, err := net.Listen(srvCfg.Network, srvCfg.Addr)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("[Sentinel] could not serve service", "reason", err)
|
|
|
|
}
|
|
|
|
// Create a gRPC server
|
2022-11-11 22:22:47 +00:00
|
|
|
gRPCserver := grpc.NewServer(grpc.Creds(creds))
|
2022-10-09 11:41:15 +00:00
|
|
|
go server.ListenToGossip()
|
|
|
|
// Regiser our server as a gRPC server
|
2022-11-28 23:00:40 +00:00
|
|
|
sentinelrpc.RegisterSentinelServer(gRPCserver, server)
|
2022-10-09 11:41:15 +00:00
|
|
|
if err := gRPCserver.Serve(lis); err != nil {
|
|
|
|
log.Warn("[Sentinel] could not serve service", "reason", err)
|
|
|
|
}
|
|
|
|
}
|