p2p

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: May 20, 2020 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultPeerRecvRateLimit      = 64
	DefaultPeerRecvRateLimitBurst = 192
	MaxPeerPacketSize             = 5 * 1024 * 1024
	IdleTimeout                   = 10 * time.Second
	ValidMessageDeadline          = 2 * time.Hour

	Inbound  PeerDirection = 0
	Outbound PeerDirection = 1
)
View Source
const (
	StandardPort = 9097

	MainnetMagic    = 0xcafecafe
	ProtocolVersion = 1

	MaxPendingInbound  = 12
	MaxPendingOutbound = 5

	DayBan  = 24 * time.Hour
	YearBan = 365 * DayBan
)
View Source
const (
	DefaultPeerMuxerGossipTimeoutMS = 5 * 60 * 1000
)

Variables

View Source
var (
	ErrInvalidEnvelopeMagic     = errors.New("envelope has invalid magic")
	ErrInvalidEnvelopeTimestamp = errors.New("envelope has stale timestamp")
	ErrInvalidEnvelopeSignature = errors.New("envelope has invalid signature")
)
View Source
var (
	ErrUnexpectedMessage    = errors.New("unexpected handshake message")
	ErrIncompatibleProtocol = errors.New("incompatible protocol version")
	ErrInvalidNonce         = errors.New("invalid nonce on hello message")
)
View Source
var (
	ErrPeerSendBufferFull = errors.New("peer send buffer full")
	ErrPeerRecvBufferFull = errors.New("peer receive buffer full")
)
View Source
var (
	ErrPeerClosed = errors.New("peer closed")
	ErrPeerHangup = errors.New("remote hung up")
)
View Source
var (
	ErrMaxOutbound       = errors.New("reached maximum outbound peers")
	ErrMaxInbound        = errors.New("reached maximum inbound peers")
	ErrAlreadyConnecting = errors.New("already connecting to this peer")
	ErrPeerIDMismatch    = errors.New("peer IDs do not match after handshake")
	ErrSelfDial          = errors.New("self-dial after handshake")
	ErrAlreadyConnected  = errors.New("already connected to this peer")
	ErrPeerBanned        = errors.New("peer is banned")
	ErrInboundBusy       = errors.New("all inbound connections busy")
	ErrOutboundBusy      = errors.New("all outbound connections busy")
)

Functions

func BroadcastAll

func BroadcastAll(mux *PeerMuxer, message wire.Message) ([]crypto.Hash, []error)

func BroadcastRandom

func BroadcastRandom(mux *PeerMuxer, size int, message wire.Message) ([]crypto.Hash, []error)

func GossipAll

func GossipAll(mux *PeerMuxer, message wire.Message) ([]crypto.Hash, []error)

func HandleIncomingHandshake

func HandleIncomingHandshake(ctx context.Context, cfg *HandshakeConfig) (crypto.Hash, error)

func HandleOutgoingHandshake

func HandleOutgoingHandshake(ctx context.Context, cfg *HandshakeConfig) (crypto.Hash, error)

func ResolveDNSSeeds

func ResolveDNSSeeds(domain string) ([]string, error)

func ValidateEnvelope

func ValidateEnvelope(expMagic uint32, expPeerID crypto.Hash, envelope *wire.Envelope) error

func WriteEnvelope

func WriteEnvelope(ctx context.Context, peer Peer, signer crypto.Signer, magic uint32, message wire.Message) error

Types

type CountingReader

type CountingReader struct {
	// contains filtered or unexported fields
}

func NewCountingReader

func NewCountingReader(r io.Reader) *CountingReader

func (*CountingReader) Count

func (c *CountingReader) Count() uint64

func (*CountingReader) Read

func (c *CountingReader) Read(p []byte) (int, error)

func (*CountingReader) Reset

func (c *CountingReader) Reset()

type CountingWriter

type CountingWriter struct {
	// contains filtered or unexported fields
}

func NewCountingWriter

func NewCountingWriter(w io.Writer) *CountingWriter

func (*CountingWriter) Count

func (c *CountingWriter) Count() uint64

func (*CountingWriter) Reset

func (c *CountingWriter) Reset()

func (*CountingWriter) Write

func (c *CountingWriter) Write(p []byte) (int, error)

type HandshakeConfig

type HandshakeConfig struct {
	Magic           uint32
	ProtocolVersion uint32
	Peer            Peer
	Signer          crypto.Signer
}

type Listener

type Listener struct {
	// contains filtered or unexported fields
}

func NewListener

func NewListener(host string, manager PeerManager) *Listener

func (*Listener) Start

func (l *Listener) Start() error

func (*Listener) Stop

func (l *Listener) Stop() error

type Peer

type Peer interface {
	Direction() PeerDirection
	LocalAddr() string
	RemoteIP() string
	RemoteAddr() string
	RemotePort() int
	SendCtx(ctx context.Context, envelope *wire.Envelope) error
	Send(envelope *wire.Envelope) error
	ReceiveCtx(ctx context.Context) (*wire.Envelope, error)
	Receive() (*wire.Envelope, error)
	CloseChan() <-chan struct{}
	Close() error
	BandwidthUsage() (uint64, uint64)
	CloseReason() error
}

func NewPeer

