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
5 changes: 4 additions & 1 deletion cmd/lightclient/clparams/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package clparams

import (
"fmt"
"math"
"time"

Expand All @@ -24,7 +25,7 @@ import (
type NetworkType int

const (
MainnetNetwork NetworkType = 0
MainnetNetwork NetworkType = 1
GoerliNetwork NetworkType = 5
SepoliaNetwork NetworkType = 11155111
)
Expand Down Expand Up @@ -649,8 +650,10 @@ var BeaconConfigs map[NetworkType]BeaconChainConfig = map[NetworkType]BeaconChai
}

func GetConfigsByNetwork(net NetworkType) (*GenesisConfig, *NetworkConfig, *BeaconChainConfig) {
fmt.Println(net)
networkConfig := NetworkConfigs[net]
genesisConfig := GenesisConfigs[net]
fmt.Println(genesisConfig)
beaconConfig := BeaconConfigs[net]
return &genesisConfig, &networkConfig, &beaconConfig
}
8 changes: 0 additions & 8 deletions cmd/lightclient/sentinel/communication/p2p/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,6 @@ func (typ *ForkData) Clone() communication.Packet {
return &ForkData{}
}

type Goodbye struct {
Reason uint64 `json:"reason" `
}

func (typ *Goodbye) Clone() communication.Packet {
return &Goodbye{}
}

type Ping struct {
Id uint64 `json:"id" `
}
Expand Down
58 changes: 1 addition & 57 deletions cmd/lightclient/sentinel/communication/p2p/generated_encoding.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions cmd/lightclient/sentinel/communication/p2p/spec_p2p.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@ structs:
tags:
json: root
ssz-size: 32
Goodbye:
- name: Reason
type: uint64
tags:
json: reason
Ping:
- name: Id
type: uint64
Expand Down
12 changes: 6 additions & 6 deletions cmd/lightclient/sentinel/communication/ssz_snappy/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ func TestMetadataPacketStream(t *testing.T) {
require.NoError(t, err)
defer h2.Close()

packet := &p2p.Goodbye{
Reason: 2,
packet := &p2p.Ping{
Id: 2,
}

doneCh := make(chan struct{})
h2.SetStreamHandler(protocol.TestingID, func(stream network.Stream) {
p := &p2p.Goodbye{}
p := &p2p.Ping{}
codecA := NewStreamCodec(stream)
_, err := codecA.Decode(p)
require.NoError(t, err)
Expand Down Expand Up @@ -61,13 +61,13 @@ func TestMetadataPacketStream(t *testing.T) {
// See https://github.com/libp2p/go-libp2p-pubsub/issues/426
func TestGossipCodecTest(t *testing.T) {
codec := NewGossipCodec(nil, nil).(*GossipCodec)
val := &p2p.Goodbye{
Reason: 89,
val := &p2p.Ping{
Id: 89,
}
ans, err := utils.EncodeSSZSnappy(val)
require.NoError(t, err)

decoded := &p2p.Goodbye{}
decoded := &p2p.Ping{}
require.NoError(t, codec.decodeData(decoded, ans))
assert.Equal(t, decoded, val)
}
5 changes: 3 additions & 2 deletions cmd/lightclient/sentinel/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ func NewConsensusHandlers(host host.Host, peers *peers.Peers, metadataV1 *lightr
}
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(GoodbyeProtocolV1): curryStreamHandler(ssz_snappy.NewStreamCodec, c.goodbyeHandler),
protocol.ID(MedataProtocolV1): curryStreamHandler(ssz_snappy.NewStreamCodec, c.metadataHandlerV1),
protocol.ID(MetadataProtocolV1): curryStreamHandler(ssz_snappy.NewStreamCodec, nilHandler),
protocol.ID(MetadataProtocolV2): curryStreamHandler(ssz_snappy.NewStreamCodec, nilHandler),
protocol.ID(BeaconBlockByRangeProtocolV1): c.blocksByRangeHandler,
protocol.ID(BeaconBlockByRootProtocolV1): c.beaconBlocksByRootHandler,
}
Expand Down
20 changes: 2 additions & 18 deletions cmd/lightclient/sentinel/handlers/heartbeats.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,6 @@ import (
"github.com/ledgerwatch/log/v3"
)

// These below are the handlers for heartbeat functions

func (c *ConsensusHandlers) goodbyeHandler(ctx *communication.StreamContext, dat *p2p.Goodbye) error {
//log.Info("[Lightclient] Received", "goodbye", dat.Reason)
defer c.peers.DisconnectPeer(ctx.Stream.Conn().RemotePeer())
_, err := ctx.Codec.WritePacket(dat, SuccessfullResponsePrefix)
if err != nil {
return err
}
return nil
}

// type safe handlers which all have access to the original stream & decompressed data
// ping handler
func pingHandler(ctx *communication.StreamContext, dat *p2p.Ping) error {
Expand All @@ -30,12 +18,8 @@ func pingHandler(ctx *communication.StreamContext, dat *p2p.Ping) error {
return nil
}

func (c *ConsensusHandlers) metadataHandlerV1(ctx *communication.StreamContext, dat *communication.EmptyPacket) error {
_, err := ctx.Codec.WritePacket(c.metadataV1, SuccessfullResponsePrefix)
if err != nil {
return err
}

// does nothing
func nilHandler(ctx *communication.StreamContext, dat *communication.EmptyPacket) error {
return nil
}

Expand Down
94 changes: 94 additions & 0 deletions cmd/lightclient/sentinel/handlers/heartbeats_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package handlers

import (
"context"
"testing"
"time"

"github.com/ledgerwatch/erigon/cmd/lightclient/rpc/lightrpc"
"github.com/ledgerwatch/erigon/cmd/lightclient/sentinel/communication/p2p"
"github.com/ledgerwatch/erigon/cmd/lightclient/sentinel/communication/ssz_snappy"
"github.com/ledgerwatch/erigon/cmd/lightclient/sentinel/peers"
"github.com/ledgerwatch/erigon/common"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/protocol"
basichost "github.com/libp2p/go-libp2p/p2p/host/basic"
swarmt "github.com/libp2p/go-libp2p/p2p/net/swarm/testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func initializeNetwork(t *testing.T, ctx context.Context) (*ConsensusHandlers, host.Host, host.Host) {
h1, err := basichost.NewHost(swarmt.GenSwarm(t), nil)
require.NoError(t, err)

h2, err := basichost.NewHost(swarmt.GenSwarm(t), nil)
require.NoError(t, err)
h2pi := h2.Peerstore().PeerInfo(h2.ID())
require.NoError(t, h1.Connect(ctx, h2pi))

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

func TestPingHandler(t *testing.T) {
ctx := context.TODO()

handlers, h1, h2 := initializeNetwork(t, ctx)
defer h1.Close()
defer h2.Close()
handlers.Start()

stream, err := h1.NewStream(ctx, h2.ID(), protocol.ID(PingProtocolV1))
require.NoError(t, err)
packet := &p2p.Ping{
Id: 32,
}
codec := ssz_snappy.NewStreamCodec(stream)
_, err = codec.WritePacket(packet)
require.NoError(t, err)
require.NoError(t, codec.CloseWriter())
time.Sleep(100 * time.Millisecond)
r := &p2p.Ping{}

code := make([]byte, 1)
stream.Read(code)
assert.Equal(t, code, []byte{SuccessfullResponsePrefix})

_, err = codec.Decode(r)
require.NoError(t, err)

assert.Equal(t, r, packet)
}

func TestStatusHandler(t *testing.T) {
ctx := context.TODO()

handlers, h1, h2 := initializeNetwork(t, ctx)
defer h1.Close()
defer h2.Close()
handlers.Start()

stream, err := h1.NewStream(ctx, h2.ID(), protocol.ID(StatusProtocolV1))
require.NoError(t, err)
packet := &p2p.Status{
ForkDigest: common.Hex2Bytes("69696969"),
HeadRoot: make([]byte, 32),
FinalizedRoot: make([]byte, 32),
HeadSlot: 666999,
}
codec := ssz_snappy.NewStreamCodec(stream)
_, err = codec.WritePacket(packet)
require.NoError(t, err)
require.NoError(t, codec.CloseWriter())
time.Sleep(100 * time.Millisecond)
r := &p2p.Status{}

code := make([]byte, 1)
stream.Read(code)
assert.Equal(t, code, []byte{SuccessfullResponsePrefix})

_, err = codec.Decode(r)
require.NoError(t, err)

assert.Equal(t, r, packet)
}
4 changes: 2 additions & 2 deletions cmd/lightclient/sentinel/handlers/topics.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ var (
PingProtocolV1 = ProtocolPrefix + PingTopic + Schema1 + EncodingProtocol
GoodbyeProtocolV1 = ProtocolPrefix + GoodbyeTopic + Schema1 + EncodingProtocol

MedataProtocolV1 = ProtocolPrefix + MetadataTopic + Schema1 + EncodingProtocol
MedataProtocolV2 = ProtocolPrefix + MetadataTopic + Schema2 + EncodingProtocol
MetadataProtocolV1 = ProtocolPrefix + MetadataTopic + Schema1 + EncodingProtocol
MetadataProtocolV2 = ProtocolPrefix + MetadataTopic + Schema2 + EncodingProtocol

StatusProtocolV1 = ProtocolPrefix + StatusTopic + Schema1 + EncodingProtocol

Expand Down
2 changes: 1 addition & 1 deletion cmd/lightclient/sentinel/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (s *Sentinel) SendMetadataReqV1() (communication.Packet, error) {
requestPacket := &lightrpc.MetadataV1{}
responsePacket := &lightrpc.MetadataV1{}

return sendRequest(s, requestPacket, responsePacket, handlers.MedataProtocolV1)
return sendRequest(s, requestPacket, responsePacket, handlers.MetadataProtocolV1)
}

// TODO: add the rest of the request topics
Expand Down