Documentation ¶
Index ¶
- Constants
- func DebugServer(n *Network)
- func IP2Location(addr string) (uint32, error)
- func IP2LocationQuick(addr string) uint32
- func StringToUint32(input string) uint32
- func WebScanner(url string, f func(line string)) error
- type Configuration
- type Dialer
- type Endpoint
- type Handshake
- type Info
- type LimitedListener
- type Measure
- type MetricsReadWriter
- type Network
- func (n *Network) AddSpecial(raw string)
- func (n *Network) Ban(hash string)
- func (n *Network) DebugMessage() (string, string, int)
- func (n *Network) Disconnect(hash string)
- func (n *Network) GetInfo() Info
- func (n *Network) GetPeerMetrics() map[string]PeerMetrics
- func (n *Network) Rounds() int
- func (n *Network) Run()
- func (n *Network) SetMetricsHook(f func(pm map[string]PeerMetrics))
- func (n *Network) Stop()
- func (n *Network) Total() int
- type NetworkID
- type Parcel
- type ParcelChannel
- type ParcelType
- type Peer
- type PeerMetrics
- type PeerStore
- func (ps *PeerStore) Add(p *Peer) error
- func (ps *PeerStore) Connected(ep Endpoint) bool
- func (ps *PeerStore) Connections(addr string) int
- func (ps *PeerStore) Count(addr string) int
- func (ps *PeerStore) Get(hash string) *Peer
- func (ps *PeerStore) Incoming() int
- func (ps *PeerStore) Outgoing() int
- func (ps *PeerStore) Remove(p *Peer)
- func (ps *PeerStore) Slice() []*Peer
- func (ps *PeerStore) Total() int
- type Persist
- type Prometheus
- type Protocol
- type ProtocolV10
- type ProtocolV9
- type ReadWriteCollector
- type StatsCollector
- type V10Msg
- type V10Share
- type V9Header
- type V9Msg
- type V9Share
Constants ¶
const ( // Broadcast sends a parcel to multiple peers (randomly selected based on fanout and special peers) Broadcast = "<BROADCAST>" // FullBroadcast sends a parcel to all peers FullBroadcast = "<FULLBORADCAST>" // RandomPeer sends a parcel to one randomly selected peer RandomPeer = "<RANDOMPEER>" )
Variables ¶
This section is empty.
Functions ¶
func IP2Location ¶
IP2Location converts an ip address to a uint32
If the address is a hostmask, it attempts to resolve the address first
func IP2LocationQuick ¶
IP2LocationQuick converts an ip address to a uint32 without a hostmask lookup
func StringToUint32 ¶
StringToUint32 hashes the input to generate a deterministic number representation
func WebScanner ¶
WebScanner is a wrapper that applies the closure f to the response body
Types ¶
type Configuration ¶
type Configuration struct { // Network is the NetworkID of the network to use, eg. MainNet, TestNet, etc Network NetworkID // NodeID is this node's id NodeID uint32 // NodeName is the internal name of the node NodeName string // === Peer Management Settings === // PeerRequestInterval dictates how often neighbors should be asked for an // updated peer list PeerRequestInterval time.Duration // PeerReseedInterval dictates how often the seed file should be accessed // to check for changes PeerReseedInterval time.Duration // PeerIPLimit specifies the maximum amount of peers to accept from a single // ip address // 0 for unlimited PeerIPLimitIncoming uint PeerIPLimitOutgoing uint // Special is a list of special peers, separated by comma. If no port is specified, the entire // ip is considered special Special string // PersistFile is the filepath to the file to save peers PersistFile string // how often to save these PersistInterval time.Duration // PeerShareAmount is the number of peers we share PeerShareAmount uint // CAT Settings RoundTime time.Duration Target uint Max uint Drop uint MinReseed uint Incoming uint // maximum inbound connections, 0 <= Incoming <= Max // Fanout controls how many random peers are selected for propagating messages // Higher values increase fault tolerance but also increase network congestion Fanout uint // SeedURL is the URL of the remote seed file SeedURL string // URL to a source of peer info // BindIP is the ip address to bind to for listening and connecting // // leave blank to bind to all BindIP string // ListenPort is the port to listen to incoming tcp connections on ListenPort string // ListenLimit is the lockout period of accepting connections from a single // ip after having a successful connection from that ip ListenLimit time.Duration // PingInterval dictates the maximum amount of time a connection can be // silent (no writes) before sending a Ping PingInterval time.Duration // RedialInterval dictates how long to wait between connection attempts RedialInterval time.Duration // ManualBan is the duration to ban an address for when banned manually ManualBan time.Duration // HandshakeDeadline is the maximum acceptable time for an incoming conneciton // to send the first parcel after connecting HandshakeTimeout time.Duration DialTimeout time.Duration // ReadDeadline is the maximum acceptable time to read a single parcel // if a connection takes longer, it is disconnected ReadDeadline time.Duration // WriteDeadline is the maximum acceptable time to send a single parcel // if a connection takes longer, it is disconnected WriteDeadline time.Duration ProtocolVersion uint16 // ProtocolVersionMinimum is the earliest version this package supports ProtocolVersionMinimum uint16 ChannelCapacity uint EnablePrometheus bool // Enable prometheus logging. Disable if you run multiple instances }
Configuration defines the behavior of the gossip network protocol
func DefaultP2PConfiguration ¶
func DefaultP2PConfiguration() (c Configuration)
DefaultP2PConfiguration returns a network configuration with base values These should be overwritten with command line and config parameters
func (*Configuration) Sanitize ¶
func (c *Configuration) Sanitize()
type Dialer ¶
type Dialer struct {
// contains filtered or unexported fields
}
Dialer is a construct to throttle dialing and limit by attempts
type Endpoint ¶
func NewEndpoint ¶
NewEndpoint creates an Endpoint struct from a given ip and port, throws error if ip could not be resolved
func ParseEndpoint ¶
ParseEndpoint takes input in the form of "ip:port" and returns its IP
type Handshake ¶
type Handshake V9Msg
Handshake is an alias of V9MSG for backward compatibility
func (*Handshake) SetPayload ¶
SetPayload adds a payload to the handshake and updates the header with metadata
func (*Handshake) Valid ¶
func (h *Handshake) Valid(conf *Configuration) error
Valid checks if the other node is compatible
type Info ¶
type Info struct { Peers int // number of peers connected Receiving float64 // download rate in Messages/s Sending float64 // upload rate in Messages/s Download float64 // download rate in Bytes/s Upload float64 // upload rate in Bytes/s Dropped uint64 // number of parcels dropped due to low speed }
Info holds the data that can be queried from the Network
type LimitedListener ¶
type LimitedListener struct {
// contains filtered or unexported fields
}
LimitedListener will block multiple connection attempts from a single ip within a specific timeframe
func NewLimitedListener ¶
func NewLimitedListener(address string, limit time.Duration) (*LimitedListener, error)
NewLimitedListener initializes a new listener for the specified address (address:port) throttling incoming connections
func (*LimitedListener) Accept ¶
func (ll *LimitedListener) Accept() (net.Conn, error)
Accept accepts a connection if no other connection attempt from that ip has been made in the specified time frame
func (*LimitedListener) Addr ¶
func (ll *LimitedListener) Addr() net.Addr
Addr returns the address the listener is listening to
func (*LimitedListener) Close ¶
func (ll *LimitedListener) Close()
Close closes the associated net.Listener
type Measure ¶
type Measure struct {
// contains filtered or unexported fields
}
Measure measures the per-second rates of messages and bandwidth based on individual calls
func NewMeasure ¶
NewMeasure initializes a new measuring tool based on the given rate Rate must be at least one second and should be multiples of seconds
func (*Measure) GetRate ¶
GetRate returns the current rates measured this interval (Parcels Received, Parcels Sent, Bytes Received, Bytes Sent)
type MetricsReadWriter ¶
type MetricsReadWriter struct {
// contains filtered or unexported fields
}
MetricsReadWriter is a wrapper for net.Conn that allows the package to observe the actual amount of bytes passing through it
func NewMetricsReadWriter ¶
func NewMetricsReadWriter(rw io.ReadWriter) *MetricsReadWriter
type Network ¶
type Network struct { ToNetwork ParcelChannel FromNetwork ParcelChannel // contains filtered or unexported fields }
Network is the main access point for outside applications.
ToNetwork is the channel over which to send parcels to the network layer ¶
FromNetwork is the channel that gets filled with parcels arriving from the network layer
func NewNetwork ¶
func NewNetwork(conf Configuration) (*Network, error)
NewNetwork initializes a new network with the given configuration. The passed Configuration is copied and cannot be modified afterwards. Does not start the network automatically.
func (*Network) AddSpecial ¶
AddSpecial takes a set of ip addresses that should be treated as special. Network will always attempt to have a connection to a special peer. Format is a single line of ip addresses and ports, separated by semicolon, eg "127.0.0.1:8088;8.8.8.8;192.168.0.1:8110"
The port is optional and the entire ip will be considered special if no port is provided
func (*Network) Ban ¶
Ban removes a peer as well as any other peer from that address and prevents any connection being established for the amount of time set in the configuration (default one week)
func (*Network) DebugMessage ¶
DebugMessage is temporary
func (*Network) Disconnect ¶
Disconnect severs connection for a specific peer. They are free to connect again afterward
func (*Network) GetPeerMetrics ¶
func (n *Network) GetPeerMetrics() map[string]PeerMetrics
func (*Network) Run ¶
func (n *Network) Run()
Run starts the network. Listens to incoming connections on the specified port and connects to other peers
func (*Network) SetMetricsHook ¶
func (n *Network) SetMetricsHook(f func(pm map[string]PeerMetrics))
SetMetricsHook allows you to read peer metrics. Gets called approximately once a second and transfers the metrics of all CONNECTED peers in the format "identifying hash" -> p2p.PeerMetrics
type NetworkID ¶
type NetworkID uint32
NetworkID represents the P2P network we are participating in (eg: test, nmain, etc.)
const ( MainNet NetworkID = 0xfeedbeef TestNet NetworkID = 0xdeadbeef LocalNet NetworkID = 0xbeaded )
NetworkID are specific uint32s to identify separate networks
The default identifiers are MainNet (the main production network), TestNet (for network=TESTNET) and LocalNet (for network=LOCAL).
Custom NetworkIDs (network=CUSTOM) are generated from the "customnet" command line flag
func NewNetworkID ¶
NewNetworkID converts a string to a network id
type Parcel ¶
type Parcel struct { Type ParcelType // 2 bytes - network level commands (eg: ping/pong) Address string // ? bytes - "" or nil for broadcast, otherwise the destination peer's hash. Payload []byte }
Parcel is the raw data interface between the network, the p2p package, and the application.
Type indicates the network or application type. Messages routed to and from the application will only have application types
Address is a unique internal identifier for origin or target of the parcel. For messages from the network to the application, the address will the id of the sender. Messages intended to be returned to the sender should bear the same address.
There are three special address constants:
Broadcast: The message will be sent to multiple peers as specified by the fanout FullBroadcast: The message will be sent to all peers RandomPeer: The message will be sent to one peer picked at random
The payload is arbitrary data defined at application level
func NewParcel ¶
NewParcel creates a new application message. The target should be either an identifier from a previous message, or one of the custom flags: Broadcast, BroadcastFull, RandomPeer
func (*Parcel) IsApplicationMessage ¶
IsApplicationMessage checks if the message is intended for the application
type ParcelChannel ¶
type ParcelChannel chan *Parcel
ParcelChannel is a channel that supports non-blocking sends
func (ParcelChannel) Capacity ¶
func (pc ParcelChannel) Capacity() float64
Capacity returns a percentage [0.0,1.0] of how full the channel is
func (ParcelChannel) Reader ¶
func (pc ParcelChannel) Reader() <-chan *Parcel
Reader returns a read-only channel
type ParcelType ¶
type ParcelType uint16
ParcelType is a list of parcel types that this node understands
const ( // TypeHeartbeat is deprecated TypeHeartbeat ParcelType = iota // TypePing is sent if no other parcels have been sent in a while TypePing // TypePong is a response to a Ping TypePong // TypePeerRequest indicates a peer wants to be be updated of endpoints TypePeerRequest // TypePeerResponse carries a payload with protocol specific endpoints TypePeerResponse // TypeAlert is deprecated TypeAlert // TypeMessage carries an application message in the payload TypeMessage // TypeMessagePart is a partial message. deprecated in p2p 2.0 TypeMessagePart // TypeHandshake is the first parcel sent after making a connection TypeHandshake // TypeRejectAlternative is sent instead of a handshake if the server refuses connection TypeRejectAlternative )
func (ParcelType) String ¶
func (t ParcelType) String() string
type Peer ¶
type Peer struct { // current state, read only "constants" after the handshake IsIncoming bool Endpoint Endpoint NodeID uint32 // a nonce to distinguish multiple nodes behind one endpoint Hash string // This is more of a connection ID than hash right now. // contains filtered or unexported fields }
Peer is an active connection to an endpoint in the network. Represents one lifetime of a connection and should not be restarted
func (*Peer) GetMetrics ¶
func (p *Peer) GetMetrics() PeerMetrics
GetMetrics returns live metrics for this connection
func (*Peer) StartWithHandshake ¶
StartWithHandshake performs a basic handshake maneouver to establish the validity of the connection. Immediately sends a Peer Request upon connection and waits for the response, which can be any parcel. The information in the header is verified, especially the port.
The handshake ensures that ALL peers have a valid Port field to start with. If there is no reply within the specified HandshakeTimeout config setting, the process fails
For outgoing connections, it is possible the endpoint will reject due to being full, in which case this function returns an error AND a list of alternate endpoints
type PeerMetrics ¶
type PeerMetrics struct { Hash string PeerAddress string MomentConnected time.Time PeerQuality int32 LastReceive time.Time LastSend time.Time MessagesSent uint64 BytesSent uint64 MessagesReceived uint64 BytesReceived uint64 Incoming bool PeerType string ConnectionState string MPSDown float64 MPSUp float64 BPSDown float64 BPSUp float64 Capacity float64 Dropped uint64 }
PeerMetrics is the data shared to the metrics hook
type PeerStore ¶
type PeerStore struct {
// contains filtered or unexported fields
}
PeerStore holds active Peers, managing them in a concurrency safe manner and providing lookup via various functions
func (*PeerStore) Add ¶
Add a peer to be managed. Throws error if a peer with that hash is already tracked
func (*PeerStore) Connections ¶
Connections tests whether there is a peer connected from a specified ip address
func (*PeerStore) Remove ¶
Remove a specific peer if it exists. This checks by pointer reference and not by hash. If you have two distinct peer instances (A and B) with the same hash and add A, removing B has no effect, even if they have the same values
type Persist ¶
type Persist struct { Bans map[string]time.Time `json:"bans"` // can be ip or ip:port Bootstrap []Endpoint `json:"bootstrap"` }
Persist is the object that gets json-marshalled and written to disk
type Prometheus ¶
type Prometheus struct { Networks prometheus.Gauge Connections prometheus.Gauge // done Unique prometheus.Gauge Connecting prometheus.Gauge // done Incoming prometheus.Gauge // done Outgoing prometheus.Gauge // done KnownPeers prometheus.Gauge // done SendRoutines prometheus.Gauge ReceiveRoutines prometheus.Gauge ParcelsSent prometheus.Counter ParcelsReceived prometheus.Counter Invalid prometheus.Counter AppSent prometheus.Counter AppReceived prometheus.Counter AppDuplicate prometheus.Counter ParcelSize prometheus.Histogram }
Prometheus holds all of the prometheus recording instruments
func (*Prometheus) Setup ¶
func (p *Prometheus) Setup()
Setup registers all of the instruments with prometheus once
type Protocol ¶
Protocol is the interface for reading and writing parcels to the underlying connection. The job of a protocol is to encode a Parcel and send it over TCP to another instance of the same Protocol on the other end
Send: Parcel => Protocol Encoder => Protocol Format => TCP Receive: TCP => Protocol Format => Protocol Decoder => Parcel
Peer Sharing creates the protocol specific payload for a TypePeerShare Parcel
Every protocol should be bootstraped in peer:bootstrapProtocol() where it can be initialized with the required serialization methods
type ProtocolV10 ¶
type ProtocolV10 struct {
// contains filtered or unexported fields
}
ProtocolV10 is the protocol introduced by p2p 2.0. It is a slimmed down version of V9, reducing overhead
func (*ProtocolV10) MakePeerShare ¶
func (v10 *ProtocolV10) MakePeerShare(share []Endpoint) ([]byte, error)
MakePeerShare serializes a list of ips via json
func (*ProtocolV10) ParsePeerShare ¶
func (v10 *ProtocolV10) ParsePeerShare(payload []byte) ([]Endpoint, error)
ParsePeerShare parses a peer share payload
func (*ProtocolV10) Receive ¶
func (v10 *ProtocolV10) Receive() (*Parcel, error)
Receive converts a V10Msg back to a Parcel
func (*ProtocolV10) Send ¶
func (v10 *ProtocolV10) Send(p *Parcel) error
Send encodes a Parcel as V10Msg, calculates the crc and encodes it as gob
type ProtocolV9 ¶
type ProtocolV9 struct {
// contains filtered or unexported fields
}
ProtocolV9 is the legacy format of the old p2p package which sends Parcels over the wire using gob. The V9Msg struct is equivalent to the old package's "Parcel" and "ParcelHeader" structure
func (*ProtocolV9) MakePeerShare ¶
func (v9 *ProtocolV9) MakePeerShare(ps []Endpoint) ([]byte, error)
MakePeerShare serializes the given endpoints to a V9Share encoded in json
func (*ProtocolV9) ParsePeerShare ¶
func (v9 *ProtocolV9) ParsePeerShare(payload []byte) ([]Endpoint, error)
ParsePeerShare unserializes the json V9Share
func (*ProtocolV9) Receive ¶
func (v9 *ProtocolV9) Receive() (*Parcel, error)
Receive a parcel from the network. Blocking.
func (*ProtocolV9) Send ¶
func (v9 *ProtocolV9) Send(p *Parcel) error
Send a parcel over the connection
type ReadWriteCollector ¶
type ReadWriteCollector interface { io.Reader io.Writer StatsCollector }
type StatsCollector ¶
type V10Msg ¶
type V10Msg struct { Type ParcelType Crc32 uint32 Payload []byte }
V10Msg is the barebone message
type V9Header ¶
type V9Header struct { Network NetworkID Version uint16 Type ParcelType Length uint32 TargetPeer string Crc32 uint32 PartNo uint16 PartsTotal uint16 NodeID uint64 PeerAddress string PeerPort string AppHash string AppType string }
V9Header carries meta information about the parcel
Source Files ¶
- configuration.go
- controller.go
- controller_cat.go
- controller_dialing.go
- controller_persistence.go
- controller_run.go
- dialer.go
- endpoint.go
- handshake.go
- info.go
- limiter.go
- measure.go
- metricsReadWriter.go
- network.go
- networkId.go
- parcel.go
- parcelChannel.go
- parcelType.go
- peer.go
- peerStore.go
- peerUtil.go
- prometheus.go
- protocol.go
- protocolV10.go
- protocolV9.go
- seed.go
- util.go