Documentation ¶
Index ¶
- Constants
- Variables
- func ParseHostOrURL(addr string) (*url.URL, error)
- func SetUserAgentHeader(header http.Header)
- type ArrayPhonebook
- type ForwardingPolicy
- type GossipNode
- type HTTPPeer
- type HandlerFunc
- type IncomingMessage
- type LimitedReaderSlurper
- type MessageHandler
- type MultiPhonebook
- func (mp *MultiPhonebook) AddOrUpdatePhonebook(bootstrapNetworkName string, p Phonebook)
- func (mp *MultiPhonebook) GetAddresses(n int) []string
- func (mp *MultiPhonebook) GetPhonebook(bootstrapNetworkName string) (p Phonebook)
- func (mp *MultiPhonebook) UpdateRetryAfter(addr string, retryAfter time.Time)
- type Multiplexer
- type NetPrioScheme
- type OutgoingMessage
- type Peer
- type PeerOption
- type Phonebook
- type RequestLogger
- type RequestTracker
- func (rt *RequestTracker) Accept() (conn net.Conn, err error)
- func (rt *RequestTracker) Addr() net.Addr
- func (rt *RequestTracker) Close() error
- func (rt *RequestTracker) GetTrackedRequest(request *http.Request) (trackedRequest *TrackerRequest)
- func (rt *RequestTracker) Listener(listener net.Listener) net.Listener
- func (rt *RequestTracker) ServeHTTP(response http.ResponseWriter, request *http.Request)
- type Tag
- type TaggedMessageHandler
- type ThreadsafePhonebook
- func (p *ThreadsafePhonebook) ExtendPeerList(more []string)
- func (p *ThreadsafePhonebook) GetAddresses(n int) []string
- func (p *ThreadsafePhonebook) Length() int
- func (p *ThreadsafePhonebook) ReplacePeerList(they []string)
- func (p *ThreadsafePhonebook) UpdateRetryAfter(addr string, retryAfter time.Time)
- type TrackerRequest
- type UnicastPeer
- type WebsocketNetwork
- func (wn *WebsocketNetwork) Address() (string, bool)
- func (wn *WebsocketNetwork) Broadcast(ctx context.Context, tag protocol.Tag, data []byte, wait bool, except Peer) error
- func (wn *WebsocketNetwork) ClearHandlers()
- func (wn *WebsocketNetwork) Disconnect(node Peer)
- func (wn *WebsocketNetwork) DisconnectPeers()
- func (wn *WebsocketNetwork) GetPeers(options ...PeerOption) []Peer
- func (wn *WebsocketNetwork) NumPeers() int
- func (wn *WebsocketNetwork) PublicAddress() string
- func (wn *WebsocketNetwork) Ready() chan struct{}
- func (wn *WebsocketNetwork) RegisterHTTPHandler(path string, handler http.Handler)
- func (wn *WebsocketNetwork) RegisterHandlers(dispatch []TaggedMessageHandler)
- func (wn *WebsocketNetwork) Relay(ctx context.Context, tag protocol.Tag, data []byte, wait bool, except Peer) error
- func (wn *WebsocketNetwork) RequestConnectOutgoing(replace bool, quit <-chan struct{})
- func (wn *WebsocketNetwork) ServeHTTP(response http.ResponseWriter, request *http.Request)
- func (wn *WebsocketNetwork) SetPrioScheme(s NetPrioScheme)
- func (wn *WebsocketNetwork) Start()
- func (wn *WebsocketNetwork) Stop()
Constants ¶
const AddressHeader = "X-Algorand-Location"
AddressHeader HTTP header by which an inbound connection reports its public address
const GenesisHeader = "X-Algorand-Genesis"
GenesisHeader HTTP header for genesis id to make sure we're on the same chain
const GossipNetworkPath = "/v1/{genesisID}/gossip"
GossipNetworkPath is the URL path to connect to the websocket gossip node at. Contains {genesisID} param to be handled by gorilla/mux
const InstanceNameHeader = "X-Algorand-InstanceName"
InstanceNameHeader HTTP header by which an inbound connection reports an ID to distinguish multiple local nodes.
const MaxInt = int((^uint(0)) >> 1)
MaxInt is the maximum int which might be int32 or int64
const NodeRandomHeader = "X-Algorand-NodeRandom"
NodeRandomHeader HTTP header that a node uses to make sure it's not talking to itself
const PriorityChallengeHeader = "X-Algorand-PriorityChallenge"
PriorityChallengeHeader HTTP header informs a client about the challenge it should sign to increase network priority.
const ProtocolVersion = "1"
ProtocolVersion is the current version attached to the ProtocolVersionHeader header
const ProtocolVersionHeader = "X-Algorand-Version"
ProtocolVersionHeader HTTP header for protocol version. TODO: this may be unneeded redundance since we also have url versioning "/v1/..."
const TelemetryIDHeader = "X-Algorand-TelId"
TelemetryIDHeader HTTP header for telemetry-id for logging
const TooManyRequestsRetryAfterHeader = "Retry-After"
TooManyRequestsRetryAfterHeader HTTP header let the client know when to make the next connection attempt
const UserAgentHeader = "User-Agent"
UserAgentHeader is the HTTP header identify the user agent.
Variables ¶
var ErrIncomingMsgTooLarge = errors.New("read limit exceeded")
ErrIncomingMsgTooLarge is returned when an incoming message is too large
var HostColonPortPattern = regexp.MustCompile("^[^:]+:\\d+$")
HostColonPortPattern matches "^[^:]+:\\d+$" e.g. "foo.com.:1234"
Functions ¶
func ParseHostOrURL ¶
ParseHostOrURL handles "host:port" or a full URL. Standard library net/url.Parse chokes on "host:port".
func SetUserAgentHeader ¶
SetUserAgentHeader adds the User-Agent header to the provided heades map.
Types ¶
type ArrayPhonebook ¶
type ArrayPhonebook struct {
Entries phonebookEntries
}
ArrayPhonebook is a simple wrapper on a phonebookEntries map
func MakeArrayPhonebook ¶
func MakeArrayPhonebook() *ArrayPhonebook
MakeArrayPhonebook creates a ArrayPhonebook
func (*ArrayPhonebook) GetAddresses ¶
func (p *ArrayPhonebook) GetAddresses(n int) []string
GetAddresses returns up to N shuffled address
func (*ArrayPhonebook) UpdateRetryAfter ¶
func (p *ArrayPhonebook) UpdateRetryAfter(addr string, retryAfter time.Time)
UpdateRetryAfter updates the retry-after field for the entries matching the given address
type ForwardingPolicy ¶
type ForwardingPolicy int
ForwardingPolicy is an enum indicating to whom we should send a message
const ( // Ignore - discard (don't forward) Ignore ForwardingPolicy = iota // Disconnect - disconnect from the peer that sent this message Disconnect // Broadcast - forward to everyone (except the sender) Broadcast )
type GossipNode ¶
type GossipNode interface { Address() (string, bool) Broadcast(ctx context.Context, tag protocol.Tag, data []byte, wait bool, except Peer) error Relay(ctx context.Context, tag protocol.Tag, data []byte, wait bool, except Peer) error Disconnect(badnode Peer) DisconnectPeers() Ready() chan struct{} // RegisterHTTPHandler path accepts gorilla/mux path annotations RegisterHTTPHandler(path string, handler http.Handler) // RequestConnectOutgoing asks the system to actually connect to peers. // `replace` optionally drops existing connections before making new ones. // `quit` chan allows cancellation. TODO: use `context` RequestConnectOutgoing(replace bool, quit <-chan struct{}) // Get a list of Peers we could potentially send a direct message to. GetPeers(options ...PeerOption) []Peer // Start threads, listen on sockets. Start() // Close sockets. Stop threads. Stop() // RegisterHandlers adds to the set of given message handlers. RegisterHandlers(dispatch []TaggedMessageHandler) // ClearHandlers deregisters all the existing message handlers. ClearHandlers() }
GossipNode represents a node in the gossip network
func NewWebsocketGossipNode ¶
func NewWebsocketGossipNode(log logging.Logger, config config.Local, phonebook Phonebook, genesisID string, networkID protocol.NetworkID) (gn GossipNode, err error)
NewWebsocketGossipNode constructs a websocket network node and returns it as a GossipNode interface implementation
type HTTPPeer ¶
type HTTPPeer interface { GetAddress() string GetHTTPClient() *http.Client // PrepareURL takes a URL that may have substitution parameters in it and returns a URL with those parameters filled in. // E.g. /v1/{genesisID}/gossip -> /v1/1234/gossip PrepareURL(string) string }
HTTPPeer is what the opaque Peer might be. If you get an opaque Peer handle from a GossipNode, maybe try a .(HTTPPeer) type assertion on it.
type HandlerFunc ¶
type HandlerFunc func(message IncomingMessage) OutgoingMessage
HandlerFunc represents an implemenation of the MessageHandler interface
func (HandlerFunc) Handle ¶
func (f HandlerFunc) Handle(message IncomingMessage) OutgoingMessage
Handle implements MessageHandler.Handle, calling the handler with the IncomingKessage and returning the OutgoingMessage
type IncomingMessage ¶
type IncomingMessage struct { Sender Peer Tag Tag Data []byte Err error Net GossipNode // Received is time.Time.UnixNano() Received int64 // contains filtered or unexported fields }
IncomingMessage represents a message arriving from some peer in our p2p network
type LimitedReaderSlurper ¶
type LimitedReaderSlurper struct { // Limit is the maximum total bytes that may be read. Limit uint64 // contains filtered or unexported fields }
LimitedReaderSlurper collects bytes from an io.Reader, but stops if a limit is reached.
func (*LimitedReaderSlurper) Bytes ¶
func (s *LimitedReaderSlurper) Bytes() []byte
Bytes returns a copy of all the collected data
func (*LimitedReaderSlurper) Read ¶
func (s *LimitedReaderSlurper) Read(reader io.Reader) error
Read does repeated Read()s on the io.Reader until it gets io.EOF. Returns underlying error or ErrIncomingMsgTooLarge if limit reached. Returns a nil error if the underlying io.Reader returned io.EOF.
func (*LimitedReaderSlurper) Reset ¶
func (s *LimitedReaderSlurper) Reset()
Reset clears the buffered data
func (*LimitedReaderSlurper) Size ¶
func (s *LimitedReaderSlurper) Size() uint64
Size returs the current total size of contained chunks read from io.Reader
type MessageHandler ¶
type MessageHandler interface {
Handle(message IncomingMessage) OutgoingMessage
}
MessageHandler takes a IncomingMessage (e.g., vote, transaction), processes it, and returns what (if anything) to send to the network in response. The ForwardingPolicy field of the returned OutgoingMessage indicates whether to reply directly to the sender (unicast), propagate to everyone except the sender (broadcast), or do nothing (ignore).
type MultiPhonebook ¶
type MultiPhonebook struct {
// contains filtered or unexported fields
}
MultiPhonebook contains a map of phonebooks
func MakeMultiPhonebook ¶
func MakeMultiPhonebook() *MultiPhonebook
MakeMultiPhonebook constructs and returns a new Multi Phonebook
func (*MultiPhonebook) AddOrUpdatePhonebook ¶
func (mp *MultiPhonebook) AddOrUpdatePhonebook(bootstrapNetworkName string, p Phonebook)
AddOrUpdatePhonebook adds or updates Phonebook in Phonebook map
func (*MultiPhonebook) GetAddresses ¶
func (mp *MultiPhonebook) GetAddresses(n int) []string
GetAddresses returns up to N address
func (*MultiPhonebook) GetPhonebook ¶
func (mp *MultiPhonebook) GetPhonebook(bootstrapNetworkName string) (p Phonebook)
GetPhonebook retrieves a phonebook by it's name
func (*MultiPhonebook) UpdateRetryAfter ¶
func (mp *MultiPhonebook) UpdateRetryAfter(addr string, retryAfter time.Time)
UpdateRetryAfter updates the retry-after field for the entries matching the given address
type Multiplexer ¶
type Multiplexer struct {
// contains filtered or unexported fields
}
Multiplexer is a message handler that sorts incoming messages by Tag and passes them along to the relevant message handler for that type of message.
func MakeMultiplexer ¶
func MakeMultiplexer() *Multiplexer
MakeMultiplexer creates an empty Multiplexer
func (*Multiplexer) ClearHandlers ¶
func (m *Multiplexer) ClearHandlers()
ClearHandlers deregisters all the existing message handlers.
func (*Multiplexer) Handle ¶
func (m *Multiplexer) Handle(msg IncomingMessage) OutgoingMessage
Handle is the "input" side of the multiplexer. It dispatches the message to the previously defined handler.
func (*Multiplexer) RegisterHandlers ¶
func (m *Multiplexer) RegisterHandlers(dispatch []TaggedMessageHandler)
RegisterHandlers registers the set of given message handlers.
type NetPrioScheme ¶
type NetPrioScheme interface { NewPrioChallenge() string MakePrioResponse(challenge string) []byte VerifyPrioResponse(challenge string, response []byte) (basics.Address, error) GetPrioWeight(addr basics.Address) uint64 }
NetPrioScheme is an implementation of network connection priorities based on a challenge-response protocol.
type OutgoingMessage ¶
type OutgoingMessage struct { Action ForwardingPolicy Tag Tag Payload []byte }
OutgoingMessage represents a message we want to send.
func Propagate ¶
func Propagate(msg IncomingMessage) OutgoingMessage
Propagate is a convenience function to save typing in the common case of a message handler telling us to propagate an incoming message "return network.Propagate(msg)" instead of "return network.OutgoingMsg{network.Broadcast, msg.Tag, msg.Data}"
type PeerOption ¶
type PeerOption int
PeerOption allows users to specify a subset of peers to query
const ( // PeersConnectedOut specifies all peers with outgoing connections PeersConnectedOut PeerOption = iota // PeersConnectedIn specifies all peers with inbound connections PeersConnectedIn PeerOption = iota // PeersPhonebook specifies all peers in the phonebook PeersPhonebook PeerOption = iota )
type Phonebook ¶
type Phonebook interface { // GetAddresses(N) returns up to N addresses, but may return fewer GetAddresses(n int) []string // UpdateRetryAfter updates the retry-after field for the entries matching the given address UpdateRetryAfter(addr string, retryAfter time.Time) }
Phonebook stores or looks up addresses of nodes we might contact
type RequestLogger ¶
type RequestLogger struct {
// contains filtered or unexported fields
}
RequestLogger is a middleware helps logging all the incoming http requests. The intended use is to place it at the bottom of the http processing. It will capture the status codes set by the upsteam handlers and write the request info/response to the logger.
func (*RequestLogger) ServeHTTP ¶
func (rl *RequestLogger) ServeHTTP(writer http.ResponseWriter, request *http.Request)
this is the http entry point for the request logger.
func (*RequestLogger) SetStatusCode ¶
func (rl *RequestLogger) SetStatusCode(writer http.ResponseWriter, statusCode int)
SetStatusCode sets the status code of a given response writer without writing it to the underlaying writer object.
type RequestTracker ¶
type RequestTracker struct {
// contains filtered or unexported fields
}
RequestTracker tracks the incoming request connections
func (*RequestTracker) Accept ¶
func (rt *RequestTracker) Accept() (conn net.Conn, err error)
Accept waits for and returns the next connection to the listener.
func (*RequestTracker) Addr ¶
func (rt *RequestTracker) Addr() net.Addr
Addr returns the listener's network address.
func (*RequestTracker) Close ¶
func (rt *RequestTracker) Close() error
Close closes the listener. Any blocked Accept operations will be unblocked and return errors.
func (*RequestTracker) GetTrackedRequest ¶
func (rt *RequestTracker) GetTrackedRequest(request *http.Request) (trackedRequest *TrackerRequest)
GetTrackedRequest return the tracked request
func (*RequestTracker) Listener ¶
func (rt *RequestTracker) Listener(listener net.Listener) net.Listener
Listener initialize the underlaying listener, and return the request tracker wrapping listener
func (*RequestTracker) ServeHTTP ¶
func (rt *RequestTracker) ServeHTTP(response http.ResponseWriter, request *http.Request)
type TaggedMessageHandler ¶
type TaggedMessageHandler struct { Tag MessageHandler }
TaggedMessageHandler receives one type of broadcast messages
type ThreadsafePhonebook ¶
type ThreadsafePhonebook struct {
// contains filtered or unexported fields
}
ThreadsafePhonebook implements Phonebook interface
func MakeThreadsafePhonebook ¶
func MakeThreadsafePhonebook() *ThreadsafePhonebook
MakeThreadsafePhonebook creates a ThreadsafePhonebook
func (*ThreadsafePhonebook) ExtendPeerList ¶
func (p *ThreadsafePhonebook) ExtendPeerList(more []string)
ExtendPeerList adds unique addresses to this set of addresses
func (*ThreadsafePhonebook) GetAddresses ¶
func (p *ThreadsafePhonebook) GetAddresses(n int) []string
GetAddresses returns up to N shuffled address
func (*ThreadsafePhonebook) Length ¶
func (p *ThreadsafePhonebook) Length() int
Length returns the number of addrs contained
func (*ThreadsafePhonebook) ReplacePeerList ¶
func (p *ThreadsafePhonebook) ReplacePeerList(they []string)
ReplacePeerList merges a set of addresses with that passed in. new entries in they are being added existing items that aren't included in they are being removed matching entries don't change
func (*ThreadsafePhonebook) UpdateRetryAfter ¶
func (p *ThreadsafePhonebook) UpdateRetryAfter(addr string, retryAfter time.Time)
UpdateRetryAfter updates the retry-after field for the entries matching the given address
type TrackerRequest ¶
type TrackerRequest struct {
// contains filtered or unexported fields
}
TrackerRequest hold the tracking data associated with a single request.
type UnicastPeer ¶
type UnicastPeer interface { GetAddress() string // Unicast sends the given bytes to this specific peer. Does not wait for message to be sent. Unicast(ctx context.Context, data []byte, tag protocol.Tag) error }
UnicastPeer is another possible interface for the opaque Peer. It is possible that we can only initiate a connection to a peer over websockets.
type WebsocketNetwork ¶
type WebsocketNetwork struct { GenesisID string NetworkID protocol.NetworkID RandomID string // contains filtered or unexported fields }
WebsocketNetwork implements GossipNode
func NewWebsocketNetwork ¶
func NewWebsocketNetwork(log logging.Logger, config config.Local, phonebook Phonebook, genesisID string, networkID protocol.NetworkID) (wn *WebsocketNetwork, err error)
NewWebsocketNetwork constructor for websockets based gossip network
func (*WebsocketNetwork) Address ¶
func (wn *WebsocketNetwork) Address() (string, bool)
Address returns a string and whether that is a 'final' address or guessed. Part of GossipNode interface
func (*WebsocketNetwork) Broadcast ¶
func (wn *WebsocketNetwork) Broadcast(ctx context.Context, tag protocol.Tag, data []byte, wait bool, except Peer) error
Broadcast sends a message. If except is not nil then we will not send it to that neighboring Peer. if wait is true then the call blocks until the packet has actually been sent to all neighbors. TODO: add `priority` argument so that we don't have to guess it based on tag
func (*WebsocketNetwork) ClearHandlers ¶
func (wn *WebsocketNetwork) ClearHandlers()
ClearHandlers deregisters all the existing message handlers.
func (*WebsocketNetwork) Disconnect ¶
func (wn *WebsocketNetwork) Disconnect(node Peer)
Disconnect from a peer, probably due to protocol errors.
func (*WebsocketNetwork) DisconnectPeers ¶
func (wn *WebsocketNetwork) DisconnectPeers()
DisconnectPeers shuts down all connections
func (*WebsocketNetwork) GetPeers ¶
func (wn *WebsocketNetwork) GetPeers(options ...PeerOption) []Peer
GetPeers returns a snapshot of our Peer list, according to the specified options. Peers may be duplicated and refer to the same underlying node.
func (*WebsocketNetwork) NumPeers ¶
func (wn *WebsocketNetwork) NumPeers() int
NumPeers returns number of peers we connect to (all peers incoming and outbound).
func (*WebsocketNetwork) PublicAddress ¶
func (wn *WebsocketNetwork) PublicAddress() string
PublicAddress what we tell other nodes to connect to. Might be different than our locally percieved network address due to NAT/etc. Returns config "PublicAddress" if available, otherwise local addr.
func (*WebsocketNetwork) Ready ¶
func (wn *WebsocketNetwork) Ready() chan struct{}
Ready returns a chan that will be closed when we have a minimum number of peer connections active
func (*WebsocketNetwork) RegisterHTTPHandler ¶
func (wn *WebsocketNetwork) RegisterHTTPHandler(path string, handler http.Handler)
RegisterHTTPHandler path accepts gorilla/mux path annotations
func (*WebsocketNetwork) RegisterHandlers ¶
func (wn *WebsocketNetwork) RegisterHandlers(dispatch []TaggedMessageHandler)
RegisterHandlers registers the set of given message handlers.
func (*WebsocketNetwork) Relay ¶
func (wn *WebsocketNetwork) Relay(ctx context.Context, tag protocol.Tag, data []byte, wait bool, except Peer) error
Relay message
func (*WebsocketNetwork) RequestConnectOutgoing ¶
func (wn *WebsocketNetwork) RequestConnectOutgoing(replace bool, quit <-chan struct{})
RequestConnectOutgoing tries to actually do the connect to new peers. `replace` drop all connections first and find new peers.
func (*WebsocketNetwork) ServeHTTP ¶
func (wn *WebsocketNetwork) ServeHTTP(response http.ResponseWriter, request *http.Request)
ServerHTTP handles the gossip network functions over websockets
func (*WebsocketNetwork) SetPrioScheme ¶
func (wn *WebsocketNetwork) SetPrioScheme(s NetPrioScheme)
SetPrioScheme specifies the network priority scheme for a network node
func (*WebsocketNetwork) Start ¶
func (wn *WebsocketNetwork) Start()
Start makes network connections and threads
func (*WebsocketNetwork) Stop ¶
func (wn *WebsocketNetwork) Stop()
Stop closes network connections and stops threads. Stop blocks until all activity on this node is done.