sebak: boscoin.io/sebak/lib/network Index | Files | Directories

package network

import "boscoin.io/sebak/lib/network"

Provides test utility to mock a network in unittests

Index

Package Files

base.go connection_manager.go discovery_message.go generate_key.go http2.go http2_client.go http2_config.go http2_network_log.go http_middlewares.go init.go memory.go memory_client.go test.go validator_connection_manager.go

Constants

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

Variables

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,
    }
)
var HeaderKeyFiltered []string = []string{
    "Content-Length",
    "Content-Type",
    "Accept",
    "Accept-Encoding",
    "User-Agent",
}
var VerboseLogs bool

func CreateNewMemoryEndpoint Uses

func CreateNewMemoryEndpoint() *common.Endpoint

func GenerateKey Uses

func GenerateKey(dirPath, certPath, keyPath string)

func RateLimitMiddleware Uses

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 Uses

func RecoverMiddleware(logger logging.Logger) mux.MiddlewareFunc

func SetHTTPLogging Uses

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

func SetLogging Uses

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

type ConnectionManager Uses

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

func NewValidatorConnectionManager Uses

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

type DiscoveryMessage Uses

type DiscoveryMessage struct {
    H   DiscoveryMessageHeader
    B   DiscoveryMessageBody
}

func DiscoveryMessageFromJSON Uses

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

func NewDiscoveryMessage Uses

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

func (DiscoveryMessage) Equal Uses

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

func (DiscoveryMessage) FilterUndiscovered Uses

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 Uses

func (dm DiscoveryMessage) GetHash() string

func (DiscoveryMessage) GetType Uses

func (dm DiscoveryMessage) GetType() common.MessageType

func (DiscoveryMessage) IsWellFormed Uses

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

func (DiscoveryMessage) Serialize Uses

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

func (*DiscoveryMessage) Sign Uses

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

func (DiscoveryMessage) Source Uses

func (dm DiscoveryMessage) Source() string

func (DiscoveryMessage) String Uses

func (dm DiscoveryMessage) String() string

func (DiscoveryMessage) Version Uses

func (dm DiscoveryMessage) Version() string

type DiscoveryMessageBody Uses

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 Uses

func (db DiscoveryMessageBody) MakeHashString() string

type DiscoveryMessageHeader Uses

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

type HTTP2ErrorLog15Writer Uses

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

func (HTTP2ErrorLog15Writer) Write Uses

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

type HTTP2Log15Handler Uses

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

func (HTTP2Log15Handler) ServeHTTP Uses

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 Uses

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

func (HTTP2MessageBroker) Receive Uses

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

func (HTTP2MessageBroker) Response Uses

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

type HTTP2Network Uses

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

func NewHTTP2Network Uses

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

func (*HTTP2Network) AddHandler Uses

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

func (*HTTP2Network) AddMiddleware Uses

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

func (*HTTP2Network) Endpoint Uses

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

func (*HTTP2Network) GetClient Uses

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

GetClient creates new keep-alive HTTP2 client

func (*HTTP2Network) IsReady Uses

func (t *HTTP2Network) IsReady() bool

func (*HTTP2Network) MessageBroker Uses

func (t *HTTP2Network) MessageBroker() MessageBroker

func (*HTTP2Network) Ready Uses

func (t *HTTP2Network) Ready() error

func (*HTTP2Network) ReceiveChannel Uses

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

func (*HTTP2Network) ReceiveMessage Uses

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

func (*HTTP2Network) SetMessageBroker Uses

func (t *HTTP2Network) SetMessageBroker(mb MessageBroker)

func (*HTTP2Network) Start Uses

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

Start will start `HTTP2Network`.

func (*HTTP2Network) Stop Uses

func (t *HTTP2Network) Stop()

type HTTP2NetworkClient Uses

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

func NewHTTP2NetworkClient Uses

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

func (*HTTP2NetworkClient) Connect Uses

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

func (*HTTP2NetworkClient) DefaultHeaders Uses

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

func (*HTTP2NetworkClient) Endpoint Uses

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

func (*HTTP2NetworkClient) Get Uses

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 Uses

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

func (*HTTP2NetworkClient) GetNodeInfo Uses

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

func (*HTTP2NetworkClient) GetTransactions Uses

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

func (*HTTP2NetworkClient) Send Uses

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

func (*HTTP2NetworkClient) SendBallot Uses

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

func (*HTTP2NetworkClient) SendDiscovery Uses

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

func (*HTTP2NetworkClient) SendMessage Uses

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

func (*HTTP2NetworkClient) SendTransaction Uses

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

func (*HTTP2NetworkClient) SetDefaultHeaders Uses

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

type HTTP2NetworkConfig Uses

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

    ReadTimeout,
    ReadHeaderTimeout,
    WriteTimeout,
    IdleTimeout time.Duration

    TLSCertFile,
    TLSKeyFile string
}

