Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/lightclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func main() {
}

logInterval := time.NewTicker(5 * time.Second)
sendReqInterval := time.NewTicker(1 * time.Second)
sendReqInterval := time.NewTicker(2 * time.Second)

for {
select {
Expand Down
6 changes: 0 additions & 6 deletions cmd/lightclient/sentinel/communication/ssz_snappy/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ import (
"errors"
"fmt"
"io"
"reflect"

ssz "github.com/ferranbt/fastssz"
"github.com/golang/snappy"
"github.com/ledgerwatch/erigon/cmd/lightclient/rpc/lightrpc"
"github.com/ledgerwatch/erigon/cmd/lightclient/sentinel/communication"
"github.com/libp2p/go-libp2p/core/network"
)
Expand Down Expand Up @@ -66,10 +64,6 @@ func (d *StreamCodec) CloseReader() error {
// write packet to stream. will add correct header + compression
// will error if packet does not implement ssz.Marshaler interface
func (d *StreamCodec) WritePacket(pkt communication.Packet, prefix ...byte) (n int, err error) {
// if its a metadata request we dont write anything
if reflect.TypeOf(pkt) == reflect.TypeOf(&lightrpc.MetadataV1{}) || reflect.TypeOf(pkt) == reflect.TypeOf(&lightrpc.MetadataV2{}) {
return 0, nil
}
val, ok := pkt.(ssz.Marshaler)
if !ok {
return 0, nil
Expand Down
25 changes: 15 additions & 10 deletions cmd/lightclient/sentinel/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,31 @@ import (
)

type ConsensusHandlers struct {
handlers map[protocol.ID]network.StreamHandler
host host.Host
peers *peers.Peers
metadataV1 *lightrpc.MetadataV1
handlers map[protocol.ID]network.StreamHandler
host host.Host
peers *peers.Peers
metadata *lightrpc.MetadataV2
}

const SuccessfullResponsePrefix = 0x00

func NewConsensusHandlers(host host.Host, peers *peers.Peers, metadataV1 *lightrpc.MetadataV1) *ConsensusHandlers {
var NoRequestHandlers = map[string]bool{
MetadataProtocolV1: true,
MetadataProtocolV2: true,
}

func NewConsensusHandlers(host host.Host, peers *peers.Peers, metadata *lightrpc.MetadataV2) *ConsensusHandlers {
c := &ConsensusHandlers{
peers: peers,
host: host,
metadataV1: metadataV1,
peers: peers,
host: host,
metadata: metadata,
}
c.handlers = map[protocol.ID]network.StreamHandler{
protocol.ID(PingProtocolV1): curryStreamHandler(ssz_snappy.NewStreamCodec, pingHandler),
protocol.ID(GoodbyeProtocolV1): curryStreamHandler(ssz_snappy.NewStreamCodec, pingHandler),
protocol.ID(StatusProtocolV1): curryStreamHandler(ssz_snappy.NewStreamCodec, statusHandler),
protocol.ID(MetadataProtocolV1): curryStreamHandler(ssz_snappy.NewStreamCodec, nilHandler),
protocol.ID(MetadataProtocolV2): curryStreamHandler(ssz_snappy.NewStreamCodec, nilHandler),
protocol.ID(MetadataProtocolV1): curryStreamHandler(ssz_snappy.NewStreamCodec, c.metadataV1Handler),
protocol.ID(MetadataProtocolV2): curryStreamHandler(ssz_snappy.NewStreamCodec, c.metadataV2Handler),
protocol.ID(BeaconBlockByRangeProtocolV1): c.blocksByRangeHandler,
protocol.ID(BeaconBlockByRootProtocolV1): c.beaconBlocksByRootHandler,
}
Expand Down
16 changes: 16 additions & 0 deletions cmd/lightclient/sentinel/handlers/heartbeats.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package handlers

import (
"github.com/ledgerwatch/erigon/cmd/lightclient/rpc/lightrpc"
"github.com/ledgerwatch/erigon/cmd/lightclient/sentinel/communication"
"github.com/ledgerwatch/erigon/cmd/lightclient/sentinel/communication/p2p"
"github.com/ledgerwatch/erigon/cmd/lightclient/utils"
Expand All @@ -31,6 +32,21 @@ func pingHandler(ctx *communication.StreamContext, dat *p2p.Ping) error {
return nil
}

func (c *ConsensusHandlers) metadataV1Handler(ctx *communication.StreamContext, _ *communication.EmptyPacket) error {
// since packets are just structs, they can be resent with no issue
_, err := ctx.Codec.WritePacket(&lightrpc.MetadataV1{
SeqNumber: c.metadata.SeqNumber,
Attnets: c.metadata.Attnets,
}, SuccessfullResponsePrefix)
return err
}

func (c *ConsensusHandlers) metadataV2Handler(ctx *communication.StreamContext, _ *communication.EmptyPacket) error {
// since packets are just structs, they can be resent with no issue
_, err := ctx.Codec.WritePacket(c.metadata, SuccessfullResponsePrefix)
return err
}

// does nothing
func nilHandler(ctx *communication.StreamContext, dat *communication.EmptyPacket) error {
return nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/lightclient/sentinel/handlers/heartbeats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func initializeNetwork(t *testing.T, ctx context.Context) (*ConsensusHandlers, h
h2pi := h2.Peerstore().PeerInfo(h2.ID())
require.NoError(t, h1.Connect(ctx, h2pi))

return NewConsensusHandlers(h2, &peers.Peers{}, &lightrpc.MetadataV1{}), h1, h2
return NewConsensusHandlers(h2, &peers.Peers{}, &lightrpc.MetadataV2{}), h1, h2
}

func TestPingHandler(t *testing.T) {
Expand Down
7 changes: 4 additions & 3 deletions cmd/lightclient/sentinel/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ func writeRequest(s *Sentinel, requestPacket communication.Packet, peerId peer.I
}

sc := ssz_snappy.NewStreamCodec(stream)

if _, err := sc.WritePacket(requestPacket); err != nil {
return nil, fmt.Errorf("failed to write packet type=%s, err=%s", reflect.TypeOf(requestPacket), err)
if _, ok := handlers.NoRequestHandlers[topic]; !ok {
if _, err := sc.WritePacket(requestPacket); err != nil {
return nil, fmt.Errorf("failed to write packet type=%s, err=%s", reflect.TypeOf(requestPacket), err)
}
}

if err := sc.CloseWriter(); err != nil {
Expand Down
5 changes: 3 additions & 2 deletions cmd/lightclient/sentinel/sentinel.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type Sentinel struct {
host host.Host
cfg *SentinelConfig
peers *peers.Peers
metadataV1 *lightrpc.MetadataV1
metadataV1 *lightrpc.MetadataV2

discoverConfig discover.Config
pubsub *pubsub.PubSub
Expand Down Expand Up @@ -121,9 +121,10 @@ func (s *Sentinel) createListener() (*discover.UDPv5, error) {
}

// TODO: Set up proper attestation number
s.metadataV1 = &lightrpc.MetadataV1{
s.metadataV1 = &lightrpc.MetadataV2{
SeqNumber: localNode.Seq(),
Attnets: 0,
Syncnets: 0,
}

// Start stream handlers
Expand Down