Documentation ¶
Index ¶
- Constants
- Variables
- func NewStreamReadHandlerFunc(ln *LibP2pNet) func(stream network.Stream)
- func SetupDiscovery(host *LibP2pHost, readySignalC chan struct{}, enableDHTBootstrapProvider bool, ...) error
- type BlackList
- func (b *BlackList) AddIPAndPort(ip string, port int)
- func (b *BlackList) AddPeerId(pid peer.ID)
- func (b *BlackList) ContainsIPAndPort(ip string, port int) bool
- func (b *BlackList) ContainsPeerId(pid peer.ID) bool
- func (b *BlackList) RemoveIPAndPort(ip string, port int)
- func (b *BlackList) RemovePeerId(pid peer.ID)
- type ConnGater
- func (cg *ConnGater) InterceptAccept(cm network.ConnMultiaddrs) bool
- func (cg *ConnGater) InterceptAddrDial(p peer.ID, mu multiaddr.Multiaddr) bool
- func (cg *ConnGater) InterceptPeerDial(p peer.ID) bool
- func (cg *ConnGater) InterceptSecured(d network.Direction, p peer.ID, cm network.ConnMultiaddrs) bool
- func (cg *ConnGater) InterceptUpgraded(c network.Conn) (bool, control.DisconnectReason)
- type ConnSupervisor
- type LibP2pHost
- func (lh *LibP2pHost) Context() context.Context
- func (lh *LibP2pHost) HasConnected(peerId peer.ID) bool
- func (lh *LibP2pHost) Host() host.Host
- func (lh *LibP2pHost) IsRunning() bool
- func (lh *LibP2pHost) PeerStreamManager() *PeerStreamManager
- func (lh *LibP2pHost) Start() error
- func (lh *LibP2pHost) Stop() error
- type LibP2pNet
- func (ln *LibP2pNet) AddAC(chainId string, ac api.AccessControlProvider)
- func (ln *LibP2pNet) AddSeed(seed string) error
- func (ln *LibP2pNet) BroadcastWithChainId(chainId string, topic string, data []byte) error
- func (ln *LibP2pNet) CancelDirectMsgHandle(chainId string, msgFlag string) error
- func (ln *LibP2pNet) CancelSubscribeWithChainId(chainId string, topic string) error
- func (ln *LibP2pNet) ChainNodesInfo(chainId string) ([]*api.ChainNodeInfo, error)
- func (ln *LibP2pNet) DirectMsgHandle(chainId string, msgFlag string, handler api.DirectMsgHandler) error
- func (ln *LibP2pNet) GetNodeUid() string
- func (ln *LibP2pNet) GetNodeUidByCertId(certId string) (string, error)
- func (ln *LibP2pNet) InitPubSub(chainId string, maxMessageSize int) error
- func (ln *LibP2pNet) IsRunning() bool
- func (ln *LibP2pNet) Prepare() *LibP2pNetPrepare
- func (ln *LibP2pNet) ReVerifyPeers(chainId string)
- func (ln *LibP2pNet) RefreshSeeds(seeds []string) error
- func (ln *LibP2pNet) SendMsg(chainId string, node string, msgFlag string, data []byte) error
- func (ln *LibP2pNet) SetChainCustomTrustRoots(chainId string, roots [][]byte)
- func (ln *LibP2pNet) SetCompressMsgBytes(enable bool)
- func (ln *LibP2pNet) SetMsgPriority(msgFlag string, priority uint8)
- func (ln *LibP2pNet) Start() error
- func (ln *LibP2pNet) Stop() error
- func (ln *LibP2pNet) SubscribeWithChainId(chainId string, topic string, handler api.PubSubMsgHandler) error
- type LibP2pNetPrepare
- func (l *LibP2pNetPrepare) AddBlackAddress(address string)
- func (l *LibP2pNetPrepare) AddBlackPeerId(pid string)
- func (l *LibP2pNetPrepare) AddBootstrapsPeer(bootstrapAddr string)
- func (l *LibP2pNetPrepare) SetCert(certPem []byte)
- func (l *LibP2pNetPrepare) SetIsInsecurity(isInsecurity bool)
- func (l *LibP2pNetPrepare) SetKey(keyPem []byte)
- func (l *LibP2pNetPrepare) SetListenAddr(listenAddr string)
- func (l *LibP2pNetPrepare) SetMaxPeerCountAllow(maxPeerCountAllow int)
- func (l *LibP2pNetPrepare) SetPeerEliminationStrategy(peerEliminationStrategy int)
- func (l *LibP2pNetPrepare) SetPeerStreamPoolSize(peerStreamPoolSize int)
- func (l *LibP2pNetPrepare) SetPktEnable(pktEnable bool)
- func (l *LibP2pNetPrepare) SetPriorityCtrlEnable(priorityCtrlEnable bool)
- func (l *LibP2pNetPrepare) SetPubKeyModeEnable(pkModeEnable bool)
- func (l *LibP2pNetPrepare) SetPubSubMaxMsgSize(pubSubMaxMsgSize int)
- func (l *LibP2pNetPrepare) SetReadySignalC(readySignalC chan struct{})
- type LibP2pPubSub
- func (ps *LibP2pPubSub) AddWhitelistPeer(pid peer.ID) error
- func (ps *LibP2pPubSub) GetTopic(name string) (*pubsub.Topic, error)
- func (ps *LibP2pPubSub) Publish(topic string, data []byte) error
- func (ps *LibP2pPubSub) RemoveWhitelistPeer(pid peer.ID) error
- func (ps *LibP2pPubSub) Start() error
- func (ps *LibP2pPubSub) Subscribe(topic string) (*pubsub.Subscription, error)
- type MessageHandlerDistributor
- type PeerConnManager
- func (cm *PeerConnManager) AddAsHighLevelPeer(peerId peer.ID)
- func (cm *PeerConnManager) AddConn(pid peer.ID, conn network.Conn) bool
- func (cm *PeerConnManager) CanConnect(pid peer.ID) bool
- func (cm *PeerConnManager) ClearHighLevelPeer()
- func (cm *PeerConnManager) ConnCount() int
- func (cm *PeerConnManager) GetConn(pid peer.ID) network.Conn
- func (cm *PeerConnManager) GetConns(pid peer.ID) []network.Conn
- func (cm *PeerConnManager) IsConnected(pid peer.ID) bool
- func (cm *PeerConnManager) IsHighLevel(peerId peer.ID) bool
- func (cm *PeerConnManager) RemoveConn(pid peer.ID, conn network.Conn) bool
- func (cm *PeerConnManager) RemoveHighLevelPeer(peerId peer.ID)
- func (cm *PeerConnManager) SetMaxSize(maxSize int)
- func (cm *PeerConnManager) SetStrategy(strategy int)
- type PeerStreamManager
- type StreamPool
Constants ¶
const ( // DefaultLibp2pListenAddress is the default address that libp2p will listen on. DefaultLibp2pListenAddress = "/ip4/0.0.0.0/tcp/0" // DefaultLibp2pServiceTag is the default service tag for discovery service finding. DefaultLibp2pServiceTag = "hercules-libp2p-net" )
const ( // Random Random eliminationStrategy = iota + 1 // FIFO FIRST_IN_FIRST_OUT FIFO // LIFO LAST_IN_FIRST_OUT LIFO )
const DefaultEliminationStrategy = LIFO
DefaultEliminationStrategy is the default strategy for elimination.
const (
// DefaultLibp2pPubSubMaxMessageSize is the default max message size for pub-sub.
DefaultLibp2pPubSubMaxMessageSize = 50 << 20
)
const DefaultMaxPeerCountAllow = 20
DefaultMaxPeerCountAllow is the default max peer count allow.
const DefaultMessageSendTimeout = 3 * time.Second
DefaultMessageSendTimeout is the default timeout for sending msg.
const ( // DefaultStreamPoolCap is the default cap of stream pool. DefaultStreamPoolCap int = 100 )
const DefaultTryTimes = 50
const MsgPID = protocol.ID("/HerculesNetMsg/1.0.0/")
MsgPID is the protocol.ID of hercules net msg.
Variables ¶
var ( ErrorPubSubNotExist = errors.New("pub-sub service not exist") ErrorPubSubExisted = errors.New("pub-sub service existed") ErrorTopicSubscribed = errors.New("topic has been subscribed") ErrorSendMsgIncompletely = errors.New("send msg incompletely") ErrorNotConnected = errors.New("node not connected") ErrorNotBelongToChain = errors.New("node not belong to chain") )
var ( // ErrStreamPoolClosed will be returned if stream pool closed. ErrStreamPoolClosed = errors.New("stream pool closed") // ErrNoStreamCanBeBorrowed will be returned if no stream can be borrowed. ErrNoStreamCanBeBorrowed = errors.New("no stream can be borrowed") )
var Config *netConfig
Functions ¶
func NewStreamReadHandlerFunc ¶
NewStreamReadHandlerFunc create new function for listening stream reading.
func SetupDiscovery ¶
func SetupDiscovery(host *LibP2pHost, readySignalC chan struct{}, enableDHTBootstrapProvider bool, bootstraps []string, log protocol.Logger) error
SetupDiscovery setup a discovery service.
Types ¶
type BlackList ¶
type BlackList struct {
// contains filtered or unexported fields
}
BlackList is a blacklist for controlling nodes connection.
func (*BlackList) AddIPAndPort ¶
AddIPAndPort add new IP and Port record to blacklist. If you want to control IP only, set port=0 pls.
func (*BlackList) ContainsIPAndPort ¶
ContainsIPAndPort return whether IP and Port exist in blacklist. If not found ip+port, but found ip only, return true.
func (*BlackList) ContainsPeerId ¶
ContainsPeerId return whether peer.ID given exist in blacklist.
func (*BlackList) RemoveIPAndPort ¶
RemoveIPAndPort remove IP and Port record from blacklist.
func (*BlackList) RemovePeerId ¶
RemovePeerId remove peer.ID given from blacklist.
type ConnGater ¶
type ConnGater struct {
// contains filtered or unexported fields
}
ConnGater is an implementation of ConnectionGater interface.
func NewConnGater ¶
func NewConnGater( connManager *PeerConnManager, blackList *BlackList, memberStatusValidator *common.MemberStatusValidator, log protocol.Logger) *ConnGater
func (*ConnGater) InterceptAccept ¶
func (cg *ConnGater) InterceptAccept(cm network.ConnMultiaddrs) bool
InterceptAccept will be checked first when other peer connect to us.
func (*ConnGater) InterceptAddrDial ¶
InterceptAddrDial
func (*ConnGater) InterceptPeerDial ¶
InterceptPeerDial
func (*ConnGater) InterceptSecured ¶
func (cg *ConnGater) InterceptSecured(d network.Direction, p peer.ID, cm network.ConnMultiaddrs) bool
InterceptSecured
func (*ConnGater) InterceptUpgraded ¶
InterceptUpgraded
type ConnSupervisor ¶
type ConnSupervisor struct {
// contains filtered or unexported fields
}
ConnSupervisor is a connections supervisor.
type LibP2pHost ¶
type LibP2pHost struct {
// contains filtered or unexported fields
}
LibP2pHost is a libP2pHost which use libp2p as local net provider.
func NewLibP2pHost ¶
func NewLibP2pHost(ctx context.Context, log protocol.Logger) *LibP2pHost
NewLibP2pHost create new LibP2pHost instance.
func (*LibP2pHost) HasConnected ¶
func (lh *LibP2pHost) HasConnected(peerId peer.ID) bool
HasConnected return true if the peer which id is the peerId given has connected. Otherwise return false.
func (*LibP2pHost) IsRunning ¶
func (lh *LibP2pHost) IsRunning() bool
IsRunning return true when libp2p has started up.Otherwise return false.
func (*LibP2pHost) PeerStreamManager ¶
func (lh *LibP2pHost) PeerStreamManager() *PeerStreamManager
PeerStreamManager
type LibP2pNet ¶
type LibP2pNet struct {
// contains filtered or unexported fields
}
LibP2pNet is an implementation of net.Net interface.
func NewLibP2pNet ¶
NewLibP2pNet create a new LibP2pNet instance.
func (*LibP2pNet) BroadcastWithChainId ¶
BroadcastWithChainId broadcast a msg to the given topic of the target chain which id is the given chainId .
func (*LibP2pNet) CancelDirectMsgHandle ¶
CancelDirectMsgHandle unregister a DirectMsgHandler for handling msg received.
func (*LibP2pNet) CancelSubscribeWithChainId ¶
CancelSubscribeWithChainId cancel subscribing the given topic of the target chain which id is the given chainId.
func (*LibP2pNet) ChainNodesInfo ¶
func (ln *LibP2pNet) ChainNodesInfo(chainId string) ([]*api.ChainNodeInfo, error)
ChainNodesInfo
func (*LibP2pNet) DirectMsgHandle ¶
func (ln *LibP2pNet) DirectMsgHandle(chainId string, msgFlag string, handler api.DirectMsgHandler) error
DirectMsgHandle register a DirectMsgHandler for handling msg received.
func (*LibP2pNet) GetNodeUid ¶
GetNodeUid is the unique id of node.
func (*LibP2pNet) GetNodeUidByCertId ¶
GetNodeUidByCertId
func (*LibP2pNet) InitPubSub ¶
InitPubSub will create new LibP2pPubSub instance for LibP2pNet with setting pub-sub uid to the given chainId .
func (*LibP2pNet) Prepare ¶
func (ln *LibP2pNet) Prepare() *LibP2pNetPrepare
func (*LibP2pNet) ReVerifyPeers ¶
ReVerifyPeers will verify permission of peers existed with the access control module of the chain which id is the given chainId.
func (*LibP2pNet) RefreshSeeds ¶
RefreshSeeds reset addresses of seed nodes with given.
func (*LibP2pNet) SetChainCustomTrustRoots ¶
SetChainCustomTrustRoots set custom trust roots of chain. In cert permission mode, if it is failed when verifying cert by access control of chains, the cert will be verified by custom trust root pool again.
func (*LibP2pNet) SetCompressMsgBytes ¶
func (*LibP2pNet) SetMsgPriority ¶
func (*LibP2pNet) SubscribeWithChainId ¶
func (ln *LibP2pNet) SubscribeWithChainId(chainId string, topic string, handler api.PubSubMsgHandler) error
SubscribeWithChainId subscribe the given topic of the target chain which id is the given chainId with the given sub-msg handler function.
type LibP2pNetPrepare ¶
type LibP2pNetPrepare struct {
// contains filtered or unexported fields
}
LibP2pNetPrepare prepare the config options.
func (*LibP2pNetPrepare) AddBlackAddress ¶
func (l *LibP2pNetPrepare) AddBlackAddress(address string)
AddBlackAddress add a black address to blacklist.
example: 192.168.1.14:8080 example: 192.168.1.14
func (*LibP2pNetPrepare) AddBlackPeerId ¶
func (l *LibP2pNetPrepare) AddBlackPeerId(pid string)
AddBlackPeerId add a black node id to blacklist.
example: QmcQHCuAXaFkbcsPUj7e37hXXfZ9DdN7bozseo5oX4qiC4
func (*LibP2pNetPrepare) AddBootstrapsPeer ¶
func (l *LibP2pNetPrepare) AddBootstrapsPeer(bootstrapAddr string)
AddBootstrapsPeer add a node address for connecting directly. It can be a seed node address or a consensus node address.
func (*LibP2pNetPrepare) SetCert ¶
func (l *LibP2pNetPrepare) SetCert(certPem []byte)
SetCert set cert with pem bytes.
func (*LibP2pNetPrepare) SetIsInsecurity ¶
func (l *LibP2pNetPrepare) SetIsInsecurity(isInsecurity bool)
func (*LibP2pNetPrepare) SetKey ¶
func (l *LibP2pNetPrepare) SetKey(keyPem []byte)
SetKey set private key with pem bytes.
func (*LibP2pNetPrepare) SetListenAddr ¶
func (l *LibP2pNetPrepare) SetListenAddr(listenAddr string)
SetListenAddr set address that the net will listen on.
example: /ip4/127.0.0.1/tcp/10001
func (*LibP2pNetPrepare) SetMaxPeerCountAllow ¶
func (l *LibP2pNetPrepare) SetMaxPeerCountAllow(maxPeerCountAllow int)
SetMaxPeerCountAllow set max count of nodes that allow to connect to us.
func (*LibP2pNetPrepare) SetPeerEliminationStrategy ¶
func (l *LibP2pNetPrepare) SetPeerEliminationStrategy(peerEliminationStrategy int)
SetPeerEliminationStrategy set the strategy for eliminating when reach the max count.
func (*LibP2pNetPrepare) SetPeerStreamPoolSize ¶
func (l *LibP2pNetPrepare) SetPeerStreamPoolSize(peerStreamPoolSize int)
SetPeerStreamPoolSize set stream pool max size of each peer.
func (*LibP2pNetPrepare) SetPktEnable ¶
func (l *LibP2pNetPrepare) SetPktEnable(pktEnable bool)
func (*LibP2pNetPrepare) SetPriorityCtrlEnable ¶
func (l *LibP2pNetPrepare) SetPriorityCtrlEnable(priorityCtrlEnable bool)
func (*LibP2pNetPrepare) SetPubKeyModeEnable ¶
func (l *LibP2pNetPrepare) SetPubKeyModeEnable(pkModeEnable bool)
SetPubKeyModeEnable set whether to use public key mode of permission.
func (*LibP2pNetPrepare) SetPubSubMaxMsgSize ¶
func (l *LibP2pNetPrepare) SetPubSubMaxMsgSize(pubSubMaxMsgSize int)
SetPubSubMaxMsgSize set max msg size for pub-sub service.(M)
func (*LibP2pNetPrepare) SetReadySignalC ¶
func (l *LibP2pNetPrepare) SetReadySignalC(readySignalC chan struct{})
type LibP2pPubSub ¶
type LibP2pPubSub struct {
// contains filtered or unexported fields
}
LibP2pPubSub is a pub-sub service implementation.
func NewPubsub ¶
func NewPubsub(pubsubUid string, host *LibP2pHost, maxMessageSize int) (*LibP2pPubSub, error)
NewPubsub create a new LibP2pPubSub instance.
func (*LibP2pPubSub) AddWhitelistPeer ¶
func (ps *LibP2pPubSub) AddWhitelistPeer(pid peer.ID) error
AddWhitelistPeer add a peer.ID to pubsub white list.
func (*LibP2pPubSub) GetTopic ¶
func (ps *LibP2pPubSub) GetTopic(name string) (*pubsub.Topic, error)
GetTopic get a topic with the name given.
func (*LibP2pPubSub) Publish ¶
func (ps *LibP2pPubSub) Publish(topic string, data []byte) error
Publish a msg to the topic.
func (*LibP2pPubSub) RemoveWhitelistPeer ¶
func (ps *LibP2pPubSub) RemoveWhitelistPeer(pid peer.ID) error
RemoveWhitelistPeer remove a peer.ID to pubsub white list.
func (*LibP2pPubSub) Subscribe ¶
func (ps *LibP2pPubSub) Subscribe(topic string) (*pubsub.Subscription, error)
Subscribe a topic.
type MessageHandlerDistributor ¶
type MessageHandlerDistributor struct {
// contains filtered or unexported fields
}
MessageHandlerDistributor is a MessageHandler distributor.
type PeerConnManager ¶
type PeerConnManager struct {
// contains filtered or unexported fields
}
PeerConnManager is a connection manager of peers.
func NewPeerConnManager ¶
func NewPeerConnManager(log protocol.Logger) *PeerConnManager
NewPeerConnManager create a new PeerConnManager.
func (*PeerConnManager) AddAsHighLevelPeer ¶
func (cm *PeerConnManager) AddAsHighLevelPeer(peerId peer.ID)
AddAsHighLevelPeer add a peer id as high level peer.
func (*PeerConnManager) CanConnect ¶
func (cm *PeerConnManager) CanConnect(pid peer.ID) bool
CanConnect return true if peer can connect to self. Otherwise, return false.
func (*PeerConnManager) ClearHighLevelPeer ¶
func (cm *PeerConnManager) ClearHighLevelPeer()
ClearHighLevelPeer clear all high level peer id records.
func (*PeerConnManager) ConnCount ¶
func (cm *PeerConnManager) ConnCount() int
ConnCount return the count num of connections.
func (*PeerConnManager) GetConn ¶
func (cm *PeerConnManager) GetConn(pid peer.ID) network.Conn
GetConn return a connection for peer.
func (*PeerConnManager) GetConns ¶
func (cm *PeerConnManager) GetConns(pid peer.ID) []network.Conn
GetConns return a connection for peer.
func (*PeerConnManager) IsConnected ¶
func (cm *PeerConnManager) IsConnected(pid peer.ID) bool
IsConnected return true if peer has connected. Otherwise, return false.
func (*PeerConnManager) IsHighLevel ¶
func (cm *PeerConnManager) IsHighLevel(peerId peer.ID) bool
IsHighLevel return true if the peer which is high-level (consensus & seeds) node. Otherwise, return false.
func (*PeerConnManager) RemoveConn ¶
RemoveConn remove a connection.
func (*PeerConnManager) RemoveHighLevelPeer ¶
func (cm *PeerConnManager) RemoveHighLevelPeer(peerId peer.ID)
RemoveHighLevelPeer remove a high level peer id.
func (*PeerConnManager) SetMaxSize ¶
func (cm *PeerConnManager) SetMaxSize(maxSize int)
SetMaxSize set max count of peers allowed. If not set, default is 20.
func (*PeerConnManager) SetStrategy ¶
func (cm *PeerConnManager) SetStrategy(strategy int)
SetStrategy set the elimination strategy. If not set, default is LIFO.
type PeerStreamManager ¶
type PeerStreamManager struct {
// contains filtered or unexported fields
}
PeerStreamManager is a stream manager of peers.
type StreamPool ¶
type StreamPool struct {
// contains filtered or unexported fields
}
StreamPool is a stream pool.
Source Files ¶
- libp2p_blacklist.go
- libp2p_config.go
- libp2p_connection_gater.go
- libp2p_connection_supervisor.go
- libp2p_discovery.go
- libp2p_host.go
- libp2p_message_handler_distributor.go
- libp2p_net.go
- libp2p_net_prepare.go
- libp2p_peer_connection_manager.go
- libp2p_peer_stream_manager.go
- libp2p_pkt_adapter.go
- libp2p_pubsub.go
- libp2p_stream_pool.go