libp2pnet

package
v0.0.0-...-e18b226 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2022 License: Apache-2.0 Imports: 34 Imported by: 1

Documentation

Index

Constants

View Source
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"
)
View Source
const (
	// Random
	Random eliminationStrategy = iota + 1
	// FIFO FIRST_IN_FIRST_OUT
	FIFO
	// LIFO LAST_IN_FIRST_OUT
	LIFO
)
View Source
const DefaultEliminationStrategy = LIFO

DefaultEliminationStrategy is the default strategy for elimination.

View Source
const (
	// DefaultLibp2pPubSubMaxMessageSize is the default max message size for pub-sub.
	DefaultLibp2pPubSubMaxMessageSize = 50 << 20
)
View Source
const DefaultMaxPeerCountAllow = 20

DefaultMaxPeerCountAllow is the default max peer count allow.

View Source
const DefaultMessageSendTimeout = 3 * time.Second

DefaultMessageSendTimeout is the default timeout for sending msg.

View Source
const (
	// DefaultStreamPoolCap is the default cap of stream pool.
	DefaultStreamPoolCap int = 100
)
View Source
const DefaultTryTimes = 50
View Source
const MsgPID = protocol.ID("/HerculesNetMsg/1.0.0/")

MsgPID is the protocol.ID of hercules net msg.

Variables

View Source
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")
)
View Source
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")
)
View Source
var Config *netConfig

Functions

func NewStreamReadHandlerFunc

func NewStreamReadHandlerFunc(ln *LibP2pNet) func(stream network.Stream)

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 NewBlackList

func NewBlackList() *BlackList

NewBlackList create new BlackList instance.

func (*BlackList) AddIPAndPort

func (b *BlackList) AddIPAndPort(ip string, port int)

AddIPAndPort add new IP and Port record to blacklist. If you want to control IP only, set port=0 pls.

func (*BlackList) AddPeerId

func (b *BlackList) AddPeerId(pid peer.ID)

AddPeerId add new peer.ID to blacklist.

func (*BlackList) ContainsIPAndPort

func (b *BlackList) ContainsIPAndPort(ip string, port int) bool

ContainsIPAndPort return whether IP and Port exist in blacklist. If not found ip+port, but found ip only, return true.

func (*BlackList) ContainsPeerId

func (b *BlackList) ContainsPeerId(pid peer.ID) bool

ContainsPeerId return whether peer.ID given exist in blacklist.

func (*BlackList) RemoveIPAndPort

func (b *BlackList) RemoveIPAndPort(ip string, port int)

RemoveIPAndPort remove IP and Port record from blacklist.

func (*BlackList) RemovePeerId

func (b *BlackList) RemovePeerId(pid peer.ID)

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

func (cg *ConnGater) InterceptAddrDial(p peer.ID, mu multiaddr.Multiaddr) bool

InterceptAddrDial

func (*ConnGater) InterceptPeerDial

func (cg *ConnGater) InterceptPeerDial(p peer.ID) bool

InterceptPeerDial

func (*ConnGater) InterceptSecured

func (cg *ConnGater) InterceptSecured(d network.Direction, p peer.ID, cm network.ConnMultiaddrs) bool

InterceptSecured

func (*ConnGater) InterceptUpgraded

func (cg *ConnGater) InterceptUpgraded(c network.Conn) (bool, control.DisconnectReason)

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) Context

func (lh *LibP2pHost) Context() context.Context

Context

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) Host

func (lh *LibP2pHost) Host() host.Host

Host is libp2p.Host.

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

func (*LibP2pHost) Start

func (lh *LibP2pHost) Start() error

Start libP2pHost.

func (*LibP2pHost) Stop

func (lh *LibP2pHost) Stop() error

Stop libP2pHost.

type LibP2pNet

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

LibP2pNet is an implementation of net.Net interface.

func NewLibP2pNet

func NewLibP2pNet(log api.Logger) (*LibP2pNet, error)

NewLibP2pNet create a new LibP2pNet instance.

func (*LibP2pNet) AddAC

func (ln *LibP2pNet) AddAC(chainId string, ac api.AccessControlProvider)

func (*LibP2pNet) AddSeed

func (ln *LibP2pNet) AddSeed(seed string) error

AddSeed add a seed node address. It can be a consensus node address.

func (*LibP2pNet) BroadcastWithChainId

func (ln *LibP2pNet) BroadcastWithChainId(chainId string, topic string, data []byte) error

BroadcastWithChainId broadcast a msg to the given topic of the target chain which id is the given chainId .

func (*LibP2pNet) CancelDirectMsgHandle

func (ln *LibP2pNet) CancelDirectMsgHandle(chainId string, msgFlag string) error

CancelDirectMsgHandle unregister a DirectMsgHandler for handling msg received.

func (*LibP2pNet) CancelSubscribeWithChainId

func (ln *LibP2pNet) CancelSubscribeWithChainId(chainId string, topic string) error

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

func (ln *LibP2pNet) GetNodeUid() string

GetNodeUid is the unique id of node.

func (*LibP2pNet) GetNodeUidByCertId

func (ln *LibP2pNet) GetNodeUidByCertId(certId string) (string, error)

GetNodeUidByCertId

func (*LibP2pNet) InitPubSub

func (ln *LibP2pNet) InitPubSub(chainId string, maxMessageSize int) error

InitPubSub will create new LibP2pPubSub instance for LibP2pNet with setting pub-sub uid to the given chainId .

func (*LibP2pNet) IsRunning

func (ln *LibP2pNet) IsRunning() bool

IsRunning

func (*LibP2pNet) Prepare

func (ln *LibP2pNet) Prepare() *LibP2pNetPrepare

func (*LibP2pNet) ReVerifyPeers

func (ln *LibP2pNet) ReVerifyPeers(chainId string)

ReVerifyPeers will verify permission of peers existed with the access control module of the chain which id is the given chainId.

func (*LibP2pNet) RefreshSeeds

func (ln *LibP2pNet) RefreshSeeds(seeds []string) error

RefreshSeeds reset addresses of seed nodes with given.

func (*LibP2pNet) SendMsg

func (ln *LibP2pNet) SendMsg(chainId string, node string, msgFlag string, data []byte) error

SendMsg send a msg to the given node belong to the given chain.

func (*LibP2pNet) SetChainCustomTrustRoots

func (ln *LibP2pNet) SetChainCustomTrustRoots(chainId string, roots [][]byte)

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 (ln *LibP2pNet) SetCompressMsgBytes(enable bool)

func (*LibP2pNet) SetMsgPriority

func (ln *LibP2pNet) SetMsgPriority(msgFlag string, priority uint8)

func (*LibP2pNet) Start

func (ln *LibP2pNet) Start() error

Start

func (*LibP2pNet) Stop

func (ln *LibP2pNet) Stop() error

Stop

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) Start

func (ps *LibP2pPubSub) Start() error

Start

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) AddConn

func (cm *PeerConnManager) AddConn(pid peer.ID, conn network.Conn) bool

AddConn add a connection.

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

func (cm *PeerConnManager) RemoveConn(pid peer.ID, conn network.Conn) bool

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.

Jump to

Keyboard shortcuts

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