pex

package
v0.0.0-...-9802667 Latest Latest
Warning

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

Go to latest
Published: May 27, 2018 License: Apache-2.0 Imports: 20 Imported by: 1

README

pex

Tools for implementing peer exchange (PEX) with Go

GoDoc

Godoc generated documentation

Documentation

Overview

Package pex is a toolkit for implementing a peer exchange system

Index

Constants

View Source
const (
	// DefaultPeerListURL is the default URL to download remote peers list from, if enabled
	DefaultPeerListURL = "https://downloads.spo.io/blockchain/peers.txt"
	// PeerDatabaseFilename filename for disk-cached peers
	PeerDatabaseFilename = "peers.txt"
	// MaxPeerRetryTimes is the maximum number of times to retry a peer
	MaxPeerRetryTimes = 10
)

Variables

View Source
var (
	// ErrPeerlistFull is returned when the Pex is at a maximum
	ErrPeerlistFull = errors.New("Peer list full")
	// ErrInvalidAddress is returned when an address appears malformed
	ErrInvalidAddress = errors.New("Invalid address")
	// ErrNoLocalhost is returned if a localhost addresses are not allowed
	ErrNoLocalhost = errors.New("Localhost address is not allowed")
	// ErrNotExternalIP is returned if an IP address is not a global unicast address
	ErrNotExternalIP = errors.New("IP is not a valid external IP")
	// ErrPortTooLow is returned if a port is less than 1024
	ErrPortTooLow = errors.New("Port must be >= 1024")
	// ErrBlacklistedAddress returned when attempting to add a blacklisted peer
	ErrBlacklistedAddress = errors.New("Blacklisted address")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	// Folder where peers database should be saved
	DataDirectory string
	// Maximum number of peers to keep account of in the PeerList
	Max int
	// Cull peers after they havent been seen in this much time
	Expiration time.Duration
	// Cull expired peers on this interval
	CullRate time.Duration
	// clear old peers on this interval
	ClearOldRate time.Duration
	// How often to clear expired blacklist entries
	UpdateBlacklistRate time.Duration
	// How often to request peers via PEX
	RequestRate time.Duration
	// How many peers to send back in response to a peers request
	ReplyCount int
	// Localhost peers are allowed in the peerlist
	AllowLocalhost bool
	// Disable exchanging of peers.  Peers are still loaded from disk
	Disabled bool
	// Whether the network is disabled
	NetworkDisabled bool
	// Download peers list from remote host
	DownloadPeerList bool
	// Download peers list from this URL
	PeerListURL string
}

Config pex config

func NewConfig

func NewConfig() Config

NewConfig creates default pex config.

type Filter

type Filter func(peer Peer) bool

Filter peers filter

type Peer

type Peer struct {
	Addr            string // An address of the form ip:port
	LastSeen        int64  // Unix timestamp when this peer was last seen
	Private         bool   // Whether it should omitted from public requests
	Trusted         bool   // Whether this peer is trusted
	HasIncomingPort bool   // Whether this peer has accessable public port
	RetryTimes      int    `json:"-"` // records the retry times
}

Peer represents a known peer

func NewPeer

func NewPeer(address string) *Peer

NewPeer returns a *Peer initialised by an address string of the form ip:port

func (*Peer) CanTry

func (peer *Peer) CanTry() bool

CanTry returns whether this peer is tryable base on the exponential backoff algorithm

func (*Peer) IncreaseRetryTimes

func (peer *Peer) IncreaseRetryTimes()

IncreaseRetryTimes adds the retry times

func (*Peer) ResetRetryTimes

func (peer *Peer) ResetRetryTimes()

ResetRetryTimes resets the retry time

func (*Peer) Seen

func (peer *Peer) Seen()

Seen marks the peer as seen

func (*Peer) String

func (peer *Peer) String() string

String returns the peer address

type PeerJSON

type PeerJSON struct {
	Addr string // An address of the form ip:port
	// Unix timestamp when this peer was last seen.
	// This could be a time.Time string or an int64 timestamp
	LastSeen        interface{}
	Private         bool  // Whether it should omitted from public requests
	Trusted         bool  // Whether this peer is trusted
	HasIncomePort   *bool `json:"HasIncomePort,omitempty"` // Whether this peer has incoming port [DEPRECATED]
	HasIncomingPort *bool // Whether this peer has incoming port
}

PeerJSON is for saving and loading peers to disk. Some fields are strange, to be backwards compatible due to variable name changes

type Peers

type Peers []Peer

Peers peer list

func (Peers) ToAddrs

func (ps Peers) ToAddrs() []string

ToAddrs returns the address list

type Pex

type Pex struct {
	sync.RWMutex

	Config Config
	// contains filtered or unexported fields
}

Pex manages a set of known peers and controls peer acquisition

func New

func New(cfg Config, defaultConns []string) (*Pex, error)

New creates pex

func (*Pex) AddPeer

func (px *Pex) AddPeer(addr string) error

AddPeer adds a peer to the peer list, given an address. If the peer list is full, PeerlistFullError is returned */

func (*Pex) AddPeers

func (px *Pex) AddPeers(addrs []string) int

AddPeers add multiple peers at once. Any errors will be logged, but not returned Returns the number of peers that were added without error. Note that adding a duplicate peer will not cause an error.

func (*Pex) GetPeerByAddr

func (px *Pex) GetPeerByAddr(addr string) (Peer, bool)

GetPeerByAddr returns peer of given address

func (*Pex) IncreaseRetryTimes

func (px *Pex) IncreaseRetryTimes(addr string)

IncreaseRetryTimes increases retry times

func (*Pex) IsFull

func (px *Pex) IsFull() bool

IsFull returns whether the peer list is full

func (*Pex) Private

func (px *Pex) Private() Peers

Private returns private peers

func (*Pex) RandomExchangeable

func (px *Pex) RandomExchangeable(n int) Peers

RandomExchangeable returns N random exchangeable peers

func (*Pex) RandomPublic

func (px *Pex) RandomPublic(n int) Peers

RandomPublic returns N random public peers

func (*Pex) RemovePeer

func (px *Pex) RemovePeer(addr string)

RemovePeer removes peer

func (*Pex) ResetAllRetryTimes

func (px *Pex) ResetAllRetryTimes()

ResetAllRetryTimes reset all peers' retry times

func (*Pex) ResetRetryTimes

func (px *Pex) ResetRetryTimes(addr string)

ResetRetryTimes reset retry times

func (*Pex) Run

func (px *Pex) Run() error

Run starts the pex service

func (*Pex) SetHasIncomingPort

func (px *Pex) SetHasIncomingPort(addr string, hasPublicPort bool) error

SetHasIncomingPort sets if the peer has public port

func (*Pex) SetPrivate

func (px *Pex) SetPrivate(addr string, private bool) error

SetPrivate updates peer's private value

func (*Pex) SetTrusted

func (px *Pex) SetTrusted(addr string) error

SetTrusted updates peer's trusted value

func (*Pex) Shutdown

func (px *Pex) Shutdown()

Shutdown notifies the pex service to exist

func (*Pex) Trusted

func (px *Pex) Trusted() Peers

Trusted returns trusted peers

func (*Pex) TrustedPublic

func (px *Pex) TrustedPublic() Peers

TrustedPublic returns trusted public peers

Jump to

Keyboard shortcuts

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