network

package
v0.0.0-...-a968a3f Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2022 License: Apache-2.0 Imports: 30 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TimesToRetryConnect   = 10    // connect retry times
	MaxBytesCanBroadcast  = 65536 // the max length of a full broadcast msg
	NumOfFullBroadcast    = 1     // full msg count to broadcast when msg was too large
	RecentReceivePoolSize = 5000  // recent receive msg pool size
	RecentMsgPoolSize     = 200   // recent send msg pool size
	NewWantDetailLockSize = 500   // recent WantDetail msg pool size

	SECLen = 16
	MACLen = 16
)
View Source
const (
	// max peer count
	MaxPeerCount = 21
	// max count for dialing in nodes
	MaxPendCount = 21

	// Tcp handshake version
	TcpHandShakerVersion = 2000000 // nopos

)
View Source
const MsgTypeLength int = 2

Variables

View Source
var (
	HandProofMsgType MsgType = [MsgTypeLength]byte{0, 0}
	PingMsgType      MsgType = [MsgTypeLength]byte{0, 1}
	PongMsgType      MsgType = [MsgTypeLength]byte{0, 2}
	DiscMsgType      MsgType = [MsgTypeLength]byte{0, 3}
	EventMsgType     MsgType = [MsgTypeLength]byte{0, 255}

	PingMsg = &Msg{
		MsgType: &PingMsgType,
		Payload: []byte{1},
	}
	PongMsg = &Msg{
		MsgType: &PongMsgType,
		Payload: []byte{2},
	}
	DiscMsg = &Msg{
		MsgType: &DiscMsgType,
		Payload: []byte{3},
	}
)
View Source
var (
	ErrInsertSameMsg    = errors.New("insert the same msg")
	ErrAlreadyConnected = errors.New("already connect to net")
)
View Source
var (
	SystemRecentRecPool = RecentReceivePool{
		// contains filtered or unexported fields
	}
)

Functions

func AddrIP

func AddrIP(addr net.Addr) net.IP

func Body2EventLoad

func Body2EventLoad(eventType models.EventType, body []byte) (eventLoad []byte)

func GetEventTypeFromMsgLoad

func GetEventTypeFromMsgLoad(msgLoad []byte) (models.EventType, error)

func GetEventTypeFromPayLoad

func GetEventTypeFromPayLoad(payLoad []byte) (models.EventType, error)

func NewP2PServer

func NewP2PServer(con map[string]common.NodeID, bootport uint16, localport uint16, eventer models.Eventer,
	chainId common.ChainID, bootId common.ChainID, netType common.NetType, infos []*common.ChainInfos,
	pms []byte, callback models.ConnectedCallBackFunc) (models.P2PServer, error)

func PayLoad2Body

func PayLoad2Body(payLoad []byte) (eventType models.EventType, body []byte, pub []byte, sig []byte, err error)

func ReadP2PMsg

func ReadP2PMsg(msg *Msg) (models.EventType, interface{}, error)

p2p.Msg to MsgEvent

func SendReasonAndClose

func SendReasonAndClose(conn net.Conn, enc cipher.Stream, erro error)

func UnpackP2PMsg

func UnpackP2PMsg(msg *Msg) (eventLoad []byte, eventType models.EventType, body []byte, pub, sig []byte, err error)

msgLoad: body + eventType + pub + sig eventType: msg type 2 bytes body: serialized msg pub: public key sig: signature for eventType+bydy

func WriteEventLoad

func WriteEventLoad(v interface{}, needSign bool) (eventType models.EventType, eventLoad, pub, sig []byte, err error)

Types

type CallbackFun

type CallbackFun func(peer *Peer, flag int, peerCount int, inboundCount int) error

type CheckPermissionFunc

type CheckPermissionFunc func(cid common.ChainID, nid common.NodeID, ntt common.NetType, proof []byte) error

type Dialer

type Dialer interface {
	Dial(network string, node *discover.Node) (net.Conn, error)
}

type DiscReason

type DiscReason uint
const (
	DiscRequested DiscReason = iota
	DiscNetworkError
	DiscProtocolError
	DiscUselessPeer
	DiscTooManyPeers
	DiscTooManyInboundPeers
	DiscAlreadyConnected
	DiscIncompatibleVersion
	DiscInvalidIdentity
	DiscQuitting
	DiscUnexpectedIdentity
	DiscSelf
	DiscReadTimeout
	DiscDifferentChain
	DiscDifferentNet
	DiscInvalidIP
	DiscTryTooOften
	DiscTooManyChildToChildPeers
	DiscMsgTooLarge
	DiscSubprotocolError = 0x13
)

func (DiscReason) Error

func (d DiscReason) Error() string

func (DiscReason) String

func (d DiscReason) String() string

type HandShakeReq

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

type HandShakeRsp

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

type HandShaker

type HandShaker interface {
	//get handshake ChainID
	GetChainID() (common.ChainID, error)

	// hand shake with a node
	ShakeHandWith(node *discover.Node) (net.Conn, *Secrets, error)

	// verify the incoming node's proof
	VerifyPeerProof(net.Conn) (*discover.Node, common.ChainID, *Secrets, error)
}

type HandleMsgFunc

type HandleMsgFunc func(peer *Peer, msg *Msg) error

type Listener

type Listener interface {
	net.Listener
	Listen(network string, addr string) error
}

type Manager

type Manager struct {
	common.AbstractService
	// contains filtered or unexported fields
}

func NewManager

func NewManager(portrange *[2]uint16, eventer models.Eventer) (*Manager, error)

func (*Manager) BroadcastFull

func (m *Manager) BroadcastFull(info string, skip *common.NodeID, cid common.ChainID, ntp common.NetType,
	pb interface{}, pub, sig []byte) error

func (*Manager) BroadcastFullSync

func (m *Manager) BroadcastFullSync(info string, skip *common.NodeID, cid common.ChainID, ntp common.NetType,
	pb interface{}, pub, sig []byte) error

func (*Manager) ClearNetWorker

func (m *Manager) ClearNetWorker(id common.ChainID)

func (*Manager) Closer

func (m *Manager) Closer() error

stop manager's all networkers

func (*Manager) ConnectNet

func (m *Manager) ConnectNet(chaininfo *common.ChainInfos) error

func (*Manager) CreateOneNet

func (m *Manager) CreateOneNet(cid common.ChainID, ntp common.NetType) error

func (*Manager) CreateOrConnectNet

func (m *Manager) CreateOrConnectNet(ntp common.NetType, bootChainID, localChandID common.ChainID) error

func (*Manager) GetAllNetInfomap

func (m *Manager) GetAllNetInfomap() map[common.ChainID]map[common.NodeID]common.NetInfo

func (*Manager) GetBootMap

func (m *Manager) GetBootMap() map[string]common.NodeID

func (*Manager) GetChainNet

func (m *Manager) GetChainNet(id common.ChainID, netType common.NetType) (map[common.NodeID]net.Addr, bool)

func (*Manager) GetDataServer

func (m *Manager) GetDataServer(chainId common.ChainID) *[]common.Dataserver

func (*Manager) GetNetInfomap

func (m *Manager) GetNetInfomap(chainid common.ChainID) (map[common.NodeID]common.NetInfo, bool)

func (*Manager) GetNetworker

func (m *Manager) GetNetworker(id common.ChainID) models.Networker

func (*Manager) InitChain

func (m *Manager) InitChain(id common.ChainID) error

func (*Manager) InitNet

func (m *Manager) InitNet(chaininfo *common.ChainInfos) error

func (*Manager) Initializer

func (m *Manager) Initializer() error

create networker for main chain

func (*Manager) IsBootNode

func (m *Manager) IsBootNode(id common.ChainID) bool

func (*Manager) Rand

func (m *Manager) Rand(size int, info string, chainId common.ChainID, ntp common.NetType, msg interface{},
	pub, sig []byte, skips ...*common.NodeID) error

func (*Manager) ResetConNet

func (m *Manager) ResetConNet(networker models.Networker, chainid common.ChainID,
	netType common.NetType) (common.NodeID, error)

func (*Manager) ResetNet

func (m *Manager) ResetNet(chainid common.ChainID, ntp common.NetType) error

func (*Manager) ResetOneNet

func (m *Manager) ResetOneNet(cid common.ChainID, ntp common.NetType) error

func (*Manager) SendToChain

func (m *Manager) SendToChain(info string, ntp common.NetType, fromChain common.ChainID, toChain common.ChainID,
	pb interface{}, pub, sig []byte) error

