libp2pnet

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2023 License: Apache-2.0 Imports: 35 Imported by: 6

Documentation

Index

Constants

View Source
const (
	// DefaultTryTimes is the default try times. Max timeout is 10m10s.
	DefaultTryTimes = 15
	// DefaultTryTimesAfterMaxTime is the default try times after max timeout, which is 90 days.
	DefaultTryTimesAfterMaxTime = 6 * 24 * 90
)
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 = "chainmaker-libp2p-net"
	// MaxReadBuff max []byte to make once
	MaxReadBuff = 1 << 34 // 17G
)
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 = 100

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 MsgPID = protocol.ID("/ChainMakerNetMsg/1.0.0/")

MsgPID is the protocol.ID of chainmaker 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 Config *netConfig

Functions

func NewPeerSendMsgHandler added in v1.2.1

func NewPeerSendMsgHandler(hostContext context.Context, pid peer.ID,
	s network.Stream, log protocol.Logger) *peerSendMsgHandler

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 . @Description: setup a discovery service. @param host: @param readySignalC: @param enableDHTBootstrapProvider: @param bootstraps: @param log: @return error:

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 . @Description: create new BlackList instance. @return *BlackList:

func (*BlackList) AddIPAndPort

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

AddIPAndPort . @Description: add new IP and Port record to blacklist.

If you want to control IP only, set port=0 pls.

@receiver b @param ip: @param port:

func (*BlackList) AddPeerId

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

AddPeerId . @Description: add new peer.ID to blacklist. @receiver b @param pid:

func (*BlackList) ContainsIPAndPort

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

ContainsIPAndPort . @Description: return whether IP and Port exist in blacklist.

If not found ip+port, but found ip only, return true.

@receiver b @param ip: @param port: @return bool:

func (*BlackList) ContainsPeerId

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

ContainsPeerId . @Description: return whether peer.ID given exist in blacklist. @receiver b @param pid: @return bool:

func (*BlackList) RemoveIPAndPort

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

RemoveIPAndPort . @Description: remove IP and Port record from blacklist. @receiver b @param ip: @param port:

func (*BlackList) RemovePeerId

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

RemovePeerId . @Description: remove peer.ID given from blacklist. @receiver b @param pid:

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

NewConnGater . @Description: @param connManager: @param blackList: @param memberStatusValidator: @param log: @return *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 . @Description: @receiver cg @param p: @param mu: @return bool:

func (*ConnGater) InterceptPeerDial

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

InterceptPeerDial . @Description: @receiver cg @param p: @return bool:

func (*ConnGater) InterceptSecured

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

InterceptSecured . @Description: @receiver cg @param d: @param p: @param cm: @return bool:

func (*ConnGater) InterceptUpgraded

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

InterceptUpgraded . @Description: @receiver cg @param c: @return bool: @return control.DisconnectReason:

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) DHT added in v1.2.1

func (lh *LibP2pHost) DHT() *dht.IpfsDHT

DHT is libp2p.peerDHT

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) 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) IsConnected added in v1.2.1

func (ln *LibP2pNet) IsConnected(nodeId string) bool

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 . @Description: add a node address for connecting directly.

It can be a seed node address or a consensus node address.

@receiver l @param bootstrapAddr:

func (*LibP2pNetPrepare) SetCert

func (l *LibP2pNetPrepare) SetCert(certPem []byte)

SetCert . @Description: set cert with pem bytes. @receiver l @param certPem:

func (*LibP2pNetPrepare) SetEncCert added in v1.2.0

func (l *LibP2pNetPrepare) SetEncCert(certPem []byte)

SetEncCert . @Description: set cert with pem bytes. @receiver l @param certPem:

func (*LibP2pNetPrepare) SetEncKey added in v1.2.0

func (l *LibP2pNetPrepare) SetEncKey(keyPem []byte)

SetEncKey . @Description: set private key with pem bytes. @receiver l @param keyPem:

func (*LibP2pNetPrepare) SetIsInsecurity

func (l *LibP2pNetPrepare) SetIsInsecurity(isInsecurity bool)

SetIsInsecurity . @Description: @receiver l @param isInsecurity:

func (*LibP2pNetPrepare) SetKey

func (l *LibP2pNetPrepare) SetKey(keyPem []byte)

SetKey . @Description: set private key with pem bytes. @receiver l @param keyPem:

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 . @Description: set max count of nodes that allow to connect to us. @receiver l @param maxPeerCountAllow:

func (*LibP2pNetPrepare) SetPeerEliminationStrategy

func (l *LibP2pNetPrepare) SetPeerEliminationStrategy(peerEliminationStrategy int)

SetPeerEliminationStrategy . @Description: set the strategy for eliminating when reach the max count. @receiver l @param peerEliminationStrategy:

func (*LibP2pNetPrepare) SetPeerStreamPoolSize

func (l *LibP2pNetPrepare) SetPeerStreamPoolSize(peerStreamPoolSize int)

SetPeerStreamPoolSize . @Description: set stream pool max size of each peer. @receiver l @param peerStreamPoolSize:

func (*LibP2pNetPrepare) SetPktEnable

func (l *LibP2pNetPrepare) SetPktEnable(pktEnable bool)

SetPktEnable . @Description: @receiver l @param pktEnable:

func (*LibP2pNetPrepare) SetPriorityCtrlEnable

func (l *LibP2pNetPrepare) SetPriorityCtrlEnable(priorityCtrlEnable bool)

SetPriorityCtrlEnable . @Description: @receiver l @param priorityCtrlEnable:

func (*LibP2pNetPrepare) SetPubKeyModeEnable

func (l *LibP2pNetPrepare) SetPubKeyModeEnable(pkModeEnable bool)

SetPubKeyModeEnable . @Description: set whether to use public key mode of permission. @receiver l @param pkModeEnable:

func (*LibP2pNetPrepare) SetPubSubMaxMsgSize

func (l *LibP2pNetPrepare) SetPubSubMaxMsgSize(pubSubMaxMsgSize int)

SetPubSubMaxMsgSize . @Description: set max msg size for pub-sub service.(M) @receiver l @param pubSubMaxMsgSize:

func (*LibP2pNetPrepare) SetReadySignalC

func (l *LibP2pNetPrepare) SetReadySignalC(readySignalC chan struct{})

SetReadySignalC . @Description: @receiver l @param readySignalC:

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 . @Description: create a new LibP2pPubSub instance. @param pubsubUid: @param host: @param maxMessageSize: @return *LibP2pPubSub: @return error:

func (*LibP2pPubSub) AddWhitelistPeer

func (ps *LibP2pPubSub) AddWhitelistPeer(pid peer.ID) error

AddWhitelistPeer . @Description: add a peer.ID to pubsub white list. @receiver ps @param pid: @return error:

func (*LibP2pPubSub) GetTopic

func (ps *LibP2pPubSub) GetTopic(name string) (*pubsub.Topic, error)

GetTopic . @Description: get a topic with the name given. @receiver ps @param name: @return *pubsub.Topic: @return error:

func (*LibP2pPubSub) Publish

func (ps *LibP2pPubSub) Publish(topic string, data []byte) error

Publish . @Description: a msg to the topic. @receiver ps @param topic: @param data: @return error:

func (*LibP2pPubSub) RemoveWhitelistPeer

func (ps *LibP2pPubSub) RemoveWhitelistPeer(pid peer.ID) error

RemoveWhitelistPeer . @Description: remove a peer.ID to pubsub white list. @receiver ps @param pid: @return error:

func (*LibP2pPubSub) Start

func (ps *LibP2pPubSub) Start() error

Start . @Description: @receiver ps @return error:

func (*LibP2pPubSub) Subscribe

func (ps *LibP2pPubSub) Subscribe(topic string) (*pubsub.Subscription, error)

Subscribe . @Description: Subscribe a topic. @receiver ps @param topic: @return *pubsub.Subscription: @return error:

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 added in v1.1.0

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.

Jump to

Keyboard shortcuts

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