Documentation
¶
Overview ¶
Package pex is a toolkit for implementing a peer exchange system
Index ¶
- Constants
- Variables
- type Config
- type Filter
- type Peer
- type PeerJSON
- type Peers
- type Pex
- func (px *Pex) AddPeer(addr string) error
- func (px *Pex) AddPeers(addrs []string) int
- func (px *Pex) GetPeerByAddr(addr string) (Peer, bool)
- func (px *Pex) IncreaseRetryTimes(addr string)
- func (px *Pex) IsFull() bool
- func (px *Pex) Private() Peers
- func (px *Pex) RandomExchangeable(n int) Peers
- func (px *Pex) RandomPublic(n int) Peers
- func (px *Pex) RemovePeer(addr string)
- func (px *Pex) ResetAllRetryTimes()
- func (px *Pex) ResetRetryTimes(addr string)
- func (px *Pex) Run() error
- func (px *Pex) SetHasIncomingPort(addr string, hasPublicPort bool) error
- func (px *Pex) SetPrivate(addr string, private bool) error
- func (px *Pex) SetTrusted(addr string) error
- func (px *Pex) Shutdown()
- func (px *Pex) Trusted() Peers
- func (px *Pex) TrustedPublic() Peers
Constants ¶
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 ¶
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
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 (*Peer) CanTry ¶
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
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 Pex ¶
Pex manages a set of known peers and controls peer acquisition
func (*Pex) AddPeer ¶
AddPeer adds a peer to the peer list, given an address. If the peer list is full, PeerlistFullError is returned */
func (*Pex) AddPeers ¶
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 ¶
GetPeerByAddr returns peer of given address
func (*Pex) IncreaseRetryTimes ¶
IncreaseRetryTimes increases retry times
func (*Pex) RandomExchangeable ¶
RandomExchangeable returns N random exchangeable peers
func (*Pex) RandomPublic ¶
RandomPublic returns N random public peers
func (*Pex) ResetAllRetryTimes ¶
func (px *Pex) ResetAllRetryTimes()
ResetAllRetryTimes reset all peers' retry times
func (*Pex) ResetRetryTimes ¶
ResetRetryTimes reset retry times
func (*Pex) SetHasIncomingPort ¶
SetHasIncomingPort sets if the peer has public port
func (*Pex) SetPrivate ¶
SetPrivate updates peer's private value
func (*Pex) SetTrusted ¶
SetTrusted updates peer's trusted value
func (*Pex) TrustedPublic ¶
TrustedPublic returns trusted public peers