func NewHTTP2NetworkConfigFromEndpoint Uses

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

func (HTTP2NetworkConfig) IsHTTPS Uses

func (config HTTP2NetworkConfig) IsHTTPS() bool

func (HTTP2NetworkConfig) String Uses

func (config HTTP2NetworkConfig) String() string

type HTTP2ResponseLog15Writer Uses

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

func (*HTTP2ResponseLog15Writer) Flush Uses

func (l *HTTP2ResponseLog15Writer) Flush()

func (*HTTP2ResponseLog15Writer) Header Uses

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

func (*HTTP2ResponseLog15Writer) Size Uses

func (l *HTTP2ResponseLog15Writer) Size() int

func (*HTTP2ResponseLog15Writer) Status Uses

func (l *HTTP2ResponseLog15Writer) Status() int

func (*HTTP2ResponseLog15Writer) Write Uses

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

func (*HTTP2ResponseLog15Writer) WriteHeader Uses

func (l *HTTP2ResponseLog15Writer) WriteHeader(s int)

type HandlerFunc Uses

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

type Handlers Uses

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

type KeyGenerator Uses

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

func NewKeyGenerator Uses

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

func (*KeyGenerator) Close Uses

func (g *KeyGenerator) Close()

func (*KeyGenerator) GetCertPath Uses

func (g *KeyGenerator) GetCertPath() string

func (*KeyGenerator) GetKeyPath Uses

func (g *KeyGenerator) GetKeyPath() string

type MemoryMessageBroker Uses

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

func (*MemoryMessageBroker) Receive Uses

func (r *MemoryMessageBroker) Receive(m common.NetworkMessage)

func (*MemoryMessageBroker) Response Uses

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

type MemoryNetwork Uses

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

func CreateMemoryNetwork Uses

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 Uses

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

func (*MemoryNetwork) AddMiddleware Uses

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

func (*MemoryNetwork) Endpoint Uses

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

func (*MemoryNetwork) GetClient Uses

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

func (*MemoryNetwork) GetNodeInfo Uses

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

func (*MemoryNetwork) IsReady Uses

func (p *MemoryNetwork) IsReady() bool

func (*MemoryNetwork) MessageBroker Uses

func (p *MemoryNetwork) MessageBroker() MessageBroker

func (*MemoryNetwork) NewMemoryNetwork Uses

func (prev *MemoryNetwork) NewMemoryNetwork() *MemoryNetwork

func (*MemoryNetwork) Ready Uses

func (p *MemoryNetwork) Ready() error

func (*MemoryNetwork) ReceiveChannel Uses

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

func (*MemoryNetwork) ReceiveMessage Uses

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

func (*MemoryNetwork) Send Uses

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

func (*MemoryNetwork) SetLocalNode Uses

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

func (*MemoryNetwork) SetMessageBroker Uses

func (p *MemoryNetwork) SetMessageBroker(mb MessageBroker)

func (*MemoryNetwork) Start Uses

func (p *MemoryNetwork) Start() error

func (*MemoryNetwork) Stop Uses

func (p *MemoryNetwork) Stop()

type MemoryTransportClient Uses

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

func NewMemoryNetworkClient Uses

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

func (*MemoryTransportClient) Connect Uses

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

func (*MemoryTransportClient) Endpoint Uses

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

func (*MemoryTransportClient) GetBallots Uses

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

func (*MemoryTransportClient) GetNodeInfo Uses

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

func (*MemoryTransportClient) GetTransactions Uses

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

func (*MemoryTransportClient) SendBallot Uses

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

func (*MemoryTransportClient) SendDiscovery Uses

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

func (*MemoryTransportClient) SendMessage Uses

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

func (*MemoryTransportClient) SendTransaction Uses

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

type MessageBroker Uses

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

type Network Uses

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 Uses

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 Uses

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

func (*ValidatorConnectionManager) AllConnected Uses

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

func (*ValidatorConnectionManager) AllValidators Uses

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

Returns:

A list of all validators

func (*ValidatorConnectionManager) Broadcast Uses

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

func (*ValidatorConnectionManager) CountConnected Uses

func (c *ValidatorConnectionManager) CountConnected() int

Returns:

the number of validators which are currently connected

func (*ValidatorConnectionManager) Discovery Uses

func (c *ValidatorConnectionManager) Discovery(dm DiscoveryMessage) error

func (*ValidatorConnectionManager) GetConnection Uses

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

func (*ValidatorConnectionManager) GetConnectionByEndpoint Uses

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

func (*ValidatorConnectionManager) IsReady Uses

func (c *ValidatorConnectionManager) IsReady() bool

func (*ValidatorConnectionManager) Start Uses

func (c *ValidatorConnectionManager) Start()

Directories

PathSynopsis
httpcache
httputils

Package network imports 42 packages (graph) and is imported by 13 packages. Updated 2019-03-27. Refresh now. Tools for package owners.