network

package
v0.51.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2019 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CommandType

type CommandType string

CommandType represents the type of a message command.

const (
	CMDAddr        CommandType = "addr"
	CMDBlock       CommandType = "block"
	CMDConsensus   CommandType = "consensus"
	CMDFilterAdd   CommandType = "filteradd"
	CMDFilterClear CommandType = "filterclear"
	CMDFilterLoad  CommandType = "filterload"
	CMDGetAddr     CommandType = "getaddr"
	CMDGetBlocks   CommandType = "getblocks"
	CMDGetData     CommandType = "getdata"
	CMDGetHeaders  CommandType = "getheaders"
	CMDHeaders     CommandType = "headers"
	CMDInv         CommandType = "inv"
	CMDMempool     CommandType = "mempool"
	CMDMerkleBlock CommandType = "merkleblock"
	CMDPing        CommandType = "ping"
	CMDPong        CommandType = "pong"
	CMDTX          CommandType = "tx"
	CMDUnknown     CommandType = "unknown"
	CMDVerack      CommandType = "verack"
	CMDVersion     CommandType = "version"
)

Valid protocol commands used to send between nodes.

type DefaultDiscovery

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

DefaultDiscovery default implementation of the Discoverer interface.

func NewDefaultDiscovery

func NewDefaultDiscovery(dt time.Duration, ts Transporter) *DefaultDiscovery

NewDefaultDiscovery returns a new DefaultDiscovery.

func (*DefaultDiscovery) BackFill

func (d *DefaultDiscovery) BackFill(addrs ...string)

BackFill implements the Discoverer interface and will backfill the the pool with the given addresses.

func (*DefaultDiscovery) BadPeers

func (d *DefaultDiscovery) BadPeers() []string

BadPeers returns all addresses of bad addrs.

func (*DefaultDiscovery) GoodPeers

func (d *DefaultDiscovery) GoodPeers() []string

GoodPeers returns all addresses of known good peers (that at least once succeeded handshaking with us).

func (*DefaultDiscovery) PoolCount

func (d *DefaultDiscovery) PoolCount() int

PoolCount returns the number of available node addresses.

func (*DefaultDiscovery) RegisterBadAddr

func (d *DefaultDiscovery) RegisterBadAddr(addr string)

RegisterBadAddr registers the given address as a bad address.

func (*DefaultDiscovery) RegisterGoodAddr

func (d *DefaultDiscovery) RegisterGoodAddr(s string)

RegisterGoodAddr registers good known connected address that passed handshake successfully.

func (*DefaultDiscovery) RequestRemote

func (d *DefaultDiscovery) RequestRemote(n int)

RequestRemote will try to establish a connection with n nodes.

func (*DefaultDiscovery) UnconnectedPeers

func (d *DefaultDiscovery) UnconnectedPeers() []string

UnconnectedPeers returns all addresses of unconnected addrs.

func (*DefaultDiscovery) UnregisterConnectedAddr

func (d *DefaultDiscovery) UnregisterConnectedAddr(s string)

UnregisterConnectedAddr tells discoverer that this address is no longer connected, but it still is considered as good one.

type Discoverer

type Discoverer interface {
	BackFill(...string)
	PoolCount() int
	RequestRemote(int)
	RegisterBadAddr(string)
	RegisterGoodAddr(string)
	UnregisterConnectedAddr(string)
	UnconnectedPeers() []string
	BadPeers() []string
	GoodPeers() []string
}

Discoverer is an interface that is responsible for maintaining a healthy connection pool.

type Message

type Message struct {
	// NetMode of the node that sends this message.
	Magic config.NetMode

	// Command is utf8 code, of which the length is 12 bytes,
	// the extra part is filled with 0.
	Command [cmdSize]byte

	// Length of the payload
	Length uint32

	// Checksum is the first 4 bytes of the value that two times SHA256
	// hash of the payload
	Checksum uint32

	// Payload send with the message.
	Payload payload.Payload
}

Message is the complete message send between nodes.

func NewMessage

func NewMessage(magic config.NetMode, cmd CommandType, p payload.Payload) *Message

NewMessage returns a new message with the given payload.

func (*Message) CommandType

func (m *Message) CommandType() CommandType

CommandType converts the 12 byte command slice to a CommandType.

func (*Message) Decode

func (m *Message) Decode(br *io.BinReader) error

Decode a Message from the given reader.

func (*Message) Encode

func (m *Message) Encode(br *io.BinWriter) error

Encode a Message to any given BinWriter.

type Peer

type Peer interface {
	NetAddr() *net.TCPAddr
	Disconnect(error)
	WriteMsg(msg *Message) error
	Done() chan error
	Version() *payload.Version
	Handshaked() bool
	SendVersion(*Message) error
	SendVersionAck(*Message) error
	HandleVersion(*payload.Version) error
	HandleVersionAck() error
}

Peer represents a network node neo-go is connected to.

type RelayReason

type RelayReason uint8

RelayReason is the type which describes the different relay outcome

const (
	RelaySucceed RelayReason = iota
	RelayAlreadyExists
	RelayOutOfMemory
	RelayUnableToVerify
	RelayInvalid
	RelayPolicyFail
	RelayUnknown
)

List of valid RelayReason.

type Server

type Server struct {
	// ServerConfig holds the Server configuration.
	ServerConfig
	// contains filtered or unexported fields
}

Server represents the local Node in the network. Its transport could be of any kind.

func NewServer

