channelinterface

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: 15 Imported by: 0

Documentation

Overview

This package contains higher level network abstractions that will run on top and along side of the channel and csnet packages.

Index

Constants

This section is empty.

Variables

View Source
var BackHalfSendRange = SendRange{
	From:  50,
	Until: 99,
}

BackHalfSendRange means to send to the second half of recipients returned from NewForwardFuncFilter

View Source
var FrontHalfSendRange = SendRange{
	From:  0,
	Until: 50,
}

FrontHalfSendRange means to send to the first half of recipients returned from NewForwardFuncFilter

View Source
var FullSendRange = SendRange{
	From:  0,
	Until: 99,
}

FullSendRange means to send to all recipients returnd from NewForwardFuncFilter

Functions

func GetNextConnCounter

func GetNextConnCounter() uint64

GetNextConnCounter is a atomic interger called each time a connection is created to assign it a unique id

func RunChannelTest

func RunChannelTest(testProcs []MainChannel, loops int, t *testing.T)

RunChannelTest is used to test network channels, where there all the channels have already been connected to eachother loops is the number of times to perform an all to all message broadcast

Types

type BehaviorTracker

type BehaviorTracker interface {
	// GotError should be called whenever there was an error on the connection conInfo.
	// For example it sent an incorrect/duplicate message
	// Or there was a problem with the connection itself
	// Returns true if requestions from the connection should be rejected.
	GotError(err error, conInfos interface{}) bool
	// CheckShouldReject returns true if messages or connections from the address of conInfo should be rejected.
	CheckShouldReject(conInfo interface{}) bool
	// RequestedOldDecisions should be called when conInfo requested an old decided value, returns true if
	// the request should be rejected.
	RequestedOldDecisions(conInfo interface{}) bool
}

BehaviorTracker objects track bad behaviors of connections. The functions must be safe to be accesses concurrently.

type ChannelCloseType

type ChannelCloseType int

ChannelCloseType dictates the reason for closing a channel Currently all the close types do the same thing

const (
	// EndTestClose must be used when the test is finished and everything is shutting down.
	// It should only be called from a thread that does not process consensus messages, otherwise it can block.
	EndTestClose ChannelCloseType = iota
	// CloseDuringTest can be used to close channels during the test from threads that process consensus messages.
	CloseDuringTest
)

type ConBehaviorInfo

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

ConBehavorInfo tracks bad behavior on a per connection basis. Based on a timeout and threhold of bad behavior, the object may say to close/reject the bad connection.

type ConMapItem

type ConMapItem struct {
	NI             NetNodeInfo
	Idx            int
	IsConnected    bool
	NewCon         bool // used in MakeConnectionsCloseOthers ( no longer used)
	ConCount       int  // how many instances are using this connection, when set to 0 connection is closed
	AddressUnknown bool // is true if we don't know the connection address
}

type ConnDetails

type ConnDetails struct {
	Addresses NetNodeInfo
	Conn      SendChannel
}

ConnDetails stores the address and a connection

type ForwardFuncFilter

type ForwardFuncFilter func(sendChans []SendChannel) []SendChannel

type MainChannel

