csnet

package
v0.0.0-...-ff61ee7 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2020 License: GPL-3.0 Imports: 20 Imported by: 0

Documentation

Overview

This package contains the code for maintining and using TCP and UDP networking for the consensus. In UDP a node may have multiple open ports being used for sending and receiving, in TCP there will be one listening address that will spawn connections on other ports as usual. All interaction with the consensus should take place through NetMainChannel (meaning the other objects should be considered private (TODO maybe actually make private).

Each node should construct a NetMainChannel object. For each external node that this node may receive messages from, NetMainChannel.AddExternal node should be called. For each node that this node will be sending message to, NetMainChannel.CreateSendConnection should additionally be called. The node will maintain send connections to at most the connCount input parameter to NewNetMainChannel, if NetMainChannel.CreateSendConnection was called for more nodes, then these nodes will be connected to when the others can no longer be reached. To send messages NetMainChannel.Send and NetMainChannel.SendTo should be used. NetMainChannel.Close will close all the connections.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewNetSendConnection

func NewNetSendConnection(conInfo channelinterface.NetNodeInfo, context context.Context,
	connStatus *ConnStatus, netMainChannel *NetMainChannel) (channelinterface.SendChannel, error)

NewNetConnection creates new connection to conInfo. For TCP conInfos must contain a single element, it creates and maintains a connection For UDP multiple addresses can be used for a node. When sending to the node, messages will be sent in a round robin order, cycling through the addresses. (UDP doesn't maintain any connections, it uses NetPortListenerUDP accessed through NetMainChannel (TODO cleanup))

Types

type ConnStatus

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

Connstats tracks and maintains open connections to other nodes Each node may use multiple addresses, in this case the addresses are stored as a list, where the first element of the list is the address that identifies the node. The method AddPendingSend creates a new NetSendCon object, in TCP these objects maintain connections, in udp they just keep the address, and use MainChannel.NetRecvConn to actually send the messages. In TCP whenever there is a error with a connection it is closed and the connection count is reduced. In UDP, given that it is connectionless, the connection count is always the number of addresses added. The connection list is updated in the following ways (TODO clean this up): - When a connection is made from this node to an external node (TCP or UDP) through this object sendCons is updated - When NetPortListenerTCP received a connection from an external node, recvCons is updated (recvCons is allways empty for UDP) - When a NetConnection connection has an error (TCP only), the connection will be removed from either sendCons or recvCons

func NewConnStatus

func NewConnStatus(nwType types.NetworkProtocolType) *ConnStatus

NewConnStatus creates and initialized a ConnStatus object

func (*ConnStatus) Close

func (cs *ConnStatus) Close()

Close unblocks anyone waiting on WaitUntilFewerSendCons and closes any connections.

func (*ConnStatus) ComputeDestinations

func (cs *ConnStatus) ComputeDestinations(forwardFunc channelinterface.NewForwardFuncFilter,
	allPubs []sig.Pub) (destList []channelinterface.SendChannel, unknownPubs []sig.Pub)

ComputeDestinations returns the list of destinations given the forward filter function. If unconnected pubs are found to send to, then it returns them as unknown pubs, and a nil destList.

func (*ConnStatus) FinishedMakingConnection

func (cs *ConnStatus) FinishedMakingConnection(pub sig.Pub)

func (*ConnStatus) RecvConnCount

func (cs *ConnStatus) RecvConnCount() int

RecvConnCount returns the sum of connections and pending connections originating from external nodes

func (*ConnStatus) Send

func (cs *ConnStatus) Send(buff []byte,
	forwardChecker channelinterface.NewForwardFuncFilter,
	allPubs []sig.Pub,
	stats stats.NwStatsInterface,
	consStats stats.ConsNwStatsInterface) []sig.Pub

Send sends a message, the forwardChecker function will receive all current connections, and should return either all or a subset of them based on forwarding rules. It returns any destinations that are not known, but were returned by the forward checker. This is called by NetMainChannel.Send

func (*ConnStatus) SendConnCount

func (cs *ConnStatus) SendConnCount() int

SendConnCount returns the sum of connections and pending connections origination from this node

func (*ConnStatus) SendTo

func (cs *ConnStatus) SendTo(buff []byte, dest channelinterface.SendChannel, stats stats.NwStatsInterface,
	consStats stats.ConsNwStatsInterface)

SendTo sends the byte slice to the destination channel. TODO should try to connect to on connection not existing?

func (*ConnStatus) SendToPub

func (cs *ConnStatus) SendToPub(buff []byte, pub sig.Pub, stats stats.NwStatsInterface,
	consStats stats.ConsNwStatsInterface) error

SendToPub sends buff to the node associated with the public key (if it exists), it returns an error if pub is not found in the list of connections. TODO should try to connect to on connection not existing?

func (*ConnStatus) SendToPubList

func (cs *ConnStatus) SendToPubList(buf []byte, pubList []sig.PubKeyStr, stats stats.NwStatsInterface,
	consStats stats.ConsNwStatsInterface) (errs []error)

SendToPubList sends buf to the list of pub keys if connections to them exist

func (*ConnStatus) WaitUntilAtLeastSendCons

func (cs *ConnStatus) WaitUntilAtLeastSendCons(n int) error

WaitUntilFewerSendCons blocks until there are at least n connections, or an error

type DualNetMainChannel

type DualNetMainChannel struct {
	*NetMainChannel
	Secondary *NetMainChannel
}

func NewDualNetMainChannel

func NewDualNetMainChannel(
	myPriv sig.Priv,
	myConInfo []channelinterface.NetNodeInfo,
	maxConnCount int,
	connectionType types.NetworkProtocolType,
	numMsgProcessThreads int,
	consItem consinterface.ConsItem,
	bt channelinterface.BehaviorTracker,
	encryptChannels bool,
	msgDropPercent int,
	stats stats.NwStatsInterface) *DualNetMainChannel

NewNetMainChannel creates a net NetMainChannel object. Ports will be opened on the addresses of myConInfo (if they are 0, then it will bind to any availalbe port). connCount is the number of open connections that this node will make and maintain to external nodes for sending messages. msgDropPercent is the percent of received messages to drop randomly (for testing). It stores the connection objects for ths consensus and returns the addresses that have been bound to locally. Only static methods of ConsItem will be used.

func (*DualNetMainChannel) Close

func (tp *DualNetMainChannel) Close()

Close closes the channels. This should only be called once

func (*DualNetMainChannel) EndInit

func (tp *DualNetMainChannel) EndInit()

EndInit is called once recovery is finished

func (*DualNetMainChannel) Send

func (tp *DualNetMainChannel) Send(buff []byte,
	isProposal, toSelf bool,
	forwardChecker channelinterface.NewForwardFuncFilter, countStats bool, consStats stats.ConsNwStatsInterface)

Send calls Send on the secondary channel if the message is a proposal, on the main channel otherwise.

func (*DualNetMainChannel) SendHeader

func (tp *DualNetMainChannel) SendHeader(headers []messages.MsgHeader, isProposal, toSelf bool,
	forwardChecker channelinterface.NewForwardFuncFilter, countStats bool, consStats stats.ConsNwStatsInterface)

SendHeader calls SendHeader on the secondary channel if the message is a proposal, on the main channel otherwise.

func (*DualNetMainChannel) SetMemberCheckerState

func (tp *DualNetMainChannel) SetMemberCheckerState(memberCheckerState consinterface.ConsStateInterface)

func (*DualNetMainChannel) StartInit

func (tp *DualNetMainChannel) StartInit()

StartInit is called when the system is starting and is recovering from disk At this point any calls to Send/SendToSelf should not send any messages as the system is just replying events

func (*DualNetMainChannel) StartMsgProcessThreads

func (tp *DualNetMainChannel) StartMsgProcessThreads()

StartMsgProcessThreads starts the threads that will process the messages.

type NetConnectionTCP

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

NetConnection represents a connection to an external node TCP connections are maintained here, constatus is updated when an error happens on an existing connection UDP connections just store the address (since it is connectionless), the operations just call the connections NetPortListenerUDP (TODO cleanup)

func (*NetConnectionTCP) Close

Close shuts down the connection

func (*NetConnectionTCP) GetConnInfos

func (nsc *NetConnectionTCP) GetConnInfos() channelinterface.NetNodeInfo

GetLocalNodeConnectionInfo returns the addresses for this connection

func (*NetConnectionTCP) GetType

GetType returns channelinterface.TCP

func (*NetConnectionTCP) Send

func (nsc *NetConnectionTCP) Send(buff []byte) (err error)

Send sends a byte slice to the node addressed by this connection

type NetConnectionUDP

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

NetConnection represents a connection to an external node TCP connections are maintained here, constatus is updated when an error happens on an existing connection UDP connections just store the address (since it is connectionless), the operations just call the connections NetPortListenerUDP (TODO cleanup)

func (*NetConnectionUDP) Close

Close shuts down the connection

func (*NetConnectionUDP) GetConnInfos

func (nsc *NetConnectionUDP) GetConnInfos() channelinterface.NetNodeInfo

GetLocalNodeConnectionInfo returns the addresses for this connection

func (*NetConnectionUDP) GetType

GetType returns channelinterface.UDP

func (*NetConnectionUDP) Send

func (nsc *NetConnectionUDP) Send(buff []byte) (err error)

Send sends a byte slice to the node addressed by this connection

type NetMainChannel

type NetMainChannel struct {
	channel.AbsMainChannel
	// contains filtered or unexported fields
}

NetMainChannel stores the state of the network connections for the consensus

func NewNetMainChannel

func NewNetMainChannel(
	myPriv sig.Priv,
	myConInfo channelinterface.NetNodeInfo,
	maxConnCount int,
	connectionType types.NetworkProtocolType,

	numMsgProcessThreads int,
	consItem consinterface.ConsItem,
	bt channelinterface.BehaviorTracker,
	encryptChannels bool,
	msgDropPercent int,
	stats stats.NwStatsInterface) (*NetMainChannel, []channelinterface.NetConInfo)

NewNetMainChannel creates a net NetMainChannel object. Ports will be opened on the addresses of myConInfo (if they are 0, then it will bind to any availalbe port). connCount is the number of open connections that this node will make and maintain to external nodes for sending messages. msgDropPercent is the percent of received messages to drop randomly (for testing). It stores the connection objects for ths consensus and returns the addresses that have been bound to locally. Only static methods of ConsItem will be used.

func (*NetMainChannel) AddExternalNode

func (tp *NetMainChannel) AddExternalNode(nodeInfo channelinterface.NetNodeInfo)

AddExternalNode adds the list of addresses for an external node. Connections and messages will then be accepted from this node.

func (*NetMainChannel) Close

func (tp *NetMainChannel) Close()

Close closes the channels. This should only be called once

func (*NetMainChannel) ComputeDestinations

func (tp *NetMainChannel) ComputeDestinations(forwardFunc channelinterface.NewForwardFuncFilter) []channelinterface.SendChannel

ComputeDestinations returns the list of destinations given the forward filter function.

func (*NetMainChannel) GetLocalNodeConnectionInfo

func (tp *NetMainChannel) GetLocalNodeConnectionInfo() channelinterface.NetNodeInfo

GetLocalNodeConnectionInfo returns the addresses that this node is listening on.

func (*NetMainChannel) MakeConnections

func (tp *NetMainChannel) MakeConnections(pubs []sig.Pub) (errs []error)

MakeConnections will connect to the nodes given by the pubs. This should be called after AddExternalNodes with a subset of the nodes added there.

func (*NetMainChannel) RemoveConnections

func (tp *NetMainChannel) RemoveConnections(pubs []sig.Pub) (errs []error)

func (*NetMainChannel) RemoveExternalNode

func (tp *NetMainChannel) RemoveExternalNode(nodeInfo channelinterface.NetNodeInfo)

RemoveExternalNode removes the external nodes list of addresses. Connections and messages will no longer be accepted from this node.

func (*NetMainChannel) Send

func (tp *NetMainChannel) Send(buff []byte,
	isProposal, toSelf bool,
	forwardChecker channelinterface.NewForwardFuncFilter, countStats bool, consStats stats.ConsNwStatsInterface)

Send sends a message to the outgoing connections, toSelf should be true if the message should also be received by the local node, the forwardChecker function will receive all current connections, and should return either all or a subset of them based on forwarding rules, the set of channels returnd will be sent the message. For example if it returns the input list, then the send is a broadcast to all nodes This method is not concurrent safe.

func (*NetMainChannel) SendAlways

func (tp *NetMainChannel) SendAlways(buff []byte, toSelf bool,
	forwardChecker channelinterface.NewForwardFuncFilter, countStats bool, consStats stats.ConsNwStatsInterface)

SendAlways is the same as Send, except the message will be sent even if the consensus is in initialization. This is just used to request the state from neighbour nodes on initialization.

func (*NetMainChannel) SendHeader

func (tp *NetMainChannel) SendHeader(headers []messages.MsgHeader, isProposal, toSelf bool,
	forwardChecker channelinterface.NewForwardFuncFilter, countStats bool, consStats stats.ConsNwStatsInterface)

SendHeader serializes the header then calls Send.

func (*NetMainChannel) SendTo

func (tp *NetMainChannel) SendTo(buff []byte, dest channelinterface.SendChannel, countStats bool,
	consStats stats.ConsNwStatsInterface)

SendTo sends a message on the given SendChannel.

func (*NetMainChannel) SendToPub

func (tp *NetMainChannel) SendToPub(headers []messages.MsgHeader, pub sig.Pub, countStats bool,
	consStats stats.ConsNwStatsInterface) error

SendToPub sends buff to the node associated with the public key (if it exists), it returns an error if pub is not found in the list of connections

func (*NetMainChannel) SetStaticNodeList

func (tp *NetMainChannel) SetStaticNodeList(staticNodeList map[sig.PubKeyStr]channelinterface.NetNodeInfo)

SetStaticNodeList can optionally set an initial read only list of nodes in the network.

func (*NetMainChannel) StartMsgProcessThreads

func (tp *NetMainChannel) StartMsgProcessThreads()

StartMsgProcessThreads starts the threads that will process the messages.

func (*NetMainChannel) WaitUntilAtLeastSendCons

func (tp *NetMainChannel) WaitUntilAtLeastSendCons(n int) error

type NetPortListener

type NetPortListener interface {
	// contains filtered or unexported methods
}

NetPortListener maintains the local open network ports In TCP it handles newly received connections and informs ConnStatus In UDP it handles all sending and receiving of messages given UDP is connectionless

func NewNetPortListener

func NewNetPortListener(connInfos []channelinterface.NetConInfo, connStatus *ConnStatus, netMainChannel *NetMainChannel) (NetPortListener, []channelinterface.NetConInfo, error)

NewNetPortListener creates a new NetPortListener on the given local addresses If the addresses are given in a format like '{address}:0' it will bind on any free port It returns the list of local addresses that the connections were made on

type NetPortListenerTCP

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

NetPortListenerTCP maintains local listening TCP addresses Whenever a connection is added or dropped it informs ConnStatus, who then handles the connection

func NewNetPortListenerTCP

func NewNetPortListenerTCP(connInfo channelinterface.NetConInfo, connStatus *ConnStatus, netMainChannel *NetMainChannel) (*NetPortListenerTCP, []channelinterface.NetConInfo, error)

NewNetPortListenerTCP creates a new NetPortListenerTCP object and starts listening on the address connInfo When a new connection is made, it informs ConnStatus. If the addresses are given in a format like '{address}:0' it will bind on any free port It returns the list of local addresses that the connections were made on

func (*NetPortListenerTCP) GetConnInfo

func (nsc *NetPortListenerTCP) GetConnInfo() channelinterface.NetConInfo

GetConnInfo returns the local listening addresses

type NetPortListenerUDP

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

NetPortListenerUDP maintains local UDP ports For each port it runs a recveive and send loop

func NewNetPortListenerUDP

func NewNetPortListenerUDP(connInfos []channelinterface.NetConInfo, netMainChannel *NetMainChannel) (*NetPortListenerUDP, []channelinterface.NetConInfo, error)

NewNetPortListenerUDP creates a new NetPortListenerUDP object and starts loops for recving and sending on the opened ports. All network sends and receive will happen through this object. NetConnectionUDP objects will go through this object to perform their sends. Each node may have multiple UDP addresses, this allows it to send and receive over multiple connections. Large messages are split into multiple packets then sent over the multiple connections per node. When messages are received, if they are multi-packet then they are reconstructed, and NetMainChannel.ProcessMessage is called. If the addresses input to this function are given in a format like '{address}:0' it will bind on any free port. It returns the list of local addresses that the connections were made on.

func (*NetPortListenerUDP) GetConnInfos

func (nrc *NetPortListenerUDP) GetConnInfos() []channelinterface.NetConInfo

GetLocalNodeConnectionInfo returns the list of addresses that this local node is listening on. With UDP we may use multiple addresses so we can split sending and receiving over them.

type UDPConnMap

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

UDPConnMap is a simple map of addresses to connection objects protected by a RW mutex

func NewUDPConnMap

func NewUDPConnMap() *UDPConnMap

NewUDPConnMap returns a new empty UDPConnMap

func (*UDPConnMap) AddConnection

func (ucm *UDPConnMap) AddConnection(addrs []channelinterface.NetConInfo, conn *NetConnectionUDP)

AddConnection adds the addresses to the mape with value connection. It is protected by a mutex.

func (*UDPConnMap) Get

Get returns the value in the map at addr, this is protected by a read mutex.

func (*UDPConnMap) RemoveConnection

func (ucm *UDPConnMap) RemoveConnection(addrs []channelinterface.NetConInfo)

RemoveConnection removes the addresses from the map. It is protected by a mutex.

Jump to

Keyboard shortcuts

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