func (*Manager) SendToNode

func (m *Manager) SendToNode(info string, ntp common.NetType, chainId common.ChainID, toNodes common.NodeIDs,
	pb interface{}, pub, sig []byte) error

func (*Manager) SendToPeer

func (m *Manager) SendToPeer(info string, ntp common.NetType, chainId common.ChainID, toNodes common.NodeIDs,
	pb interface{}, pub, sig []byte) error

func (*Manager) SetDataManager

func (m *Manager) SetDataManager(dmanager models.DataManager)

func (*Manager) StartConNet

func (m *Manager) StartConNet(networker models.Networker, chainid common.ChainID,
	netType common.NetType) (common.NodeID, error)

func (*Manager) Starter

func (m *Manager) Starter() error

func (*Manager) Status

func (m *Manager) Status()

func (*Manager) StopOneChain

func (m *Manager) StopOneChain(id *common.ChainID) error

stop manager's one networker

func (*Manager) StopOneNet

func (m *Manager) StopOneNet(cid common.ChainID, ntp common.NetType) (int, error)

stop manager's one net of a networker

func (*Manager) String

func (m *Manager) String() string

type Msg

type Msg struct {
	MsgType    *MsgType
	Payload    []byte
	ReceivedAt time.Time
}

func CopyP2PMsg

func CopyP2PMsg(pm Msg) Msg

func PackP2PMsg

func PackP2PMsg(eventOrPayLoad, pub, sig []byte) *Msg

func WriteP2PMsg

func WriteP2PMsg(pb interface{}, needSign bool) (models.EventType, *Msg, error)

message to p2p.Msg

func (*Msg) LoadSize

func (msg *Msg) LoadSize() int

type MsgHandler

type MsgHandler interface {
	// interface to handle the received p2p msg
	HandleMsg(peer *Peer, msg *Msg) error
}

type MsgType

type MsgType [MsgTypeLength]byte

func (*MsgType) Bytes

func (t *MsgType) Bytes() [MsgTypeLength]byte

type NetWorker

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

func NewNetWorker

func NewNetWorker(chainID common.ChainID, eventer models.Eventer, dmanager models.DataManager, bootservers map[string]common.NodeID,
	pool *PortPool) *NetWorker

func (*NetWorker) AbandonUselessPeers

func (n *NetWorker) AbandonUselessPeers(nt common.NetType)

func (*NetWorker) Broadcast

func (n *NetWorker) Broadcast(info string, typ common.NetType, msg interface{}, pub, sig []byte,
	skips ...*common.NodeID) ([]byte, []byte, error)

func (*NetWorker) BroadcastSync

func (n *NetWorker) BroadcastSync(info string, typ common.NetType, msg interface{}, pub, sig []byte,
	skips ...*common.NodeID) ([]byte, []byte, error)

func (*NetWorker) Close

func (n *NetWorker) Close() error

func (*NetWorker) Connect

func (n *NetWorker) Connect(typ common.NetType, bootId common.ChainID, boots map[common.NodeID]net.Addr, infos []*common.ChainInfos, permission []byte, callback models.ConnectedCallBackFunc) (err error)

connect to a boot node

func (*NetWorker) Create

func (n *NetWorker) Create(typ common.NetType, address net.Addr, boots map[common.NodeID]net.Addr, infos []*common.ChainInfos, callback models.ConnectedCallBackFunc) error

start a boot node

func (*NetWorker) Exit

func (n *NetWorker) Exit(typ common.NetType) (int, error)

quit from current network

func (*NetWorker) GetChainID

func (n *NetWorker) GetChainID() common.ChainID

func (*NetWorker) IsIn

func (n *NetWorker) IsIn(netType common.NetType) bool

func (*NetWorker) NetTypes

func (n *NetWorker) NetTypes() (types []common.NetType)

func (*NetWorker) Rand

func (n *NetWorker) Rand(size int, info string, typ common.NetType, msg interface{},
	pub, sig []byte, skips ...*common.NodeID) ([]byte, []byte, error)

func (*NetWorker) ReplaceDataNodes

func (n *NetWorker) ReplaceDataNodes(nt common.NetType)

func (*NetWorker) Reset

func (n *NetWorker) Reset(typ common.NetType, addr net.Addr, callback models.ConnectedCallBackFunc) error

