fixed no handlers responses (MetadataV1/MetadataV2) (#5679)

Co-authored-by: giuliorebuffo <giuliorebuffo@system76-pc.localdomain>
This commit is contained in:
Giulio rebuffo 2022-10-09 20:28:49 +02:00 committed by GitHub
parent b8d7219d9d
commit 6193d02543
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 40 additions and 23 deletions

View File

@ -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 {

View File

@ -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"
)
@ -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

View File

@ -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,
}

View File

@ -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"
@ -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

View File

@ -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) {

View File

@ -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 {

View File

@ -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
@ -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