erigon-pulse/cmd/lightclient/sentinel/handlers/handlers.go
Enrique Jose Avila Asapche f2d95d16cc
Adding light client requesting (#5580)
* separated the encoding

* picking random peer node

* sending ping request

* updated enconding and reading

* requesting ping interval and more verbose vars

* disconnecting from unresponsive peers

* penalizing instead of disconnecting for irresponsiveness

* closing stream for streamCodec

* solved meged issues

* changed back

* separated const values

* requesting ping interval to 1 sec

* added closing of read and write stream && receiving responses!

* fixecd typo

* general sending request function

* added constants of resqresp topics

* fixed uncorrect name

* refactored sending requests

* added todo

* little detail

* moved to main

* no need to sleep

* sending request retries until timeout

* type

* lint
2022-10-03 11:05:59 +02:00

63 lines
2.2 KiB
Go

/*
Copyright 2022 Erigon-Lightclient contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package handlers
import (
"github.com/ledgerwatch/erigon/cmd/lightclient/sentinel/peers"
"github.com/ledgerwatch/erigon/cmd/lightclient/sentinel/proto/ssz_snappy"
"github.com/ledgerwatch/log/v3"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/protocol"
)
type ConsensusHandlers struct {
handlers map[protocol.ID]network.StreamHandler
host host.Host
peers *peers.Peers
}
func NewConsensusHandlers(host host.Host, peers *peers.Peers) *ConsensusHandlers {
c := &ConsensusHandlers{
peers: peers,
host: host,
}
c.handlers = map[protocol.ID]network.StreamHandler{
protocol.ID(PingProtocolV1): 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, metadataHandler),
protocol.ID(BeaconBlockByRangeProtocolV1): c.blocksByRangeHandler,
protocol.ID(BeaconBlockByRootProtocolV1): c.beaconBlocksByRootHandler,
}
return c
}
func (c *ConsensusHandlers) blocksByRangeHandler(stream network.Stream) {
defer stream.Close()
log.Info("Got block by range handler call")
}
func (c *ConsensusHandlers) beaconBlocksByRootHandler(stream network.Stream) {
defer stream.Close()
log.Info("Got beacon block by root handler call")
}
func (c *ConsensusHandlers) Start() {
for id, handler := range c.handlers {
c.host.SetStreamHandler(id, handler)
}
}