mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2025-01-09 12:31:21 +00:00
9f6fd488d2
* 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>
70 lines
1.4 KiB
Go
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
|
|
}
|