erigon-pulse/cmd/lightclient/sentinel/service/service.go
Mike Neuder 4b5880e8d3
Adding VoluntaryExit, ProposerSlashing, and AttesterSlashing topics to pubsub service (#5851)
This is a follow on PR in support of
https://github.com/ledgerwatch/erigon/issues/5824. See
https://github.com/ledgerwatch/erigon/pull/5841 for the
`beacon_aggregate_and_proof` topic.

This PR adds support for the `voluntary_exit, proposer_slashing, and
attester_slashing` topics as defined in the phase 0 spec
https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/p2p-interface.md#global-topics.

The CL types were already defined so this just adds the listening to the
pub sub service.
2022-10-24 17:52:49 +02:00

113 lines
3.3 KiB
Go

package service
import (
"context"
ssz "github.com/ferranbt/fastssz"
"github.com/ledgerwatch/erigon/cmd/lightclient/cltypes"
"github.com/ledgerwatch/erigon/cmd/lightclient/rpc/lightrpc"
"github.com/ledgerwatch/erigon/cmd/lightclient/sentinel"
"github.com/ledgerwatch/erigon/cmd/lightclient/sentinel/communication"
"github.com/ledgerwatch/log/v3"
)
type SentinelServer struct {
lightrpc.UnimplementedSentinelServer
ctx context.Context
sentinel *sentinel.Sentinel
gossipNotifier *gossipNotifier
}
func NewSentinelServer(ctx context.Context, sentinel *sentinel.Sentinel) *SentinelServer {
return &SentinelServer{
sentinel: sentinel,
ctx: ctx,
gossipNotifier: newGossipNotifier(),
}
}
func (s *SentinelServer) SubscribeGossip(_ *lightrpc.EmptyRequest, stream lightrpc.Sentinel_SubscribeGossipServer) error {
// first of all subscribe
ch, subId, err := s.gossipNotifier.addSubscriber()
if err != nil {
return err
}
defer s.gossipNotifier.removeSubscriber(subId)
for {
select {
// Exit on stream context done
case <-stream.Context().Done():
return nil
case packet := <-ch:
if err := stream.Send(&lightrpc.GossipData{
Data: packet.data,
Type: packet.t,
}); err != nil {
log.Warn("Could not relay gossip packet", "reason", err)
}
}
}
}
func (s *SentinelServer) SendRequest(_ context.Context, req *lightrpc.RequestData) (*lightrpc.ResponseData, error) {
// Send the request and get the data if we get an answer.
respData, foundErrReq, err := s.sentinel.SendRequestRaw(req.Data, req.Topic)
return &lightrpc.ResponseData{
Data: respData,
Error: foundErrReq,
}, err
}
func (s *SentinelServer) GetPeers(_ context.Context, _ *lightrpc.EmptyRequest) (*lightrpc.PeerCount, error) {
// Send the request and get the data if we get an answer.
return &lightrpc.PeerCount{
Amount: uint64(s.sentinel.GetPeersCount()),
}, nil
}
func (s *SentinelServer) ListenToGossip() {
for {
select {
case pkt := <-s.sentinel.RecvGossip():
s.handleGossipPacket(pkt)
case <-s.ctx.Done():
return
}
}
}
func (s *SentinelServer) handleGossipPacket(pkt *communication.GossipContext) error {
log.Trace("[Gossip] Received Packet", "topic", pkt.Topic)
err := pkt.Codec.WritePacket(context.TODO(), pkt.Packet)
if err != nil {
log.Warn("[Gossip] Error Forwarding Packet", "err", err)
}
// Compute data
u := pkt.Packet.(ssz.Marshaler)
var data []byte
// Make data
if data, err = u.MarshalSSZ(); err != nil {
return err
}
switch pkt.Packet.(type) {
case *cltypes.SignedBeaconBlockBellatrix:
s.gossipNotifier.notify(lightrpc.GossipType_BeaconBlockGossipType, data)
case *cltypes.SignedAggregateAndProof:
s.gossipNotifier.notify(lightrpc.GossipType_AggregateAndProofGossipType, data)
case *cltypes.SignedVoluntaryExit:
s.gossipNotifier.notify(lightrpc.GossipType_VoluntaryExitGossipType, data)
case *cltypes.ProposerSlashing:
s.gossipNotifier.notify(lightrpc.GossipType_ProposerSlashingGossipType, data)
case *cltypes.AttesterSlashing:
s.gossipNotifier.notify(lightrpc.GossipType_AttesterSlashingGossipType, data)
case *cltypes.LightClientFinalityUpdate:
s.gossipNotifier.notify(lightrpc.GossipType_LightClientFinalityUpdateGossipType, data)
case *cltypes.LightClientOptimisticUpdate:
s.gossipNotifier.notify(lightrpc.GossipType_LightClientOptimisticUpdateGossipType, data)
default:
}
return nil
}