2022-10-09 11:41:15 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
|
2023-10-21 23:17:18 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cl/sentinel"
|
|
|
|
|
2023-08-11 23:54:45 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/direct"
|
2022-11-28 23:00:40 +00:00
|
|
|
sentinelrpc "github.com/ledgerwatch/erigon-lib/gointerfaces/sentinel"
|
2023-12-23 13:32:00 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
2022-12-05 00:25:12 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cl/cltypes"
|
2023-09-17 11:15:06 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cl/persistence"
|
2022-10-09 11:41:15 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
|
|
|
"google.golang.org/grpc"
|
2022-11-11 22:22:47 +00:00
|
|
|
"google.golang.org/grpc/credentials"
|
2022-10-09 11:41:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ServerConfig struct {
|
|
|
|
Network string
|
|
|
|
Addr string
|
|
|
|
}
|
|
|
|
|
2023-12-23 13:32:00 +00:00
|
|
|
func createSentinel(cfg *sentinel.SentinelConfig, db persistence.RawBeaconBlockChain, indiciesDB kv.RwDB, logger log.Logger) (*sentinel.Sentinel, error) {
|
|
|
|
sent, err := sentinel.New(context.Background(), cfg, db, indiciesDB, 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,
|
2024-01-08 16:13:25 +00:00
|
|
|
sentinel.BeaconAggregateAndProofSsz,
|
2023-09-29 21:42:07 +00:00
|
|
|
sentinel.VoluntaryExitSsz,
|
|
|
|
sentinel.ProposerSlashingSsz,
|
|
|
|
sentinel.AttesterSlashingSsz,
|
2023-10-01 15:16:55 +00:00
|
|
|
sentinel.BlsToExecutionChangeSsz,
|
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-12-23 13:32:00 +00:00
|
|
|
func StartSentinelService(cfg *sentinel.SentinelConfig, db persistence.RawBeaconBlockChain, indiciesDB kv.RwDB, 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-12-23 13:32:00 +00:00
|
|
|
sent, err := createSentinel(cfg, db, indiciesDB, logger)
|
2023-04-26 13:33:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-11 23:54:45 +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)
|
2023-08-16 02:32:40 +00:00
|
|
|
go StartServe(server, srvCfg, creds)
|
2022-10-09 11:41:15 +00:00
|
|
|
|
2023-08-11 23:54:45 +00:00
|
|
|
return direct.NewSentinelClientDirect(server), 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)
|
|
|
|
}
|
|
|
|
}
|