func NewServer(config ServerConfig, chain core.Blockchainer) *Server

NewServer returns a new Server, initialized with the given configuration.

func (*Server) BadPeers

func (s *Server) BadPeers() []string

BadPeers returns a list of peers the are flagged as "bad" peers.

func (*Server) ID

func (s *Server) ID() uint32

ID returns the servers ID.

func (*Server) PeerCount

func (s *Server) PeerCount() int

PeerCount returns the number of current connected peers.

func (*Server) Peers

func (s *Server) Peers() map[Peer]bool

Peers returns the current list of peers connected to the server.

func (*Server) RelayDirectly

func (s *Server) RelayDirectly(p Peer, inv *payload.Inventory)

RelayDirectly relay directly the inventory to the remote peers. Reference: the method OnRelayDirectly in C#: https://github.com/neo-project/neo/blob/master/neo/Network/P2P/LocalNode.cs#L166

func (*Server) RelayTxn

func (s *Server) RelayTxn(t *transaction.Transaction) RelayReason

RelayTxn a new transaction to the local node and the connected peers. Reference: the method OnRelay in C#: https://github.com/neo-project/neo/blob/master/neo/Network/P2P/LocalNode.cs#L159

func (*Server) Shutdown

func (s *Server) Shutdown()

Shutdown disconnects all peers and stops listening.

func (*Server) Start

func (s *Server) Start(errChan chan error)

Start will start the server and its underlying transport.

func (*Server) UnconnectedPeers

func (s *Server) UnconnectedPeers() []string

UnconnectedPeers returns a list of peers that are in the discovery peer list but are not connected to the server.

type ServerConfig

type ServerConfig struct {
	// MaxPeers it the maximum numbers of peers that can
	// be connected to the server.
	MaxPeers int

	// The user agent of the server.
	UserAgent string

	// The listen address of the TCP server.
	ListenTCP uint16

	// The network mode the server will operate on.
	// ModePrivNet docker private network.
	// ModeTestNet NEO test network.
	// ModeMainNet NEO main network.
	Net config.NetMode

	// Relay determines whether the server is forwarding its inventory.
	Relay bool

	// Seeds are a list of initial nodes used to establish connectivity.
	Seeds []string

	// Maximum duration a single dial may take.
	DialTimeout time.Duration

	// The duration between protocol ticks with each connected peer.
	// When this is 0, the default interval of 5 seconds will be used.
	ProtoTickInterval time.Duration

	// Level of the internal logger.
	LogLevel log.Level
}

ServerConfig holds the server configuration.

func NewServerConfig

func NewServerConfig(cfg config.Config) ServerConfig

NewServerConfig creates a new ServerConfig struct using the main applications config.

type TCPPeer

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

TCPPeer represents a connected remote node in the network over TCP.

func NewTCPPeer

func NewTCPPeer(conn net.Conn) *TCPPeer

NewTCPPeer returns a TCPPeer structure based on the given connection.

func (*TCPPeer) Disconnect

func (p *TCPPeer) Disconnect(err error)

Disconnect will fill the peer's done channel with the given error.

func (*TCPPeer) Done

func (p *TCPPeer) Done() chan error

Done implements the Peer interface and notifies all other resources operating on it that this peer is no longer running.

func (*TCPPeer) HandleVersion

func (p *TCPPeer) HandleVersion(version *payload.Version) error

HandleVersion checks for the handshake state and version message contents.

func (*TCPPeer) HandleVersionAck

func (p *TCPPeer) HandleVersionAck() error

HandleVersionAck checks handshake sequence correctness when VerAck message is received.

func (*TCPPeer) Handshaked

func (p *TCPPeer) Handshaked() bool

Handshaked returns status of the handshake, whether it's completed or not.

func (*TCPPeer) NetAddr

func (p *TCPPeer) NetAddr() *net.TCPAddr

NetAddr implements the Peer interface.

func (*TCPPeer) SendVersion

func (p *TCPPeer) SendVersion(msg *Message) error

SendVersion checks for the handshake state and sends a message to the peer.

func (*TCPPeer) SendVersionAck

func (p *TCPPeer) SendVersionAck(msg *Message) error

SendVersionAck checks for the handshake state and sends a message to the peer.

func (*TCPPeer) Version

func (p *TCPPeer) Version() *payload.Version

Version implements the Peer interface.

func (*TCPPeer) WriteMsg

func (p *TCPPeer) WriteMsg(msg *Message) error

WriteMsg implements the Peer interface. This will write/encode the message to the underlying connection, this only works for messages other than Version or VerAck.

type TCPTransport

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

TCPTransport allows network communication over TCP.

func NewTCPTransport

func NewTCPTransport(s *Server, bindAddr string) *TCPTransport

NewTCPTransport return a new TCPTransport that will listen for new incoming peer connections.

func (*TCPTransport) Accept

func (t *TCPTransport) Accept()

Accept implements the Transporter interface.

func (*TCPTransport) Close

func (t *TCPTransport) Close()

Close implements the Transporter interface.

func (*TCPTransport) Dial

func (t *TCPTransport) Dial(addr string, timeout time.Duration) error

Dial implements the Transporter interface.

func (*TCPTransport) Proto

func (t *TCPTransport) Proto() string

Proto implements the Transporter interface.

type Transporter

type Transporter interface {
	Dial(addr string, timeout time.Duration) error
	Accept()
	Proto() string
	Close()
}

Transporter is an interface that allows us to abstract any form of communication between the server and its peers.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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