erigon-pulse/cmd/lightclient/sentinel/proto/ssz_snappy/utils.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

43 lines
1.0 KiB
Go

package ssz_snappy
import (
"bufio"
"encoding/binary"
"fmt"
"reflect"
ssz "github.com/ferranbt/fastssz"
"github.com/golang/snappy"
"github.com/ledgerwatch/erigon/cmd/lightclient/clparams"
"github.com/ledgerwatch/erigon/cmd/lightclient/sentinel/proto"
"github.com/libp2p/go-libp2p/core/network"
)
func EncodePacket(pkt proto.Packet, stream network.Stream) ([]byte, *snappy.Writer, error) {
if val, ok := pkt.(ssz.Marshaler); ok {
wr := bufio.NewWriter(stream)
sw := snappy.NewWriter(wr)
p := make([]byte, 10)
vin := binary.PutVarint(p, int64(val.SizeSSZ()))
enc, err := val.MarshalSSZ()
if err != nil {
return nil, nil, fmt.Errorf("marshal ssz: %w", err)
}
if len(enc) > int(clparams.MaxChunkSize) {
return nil, nil, fmt.Errorf("chunk size too big")
}
_, err = wr.Write(p[:vin])
if err != nil {
return nil, nil, fmt.Errorf("write varint: %w", err)
}
return enc, sw, nil
}
return nil, nil, fmt.Errorf("packet %s does not implement ssz.Marshaler", reflect.TypeOf(pkt))
}