net

package
v0.8.5 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2021 License: MIT Imports: 16 Imported by: 2

Documentation

Overview

Package net implements different transports to communicate between Babble nodes.

This package contains various implementations of the Transport interface, which is used by Babble nodes to send and receive RPC requests (SyncRequest, EagerSyncRequest, JoinRequest, etc.). There are three implementations:

- Inmem: in-memory transport used only for testing

- TCP: communicating over plain TCP

- WebRTC: using WebRTC

TCP

The TCP transport is suitable when nodes are in the same local network, or when users are able to configure their connections appropriately to avoid NAT issues.

To use a TCP transport, set the following configuration options in the Babble Config object (cf config package):

- BinAdddr: the IP:PORT of the TCP socket that Babble binds to.

- AdvertiseAddr: (optional) The address that is advertised to other nodes. If BindAddr is a local address not reachable by other peers, it is usefull to set AdvertiseAddr to the reachable public address.

WebRTC

Because Babble is a peer-to-peer application, it can run into issues with NATs and firewalls. The WebRTC transport addresses the NAT traversal issue, but it requires centralised servers for peers to exchange connection information and to provide STUN/TURN services.

To use a WebRTC transport, use the following properties in the Config object:

- WebRTC: tells Babble to use a WebRTC transport

- SignalAddr: address of the WebRTC signaling server

- SignalRealm: routing domain within the signaling server

WebRTC requires a signaling mechanism for peers to exchange connection information. Usually, this would be implemented in a centralized server. So when the WebRTC transport is used, Babble is not fully p2p anymore. That being said, all the computation and data at the application layer remains p2p; the signaling server is only used as a sort of peer-discovery mechanism.

The WebRTCTransport ignores the BindAddr and AdvertiseAddr configuration values, but it requires the address of the signaling server. This address is specified by the SignalAddr configuration value. The SignalRealm defines a domain within the signaling server, such that signaling messages are only routed withing this domain.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTransportShutdown is returned when operations on a transport are
	// invoked after it's been terminated.
	ErrTransportShutdown = errors.New("transport shutdown")
)

Functions

This section is empty.

Types

type EagerSyncRequest

type EagerSyncRequest struct {
	FromID uint32
	Events []hashgraph.WireEvent
}

EagerSyncRequest corresponds to the push part of the pull-push gossip protocol. It is used to actively push Events to a node without it being requested.

type EagerSyncResponse

type EagerSyncResponse struct {
	FromID  uint32
	Success bool
}

EagerSyncResponse indicates the success or failure of an EagerSyncRequest.

type FastForwardRequest

type FastForwardRequest struct {
	FromID uint32
}

FastForwardRequest is used to request a Block, Frame, and Snapshot, from which to fast-forward.

type FastForwardResponse

type FastForwardResponse struct {
	FromID   uint32
	Block    hashgraph.Block
	Frame    hashgraph.Frame
	Snapshot []byte
}

FastForwardResponse encapsulates the response to a FastForwardRequest.

type InmemTransport

type InmemTransport struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

InmemTransport implements the Transport interface to allow testing Babble internally.

func NewInmemTransport

func NewInmemTransport(addr string) (string, *InmemTransport)

NewInmemTransport is used to initialize a new InmemTransport and generates a random local address if none is specified

func (*InmemTransport) AdvertiseAddr added in v0.5.12

func (i *InmemTransport) AdvertiseAddr() string

AdvertiseAddr implements the Transport interface.

func (*InmemTransport) Close

func (i *InmemTransport) Close() error

Close is used to permanently disable the transport

func (*InmemTransport) Connect

func (i *InmemTransport) Connect(peer string, t Transport)

Connect is used to connect this transport to another transport for a given peer name. This allows for local routing.

func (*InmemTransport) Consumer

func (i *InmemTransport) Consumer() <-chan RPC

Consumer implements the Transport interface.

