erigon-pulse/cmd/lightclient/sentinel/proto/ssz_snappy/sub.go
a 9f6fd488d2
[cl lightclient] - packet decoding & ssz code generation (#5558)
* added subscription to gossip

* added all handlers with logs

* disconnecting from peers with goodbye message received

* added time out for stream

* wip

* Remove unused

* remove extra structs

* status handler

* minor clean up

* add structs for altair light client

* begin writing out altair light client sync protocol to figure out what other structs are needed

* remove sszgen

* cleanup pt 1

* cleanup pt 2

* remove go 1.19 function

* less ambigious variable name

* run go fmt

* rename snappy_ssz to ssz_snappy to better align with wire name

* move more structs over

* poof set deadline

Co-authored-by: Enrique Jose Avila Asapche <eavilaasapche@gmail.com>
Co-authored-by: a <a@a.a>
2022-09-29 19:07:57 +02:00

70 lines
1.4 KiB
Go

package ssz_snappy
import (
"bytes"
"context"
"fmt"
"io"
"sync"
ssz "github.com/ferranbt/fastssz"
"github.com/golang/snappy"
"github.com/ledgerwatch/erigon/cmd/lightclient/sentinel/proto"
pubsub "github.com/libp2p/go-libp2p-pubsub"
)
type SubCodec struct {
sub *pubsub.Subscription
mu sync.Mutex
}
func NewSubCodec(
sub *pubsub.Subscription,
) proto.SubCodec {
return &SubCodec{
sub: sub,
}
}
// decode into packet p, then return the packet context
func (d *SubCodec) Decode(ctx context.Context, p proto.Packet) (sctx *proto.SubContext, err error) {
sctx, err = d.readPacket(ctx, p)
return
}
func (d *SubCodec) readPacket(ctx context.Context, p proto.Packet) (*proto.SubContext, error) {
c := &proto.SubContext{
Packet: p,
Codec: d,
}
// read the next message
msg, err := d.sub.Next(ctx)
if err != nil {
return c, err
}
c.Topic = msg.GetTopic()
c.Msg = msg
if p != nil {
s := bytes.NewBuffer(msg.Data)
sr := snappy.NewReader(s)
ln, _, err := proto.ReadUvarint(s)
if err != nil {
return c, err
}
//TODO: we should probably take this from a buffer pool.
c.Raw = make([]byte, ln)
_, err = io.ReadFull(sr, c.Raw)
if err != nil {
return c, fmt.Errorf("readPacket: %w", err)
}
if val, ok := p.(ssz.Unmarshaler); ok {
err = val.UnmarshalSSZ(c.Raw)
if err != nil {
return c, fmt.Errorf("readPacket: %w", err)
}
}
}
return c, nil
}