Documentation ¶
Index ¶
- Constants
- func Connect2Switches(switches []*Switch, i, j int)
- func FuzzConnAfterFromConfig(conn net.Conn, d time.Duration, config *config.FuzzConnConfig) net.Conn
- func MConnConfig(cfg *config.P2PConfig) conn.MConnConfig
- func MakePoWTarget(difficulty, targetBits uint) []byte
- func MaxNodeInfoSize() int
- func NetAddressString(id ID, protocolHostPort string) string
- func NewConnSet() *connSet
- func StartSwitches(switches []*Switch) error
- type BaseReactor
- func (*BaseReactor) AddPeer(peer Peer)
- func (*BaseReactor) GetChannels() []*conn.ChannelDescriptor
- func (*BaseReactor) InitPeer(peer Peer) Peer
- func (*BaseReactor) Receive(chID byte, peer Peer, msgBytes []byte)
- func (*BaseReactor) RemovePeer(peer Peer, reason interface{})
- func (br *BaseReactor) SetSwitch(sw *Switch)
- type ChannelDescriptor
- type ConnFilterFunc
- type ConnSet
- type ConnectionStatus
- type CurrentlyDialingOrExistingAddressError
- type FilterTimeoutError
- type FuzzedConnection
- func (fc *FuzzedConnection) Close() error
- func (fc *FuzzedConnection) Config() *config.FuzzConnConfig
- func (fc *FuzzedConnection) LocalAddr() net.Addr
- func (fc *FuzzedConnection) Read(data []byte) (n int, err error)
- func (fc *FuzzedConnection) RemoteAddr() net.Addr
- func (fc *FuzzedConnection) SetDeadline(t time.Time) error
- func (fc *FuzzedConnection) SetReadDeadline(t time.Time) error
- func (fc *FuzzedConnection) SetWriteDeadline(t time.Time) error
- func (fc *FuzzedConnection) Write(data []byte) (n int, err error)
- type ID
- type IPResolver
- type IPeerSet
- type MultiplexTransport
- func (mt *MultiplexTransport) Accept(cfg peerConfig) (Peer, error)
- func (mt *MultiplexTransport) Cleanup(p Peer)
- func (mt *MultiplexTransport) Close() error
- func (mt *MultiplexTransport) Dial(addr NetAddress, cfg peerConfig) (Peer, error)
- func (mt *MultiplexTransport) Listen(addr NetAddress) error
- func (mt *MultiplexTransport) NetAddress() NetAddress
- type MultiplexTransportOption
- type NetAddress
- func CreateRoutableAddr() (addr string, netAddr *NetAddress)
- func NewNetAddress(id ID, addr net.Addr) *NetAddress
- func NewNetAddressFromIPPort(id ID, ip net.IP, port uint16) *NetAddress
- func NewNetAddressFromString(idaddr string) (*NetAddress, error)
- func NewNetAddressFromStrings(idaddrs []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) HasID() bool
- func (na *NetAddress) Local() bool
- func (na NetAddress) MarshalAmino() (string, error)
- 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) UnmarshalAmino(str string) (err error)
- func (na *NetAddress) Validate() error
- func (na *NetAddress) ValidateLocal() error
- type NetAddressInvalidError
- type NetAddressLookupError
- type NetAddressNoIDError
- type NodeInfo
- type NodeInfoOther
- type NodeKey
- type Peer
- type PeerFilterFunc
- type PeerOption
- type PeerSet
- type Reactor
- type RejectedError
- func (e RejectedError) Addr() NetAddress
- func (e RejectedError) Error() string
- func (e RejectedError) IsAuthFailure() bool
- func (e RejectedError) IsDuplicate() bool
- func (e RejectedError) IsFiltered() bool
- func (e RejectedError) IsIncompatible() bool
- func (e RejectedError) IsNodeInfoInvalid() bool
- func (e RejectedError) IsSelf() bool
- type Switch
- func (sw *Switch) AddPersistentPeers(addrs []string) error
- 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) error
- func (sw *Switch) DialPeersAsync(peers []string) error
- func (sw *Switch) IsDialingOrExistingAddress(addr *NetAddress) bool
- func (sw *Switch) MaxNumOutboundPeers() int
- func (sw *Switch) NetAddress() *NetAddress
- 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) RemoveReactor(name string, reactor Reactor)
- 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 SwitchAuthenticationFailureError
- type SwitchConnectToSelfError
- type SwitchDuplicatePeerIDError
- type SwitchDuplicatePeerIPError
- type SwitchOption
- type Transport
- type TransportClosedError
- type TransportLifecycle
Constants ¶
const TEST_HOST = "localhost"
Variables ¶
This section is empty.
Functions ¶
func Connect2Switches ¶
Connect2Switches will connect switches i and j via net.Pipe(). Blocks until a connection is established. NOTE: caller ensures i and j are within bounds.
func FuzzConnAfterFromConfig ¶
func FuzzConnAfterFromConfig( conn net.Conn, d time.Duration, config *config.FuzzConnConfig, ) net.Conn
FuzzConnAfterFromConfig creates a new FuzzedConnection from a config. Fuzzing starts when the duration elapses.
func MConnConfig ¶
func MConnConfig(cfg *config.P2PConfig) conn.MConnConfig
MConnConfig returns an MConnConfig with fields updated from the P2PConfig.
func MakePoWTarget ¶
MakePoWTarget returns the big-endian encoding of 2^(targetBits - difficulty) - 1. It can be used as a Proof of Work target. NOTE: targetBits must be a multiple of 8 and difficulty must be less than targetBits.
func NetAddressString ¶
NetAddressString returns id@addr. It strips the leading protocol from protocolHostPort if it exists.
func StartSwitches ¶
StartSwitches calls sw.Start() for each given switch. It returns the first encountered error.
Types ¶
type BaseReactor ¶
type BaseReactor struct { service.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) InitPeer ¶
func (*BaseReactor) InitPeer(peer Peer) Peer
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 ConnFilterFunc ¶
ConnFilterFunc to be implemented by filter hooks after a new connection has been established. The set of existing connections is passed along together with all resolved IPs for the new connection.
func ConnDuplicateIPFilter ¶
func ConnDuplicateIPFilter() ConnFilterFunc
ConnDuplicateIPFilter resolves and keeps all ips for an incoming connection and refuses new ones if they come from a known ip.
type ConnSet ¶
type ConnSet interface { Has(net.Conn) bool HasIP(net.IP) bool Set(net.Conn, []net.IP) Remove(net.Conn) RemoveAddr(net.Addr) }
ConnSet is a lookup table for connections and all their ips.
type ConnectionStatus ¶
type ConnectionStatus = conn.ConnectionStatus
type CurrentlyDialingOrExistingAddressError ¶
type CurrentlyDialingOrExistingAddressError struct {
Addr string
}
CurrentlyDialingOrExistingAddressError indicates that we're currently dialing this address or it belongs to an existing peer.
func (CurrentlyDialingOrExistingAddressError) Error ¶
func (e CurrentlyDialingOrExistingAddressError) Error() string
type FilterTimeoutError ¶
type FilterTimeoutError struct{}
FilterTimeoutError indicates that a filter operation timed out.
func (FilterTimeoutError) Error ¶
func (e FilterTimeoutError) Error() string
type FuzzedConnection ¶
type FuzzedConnection struct {
// contains filtered or unexported fields
}
FuzzedConnection wraps any net.Conn and depending on the mode either delays reads/writes or randomly drops reads/writes/connections.
func (*FuzzedConnection) Close ¶
func (fc *FuzzedConnection) Close() error
Close implements net.Conn.
func (*FuzzedConnection) Config ¶
func (fc *FuzzedConnection) Config() *config.FuzzConnConfig
Config returns the connection's config.
func (*FuzzedConnection) LocalAddr ¶
func (fc *FuzzedConnection) LocalAddr() net.Addr
LocalAddr implements net.Conn.
func (*FuzzedConnection) Read ¶
func (fc *FuzzedConnection) Read(data []byte) (n int, err error)
Read implements net.Conn.
func (*FuzzedConnection) RemoteAddr ¶
func (fc *FuzzedConnection) RemoteAddr() net.Addr
RemoteAddr implements net.Conn.
func (*FuzzedConnection) SetDeadline ¶
func (fc *FuzzedConnection) SetDeadline(t time.Time) error
SetDeadline implements net.Conn.
func (*FuzzedConnection) SetReadDeadline ¶
func (fc *FuzzedConnection) SetReadDeadline(t time.Time) error
SetReadDeadline implements net.Conn.
func (*FuzzedConnection) SetWriteDeadline ¶
func (fc *FuzzedConnection) SetWriteDeadline(t time.Time) error
SetWriteDeadline implements net.Conn.
type IPResolver ¶
IPResolver is a behaviour subset of net.Resolver.
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 MultiplexTransport ¶
type MultiplexTransport struct {
// contains filtered or unexported fields
}
MultiplexTransport accepts and dials tcp connections and upgrades them to multiplexed peers.
func NewMultiplexTransport ¶
func NewMultiplexTransport( nodeInfo NodeInfo, nodeKey NodeKey, mConfig conn.MConnConfig, ) *MultiplexTransport
NewMultiplexTransport returns a tcp connected multiplexed peer.
func (*MultiplexTransport) Accept ¶
func (mt *MultiplexTransport) Accept(cfg peerConfig) (Peer, error)
Accept implements Transport.
func (*MultiplexTransport) Cleanup ¶
func (mt *MultiplexTransport) Cleanup(p Peer)
Cleanup removes the given address from the connections set and closes the connection.
func (*MultiplexTransport) Close ¶
func (mt *MultiplexTransport) Close() error
Close implements TransportLifecycle.
func (*MultiplexTransport) Dial ¶
func (mt *MultiplexTransport) Dial( addr NetAddress, cfg peerConfig, ) (Peer, error)
Dial implements Transport.
func (*MultiplexTransport) Listen ¶
func (mt *MultiplexTransport) Listen(addr NetAddress) error
Listen implements TransportLifecycle.
func (*MultiplexTransport) NetAddress ¶
func (mt *MultiplexTransport) NetAddress() NetAddress
NetAddress implements Transport.
type MultiplexTransportOption ¶
type MultiplexTransportOption func(*MultiplexTransport)
MultiplexTransportOption sets an optional parameter on the MultiplexTransport.
func MultiplexTransportConnFilters ¶
func MultiplexTransportConnFilters( filters ...ConnFilterFunc, ) MultiplexTransportOption
MultiplexTransportConnFilters sets the filters for rejection new connections.
func MultiplexTransportFilterTimeout ¶
func MultiplexTransportFilterTimeout( timeout time.Duration, ) MultiplexTransportOption
MultiplexTransportFilterTimeout sets the timeout waited for filter calls to return.
func MultiplexTransportResolver ¶
func MultiplexTransportResolver(resolver IPResolver) MultiplexTransportOption
MultiplexTransportResolver sets the Resolver used for ip lookups, defaults to net.DefaultResolver.
type NetAddress ¶
type NetAddress struct { ID ID `json:"id"` // authenticated identifier (TODO) IP net.IP `json:"ip"` // part of "addr" Port uint16 `json:"port"` // part of "addr" // contains filtered or unexported fields }
NetAddress defines information about a peer on the network including its Address, IP address, and port. NOTE: NetAddress is not meant to be mutated due to memoization. @amino2: immutable XXX
func CreateRoutableAddr ¶
func CreateRoutableAddr() (addr string, netAddr *NetAddress)
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. Panics if ID is invalid. TODO: socks proxies?
func NewNetAddressFromIPPort ¶
func NewNetAddressFromIPPort(id ID, ip net.IP, port uint16) *NetAddress
NewNetAddressFromIPPort returns a new NetAddress using the provided IP and port number.
func NewNetAddressFromString ¶
func NewNetAddressFromString(idaddr string) (*NetAddress, error)
NewNetAddressFromString 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 NewNetAddressFromStrings ¶
func NewNetAddressFromStrings(idaddrs []string) ([]*NetAddress, []error)
NewNetAddressFromStrings 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) HasID ¶
func (na *NetAddress) HasID() bool
HasID returns true if the address has an ID. NOTE: It does not check whether the ID is valid or not.
func (*NetAddress) Local ¶
func (na *NetAddress) Local() bool
Local returns true if it is a local address.
func (NetAddress) MarshalAmino ¶
func (na NetAddress) MarshalAmino() (string, error)
Needed because (a) IP doesn't encode, and (b) the intend of this type is to serialize to a string anyways.
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) UnmarshalAmino ¶
func (na *NetAddress) UnmarshalAmino(str string) (err error)
func (*NetAddress) Validate ¶
func (na *NetAddress) Validate() error
func (*NetAddress) ValidateLocal ¶
func (na *NetAddress) ValidateLocal() error
type NetAddressInvalidError ¶
func (NetAddressInvalidError) Error ¶
func (e NetAddressInvalidError) Error() string
type NetAddressLookupError ¶
func (NetAddressLookupError) Error ¶
func (e NetAddressLookupError) Error() string
type NetAddressNoIDError ¶
type NetAddressNoIDError struct {
Addr string
}
func (NetAddressNoIDError) Error ¶
func (e NetAddressNoIDError) Error() string
type NodeInfo ¶
type NodeInfo struct { // Set of protocol versions VersionSet versionset.VersionSet `json:"version_set"` // Authenticate NetAddress *NetAddress `json:"net_address"` // Check compatibility. // Channels are HexBytes so easier to read as JSON Network string `json:"network"` // network/chain ID Software string `json:"software"` // name of immediate software Version string `json:"version"` // software major.minor.revision Channels []byte `json:"channels"` // channels this node knows about // ASCIIText fields Moniker string `json:"moniker"` // arbitrary moniker Other NodeInfoOther `json:"other"` // other application specific data }
NodeInfo is the basic node information exchanged between two peers during the Tendermint P2P handshake.
func (NodeInfo) CompatibleWith ¶
CompatibleWith checks if two NodeInfo are compatible with eachother. CONTRACT: two nodes are compatible if the Block version and network match and they have at least one channel in common.
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 NodeInfoOther ¶
type NodeInfoOther struct { TxIndex string `json:"tx_index"` RPCAddress string `json:"rpc_address"` }
NodeInfoOther is the misc. application specific data
type NodeKey ¶
NodeKey is the persistent peer key. It contains the nodes private key for authentication. NOTE: keep in sync with gno.land/cmd/gnoland/secrets.go
func LoadNodeKey ¶
func LoadOrGenNodeKey ¶
LoadOrGenNodeKey attempts to load the NodeKey from the given filePath. If the file does not exist, it generates and saves a new NodeKey.
type Peer ¶
type Peer interface { service.Service FlushStop() ID() ID // peer's cryptographic ID RemoteIP() net.IP // remote IP of the connection RemoteAddr() net.Addr // remote address of the connection IsOutbound() bool // did we dial the peer IsPersistent() bool // do we redial this peer when we disconnect CloseConn() error // close original connection NodeInfo() NodeInfo // peer's info Status() connm.ConnectionStatus SocketAddr() *NetAddress // actual address of the socket 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 PeerFilterFunc ¶
PeerFilterFunc to be implemented by filter hooks after a new Peer has been fully setup.
type PeerOption ¶
type PeerOption func(*peer)
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 { service.Service // Start, Stop // SetSwitch allows setting a switch. SetSwitch(*Switch) // GetChannels returns the list of MConnection.ChannelDescriptor. Make sure // that each ID is unique across all the reactors added to the switch. GetChannels() []*conn.ChannelDescriptor // InitPeer is called by the switch before the peer is started. Use it to // initialize data for the peer (e.g. peer state). // // NOTE: The switch won't call AddPeer nor RemovePeer if it fails to start // the peer. Do not store any data associated with the peer in the reactor // itself unless you don't want to have a state, which is never cleaned up. InitPeer(peer Peer) Peer // AddPeer is called by the switch after the peer is added and successfully // started. Use it to start goroutines communicating with the peer. 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 by the switch when msgBytes is received from the 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) }
Reactor is responsible for handling incoming messages on one or more Channel. Switch calls GetChannels when reactor is added to it. When a new peer joins our node, InitPeer and AddPeer are called. RemovePeer is called when the peer is stopped. Receive is called when a message is received on a channel associated with this reactor.
Peer#Send or Peer#TrySend should be used to send the message to a peer.
type RejectedError ¶
type RejectedError struct {
// contains filtered or unexported fields
}
RejectedError indicates that a Peer was rejected carrying additional information as to the reason.
func (RejectedError) Addr ¶
func (e RejectedError) Addr() NetAddress
Addr returns the NetAddress for the rejected Peer.
func (RejectedError) Error ¶
func (e RejectedError) Error() string
func (RejectedError) IsAuthFailure ¶
func (e RejectedError) IsAuthFailure() bool
IsAuthFailure when Peer authentication was unsuccessful.
func (RejectedError) IsDuplicate ¶
func (e RejectedError) IsDuplicate() bool
IsDuplicate when Peer ID or IP are present already.
func (RejectedError) IsFiltered ¶
func (e RejectedError) IsFiltered() bool
IsFiltered when Peer ID or IP was filtered.
func (RejectedError) IsIncompatible ¶
func (e RejectedError) IsIncompatible() bool
IsIncompatible when Peer NodeInfo is not compatible with our own.
func (RejectedError) IsNodeInfoInvalid ¶
func (e RejectedError) IsNodeInfoInvalid() bool
IsNodeInfoInvalid when the sent NodeInfo is not valid.
func (RejectedError) IsSelf ¶
func (e RejectedError) IsSelf() bool
IsSelf when Peer is our own node.
type Switch ¶
type Switch struct { service.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 MakeConnectedSwitches ¶
func MakeConnectedSwitches(cfg *config.P2PConfig, n int, initSwitch func(int, *Switch) *Switch, connect func([]*Switch, int, int)) []*Switch
MakeConnectedSwitches returns n switches, connected according to the connect func. If connect==Connect2Switches, the switches will be fully connected. initSwitch defines how the i'th switch should be initialized (ie. with what reactors). NOTE: panics if any switch fails to start.
func MakeSwitch ¶
func NewSwitch ¶
func NewSwitch( cfg *config.P2PConfig, transport Transport, options ...SwitchOption, ) *Switch
NewSwitch creates a new Switch with the given config.
func (*Switch) AddPersistentPeers ¶
AddPersistentPeers allows you to set persistent peers. It ignores NetAddressLookupError. However, if there are other errors, first encounter is returned.
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) error
DialPeerWithAddress dials the given peer and runs sw.addPeer if it connects and authenticates successfully. If we're currently dialing this address or it belongs to an existing peer, CurrentlyDialingOrExistingAddressError is returned.
func (*Switch) DialPeersAsync ¶
DialPeersAsync dials a list of peers asynchronously in random order. Used to dial peers from config on startup or from unsafe-RPC (trusted sources). It ignores NetAddressLookupError. However, if there are other errors, first encounter is returned. Nop if there are no peers.
func (*Switch) IsDialingOrExistingAddress ¶
func (sw *Switch) IsDialingOrExistingAddress(addr *NetAddress) bool
IsDialingOrExistingAddress returns true if switch has a peer with the given address or dialing it at the moment.
func (*Switch) MaxNumOutboundPeers ¶
MaxNumOutboundPeers returns a maximum number of outbound peers.
func (*Switch) NetAddress ¶
func (sw *Switch) NetAddress() *NetAddress
NetAddress returns the address the switch is listening on.
func (*Switch) NumPeers ¶
NumPeers returns the count of outbound/inbound and outbound-dialing peers.
func (*Switch) OnStop ¶
func (sw *Switch) OnStop()
OnStop implements BaseService. It stops all peers and reactors.
func (*Switch) Reactors ¶
Reactors returns a map of reactors registered on the switch. NOTE: Not goroutine safe.
func (*Switch) RemoveReactor ¶
RemoveReactor removes the given Reactor from the Switch. NOTE: Not goroutine safe.
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 SwitchAuthenticationFailureError ¶
type SwitchAuthenticationFailureError struct { Dialed *NetAddress Got ID }
func (SwitchAuthenticationFailureError) Error ¶
func (e SwitchAuthenticationFailureError) Error() string
type SwitchConnectToSelfError ¶
type SwitchConnectToSelfError struct {
Addr *NetAddress
}
SwitchConnectToSelfError to be raised when trying to connect to itself.
func (SwitchConnectToSelfError) Error ¶
func (e SwitchConnectToSelfError) Error() string
type SwitchDuplicatePeerIDError ¶
type SwitchDuplicatePeerIDError struct {
ID ID
}
SwitchDuplicatePeerIDError to be raised when a peer is connecting with a known ID.
func (SwitchDuplicatePeerIDError) Error ¶
func (e SwitchDuplicatePeerIDError) Error() string
type SwitchDuplicatePeerIPError ¶
SwitchDuplicatePeerIPError to be raised when a peer is connecting with a known IP.
func (SwitchDuplicatePeerIPError) Error ¶
func (e SwitchDuplicatePeerIPError) Error() string
type SwitchOption ¶
type SwitchOption func(*Switch)
SwitchOption sets an optional parameter on the Switch.
func SwitchFilterTimeout ¶
func SwitchFilterTimeout(timeout time.Duration) SwitchOption
SwitchFilterTimeout sets the timeout used for peer filters.
func SwitchPeerFilters ¶
func SwitchPeerFilters(filters ...PeerFilterFunc) SwitchOption
SwitchPeerFilters sets the filters for rejection of new peers.
type Transport ¶
type Transport interface { // Listening address. NetAddress() NetAddress // Accept returns a newly connected Peer. Accept(peerConfig) (Peer, error) // Dial connects to the Peer for the address. Dial(NetAddress, peerConfig) (Peer, error) // Cleanup any resources associated with Peer. Cleanup(Peer) }
Transport emits and connects to Peers. The implementation of Peer is left to the transport. Each transport is also responsible to filter establishing peers specific to its domain.
type TransportClosedError ¶
type TransportClosedError struct{}
TransportClosedError is raised when the Transport has been closed.
func (TransportClosedError) Error ¶
func (e TransportClosedError) Error() string
type TransportLifecycle ¶
type TransportLifecycle interface { Close() error Listen(NetAddress) error }
TransportLifecycle bundles the methods for callers to control start and stop behaviour.