mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2025-01-07 11:32:20 +00:00
f2d95d16cc
* 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
89 lines
2.0 KiB
Go
89 lines
2.0 KiB
Go
package proto
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/libp2p/go-libp2p-core/protocol"
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
|
"github.com/libp2p/go-libp2p/core/network"
|
|
)
|
|
|
|
// packet simply needs to implement clone so that it can be instantiated within the generic
|
|
type Packet interface {
|
|
Clone() Packet
|
|
}
|
|
|
|
// context includes the original stream, the raw decompressed bytes, the codec the context was generated from, and the protocol ID
|
|
type StreamContext struct {
|
|
Packet Packet
|
|
Protocol protocol.ID
|
|
Codec StreamCodec
|
|
Stream network.Stream
|
|
|
|
Raw []byte
|
|
}
|
|
|
|
func (c *StreamContext) String() string {
|
|
return fmt.Sprintf("peer %s | packet %s | len %d", c.Stream.ID(), c.Protocol, c.Packet)
|
|
}
|
|
|
|
type SubContext struct {
|
|
// the packet
|
|
Packet Packet
|
|
// the topic of the message
|
|
Topic string
|
|
// the actual message
|
|
Msg *pubsub.Message
|
|
|
|
// the codec used to decode the message
|
|
Codec SubCodec
|
|
|
|
// the decompressed message in the native encoding of msg
|
|
Raw []byte
|
|
}
|
|
|
|
// PacketCodec describes a wire format.
|
|
type StreamCodec interface {
|
|
Close() error
|
|
CloseWriter() error
|
|
CloseReader() error
|
|
|
|
Write(payload []byte) (n int, err error)
|
|
WritePacket(pck Packet) (n int, err error)
|
|
Decode(Packet) (ctx *StreamContext, err error)
|
|
|
|
Read(payload []byte) (n int, err error)
|
|
ReadByte() (b byte, err error)
|
|
}
|
|
|
|
// SubCodec describes a wire format for pubsub messages
|
|
type SubCodec interface {
|
|
Decode(context.Context, Packet) (*SubContext, error)
|
|
}
|
|
|
|
func (c *SubContext) String() string {
|
|
return fmt.Sprintf("peer %s | topic %s | len %d", c.Msg.ReceivedFrom, c.Topic, c.Packet)
|
|
}
|
|
|
|
// the empty packet doesn't implement any serialization, so it means to skip.
|
|
type EmptyPacket struct{}
|
|
|
|
func (e *EmptyPacket) Clone() Packet {
|
|
return &EmptyPacket{}
|
|
}
|
|
|
|
// the error message skips decoding but does do the decompression.
|
|
type ErrorMessage struct {
|
|
Message []byte `json:"message"`
|
|
}
|
|
|
|
func (typ *ErrorMessage) Clone() Packet {
|
|
return &ErrorMessage{}
|
|
}
|
|
|
|
func (typ *ErrorMessage) UnmarshalSSZ(buf []byte) error {
|
|
typ.Message = buf
|
|
return nil
|
|
}
|