encoding

package
v0.4.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 25, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// IDLen PeerInfo ID length.
	IDLen = 16

	// NonceLen PoW-Nonce length.
	NonceLen = 4

	// PingMsg wire Ping message id.
	PingMsg = 0

	// PongMsg wire Pong message id.
	PongMsg = 1

	// FindNodesMsg wire FindNodes message id.
	FindNodesMsg = 2

	// NodesMsg wire Nodes message id.
	NodesMsg = 3

	// BroadcastMsg Message propagation type.
	BroadcastMsg = 10
)
View Source
const PeerBytesSize int = 22

PeerBytesSize represents the amount of bytes necessary to represent .

Variables

This section is empty.

Functions

func ComputeNonce

func ComputeNonce(id []byte) uint32

ComputeNonce receives the user's `Peer` ID and computes the ID nonce in order to be able to join the network.

This operation is basically a PoW algorithm that ensures that Sybil attacks are more costly.

func MarshalBinary

func MarshalBinary(header Header, payload BinaryMarshaler, buf *bytes.Buffer) error

MarshalBinary marshals message into binary buffer.

func MsgTypeToString

func MsgTypeToString(t byte) (string, error)

MsgTypeToString translates wire message into string name.

Types

type BinaryMarshaler

type BinaryMarshaler interface {
	// Marshal payload to binary.
	MarshalBinary(buf *bytes.Buffer) error

	// Unmarshal payload from binary.
	UnmarshalBinary(buf *bytes.Buffer) error
}

BinaryMarshaler interface for marshal/unmarshal wire unit.

type BroadcastPayload

type BroadcastPayload struct {
	Height      byte
	GossipFrame []byte
}

BroadcastPayload payload data of BROADCAST message.

func (*BroadcastPayload) MarshalBinary

func (payload *BroadcastPayload) MarshalBinary(buf *bytes.Buffer) error

MarshalBinary implements BinaryMarshaler.

func (*BroadcastPayload) UnmarshalBinary

func (payload *BroadcastPayload) UnmarshalBinary(buf *bytes.Buffer) error

UnmarshalBinary implements BinaryMarshaler.

type Header struct {
	MsgType byte

	// Remote peer details.
	RemotePeerID    [IDLen]byte
	RemotePeerNonce uint32
	RemotePeerPort  uint16

	// Reserved bytes for future use.
	Reserved [2]byte
}

Header represents the header part of kadcast wire messages. Both TCP and UDP are sharing same header structure.

func (*Header) MarshalBinary

func (h *Header) MarshalBinary(buf *bytes.Buffer) error

MarshalBinary marshal wire header into bytes buffer, if valid.

func (*Header) UnmarshalBinary

func (h *Header) UnmarshalBinary(buf *bytes.Buffer) error

UnmarshalBinary umarshal wire header from bytes buffer, if valid It ensures ID-nonce pair is correct.

type NodesPayload

type NodesPayload struct {
	Peers []PeerInfo
}

NodesPayload payload data of NODES message.

func (*NodesPayload) MarshalBinary

func (payload *NodesPayload) MarshalBinary(buf *bytes.Buffer) error

MarshalBinary implements BinaryMarshaler.

func (*NodesPayload) UnmarshalBinary

func (payload *NodesPayload) UnmarshalBinary(buf *bytes.Buffer) error

UnmarshalBinary implements BinaryMarshaler.

type PeerInfo

type PeerInfo struct {
	IP   [4]byte
	Port uint16

	ID [16]byte
}

PeerInfo stores peer addr and ID. A slice of PeerInfo is wired on NODES message.

func MakePeer

func MakePeer(ip [4]byte, port uint16) PeerInfo

MakePeer builds a peer tuple by computing ID over IP and port.

func MakePeerFromAddr

func MakePeerFromAddr(addr string) (PeerInfo, error)

MakePeerFromAddr is same as MakePeer but resolve addr with ResolveTCPAddr.

func MakePeerFromIP

func MakePeerFromIP(ipaddr string, port uint16) (PeerInfo, error)

MakePeerFromIP from ipaddr (resolvable by ResolveTCPAddr) and port.

func (PeerInfo) Address

func (peer PeerInfo) Address() string

Address returns peer address as a string.

func (PeerInfo) GetUDPAddr

func (peer PeerInfo) GetUDPAddr() net.UDPAddr

GetUDPAddr make net.UDPAddr from PeerInfo IP:port.

func (PeerInfo) IsEqual

func (peer PeerInfo) IsEqual(p PeerInfo) bool

IsEqual returns true if two peers tuples are identical.

func (*PeerInfo) MarshalBinary

func (peer *PeerInfo) MarshalBinary(buf *bytes.Buffer) error

MarshalBinary marshal peer tuple into binary buffer.

func (PeerInfo) String

func (peer PeerInfo) String() string

String returns peerinfo address and ID as a string.

func (*PeerInfo) UnmarshalBinary

func (peer *PeerInfo) UnmarshalBinary(buf *bytes.Buffer) error

UnmarshalBinary build peer tuple from binary buffer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL