network

package
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2019 License: AGPL-3.0 Imports: 36 Imported by: 0

Documentation

Index

Constants

View Source
const AddressHeader = "X-Algorand-Location"

AddressHeader HTTP header by which an inbound connection reports its public address

View Source
const GenesisHeader = "X-Algorand-Genesis"

GenesisHeader HTTP header for genesis id to make sure we're on the same chain

View Source
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

View Source
const InstanceNameHeader = "X-Algorand-InstanceName"

InstanceNameHeader HTTP header by which an inbound connection reports an ID to distinguish multiple local nodes.

View Source
const MaxInt = int((^uint(0)) >> 1)

MaxInt is the maximum int which might be int32 or int64

View Source
const NodeRandomHeader = "X-Algorand-NodeRandom"

NodeRandomHeader HTTP header that a node uses to make sure it's not talking to itself

View Source
const PriorityChallengeHeader = "X-Algorand-PriorityChallenge"

PriorityChallengeHeader HTTP header informs a client about the challenge it should sign to increase network priority.

View Source
const ProtocolVersion = "1"

ProtocolVersion is the current version attached to the ProtocolVersionHeader header

View Source
const ProtocolVersionHeader = "X-Algorand-Version"

ProtocolVersionHeader HTTP header for protocol version. TODO: this may be unneeded redundance since we also have url versioning "/v1/..."

View Source
const TelemetryIDHeader = "X-Algorand-TelId"

TelemetryIDHeader HTTP header for telemetry-id for logging

View Source
const TooManyRequestsRetryAfterHeader = "Retry-After"

TooManyRequestsRetryAfterHeader HTTP header let the client know when to make the next connection attempt

View Source
const UserAgentHeader = "User-Agent"

UserAgentHeader is the HTTP header identify the user agent.

Variables

View Source
var ErrIncomingMsgTooLarge = errors.New("read limit exceeded")

ErrIncomingMsgTooLarge is returned when an incoming message is too large

View Source
var HostColonPortPattern = regexp.MustCompile("^[^:]+:\\d+$")

HostColonPortPattern matches "^[^:]+:\\d+$" e.g. "foo.com.:1234"

Functions

func ParseHostOrURL

func ParseHostOrURL(addr string) (*url.URL, error)

ParseHostOrURL handles "host:port" or a full URL. Standard library net/url.Parse chokes on "host:port".

func SetUserAgentHeader

func SetUserAgentHeader(header http.Header)

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

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 Peer

type Peer interface{}

Peer opaque interface for referring to a neighbor in the network

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 Tag

type Tag = protocol.Tag

Tag is a short string (2 bytes) marking a type of message

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL