network

package
v0.0.0-...-8aa4d74 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2019 License: GPL-3.0 Imports: 42 Imported by: 1

Documentation

Overview

Provides test utility to mock a network in unittests

Index

Constants

View Source
const (
	RouterNameNode   = "node"
	RouterNameAPI    = "api"
	RouterNameMetric = "metrics"
	RouterNameDebug  = "debug"
)

Variables

View Source
var (
	UrlPathPrefixNode   = fmt.Sprintf("/%s", RouterNameNode)
	UrlPathPrefixAPI    = fmt.Sprintf("/%s", RouterNameAPI)
	UrlPathPrefixDebug  = fmt.Sprintf("/%s", RouterNameDebug)
	UrlPathPrefixMetric = fmt.Sprintf("/%s", RouterNameMetric)
	UrlPathPrefixes     = []string{
		UrlPathPrefixNode,
		UrlPathPrefixAPI,
		UrlPathPrefixDebug,
		UrlPathPrefixMetric,
	}
)
View Source
var HeaderKeyFiltered []string = []string{
	"Content-Length",
	"Content-Type",
	"Accept",
	"Accept-Encoding",
	"User-Agent",
}
View Source
var VerboseLogs bool

Functions

func CreateNewMemoryEndpoint

func CreateNewMemoryEndpoint() *common.Endpoint

func GenerateKey

func GenerateKey(dirPath, certPath, keyPath string)

func RateLimitMiddleware

func RateLimitMiddleware(logger logging.Logger, rule common.RateLimitRule) mux.MiddlewareFunc

RateLimitMiddleware throttles the incoming requests; if `Limit` is 0, there will be no limit.

func RecoverMiddleware

func RecoverMiddleware(logger logging.Logger) mux.MiddlewareFunc

func SetHTTPLogging

func SetHTTPLogging(level logging.Lvl, handler logging.Handler)

func SetLogging

func SetLogging(level logging.Lvl, handler logging.Handler)

Types

type ConnectionManager

type ConnectionManager interface {
	GetConnection(string) NetworkClient
	Broadcast(common.Message)
	Start()
	AllConnected() []string
	AllValidators() []string
	CountConnected() int
	IsReady() bool
	Discovery(DiscoveryMessage) error
}

func NewValidatorConnectionManager

func NewValidatorConnectionManager(
	localNode *node.LocalNode,
	network Network,
	policy voting.ThresholdPolicy,
	config common.Config,
) ConnectionManager

type DiscoveryMessage

type DiscoveryMessage struct {
	H DiscoveryMessageHeader
	B DiscoveryMessageBody
}

func DiscoveryMessageFromJSON

func DiscoveryMessageFromJSON(b []byte) (dm DiscoveryMessage, err error)

func NewDiscoveryMessage

func NewDiscoveryMessage(localNode *node.LocalNode, validators ...*node.Validator) (dm DiscoveryMessage, err error)

func (DiscoveryMessage) Equal

func (dm DiscoveryMessage) Equal(common.Message) bool

func (DiscoveryMessage) FilterUndiscovered

func (dm DiscoveryMessage) FilterUndiscovered(validators map[string]*node.Validator) []*node.Validator

FilterUndiscovered returns,

  • not yet registered validators
  • registered, but endpoint is changed.

FilterUndiscovered can get the `node.LocalNode.GetValidators()` directly.

func (DiscoveryMessage) GetHash

func (dm DiscoveryMessage) GetHash() string

func (DiscoveryMessage) GetType

func (dm DiscoveryMessage) GetType() common.MessageType

func (DiscoveryMessage) IsWellFormed

func (dm DiscoveryMessage) IsWellFormed(conf common.Config) error

func (DiscoveryMessage) Serialize

func (dm DiscoveryMessage) Serialize() ([]byte, error)

func (*DiscoveryMessage) Sign

func (dm *DiscoveryMessage) Sign(kp keypair.KP, networkID []byte)

func (DiscoveryMessage) Source

func (dm DiscoveryMessage) Source() string

func (DiscoveryMessage) String

func (dm DiscoveryMessage) String() string

func (DiscoveryMessage) Version

func (dm DiscoveryMessage) Version() string

type DiscoveryMessageBody

type DiscoveryMessageBody struct {
	Created    string            `json:"created"`
	Address    string            `json:"address"`  // LocalNode.Address()
	Endpoint   *common.Endpoint  `json:"endpoint"` // LocalNode.publishEndpoint()
	Validators []*node.Validator `json:"validators"`
}

func (DiscoveryMessageBody) MakeHashString