type MainChannel interface {
	// MakeConnections creates connections to the nodes at the list of pubs.
	// The pubs must have been added previously by AddExternalNode.
	// If a connection is added multiple times it increments a counter for that connection.
	MakeConnections(pubs []sig.Pub) (errs []error)
	// RemoveConnections removes connections to the list of pubs.
	// The pubs should have been added previously with MakeConnections.
	// If the connection has been added multiple times this decrements the counter,
	// and closes the connection when the counter reaches 0.
	RemoveConnections(pubs []sig.Pub) (errs []error)
	// GetConnections returns the list of connections that the node is currently connected to or trying to connect to.
	// GetConnections() (ret sig.PubKeyStrList)
	// MakeConnectionsCloseOther will connect to the nodes given by the pubs.
	// Any existing connections not in pubs will be closed.
	// This should be called after AddExternalNodes with a subset of the nodes
	// added there.
	// MakeConnectionsCloseOthers(pubs []sig.Pub) (errs []error)
	// SendToSelf sends a message to the current processes after a timout, it returns the timer
	// this method is concurrent safe.
	// The timer should either fire or be closed before the program exits.
	// I.E. it should be closed during the Collect() method called on consensus items.
	SendToSelf(deser []*deserialized.DeserializedItem, timeout time.Duration) TimerInterface
	// ComputeDestinations returns the list of destinations given the forward filter function.
	ComputeDestinations(forwardFunc NewForwardFuncFilter) []SendChannel
	// 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
	// IsProposal should be true if the message is a proposal message.
	// This method is not concurrent safe.
	// consStats is the statistics object of the specific consensus instance broadcasting the message
	// or nil if there is none
	Send(buff []byte, isProposal, toSelf bool, forwardChecker NewForwardFuncFilter, countStats bool,
		consStats stats.ConsNwStatsInterface)
	// SendHeader is the same as Send except it take a messasges.MsgHeader instead of a byte slice.
	// This should be used in the consensus implementations.
	// IsProposal should be true if the message is a proposal message.
	// consStats is the statistics object of the specific consensus instance broadcasting the message
	// or nil if there is none
	SendHeader(headers []messages.MsgHeader, isProposal, toSelf bool, forwardChecker 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.
	// consStats is the statistics object of the specific consensus instance broadcasting the message
	// or nil if there is none
	SendAlways(buff []byte, toSelf bool, forwardChecker NewForwardFuncFilter, countStats bool, consStats stats.ConsNwStatsInterface)
	// SendTo sends buff to dest
	// consStats is the statistics object of the specific consensus instance broadcasting the message
	// or nil if there is none
	SendTo(buff []byte, dest SendChannel, countStats bool, consStats stats.ConsNwStatsInterface)
	// 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
	// consStats is the statistics object of the specific consensus instance broadcasting the message
	// or nil if there is none
	SendToPub(headers []messages.MsgHeader, pub sig.Pub, countStats bool, consStats stats.ConsNwStatsInterface) error
	// HasProposal should be called by the state machine when it is ready with its proposal for the next round of consensus.
	// It should be called after ProposalInfo object interface (package consinterface) method HasDecided had been called for the previous consensus instance.
	HasProposal(*deserialized.DeserializedItem)
	// Recv should be called as the main consensus loop every time the node is ready to process a message.
	// It is expected to be called one at a time (not concurrent safe).
	// It will return utils.ErrTimeout after a timeout to ensure progress.
	Recv() (*RcvMsg, error)
	// Close should be called when the system is shutting down
	Close()
	// GotRcvConnection(rcvcon *SendRecvChannel)
	// CreateSendConnection adds the list of connections to the list of nodes to send messages to
	// CreateSendConnection(NetNodeInfo) error
	// StartMsgProcessThreads starts the threads that will process the messages.
	StartMsgProcessThreads()

	// SetStaticNodeList can optionally set an initial read only list of nodes in the network.
	SetStaticNodeList(staticNodeList map[sig.PubKeyStr]NetNodeInfo)
	// AddExternalNode should be called with the list of addresses that a single external node will have.
	// In UDP a node might split packets over multiple connections, so this lets us know this list for each node.
	// In TCP this does nothing. // TODO should do something?
	AddExternalNode(NetNodeInfo)
	// RemoveExternalNode should be called when we will no longer receive messages from an external node,
	// it should be called with the list of addresses that a single external node will have.
	RemoveExternalNode(NetNodeInfo)
	// WaitUntilFewerSendCons blocks until there are at least n connections, or an error.
	WaitUntilAtLeastSendCons(n int) error
	// GetLocalNodeConnectionInfo returns the list of local addresses that this node is listening for messages(udp)/connections(tcp) on
	GetLocalNodeConnectionInfo() NetNodeInfo
	// 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
	StartInit()
	// EndInit is called once recovery is finished
	EndInit()
	// InitInProgress returns true if StartInit has been called and EndInit has not.
	InitInProgress() bool
	// Reprocess is called on messages that were unable to be deserialized upon first reception, it is called by concurrent threads
	ReprocessMessage(*RcvMsg)
	// ReprocessMessageBytes is called on messages that have already been received and need to be reprocesses.
	// It is safe to be called by many threads.
	ReprocessMessageBytes(msg []byte)
	// GetBehaviorTracker returns the BehaviorTracker object
	GetBehaviorTracker() BehaviorTracker
	// GetStats returns the stats object being used
	GetStats() stats.NwStatsInterface
}

type MsgConstructFunc

type MsgConstructFunc func(pieces int) (toSelf *deserialized.DeserializedItem, toOthers [][]byte)

type NetConInfo

type NetConInfo struct {
	Addr string // The address, ip or ipv6
	Nw   string // The netowrk type, tcp of udp
}

NewConInfo stores network addresses

func NetConInfoFromAddr

func NetConInfoFromAddr(addr net.Addr) NetConInfo

NetConInfoFromAddr creates a new NewConInfo object from a net.Addr object

func NetConInfoFromString

func NetConInfoFromString(str string) (NetConInfo, error)

NetConInfoFromString creates a new NetConInfo object from a string for example tcp://127.0.0.1:1234

func NewConInfoList

func NewConInfoList(conType types.NetworkProtocolType) []NetConInfo

func UpdateNw

func UpdateNw(nci NetConInfo, addr net.Addr) NetConInfo

UpdateNw takes creates a new NetConInfo object using an existing NetConInfo object and an address, it takes the ip from nci, and the rest of the information from addr

func (NetConInfo) Network

func (nci NetConInfo) Network() string

Network returns nci.Nw

func (NetConInfo) String

func (nci NetConInfo) String() string

String returns Nci.Addr

type NetNodeInfo

type NetNodeInfo struct {
	AddrList []NetConInfo
	Pub      sig.Pub
}

type NewForwardFuncFilter

type NewForwardFuncFilter func(allConnections []sig.Pub) (destinations sig.PubList,
	sendToRecv, sendToSend bool, sendRange SendRange)

type RcvMsg

type RcvMsg struct {
	CameFrom     int                              // For debugging to see where this message was processed
	Msg          []*deserialized.DeserializedItem // A list of deserialized messages
	SendRecvChan *SendRecvChannel                 // The channel that the messages were received from (or nil if it is not availble)
	IsLocal      bool                             // True if the messages were sent from the local node
}

RcvMsg is the struct returned by the Recv call of MainChannel

type SendChannel

type SendChannel interface {
	// ConnectSend() error
	// Close closes a connection
	Close(closeType ChannelCloseType) error
	Send([]byte) error
	// SendReturn([]byte, NetConInfo) error
	// GetConnID() string
	GetConnInfos() NetNodeInfo
	// ConnectRecv(chan RcvMsg, NetConInfo) error
	GetType() types.NetworkProtocolType
}

SendChannel represents a connection to an external node An object implementing MainChannel will keep a SendChannel for each open connection it has

func ForwardAll

func ForwardAll(sendChans []SendChannel) []SendChannel

type SendRange

type SendRange struct {
	From, Until int
}

SendRange is used as a percentage of the recipients returned from NewForwardFuncFilter to send to.

func BaseP2pForward

func BaseP2pForward(allPubs []sig.Pub) (pubs sig.PubList, sendToRecv, sendToSend bool,
	sendRange SendRange)

func BaseP2pForwardBackHalf

func BaseP2pForwardBackHalf(allPubs []sig.Pub) (pubs sig.PubList, sendToRecv, sendToSend bool,
	sendRange SendRange)

func BaseP2pForwardFrontHalf

func BaseP2pForwardFrontHalf(allPubs []sig.Pub) (pubs sig.PubList, sendToRecv, sendToSend bool,
	sendRange SendRange)

func BaseP2pNoProgress

func BaseP2pNoProgress(allPubs []sig.Pub) (pubs sig.PubList, sendToRecv, sendToSend bool,
	sendRange SendRange)

func ForwardAllPub

func ForwardAllPub(allPubs []sig.Pub) (pubs sig.PubList, sendToRecv,
	sendToSend bool, sendRange SendRange)

func ForwardAllPubBackHalf

func ForwardAllPubBackHalf(allPubs []sig.Pub) (pubs sig.PubList, sendToRecv,
	sendToSend bool, sendRange SendRange)

func ForwardAllPubFrontHalf

func ForwardAllPubFrontHalf(allPubs []sig.Pub) (pubs sig.PubList, sendToRecv,
	sendToSend bool, sendRange SendRange)

func (SendRange) ComputeIndicies

func (sc SendRange) ComputeIndicies(sliLen int) (startIndex, endIndex int)

type SendRecvChannel

type SendRecvChannel struct {
	MainChan   MainChannel // MainChan is where messages are received from
	ReturnChan SendChannel // ReturnChan is for sending messages back to the receiver
}

SendRecChannel stores information about a channel that can send and recv messages.

func (*SendRecvChannel) Close

func (src *SendRecvChannel) Close(closeType ChannelCloseType)

Close closes the channels.

type SimpleBehaviorTracker

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

SimpleBehaviorTracker keeps state on each connection through a ConBehaviorInfo objet stored in a map and protected by a mutex. The operations of this object are safe to access from multiple threads. TODO better tracking/more scalaible/garbage collection.

func NewSimpleBehaviorTracker

func NewSimpleBehaviorTracker() *SimpleBehaviorTracker

NewSimpleBehaviorTracker returns an empty simple behavior tracker object,

func (*SimpleBehaviorTracker) CheckShouldReject

func (bt *SimpleBehaviorTracker) CheckShouldReject(conInfo interface{}) bool

CheckShouldReject returns true if the connection should be rejected based on past behaviour.

func (*SimpleBehaviorTracker) GotError

func (bt *SimpleBehaviorTracker) GotError(err error, conInfo interface{}) bool

GotError should be called whenever there was an error on the connection conInfo. For example it sent an incorrect/duplicate message. Or there was a problem with the connection itself. Returns true if requestions from the connection should be rejected.

func (*SimpleBehaviorTracker) RequestedOldDecisions

func (bt *SimpleBehaviorTracker) RequestedOldDecisions(conInfo interface{}) bool

RequestedOldDecisions should be called when conInfo requested an old decided value, returns true if the request should be rejected.

type TimerInterface

type TimerInterface interface {
	// Stop should be called when the timer is no longer needed.
	Stop() bool
}

TimerInterface is returned from SendToSelf. The process that creates the timer is responsible for stopping it before the test returns.

Jump to

Keyboard shortcuts

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