diff --git a/cmd/lightclient/clparams/config.go b/cmd/lightclient/clparams/config.go index 3e379e8cac9..971268eb380 100644 --- a/cmd/lightclient/clparams/config.go +++ b/cmd/lightclient/clparams/config.go @@ -14,6 +14,7 @@ package clparams import ( + "fmt" "math" "time" @@ -24,7 +25,7 @@ import ( type NetworkType int const ( - MainnetNetwork NetworkType = 0 + MainnetNetwork NetworkType = 1 GoerliNetwork NetworkType = 5 SepoliaNetwork NetworkType = 11155111 ) @@ -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 } diff --git a/cmd/lightclient/sentinel/communication/p2p/generated.go b/cmd/lightclient/sentinel/communication/p2p/generated.go index 7825441b6f6..8693747c7b3 100644 --- a/cmd/lightclient/sentinel/communication/p2p/generated.go +++ b/cmd/lightclient/sentinel/communication/p2p/generated.go @@ -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" ` } diff --git a/cmd/lightclient/sentinel/communication/p2p/generated_encoding.go b/cmd/lightclient/sentinel/communication/p2p/generated_encoding.go index 686207737d5..ff2c0ee0d4d 100644 --- a/cmd/lightclient/sentinel/communication/p2p/generated_encoding.go +++ b/cmd/lightclient/sentinel/communication/p2p/generated_encoding.go @@ -1,5 +1,5 @@ // Code generated by fastssz. DO NOT EDIT. -// Hash: bbae36f2bb454c91114281828520ec1084185bed49e94b288f3ca6ad815c9f6e +// Hash: 6c24bfc6e641d48afee20ff8a60ced263fd1394cb922659d4260b97310525fe8 // Version: 0.1.2 package p2p @@ -233,62 +233,6 @@ func (f *ForkData) GetTree() (*ssz.Node, error) { return ssz.ProofTree(f) } -// MarshalSSZ ssz marshals the Goodbye object -func (g *Goodbye) MarshalSSZ() ([]byte, error) { - return ssz.MarshalSSZ(g) -} - -// MarshalSSZTo ssz marshals the Goodbye object to a target array -func (g *Goodbye) MarshalSSZTo(buf []byte) (dst []byte, err error) { - dst = buf - - // Field (0) 'Reason' - dst = ssz.MarshalUint64(dst, g.Reason) - - return -} - -// UnmarshalSSZ ssz unmarshals the Goodbye object -func (g *Goodbye) UnmarshalSSZ(buf []byte) error { - var err error - size := uint64(len(buf)) - if size != 8 { - return ssz.ErrSize - } - - // Field (0) 'Reason' - g.Reason = ssz.UnmarshallUint64(buf[0:8]) - - return err -} - -// SizeSSZ returns the ssz encoded size in bytes for the Goodbye object -func (g *Goodbye) SizeSSZ() (size int) { - size = 8 - return -} - -// HashTreeRoot ssz hashes the Goodbye object -func (g *Goodbye) HashTreeRoot() ([32]byte, error) { - return ssz.HashWithDefaultHasher(g) -} - -// HashTreeRootWith ssz hashes the Goodbye object with a hasher -func (g *Goodbye) HashTreeRootWith(hh ssz.HashWalker) (err error) { - indx := hh.Index() - - // Field (0) 'Reason' - hh.PutUint64(g.Reason) - - hh.Merkleize(indx) - return -} - -// GetTree ssz hashes the Goodbye object -func (g *Goodbye) GetTree() (*ssz.Node, error) { - return ssz.ProofTree(g) -} - // MarshalSSZ ssz marshals the Ping object func (p *Ping) MarshalSSZ() ([]byte, error) { return ssz.MarshalSSZ(p) diff --git a/cmd/lightclient/sentinel/communication/p2p/spec_p2p.yaml b/cmd/lightclient/sentinel/communication/p2p/spec_p2p.yaml index 3363de26fe4..ac679ccbb78 100644 --- a/cmd/lightclient/sentinel/communication/p2p/spec_p2p.yaml +++ b/cmd/lightclient/sentinel/communication/p2p/spec_p2p.yaml @@ -63,11 +63,6 @@ structs: tags: json: root ssz-size: 32 - Goodbye: - - name: Reason - type: uint64 - tags: - json: reason Ping: - name: Id type: uint64 diff --git a/cmd/lightclient/sentinel/communication/ssz_snappy/stream_test.go b/cmd/lightclient/sentinel/communication/ssz_snappy/stream_test.go index 512210b05c3..2a36e4ca822 100644 --- a/cmd/lightclient/sentinel/communication/ssz_snappy/stream_test.go +++ b/cmd/lightclient/sentinel/communication/ssz_snappy/stream_test.go @@ -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) @@ -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) } diff --git a/cmd/lightclient/sentinel/handlers/streamHandler.go b/cmd/lightclient/sentinel/handlers/curry.go similarity index 100% rename from cmd/lightclient/sentinel/handlers/streamHandler.go rename to cmd/lightclient/sentinel/handlers/curry.go diff --git a/cmd/lightclient/sentinel/handlers/handlers.go b/cmd/lightclient/sentinel/handlers/handlers.go index 819438cbe7a..7617586c264 100644 --- a/cmd/lightclient/sentinel/handlers/handlers.go +++ b/cmd/lightclient/sentinel/handlers/handlers.go @@ -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, } diff --git a/cmd/lightclient/sentinel/handlers/heartbeats.go b/cmd/lightclient/sentinel/handlers/heartbeats.go index f6a3a58a770..bf7ed0876ae 100644 --- a/cmd/lightclient/sentinel/handlers/heartbeats.go +++ b/cmd/lightclient/sentinel/handlers/heartbeats.go @@ -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 { @@ -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 } diff --git a/cmd/lightclient/sentinel/handlers/heartbeats_test.go b/cmd/lightclient/sentinel/handlers/heartbeats_test.go new file mode 100644 index 00000000000..b4efebe930d --- /dev/null +++ b/cmd/lightclient/sentinel/handlers/heartbeats_test.go @@ -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) +} diff --git a/cmd/lightclient/sentinel/handlers/topics.go b/cmd/lightclient/sentinel/handlers/topics.go index 0565126774b..b8351e29765 100644 --- a/cmd/lightclient/sentinel/handlers/topics.go +++ b/cmd/lightclient/sentinel/handlers/topics.go @@ -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 diff --git a/cmd/lightclient/sentinel/request.go b/cmd/lightclient/sentinel/request.go index 143bb7b98ca..59a18ed8cbd 100644 --- a/cmd/lightclient/sentinel/request.go +++ b/cmd/lightclient/sentinel/request.go @@ -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