func (*InmemTransport) Disconnect

func (i *InmemTransport) Disconnect(peer string)

Disconnect is used to remove the ability to route to a given peer.

func (*InmemTransport) DisconnectAll

func (i *InmemTransport) DisconnectAll()

DisconnectAll is used to remove all routes to peers.

func (*InmemTransport) EagerSync

func (i *InmemTransport) EagerSync(target string, args *EagerSyncRequest, resp *EagerSyncResponse) error

EagerSync implements the Transport interface.

func (*InmemTransport) FastForward

func (i *InmemTransport) FastForward(target string, args *FastForwardRequest, resp *FastForwardResponse) error

FastForward implements the Transport interface.

func (*InmemTransport) Join added in v0.5.0

func (i *InmemTransport) Join(target string, args *JoinRequest, resp *JoinResponse) error

Join implements the Transport interface

func (*InmemTransport) Listen added in v0.5.9

func (i *InmemTransport) Listen()

Listen is an empty function as there is no need to defer initialisation of the InmemTransport

func (*InmemTransport) LocalAddr

func (i *InmemTransport) LocalAddr() string

LocalAddr implements the Transport interface.

func (*InmemTransport) Sync

func (i *InmemTransport) Sync(target string, args *SyncRequest, resp *SyncResponse) error

Sync implements the Transport interface.

type JoinRequest added in v0.5.0

type JoinRequest struct {
	InternalTransaction hashgraph.InternalTransaction
}

JoinRequest is used to submit an InternalTransaction to join a Babble group.

type JoinResponse added in v0.5.0

type JoinResponse struct {
	FromID        uint32
	Accepted      bool
	AcceptedRound int
	Peers         []*peers.Peer
}

JoinResponse contains the response to a JoinRequest.

type NetworkTransport

type NetworkTransport struct {
	// contains filtered or unexported fields
}

NetworkTransport provides a network based transport that can be used to communicate with babble on remote machines. It requires an underlying stream layer to provide a stream abstraction, which can be simple TCP, or WebRTC.

This transport is very simple and lightweight. Each RPC request is framed by sending a byte that indicates the message type, followed by the json encoded request.

The response is an error string followed by the response object, both are encoded using JSON

func NewNetworkTransport

func NewNetworkTransport(
	stream StreamLayer,
	maxPool int,
	timeout time.Duration,
	joinTimeout time.Duration,
	logger *logrus.Entry,
) *NetworkTransport

NewNetworkTransport creates a new network transport with the given StreamLayer. The maxPool controls how many connections we will pool (per target). The timeout is used to apply I/O deadlines.

func NewTCPTransport

func NewTCPTransport(
	bindAddr string,
	advertise string,
	maxPool int,
	timeout time.Duration,
	joinTimeout time.Duration,
	logger *logrus.Entry,
) (*NetworkTransport, error)

NewTCPTransport returns a NetworkTransport that is built on top of a TCP StreamLayer.

func NewWebRTCTransport added in v0.8.0

func NewWebRTCTransport(
	signal signal.Signal,
	iceServers []webrtc.ICEServer,
	maxPool int,
	timeout time.Duration,
	joinTimeout time.Duration,
	logger *logrus.Entry,
) (*NetworkTransport, error)

NewWebRTCTransport returns a NetworkTransport that is built on top of a WebRTC StreamLayer. The signal is a mechanism for peers to exchange connection information prior to establishing a direct p2p link.

func (*NetworkTransport) AdvertiseAddr added in v0.5.12

func (n *NetworkTransport) AdvertiseAddr() string

AdvertiseAddr implements the Transport interface.

func (*NetworkTransport) Close

func (n *NetworkTransport) Close() error

Close is used to stop the network transport.

func (*NetworkTransport) Consumer

func (n *NetworkTransport) Consumer() <-chan RPC

Consumer implements the Transport interface.

func (*NetworkTransport) EagerSync