func NewPeer(direction PeerDirection, conn net.Conn) Peer

type PeerDialer

type PeerDialer interface {
	DialPeer(id crypto.Hash, ip string, verify bool) error
}

type PeerDirection

type PeerDirection int

func (PeerDirection) String

func (p PeerDirection) String() string

type PeerImpl

type PeerImpl struct {
	// contains filtered or unexported fields
}

func (*PeerImpl) BandwidthUsage

func (p *PeerImpl) BandwidthUsage() (uint64, uint64)

func (*PeerImpl) Close

func (p *PeerImpl) Close() error

func (*PeerImpl) CloseChan

func (p *PeerImpl) CloseChan() <-chan struct{}

func (*PeerImpl) CloseReason

func (p *PeerImpl) CloseReason() error

func (*PeerImpl) Direction

func (p *PeerImpl) Direction() PeerDirection

func (*PeerImpl) LocalAddr

func (p *PeerImpl) LocalAddr() string

func (*PeerImpl) Receive

func (p *PeerImpl) Receive() (*wire.Envelope, error)

func (*PeerImpl) ReceiveCtx

func (p *PeerImpl) ReceiveCtx(ctx context.Context) (*wire.Envelope, error)

func (*PeerImpl) RemoteAddr

func (p *PeerImpl) RemoteAddr() string

func (*PeerImpl) RemoteIP

func (p *PeerImpl) RemoteIP() string

func (*PeerImpl) RemotePort

func (p *PeerImpl) RemotePort() int

func (*PeerImpl) Send

func (p *PeerImpl) Send(envelope *wire.Envelope) error

func (*PeerImpl) SendCtx

func (p *PeerImpl) SendCtx(ctx context.Context, envelope *wire.Envelope) error

type PeerManager

type PeerManager interface {
	service.Service
	PeerDialer
	AcceptPeer(conn *net.TCPConn) error
}

func NewPeerManager

func NewPeerManager(opts *PeerManagerOpts) PeerManager

type PeerManagerOpts

type PeerManagerOpts struct {
	Mux         *PeerMuxer
	DB          *leveldb.DB
	SeedPeers   []SeedPeer
	Signer      crypto.Signer
	ListenHost  string
	MaxInbound  int
	MaxOutbound int
}

type PeerMessageHandler

type PeerMessageHandler func(peerID crypto.Hash, envelope *wire.Envelope)

func PeerMessageHandlerForType

func PeerMessageHandlerForType(msgType wire.MessageType, hdlr func(id crypto.Hash, envelope *wire.Envelope)) PeerMessageHandler

type PeerMeta

type PeerMeta struct {
	ID        crypto.Hash
	IP        string
	Port      int
	SentBytes int64
	RecvBytes int64
}

type PeerMuxer

type PeerMuxer struct {
	GossipTimeoutMS int
	// contains filtered or unexported fields
}

func NewPeerMuxer

func NewPeerMuxer(magic uint32, signer crypto.Signer) *PeerMuxer

func (*PeerMuxer) AddMessageHandler

func (p *PeerMuxer) AddMessageHandler(handler PeerMessageHandler) util.Unsubscriber

func (*PeerMuxer) AddPeer

func (p *PeerMuxer) AddPeer(id crypto.Hash, peer Peer) error

func (*PeerMuxer) AddPeerOpenHandler

func (p *PeerMuxer) AddPeerOpenHandler(handler PeerStateHandler) util.Unsubscriber

func (*PeerMuxer) BandwidthUsage

func (p *PeerMuxer) BandwidthUsage() (uint64, uint64)

func (*PeerMuxer) ClosePeer

func (p *PeerMuxer) ClosePeer(id crypto.Hash) error

func (*PeerMuxer) GossipPeerIDs

func (p *PeerMuxer) GossipPeerIDs(message wire.Message) []crypto.Hash

func (*PeerMuxer) HasOutboundPeerIP

func (p *PeerMuxer) HasOutboundPeerIP(ip string) bool

func (*PeerMuxer) HasPeerID

func (p *PeerMuxer) HasPeerID(id crypto.Hash) bool

func (*PeerMuxer) PeerByID

func (p *PeerMuxer) PeerByID(id crypto.Hash) (Peer, error)

func (*PeerMuxer) PeerByIP

func (p *PeerMuxer) PeerByIP(ip string) (Peer, error)

func (*PeerMuxer) PeerCount

func (p *PeerMuxer) PeerCount() (int, int)

func (*PeerMuxer) PeerIDs

func (p *PeerMuxer) PeerIDs() []crypto.Hash

func (*PeerMuxer) Peers

func (p *PeerMuxer) Peers() map[crypto.Hash]Peer

func (*PeerMuxer) PeersByIP

func (p *PeerMuxer) PeersByIP(ip string) []Peer

func (*PeerMuxer) Send

func (p *PeerMuxer) Send(id crypto.Hash, message wire.Message) error

type PeerStateHandler

type PeerStateHandler func(peerID crypto.Hash)

type SeedPeer

type SeedPeer struct {
	ID crypto.Hash
	IP string
}

func ParseSeedPeers

func ParseSeedPeers(seedPeers []string) ([]SeedPeer, error)

Jump to

Keyboard shortcuts

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