func (db DiscoveryMessageBody) MakeHashString() string

type DiscoveryMessageHeader

type DiscoveryMessageHeader struct {
	Signature string `json:"signature"`
}

type HTTP2ErrorLog15Writer

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

func (HTTP2ErrorLog15Writer) Write

func (w HTTP2ErrorLog15Writer) Write(b []byte) (int, error)

type HTTP2Log15Handler

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

func (HTTP2Log15Handler) ServeHTTP

func (l HTTP2Log15Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP will log in 2 phase, when request received and response sent. This was derived from github.com/gorilla/handlers/handlers.go

type HTTP2MessageBroker

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

func (HTTP2MessageBroker) Receive

func (r HTTP2MessageBroker) Receive(msg common.NetworkMessage)

func (HTTP2MessageBroker) Response

func (r HTTP2MessageBroker) Response(w io.Writer, o []byte) error

type HTTP2Network

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

func NewHTTP2Network

func NewHTTP2Network(config *HTTP2NetworkConfig) (h2n *HTTP2Network)

func (*HTTP2Network) AddHandler

func (t *HTTP2Network) AddHandler(pattern string, handler http.HandlerFunc) (router *mux.Route)

func (*HTTP2Network) AddMiddleware

func (t *HTTP2Network) AddMiddleware(routerName string, mws ...mux.MiddlewareFunc) error

func (*HTTP2Network) Endpoint

func (t *HTTP2Network) Endpoint() *common.Endpoint

func (*HTTP2Network) GetClient

func (t *HTTP2Network) GetClient(endpoint *common.Endpoint) NetworkClient

GetClient creates new keep-alive HTTP2 client

func (*HTTP2Network) IsReady

func (t *HTTP2Network) IsReady() bool

func (*HTTP2Network) MessageBroker

func (t *HTTP2Network) MessageBroker() MessageBroker

func (*HTTP2Network) Ready

func (t *HTTP2Network) Ready() error

func (*HTTP2Network) ReceiveChannel

func (t *HTTP2Network) ReceiveChannel() chan common.NetworkMessage

func (*HTTP2Network) ReceiveMessage

func (t *HTTP2Network) ReceiveMessage() <-chan common.NetworkMessage

func (*HTTP2Network) SetMessageBroker

func (t *HTTP2Network) SetMessageBroker(mb MessageBroker)

func (*HTTP2Network) Start

func (t *HTTP2Network) Start() (err error)

Start will start `HTTP2Network`.

func (*HTTP2Network) Stop

func (t *HTTP2Network) Stop()

type HTTP2NetworkClient

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

func NewHTTP2NetworkClient

func NewHTTP2NetworkClient(endpoint *common.Endpoint, client *common.HTTP2Client) *HTTP2NetworkClient

func (*HTTP2NetworkClient) Connect

func (c *HTTP2NetworkClient) Connect(n node.Node) (body []byte, err error)

func (*HTTP2NetworkClient) DefaultHeaders

func (c *HTTP2NetworkClient) DefaultHeaders() http.Header

func (*HTTP2NetworkClient) Endpoint

func (c *HTTP2NetworkClient) Endpoint() *common.Endpoint

func (*HTTP2NetworkClient) Get

func (client *HTTP2NetworkClient) Get(endpoint string) ([]byte, error)

/ / Perform a raw Get request on this peer / / This is a quick way to request the API. / As APIs are rapidly evolving, wrapping all of them properly / would be counter productive, to this function is provided. / / Params: / endpoint = URL chunk to request (e.g. `/api/foo?bar=baguette`) / / Returns: / []byte = Body part returned by the query if it was successful / error = Error information if the query wasn't successful /

func (*HTTP2NetworkClient) GetBallots

func (c *HTTP2NetworkClient) GetBallots() (retBody []byte, err error)

func (*HTTP2NetworkClient) GetNodeInfo

func (c *HTTP2NetworkClient) GetNodeInfo() (body []byte, err error)

func (*HTTP2NetworkClient) GetTransactions

func (c *HTTP2NetworkClient) GetTransactions(txs []string) (retBody []byte, err error)

func (*HTTP2NetworkClient) Send

func (c *HTTP2NetworkClient) Send(path string, message interface{}) (retBody []byte, err error)

func (*HTTP2NetworkClient) SendBallot

func (c *HTTP2NetworkClient) SendBallot(message interface{}) (retBody []byte, err error)

func (*HTTP2NetworkClient) SendDiscovery

func (c *HTTP2NetworkClient) SendDiscovery(message interface{}) (retBody []byte, err error)

func (*HTTP2NetworkClient) SendMessage

func (c *HTTP2NetworkClient) SendMessage(message interface{}) (retBody []byte, err error)

func (*HTTP2NetworkClient) SendTransaction

func (c *HTTP2NetworkClient) SendTransaction(message interface{}) (retBody []byte, err error)

func (*HTTP2NetworkClient) SetDefaultHeaders

func (c *HTTP2NetworkClient) SetDefaultHeaders(headers http.Header)

type HTTP2NetworkConfig

type HTTP2NetworkConfig struct {
	NodeName string
	Endpoint *common.Endpoint
	Addr     string

	ReadTimeout,
	ReadHeaderTimeout,
	WriteTimeout,
	IdleTimeout time.Duration

	TLSCertFile,
	TLSKeyFile string
}

func NewHTTP2NetworkConfigFromEndpoint

func NewHTTP2NetworkConfigFromEndpoint(nodeName string, endpoint *common.Endpoint) (config *HTTP2NetworkConfig, err error)

func (HTTP2NetworkConfig) IsHTTPS

func (config HTTP2NetworkConfig) IsHTTPS() bool

func (HTTP2NetworkConfig) String

func (config HTTP2NetworkConfig) String() string

type HTTP2ResponseLog15Writer

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

func (*HTTP2ResponseLog15Writer) Flush

func (l *HTTP2ResponseLog15Writer) Flush()

func (*HTTP2ResponseLog15Writer) Header

func (l *HTTP2ResponseLog15Writer) Header() http.Header

func (*HTTP2ResponseLog15Writer) Size

func (l *HTTP2ResponseLog15Writer) Size() int

func (*HTTP2ResponseLog15Writer) Status

func (l *HTTP2ResponseLog15Writer) Status() int

func (*HTTP2ResponseLog15Writer) Write

func (l *HTTP2ResponseLog15Writer) Write(b []byte) (int, error)

func (*HTTP2ResponseLog15Writer) WriteHeader

func (l *HTTP2ResponseLog15Writer) WriteHeader(s int)

type HandlerFunc

type HandlerFunc func(w http.ResponseWriter, r *http.Request)

type Handlers

type Handlers map[string]func(http.ResponseWriter, *http.Request)

type KeyGenerator

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

func NewKeyGenerator

func NewKeyGenerator(dirPath, certPath, keyPath string) *KeyGenerator

func (*KeyGenerator) Close

func (g *KeyGenerator) Close()

func (*KeyGenerator) GetCertPath

func (g *KeyGenerator) GetCertPath() string

func (*KeyGenerator) GetKeyPath

func (g *KeyGenerator) GetKeyPath() string

type MemoryMessageBroker

type MemoryMessageBroker struct {
	Messages []common.NetworkMessage
	// contains filtered or unexported fields
}

func (*MemoryMessageBroker) Receive

func (*MemoryMessageBroker) Response

func (r *MemoryMessageBroker) Response(w io.Writer, o []byte) error

type MemoryNetwork

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

func CreateMemoryNetwork

func CreateMemoryNetwork(prev *MemoryNetwork) (*MemoryNetwork, *node.LocalNode)

Create a MemoryNetwork for unittests purpose

If the argument `prev` is `nil`, a whole new network, disconnected from any other network, is created. If `prev` is not `nil`, the returned `MemoryNetwork` will be reachable from every node `prev` can reference

func (*MemoryNetwork) AddHandler

func (p *MemoryNetwork) AddHandler(string, http.HandlerFunc) *mux.Route

func (*MemoryNetwork) AddMiddleware

func (p *MemoryNetwork) AddMiddleware(string, ...mux.MiddlewareFunc) error

func (*MemoryNetwork) Endpoint

func (p *MemoryNetwork) Endpoint() *common.Endpoint

func (*MemoryNetwork) GetClient

func (t *MemoryNetwork) GetClient(endpoint *common.Endpoint) NetworkClient

func (*MemoryNetwork) GetNodeInfo

func (p *MemoryNetwork) GetNodeInfo() []byte

func (*MemoryNetwork) IsReady

func (p *MemoryNetwork) IsReady() bool

func (*MemoryNetwork) MessageBroker

func (p *MemoryNetwork) MessageBroker() MessageBroker

func (*MemoryNetwork) NewMemoryNetwork

func (prev *MemoryNetwork) NewMemoryNetwork() *MemoryNetwork

func (*MemoryNetwork) Ready

func (p *MemoryNetwork) Ready() error

func (*MemoryNetwork) ReceiveChannel

func (p *MemoryNetwork) ReceiveChannel() chan common.NetworkMessage

func (*MemoryNetwork) ReceiveMessage

func (p *MemoryNetwork) ReceiveMessage() <-chan common.NetworkMessage

func (*MemoryNetwork) Send

func (p *MemoryNetwork) Send(mt common.MessageType, b []byte) (err error)

func (*MemoryNetwork) SetLocalNode

func (p *MemoryNetwork) SetLocalNode(localNode *node.LocalNode)

func (*MemoryNetwork) SetMessageBroker

func (p *MemoryNetwork) SetMessageBroker(mb MessageBroker)

func (*MemoryNetwork) Start

func (p *MemoryNetwork) Start() error

func (*MemoryNetwork) Stop

func (p *MemoryNetwork) Stop()

type MemoryTransportClient

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

func NewMemoryNetworkClient

func NewMemoryNetworkClient(endpoint *common.Endpoint, server *MemoryNetwork) *MemoryTransportClient

func (*MemoryTransportClient) Connect

func (m *MemoryTransportClient) Connect(node node.Node) (b []byte, err error)

func (*MemoryTransportClient) Endpoint

func (m *MemoryTransportClient) Endpoint() *common.Endpoint

func (*MemoryTransportClient) GetBallots

func (m *MemoryTransportClient) GetBallots() ([]byte, error)

func (*MemoryTransportClient) GetNodeInfo

func (m *MemoryTransportClient) GetNodeInfo() ([]byte, error)

func (*MemoryTransportClient) GetTransactions

func (m *MemoryTransportClient) GetTransactions([]string) ([]byte, error)

func (*MemoryTransportClient) SendBallot

func (m *MemoryTransportClient) SendBallot(message interface{}) (body []byte, err error)

func (*MemoryTransportClient) SendDiscovery

func (m *MemoryTransportClient) SendDiscovery(message interface{}) (body []byte, err error)

func (*MemoryTransportClient) SendMessage

func (m *MemoryTransportClient) SendMessage(message interface{}) (body []byte, err error)

func (*MemoryTransportClient) SendTransaction

func (m *MemoryTransportClient) SendTransaction(message interface{}) (body []byte, err error)

type MessageBroker

type MessageBroker interface {
	Response(io.Writer, []byte) error
	Receive(common.NetworkMessage)
}

type Network

type Network interface {
	Endpoint() *common.Endpoint
	GetClient(endpoint *common.Endpoint) NetworkClient
	AddHandler(string, http.HandlerFunc) *mux.Route
	AddMiddleware(string, ...mux.MiddlewareFunc) error

	// Starts network handling
	// Blocks until finished, either because of an error
	// or because `Stop` was called
	Start() error
	Stop()
	SetMessageBroker(MessageBroker)
	MessageBroker() MessageBroker
	Ready() error
	IsReady() bool

	ReceiveChannel() chan common.NetworkMessage
	ReceiveMessage() <-chan common.NetworkMessage
}

type NetworkClient

type NetworkClient interface {
	Endpoint() *common.Endpoint

	Connect(node node.Node) ([]byte, error)
	GetNodeInfo() ([]byte, error)
	SendMessage(interface{}) ([]byte, error)
	SendTransaction(interface{}) ([]byte, error)
	SendBallot(interface{}) ([]byte, error)
	SendDiscovery(interface{}) ([]byte, error)
	GetTransactions([]string) ([]byte, error)
	GetBallots() ([]byte, error)
}

type ValidatorConnectionManager

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

func (*ValidatorConnectionManager) AllConnected

func (c *ValidatorConnectionManager) AllConnected() []string

func (*ValidatorConnectionManager) AllValidators

func (c *ValidatorConnectionManager) AllValidators() []string

Returns:

A list of all validators

func (*ValidatorConnectionManager) Broadcast

func (c *ValidatorConnectionManager) Broadcast(message common.Message)

func (*ValidatorConnectionManager) CountConnected

func (c *ValidatorConnectionManager) CountConnected() int

Returns:

the number of validators which are currently connected

func (*ValidatorConnectionManager) Discovery

func (*ValidatorConnectionManager) GetConnection

func (c *ValidatorConnectionManager) GetConnection(address string) (client NetworkClient)

func (*ValidatorConnectionManager) GetConnectionByEndpoint

func (c *ValidatorConnectionManager) GetConnectionByEndpoint(endpoint *common.Endpoint) (client NetworkClient)

func (*ValidatorConnectionManager) IsReady

func (c *ValidatorConnectionManager) IsReady() bool

func (*ValidatorConnectionManager) Start

func (c *ValidatorConnectionManager) Start()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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