Documentation ¶
Index ¶
- Constants
- func IDAddressString(id ID, hostPort string) string
- func MaxNodeInfoSize() int
- func SplitHostPort(addr string) (host string, port int)
- type AddrBook
- type BaseReactor
- type ChannelDescriptor
- type ConnectionStatus
- type DefaultListener
- func (l *DefaultListener) Connections() <-chan net.Conn
- func (l *DefaultListener) ExternalAddress() *NetAddress
- func (l *DefaultListener) ExternalAddressHost() string
- func (l *DefaultListener) InternalAddress() *NetAddress
- func (l *DefaultListener) OnStart() error
- func (l *DefaultListener) OnStop()
- func (l *DefaultListener) String() string
- type ErrNetAddressInvalid
- type ErrNetAddressLookup
- type ErrNetAddressNoID
- type ErrSwitchAuthenticationFailure
- type ErrSwitchConnectToSelf
- type ErrSwitchDuplicatePeerID
- type ErrSwitchDuplicatePeerIP
- type ID
- type IPeerSet
- type Listener
- type NetAddress
- func NewNetAddress(id ID, addr net.Addr) *NetAddress
- func NewNetAddressIPPort(ip net.IP, port uint16) *NetAddress
- func NewNetAddressString(addr string) (*NetAddress, error)
- func NewNetAddressStringWithOptionalID(addr string) (*NetAddress, error)
- func NewNetAddressStrings(addrs []string) ([]*NetAddress, []error)
- func (na *NetAddress) Dial() (net.Conn, error)
- func (na *NetAddress) DialString() string
- func (na *NetAddress) DialTimeout(timeout time.Duration) (net.Conn, error)
- func (na *NetAddress) Equals(other interface{}) bool
- func (na *NetAddress) Local() bool
- func (na *NetAddress) RFC1918() bool
- func (na *NetAddress) RFC3849() bool
- func (na *NetAddress) RFC3927() bool
- func (na *NetAddress) RFC3964() bool
- func (na *NetAddress) RFC4193() bool
- func (na *NetAddress) RFC4380() bool
- func (na *NetAddress) RFC4843() bool
- func (na *NetAddress) RFC4862() bool
- func (na *NetAddress) RFC6052() bool
- func (na *NetAddress) RFC6145() bool
- func (na *NetAddress) ReachabilityTo(o *NetAddress) int
- func (na *NetAddress) Routable() bool
- func (na *NetAddress) Same(other interface{}) bool
- func (na *NetAddress) String() string
- func (na *NetAddress) Valid() bool
- type NodeInfo
- type NodeKey
- type Peer
- type PeerSet
- type Reactor
- type Switch
- func (sw *Switch) AddListener(l Listener)
- func (sw *Switch) AddReactor(name string, reactor Reactor) Reactor
- func (sw *Switch) Broadcast(chID byte, msgBytes []byte) chan bool
- func (sw *Switch) DialPeerWithAddress(addr *NetAddress, persistent bool) error
- func (sw *Switch) DialPeersAsync(addrBook AddrBook, peers []string, persistent bool) error
- func (sw *Switch) FilterConnByAddr(addr net.Addr) error
- func (sw *Switch) FilterConnByID(id ID) error
- func (sw *Switch) GetPeerForID(id string) Peer
- func (sw *Switch) IsDialing(id ID) bool
- func (sw *Switch) IsListening() bool
- func (sw *Switch) Listeners() []Listener
- func (sw *Switch) MarkPeerAsGood(peer Peer)
- func (sw *Switch) NodeInfo() NodeInfo
- func (sw *Switch) NumPeers() (outbound, inbound, dialing int)
- func (sw *Switch) OnStart() error
- func (sw *Switch) OnStop()
- func (sw *Switch) Peers() IPeerSet
- func (sw *Switch) Reactor(name string) Reactor
- func (sw *Switch) Reactors() map[string]Reactor
- func (sw *Switch) ReadPeerList()
- func (sw *Switch) SetAddrBook(addrBook AddrBook)
- func (sw *Switch) SetAddrFilter(f func(net.Addr) error)
- func (sw *Switch) SetIDFilter(f func(ID) error)
- func (sw *Switch) SetNodeInfo(nodeInfo NodeInfo)
- func (sw *Switch) SetNodeKey(nodeKey *NodeKey)
- func (sw *Switch) StopPeerForError(peer Peer, reason interface{})
- func (sw *Switch) StopPeerGracefully(peer Peer)
- type SwitchOption
Constants ¶
const ( // keep at least this many outbound peers // TODO: move to config DefaultMinNumOutboundPeers = 10 )
const IDByteLength = 20
IDByteLength is the length of a crypto.Address. Currently only 20. TODO: support other length addresses ?
Variables ¶
This section is empty.
Functions ¶
func IDAddressString ¶
IDAddressString returns id@hostPort.
func SplitHostPort ¶
Types ¶
type AddrBook ¶
type AddrBook interface { AddAddress(addr *NetAddress, src *NetAddress) error AddOurAddress(*NetAddress) OurAddress(*NetAddress) bool MarkGood(*NetAddress) RemoveAddress(*NetAddress) HasAddress(*NetAddress) bool Save() }
An AddrBook represents an address book from the pex package, which is used to store peer addresses.
type BaseReactor ¶
type BaseReactor struct { help.BaseService // Provides Start, Stop, .Quit Switch *Switch }
func NewBaseReactor ¶
func NewBaseReactor(name string, impl Reactor) *BaseReactor
func (*BaseReactor) AddPeer ¶
func (*BaseReactor) AddPeer(peer Peer)
func (*BaseReactor) GetChannels ¶
func (*BaseReactor) GetChannels() []*conn.ChannelDescriptor
func (*BaseReactor) RemovePeer ¶
func (*BaseReactor) RemovePeer(peer Peer, reason interface{})
func (*BaseReactor) SetSwitch ¶
func (br *BaseReactor) SetSwitch(sw *Switch)
type ChannelDescriptor ¶
type ChannelDescriptor = conn.ChannelDescriptor
type ConnectionStatus ¶
type ConnectionStatus = conn.ConnectionStatus
type DefaultListener ¶
type DefaultListener struct { help.BaseService // contains filtered or unexported fields }
DefaultListener is a help.Service, running net.Listener underneath. Optionally, UPnP is used upon calling NewDefaultListener to resolve external address.
func (*DefaultListener) Connections ¶
func (l *DefaultListener) Connections() <-chan net.Conn
Connections returns a channel of inbound connections. It gets closed when the listener closes. It is the callers responsibility to close any connections received over this channel.
func (*DefaultListener) ExternalAddress ¶
func (l *DefaultListener) ExternalAddress() *NetAddress
ExternalAddress returns the external NetAddress (publicly available, determined using either UPnP or local resolver).
func (*DefaultListener) ExternalAddressHost ¶
func (l *DefaultListener) ExternalAddressHost() string
ExternalAddressHost returns the external NetAddress IP string. If an IP is IPv6, it's wrapped in brackets ("[2001:db8:1f70::999:de8:7648:6e8]").
func (*DefaultListener) InternalAddress ¶
func (l *DefaultListener) InternalAddress() *NetAddress
InternalAddress returns the internal NetAddress (address used for listening).
func (*DefaultListener) OnStart ¶
func (l *DefaultListener) OnStart() error
OnStart implements help.Service by spinning a goroutine, listening for new connections.
func (*DefaultListener) OnStop ¶
func (l *DefaultListener) OnStop()
OnStop implements help.Service by closing the listener.
func (*DefaultListener) String ¶
func (l *DefaultListener) String() string
type ErrNetAddressInvalid ¶
func (ErrNetAddressInvalid) Error ¶
func (e ErrNetAddressInvalid) Error() string
type ErrNetAddressLookup ¶
func (ErrNetAddressLookup) Error ¶
func (e ErrNetAddressLookup) Error() string
type ErrNetAddressNoID ¶
type ErrNetAddressNoID struct {
Addr string
}
func (ErrNetAddressNoID) Error ¶
func (e ErrNetAddressNoID) Error() string
type ErrSwitchAuthenticationFailure ¶
type ErrSwitchAuthenticationFailure struct { Dialed *NetAddress Got ID }
func (ErrSwitchAuthenticationFailure) Error ¶
func (e ErrSwitchAuthenticationFailure) Error() string
type ErrSwitchConnectToSelf ¶
type ErrSwitchConnectToSelf struct {
Addr *NetAddress
}
ErrSwitchConnectToSelf to be raised when trying to connect to itself.
func (ErrSwitchConnectToSelf) Error ¶
func (e ErrSwitchConnectToSelf) Error() string
type ErrSwitchDuplicatePeerID ¶
type ErrSwitchDuplicatePeerID struct {
ID ID
}
ErrSwitchDuplicatePeerID to be raised when a peer is connecting with a known ID.
func (ErrSwitchDuplicatePeerID) Error ¶
func (e ErrSwitchDuplicatePeerID) Error() string
type ErrSwitchDuplicatePeerIP ¶
ErrSwitchDuplicatePeerIP to be raised whena a peer is connecting with a known IP.
func (ErrSwitchDuplicatePeerIP) Error ¶
func (e ErrSwitchDuplicatePeerIP) Error() string
type ID ¶
type ID string
ID is a hex-encoded crypto.Address
func PubKeyToID ¶
PubKeyToID returns the ID corresponding to the given PubKey. It's the hex-encoding of the pubKey.Address().
type IPeerSet ¶
type IPeerSet interface { Has(key ID) bool HasIP(ip net.IP) bool Get(key ID) Peer List() []Peer Size() int }
IPeerSet has a (immutable) subset of the methods of PeerSet.
type Listener ¶
type Listener interface { Connections() <-chan net.Conn InternalAddress() *NetAddress ExternalAddress() *NetAddress ExternalAddressHost() string String() string Stop() error }
Listener is a network listener for stream-oriented protocols, providing convenient methods to get listener's internal and external addresses. Clients are supposed to read incoming connections from a channel, returned by Connections() method.
type NetAddress ¶
type NetAddress struct { ID ID `json:"id"` IP net.IP `json:"ip"` Port uint16 `json:"port"` // contains filtered or unexported fields }
NetAddress defines information about a peer on the network including its ID, IP address, and port.
func NewNetAddress ¶
func NewNetAddress(id ID, addr net.Addr) *NetAddress
NewNetAddress returns a new NetAddress using the provided TCP address. When testing, other net.Addr (except TCP) will result in using 0.0.0.0:0. When normal run, other net.Addr (except TCP) will panic. TODO: socks proxies?
func NewNetAddressIPPort ¶
func NewNetAddressIPPort(ip net.IP, port uint16) *NetAddress
NewNetAddressIPPort returns a new NetAddress using the provided IP and port number.
func NewNetAddressString ¶
func NewNetAddressString(addr string) (*NetAddress, error)
NewNetAddressString returns a new NetAddress using the provided address in the form of "ID@IP:Port". Also resolves the host if host is not an IP. Errors are of type ErrNetAddressXxx where Xxx is in (NoID, Invalid, Lookup)
func NewNetAddressStringWithOptionalID ¶
func NewNetAddressStringWithOptionalID(addr string) (*NetAddress, error)
NewNetAddressStringWithOptionalID returns a new NetAddress using the provided address in the form of "ID@IP:Port", where the ID is optional. Also resolves the host if host is not an IP.
func NewNetAddressStrings ¶
func NewNetAddressStrings(addrs []string) ([]*NetAddress, []error)
NewNetAddressStrings returns an array of NetAddress'es build using the provided strings.
func (*NetAddress) Dial ¶
func (na *NetAddress) Dial() (net.Conn, error)
Dial calls net.Dial on the address.
func (*NetAddress) DialString ¶
func (na *NetAddress) DialString() string
func (*NetAddress) DialTimeout ¶
DialTimeout calls net.DialTimeout on the address.
func (*NetAddress) Equals ¶
func (na *NetAddress) Equals(other interface{}) bool
Equals reports whether na and other are the same addresses, including their ID, IP, and Port.
func (*NetAddress) Local ¶
func (na *NetAddress) Local() bool
Local returns true if it is a local address.
func (*NetAddress) RFC1918 ¶
func (na *NetAddress) RFC1918() bool
func (*NetAddress) RFC3849 ¶
func (na *NetAddress) RFC3849() bool
func (*NetAddress) RFC3927 ¶
func (na *NetAddress) RFC3927() bool
func (*NetAddress) RFC3964 ¶
func (na *NetAddress) RFC3964() bool
func (*NetAddress) RFC4193 ¶
func (na *NetAddress) RFC4193() bool
func (*NetAddress) RFC4380 ¶
func (na *NetAddress) RFC4380() bool
func (*NetAddress) RFC4843 ¶
func (na *NetAddress) RFC4843() bool
func (*NetAddress) RFC4862 ¶
func (na *NetAddress) RFC4862() bool
func (*NetAddress) RFC6052 ¶
func (na *NetAddress) RFC6052() bool
func (*NetAddress) RFC6145 ¶
func (na *NetAddress) RFC6145() bool
func (*NetAddress) ReachabilityTo ¶
func (na *NetAddress) ReachabilityTo(o *NetAddress) int
ReachabilityTo checks whenever o can be reached from na.
func (*NetAddress) Routable ¶
func (na *NetAddress) Routable() bool
Routable returns true if the address is routable.
func (*NetAddress) Same ¶
func (na *NetAddress) Same(other interface{}) bool
Same returns true is na has the same non-empty ID or DialString as other.
func (*NetAddress) String ¶
func (na *NetAddress) String() string
String representation: <ID>@<IP>:<PORT>
func (*NetAddress) Valid ¶
func (na *NetAddress) Valid() bool
For IPv4 these are either a 0 or all bits set address. For IPv6 a zero address or one that matches the RFC3849 documentation address format.
type NodeInfo ¶
type NodeInfo struct { // Authenticate // TODO: replace with NetAddress ID ID `json:"id"` // authenticated identifier ListenAddr string `json:"listen_addr"` // accepting incoming // Check compatibility. // Channels are HexBytes so easier to read as JSON Network string `json:"network"` // network/chain ID Version string `json:"version"` // major.minor.revision Channels help.HexBytes `json:"channels"` // channels this node knows about // ASCIIText fields Moniker string `json:"moniker"` // arbitrary moniker Other []string `json:"other"` // other application specific data }
NodeInfo is the basic node information exchanged between two peers during the taichain P2P handshake.
func (NodeInfo) CompatibleWith ¶
CompatibleWith checks if two NodeInfo are compatible with eachother. CONTRACT: two nodes are compatible if the major version matches and network match and they have at least one channel in common.
func (NodeInfo) NetAddress ¶
func (info NodeInfo) NetAddress() *NetAddress
NetAddress returns a NetAddress derived from the NodeInfo - it includes the authenticated peer ID and the self-reported ListenAddr. Note that the ListenAddr is not authenticated and may not match that address actually dialed if its an outbound peer.
func (NodeInfo) Validate ¶
Validate checks the self-reported NodeInfo is safe. It returns an error if there are too many Channels, if there are any duplicate Channels, if the ListenAddr is malformed, or if the ListenAddr is a host name that can not be resolved to some IP. TODO: constraints for Moniker/Other? Or is that for the UI ? JAE: It needs to be done on the client, but to prevent ambiguous unicode characters, maybe it's worth sanitizing it here. In the future we might want to validate these, once we have a name-resolution system up. International clients could then use punycode (or we could use url-encoding), and we just need to be careful with how we handle that in our clients. (e.g. off by default).
type NodeKey ¶
NodeKey is the persistent peer key. It contains the nodes private key for authentication.
type Peer ¶
type Peer interface { help.Service ID() ID // peer's cryptographic ID RemoteIP() net.IP // remote IP of the connection IsOutbound() bool // did we dial the peer IsPersistent() bool // do we redial this peer when we disconnect NodeInfo() NodeInfo // peer's info Status() tmconn.ConnectionStatus OriginalAddr() *NetAddress Send(byte, []byte) bool TrySend(byte, []byte) bool Set(string, interface{}) Get(string) interface{} }
Peer is an interface representing a peer connected on a reactor.
type PeerSet ¶
type PeerSet struct {
// contains filtered or unexported fields
}
PeerSet is a special structure for keeping a table of peers. Iteration over the peers is super fast and thread-safe.
func NewPeerSet ¶
func NewPeerSet() *PeerSet
NewPeerSet creates a new peerSet with a list of initial capacity of 256 items.
func (*PeerSet) Add ¶
Add adds the peer to the PeerSet. It returns an error carrying the reason, if the peer is already present.
func (*PeerSet) Get ¶
Get looks up a peer by the provided peerKey. Returns nil if peer is not found.
func (*PeerSet) Has ¶
Has returns true if the set contains the peer referred to by this peerKey, otherwise false.
func (*PeerSet) HasIP ¶
HasIP returns true if the set contains the peer referred to by this IP address, otherwise false.
type Reactor ¶
type Reactor interface { help.Service // Start, Stop // SetSwitch allows setting a switch. SetSwitch(*Switch) // GetChannels returns the list of channel descriptors. GetChannels() []*conn.ChannelDescriptor // AddPeer is called by the switch when a new peer is added. AddPeer(peer Peer) // RemovePeer is called by the switch when the peer is stopped (due to error // or other reason). RemovePeer(peer Peer, reason interface{}) // Receive is called when msgBytes is received from peer. // // NOTE reactor can not keep msgBytes around after Receive completes without // copying. // // CONTRACT: msgBytes are not nil. Receive(chID byte, peer Peer, msgBytes []byte) }
type Switch ¶
type Switch struct { help.BaseService // contains filtered or unexported fields }
Switch handles peer connections and exposes an API to receive incoming messages on `Reactors`. Each `Reactor` is responsible for handling incoming messages of one or more `Channels`. So while sending outgoing messages is typically performed on the peer, incoming messages are received on the reactor.
func NewSwitch ¶
func NewSwitch(cfg *config.P2PConfig, hasPeer help.PeerInValidators, options ...SwitchOption) *Switch
NewSwitch creates a new Switch with the given config.
func (*Switch) AddListener ¶
AddListener adds the given listener to the switch for listening to incoming peer connections. NOTE: Not goroutine safe.
func (*Switch) AddReactor ¶
AddReactor adds the given reactor to the switch. NOTE: Not goroutine safe.
func (*Switch) Broadcast ¶
Broadcast runs a go routine for each attempted send, which will block trying to send for defaultSendTimeoutSeconds. Returns a channel which receives success values for each attempted send (false if times out). Channel will be closed once msg bytes are sent to all peers (or time out).
NOTE: Broadcast uses goroutines, so order of broadcast may not be preserved.
func (*Switch) DialPeerWithAddress ¶
func (sw *Switch) DialPeerWithAddress(addr *NetAddress, persistent bool) error
DialPeerWithAddress dials the given peer and runs sw.addPeer if it connects and authenticates successfully. If `persistent == true`, the switch will always try to reconnect to this peer if the connection ever fails.
func (*Switch) DialPeersAsync ¶
DialPeersAsync dials a list of peers asynchronously in random order (optionally, making them persistent). Used to dial peers from config on startup or from unsafe-RPC (trusted sources). TODO: remove addrBook arg since it's now set on the switch
func (*Switch) FilterConnByAddr ¶
FilterConnByAddr returns an error if connecting to the given address is forbidden.
func (*Switch) FilterConnByID ¶
FilterConnByID returns an error if connecting to the given peer ID is forbidden.
func (*Switch) GetPeerForID ¶
func (*Switch) IsListening ¶
IsListening returns true if the switch has at least one listener. NOTE: Not goroutine safe.
func (*Switch) Listeners ¶
Listeners returns the list of listeners the switch listens on. NOTE: Not goroutine safe.
func (*Switch) MarkPeerAsGood ¶
MarkPeerAsGood marks the given peer as good when it did something useful like contributed to consensus.
func (*Switch) NumPeers ¶
NumPeers returns the count of outbound/inbound and outbound-dialing peers.
func (*Switch) OnStart ¶
OnStart implements BaseService. It starts all the reactors, peers, and listeners.
func (*Switch) OnStop ¶
func (sw *Switch) OnStop()
OnStop implements BaseService. It stops all listeners, peers, and reactors.
func (*Switch) Reactors ¶
Reactors returns a map of reactors registered on the switch. NOTE: Not goroutine safe.
func (*Switch) ReadPeerList ¶
func (sw *Switch) ReadPeerList()
func (*Switch) SetAddrBook ¶
SetAddrBook allows to set address book on Switch.
func (*Switch) SetAddrFilter ¶
SetAddrFilter sets the function for filtering connections by address.
func (*Switch) SetIDFilter ¶
SetIDFilter sets the function for filtering connections by peer ID.
func (*Switch) SetNodeInfo ¶
SetNodeInfo sets the switch's NodeInfo for checking compatibility and handshaking with other nodes. NOTE: Not goroutine safe.
func (*Switch) SetNodeKey ¶
SetNodeKey sets the switch's private key for authenticated encryption. NOTE: Not goroutine safe.
func (*Switch) StopPeerForError ¶
StopPeerForError disconnects from a peer due to external error. If the peer is persistent, it will attempt to reconnect. TODO: make record depending on reason.
func (*Switch) StopPeerGracefully ¶
StopPeerGracefully disconnects from a peer gracefully. TODO: handle graceful disconnects.
type SwitchOption ¶
type SwitchOption func(*Switch)
SwitchOption sets an optional parameter on the Switch.