func (*NetWorker) SendToChain

func (n *NetWorker) SendToChain(info string, typ common.NetType, chainid common.ChainID,
	msg interface{}, pub, sig []byte) ([]byte, []byte, error)

func (*NetWorker) SendToNode

func (n *NetWorker) SendToNode(info string, typ common.NetType, nodeids common.NodeIDs,
	msg interface{}, pub, sig []byte) ([]byte, []byte, error)

func (*NetWorker) SendToPeer

func (n *NetWorker) SendToPeer(info string, typ common.NetType, nodeids common.NodeIDs,
	msg interface{}, pub, sig []byte) ([]byte, []byte, error)

func (*NetWorker) SetTmpDataNodes

func (n *NetWorker) SetTmpDataNodes(nt common.NetType)

type Peer

type Peer struct {
	discover.Node

	RW net.Conn
	MC chan *Msg
	// contains filtered or unexported fields
}

func NewPeer

func NewPeer(n discover.Node, chainId common.ChainID, con net.Conn, flag connFlag, sec *Secrets, logger logrus.FieldLogger, handleFunc HandleMsgFunc, callbackFun CallbackFun) *Peer

func (*Peer) Disconnect

func (p *Peer) Disconnect(reason DiscReason)

func (*Peer) Ping

func (p *Peer) Ping() error

func (*Peer) PingLoop

func (p *Peer) PingLoop()

检查对端节点活性

func (*Peer) Pong

func (p *Peer) Pong() error

func (*Peer) ReadLoop

func (p *Peer) ReadLoop(readErrChan chan<- error)

func (*Peer) ReadMsg

func (p *Peer) ReadMsg() (*Msg, error)

length(4 bytes) + type(2 bytes) + msg body

func (*Peer) Run

func (p *Peer) Run() (err error)

func (*Peer) WriteMsg

func (p *Peer) WriteMsg(msg *Msg) error

func (*Peer) WriteMsgLoad

func (p *Peer) WriteMsgLoad(msgLoad []byte) error

type PortPool

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

func NewPortPool

func NewPortPool(start uint16, end uint16) *PortPool

func (*PortPool) Get

func (p *PortPool) Get() (uint16, bool)

func (*PortPool) Put

func (p *PortPool) Put(port uint16)

type RecentMsgPool

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

func NewRecentMsgPool

func NewRecentMsgPool(size int) *RecentMsgPool

func (*RecentMsgPool) Clear

func (rm *RecentMsgPool) Clear()

func (*RecentMsgPool) GetLoad

func (rm *RecentMsgPool) GetLoad(loadHash common.Hash) (load []byte, exist bool)

func (*RecentMsgPool) IsExist

func (rm *RecentMsgPool) IsExist(loadHash common.Hash) bool

func (*RecentMsgPool) PutLoad

func (rm *RecentMsgPool) PutLoad(key common.Hash, load []byte) error

func (*RecentMsgPool) Start

func (rm *RecentMsgPool) Start()

func (*RecentMsgPool) Stop

func (rm *RecentMsgPool) Stop()

type RecentReceivePool

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

func (*RecentReceivePool) Add

func (p *RecentReceivePool) Add(hashOfLoad common.Hash, fromid *common.NodeID) bool

func (*RecentReceivePool) IsExist

func (p *RecentReceivePool) IsExist(hashOfLoad common.Hash, fromid *common.NodeID) (exist bool, inNodes bool)

type Secrets

type Secrets struct {
	AES []byte
	MAC []byte
}

func (*Secrets) String

func (s *Secrets) String() string

type Server

type Server struct {
	SID uint64

	discover.Node
	discover.P2PConfig

	Peers sync.Map

	ChainToPeers sync.Map

	Eventer models.Eventer
	// contains filtered or unexported fields
}

func (*Server) AbandonUselessPeers

func (s *Server) AbandonUselessPeers()

abandon useless peers

func (*Server) BootChain

func (s *Server) BootChain() common.ChainID

func (*Server) BroadcastAsync

func (s *Server) BroadcastAsync(info string, msgv interface{}, pub, sig []byte, skips ...*common.NodeID) ([]byte, []byte, error)

func (*Server) BroadcastFull