func (n *NetworkTransport) EagerSync(target string, args *EagerSyncRequest, resp *EagerSyncResponse) error

EagerSync implements the Transport interface.

func (*NetworkTransport) FastForward

func (n *NetworkTransport) FastForward(target string, args *FastForwardRequest, resp *FastForwardResponse) error

FastForward implements the Transport interface.

func (*NetworkTransport) IsShutdown

func (n *NetworkTransport) IsShutdown() bool

IsShutdown is used to check if the transport is shutdown.

func (*NetworkTransport) Join added in v0.5.0

func (n *NetworkTransport) Join(target string, args *JoinRequest, resp *JoinResponse) error

Join implements the Transport interface.

func (*NetworkTransport) Listen added in v0.5.9

func (n *NetworkTransport) Listen()

Listen opens the stream and handles incoming connections.

func (*NetworkTransport) LocalAddr

func (n *NetworkTransport) LocalAddr() string

LocalAddr implements the Transport interface.

func (*NetworkTransport) Sync

func (n *NetworkTransport) Sync(target string, args *SyncRequest, resp *SyncResponse) error

Sync implements the Transport interface.

type RPC

type RPC struct {
	Command  interface{}
	RespChan chan<- RPCResponse
}

RPC encapsulates an RPC request and provides a response mechanism.

func (*RPC) Respond

func (r *RPC) Respond(resp interface{}, err error)

Respond is used to respond with a response, error or both.

type RPCResponse

type RPCResponse struct {
	Response interface{}
	Error    error
}

RPCResponse captures both a response and a potential error.

type StreamLayer

type StreamLayer interface {
	net.Listener

	// Dial is used to create a new outgoing connection.
	Dial(address string, timeout time.Duration) (net.Conn, error)

	// AdvertiseAddr returns the publicly-reachable address of the stream.
	AdvertiseAddr() string
}

StreamLayer is used with the NetworkTransport to provide the low level stream abstraction.

type SyncRequest

type SyncRequest struct {
	FromID    uint32
	Known     map[uint32]int
	SyncLimit int
}

SyncRequest corresponds to the pull part of the pull-push gossip protocol. It is used to retrieve unknown Events from another node. The Known map represents how much the requester currently knows about the hashgraph. The SyncLimit indicates the max number of Events to include in the response.

type SyncResponse

type SyncResponse struct {
	FromID uint32
	Events []hashgraph.WireEvent
	Known  map[uint32]int
}

SyncResponse returns a list of Events as requested by a SyncRequest. The known map indicates how much the responder knows about the hashgraph. Events are encoded in light-weight wire format to take less space.

type Transport

type Transport interface {

	// Starts the transport listening
	Listen()

	// Consumer returns a channel that can be used to consume and respond to RPC
	// requests.
	Consumer() <-chan RPC

	// LocalAddr is used to return our local address
	LocalAddr() string

	// AdvertiseAddr is used to return our advertise address where other peers
	// can reach us
	AdvertiseAddr() string

	Sync(target string, args *SyncRequest, resp *SyncResponse) error

	EagerSync(target string, args *EagerSyncRequest, resp *EagerSyncResponse) error

	FastForward(target string, args *FastForwardRequest, resp *FastForwardResponse) error

	Join(target string, args *JoinRequest, resp *JoinResponse) error

	// Close permanently closes a transport, stopping any associated goroutines
	// and freeing other resources.
	Close() error
}

Transport provides an interface for network transports to allow a node to communicate with other nodes.

Directories

Path Synopsis
Package signal defines an interface for WebRTC signaling mechanisms used by the WebRTCTransport.
Package signal defines an interface for WebRTC signaling mechanisms used by the WebRTCTransport.
wamp
Package wamp implements a WebRTC signaling system using RPC over WebSockets.
Package wamp implements a WebRTC signaling system using RPC over WebSockets.

Jump to

Keyboard shortcuts

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