func (s *Server) BroadcastFull(info string, msgv interface{}, pub, sig []byte, skips ...*common.NodeID) error

func (*Server) BroadcastFullPayLoad

func (s *Server) BroadcastFullPayLoad(info string, eventType models.EventType,
	eventLoad, pub, sig []byte, skips ...*common.NodeID) error

func (*Server) BroadcastPartPayLoad

func (s *Server) BroadcastPartPayLoad(info string, eventType models.EventType,
	eventLoad, pub, sig []byte, skips ...*common.NodeID) error

func (*Server) BroadcastSync

func (s *Server) BroadcastSync(info string, msgv interface{}, pub, sig []byte, skips ...*common.NodeID) ([]byte, []byte, error)

func (*Server) DiscoverTypeIsSRT

func (s *Server) DiscoverTypeIsSRT() bool

func (*Server) HandPeerInfo

func (s *Server) HandPeerInfo(p *Peer, flag int, peerCount int, inboundCount int) error

func (*Server) HandleMsg

func (s *Server) HandleMsg(peer *Peer, msg *Msg) error

func (*Server) LocalPort

func (s *Server) LocalPort() uint16

func (*Server) NewPeerLogger

func (s *Server) NewPeerLogger(nid common.NodeID) logrus.FieldLogger

func (*Server) NodeID

func (s *Server) NodeID() *common.NodeID

func (*Server) PeerCount

func (s *Server) PeerCount() int

func (*Server) PeerIDs

func (s *Server) PeerIDs() []common.NodeID

func (*Server) RandBroadcast

func (s *Server) RandBroadcast(size int, info string, msgv interface{}, pub, sig []byte,
	skips ...*common.NodeID) ([]byte, []byte, error)

func (*Server) ReplaceDataNodes

func (s *Server) ReplaceDataNodes()

change the current chain structure with tmp and clear tmp

func (*Server) SendToChain

func (s *Server) SendToChain(info string, chainid common.ChainID, pb interface{}, pub, sig []byte) ([]byte, []byte, error)

func (*Server) SendToNode

func (s *Server) SendToNode(info string, toNodes common.NodeIDs, pb interface{}, pub, sig []byte) ([]byte, []byte, error)

func (*Server) SendToPeer

func (s *Server) SendToPeer(info string, toNodes common.NodeIDs, pb interface{}, pub, sig []byte) ([]byte, []byte, error)

func (*Server) SetTmpDataNodes

func (s *Server) SetTmpDataNodes(infos []*common.ChainInfos)

set the new chain structure to tmp

func (*Server) Start

func (s *Server) Start() error

func (*Server) Stop

func (s *Server) Stop()

type TcpDialer

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

func NewTcpDialer

func NewTcpDialer() *TcpDialer

func (*TcpDialer) Dial

func (t *TcpDialer) Dial(network string, node *discover.Node) (net.Conn, error)

type TcpHandShaker

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

func (*TcpHandShaker) GetChainID

func (s *TcpHandShaker) GetChainID() (common.ChainID, error)

func (*TcpHandShaker) ShakeHandWith

func (s *TcpHandShaker) ShakeHandWith(node *discover.Node) (net.Conn, *Secrets, error)

func (*TcpHandShaker) VerifyPeerProof

func (s *TcpHandShaker) VerifyPeerProof(con net.Conn) (*discover.Node, common.ChainID, *Secrets, error)

type TcpListener

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

func (*TcpListener) Accept

func (t *TcpListener) Accept() (net.Conn, error)

Accept waits for and returns the next connection to the listener.

func (*TcpListener) Addr

func (t *TcpListener) Addr() net.Addr

Addr returns the listener's network address.

func (*TcpListener) Close

func (t *TcpListener) Close() error

Close closes the listener. Any blocked Accept operations will be unblocked and return errors.

func (*TcpListener) Listen

func (t *TcpListener) Listen(network string, addr string) error

type WantDetailLock

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

lock for WantDetailEvent

func NewWantDetailLock

func NewWantDetailLock(size int) *WantDetailLock

func (*WantDetailLock) Lock

func (w *WantDetailLock) Lock(h common.Hash) bool

func (*WantDetailLock) Unlock

func (w *WantDetailLock) Unlock(h common.Hash)

func (*WantDetailLock) UnlockAll

func (w *WantDetailLock) UnlockAll()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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