p2p

package module
v0.3.6 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2024 License: Apache-2.0 Imports: 35 Imported by: 6

README

go-p2p

It's the assembly of libp2p to build a working network layer for p2p applications.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultConfig is a set of default configs
	DefaultConfig = Config{
		HostName:                 "127.0.0.1",
		Port:                     30001,
		ExternalHostName:         "",
		ExternalPort:             30001,
		SecureIO:                 false,
		Gossip:                   false,
		ConnectTimeout:           time.Minute,
		BlackListTimeout:         10 * time.Minute,
		MasterKey:                "",
		Relay:                    "disable",
		ConnLowWater:             200,
		ConnHighWater:            500,
		RateLimiterLRUSize:       1000,
		BlockListLRUSize:         1000,
		BlockListErrorThreshold:  3,
		BlockListCleanupInterval: 600 * time.Second,
		ConnGracePeriod:          0,
		EnableRateLimit:          false,
		RateLimit:                DefaultRatelimitConfig,
		PrivateNetworkPSK:        "",
		ProtocolID:               "/iotex",
		GroupID:                  "iotex",
		MaxPeer:                  30,
		MaxMessageSize:           12582912,
		BlacklistTolerance:       3,
	}

	// DefaultRatelimitConfig is the default rate limit config
	DefaultRatelimitConfig = RateLimitConfig{
		GlobalUnicastAvg:   300,
		GlobalUnicastBurst: 500,
		PeerAvg:            300,
		PeerBurst:          500,
	}
)

Functions

func EnsureIPv4

func EnsureIPv4(ipOrHost string) (string, error)

EnsureIPv4 returns an IPv4 address regardless the input is a IPv4 address or host name. If the host name has multiple IPv4 address be associated, a random one will be returned.

func GetBroadcastMsg

func GetBroadcastMsg(ctx context.Context) (*pubsub.Message, bool)

GetBroadcastMsg retrieves *pubsub.Message from broadcast message context.

func GetUnicastStream

func GetUnicastStream(ctx context.Context) (core.Stream, bool)

GetUnicastStream retrieves net.Stream from unicast request context.

func Logger

func Logger() *zap.Logger

Logger returns the logger

func SetLogger

func SetLogger(l *zap.Logger)

SetLogger sets the logger

Types

type Config

type Config struct {
	ProtocolID               protocol.ID     `yaml:"protocolID "`
	HostName                 string          `yaml:"hostName"`
	Port                     int             `yaml:"port"`
	ExternalHostName         string          `yaml:"externalHostName"`
	ExternalPort             int             `yaml:"externalPort"`
	SecureIO                 bool            `yaml:"secureIO"`
	Gossip                   bool            `yaml:"gossip"`
	ConnectTimeout           time.Duration   `yaml:"connectTimeout"`
	BlackListTimeout         time.Duration   `yaml:"blackListTimeout"`
	MasterKey                string          `yaml:"masterKey"`
	Relay                    string          `yaml:"relay"` // could be `active`, `nat`, `disable`
	ConnLowWater             int             `yaml:"connLowWater"`
	ConnHighWater            int             `yaml:"connHighWater"`
	RateLimiterLRUSize       int             `yaml:"rateLimiterLRUSize"`
	BlockListLRUSize         int             `yaml:"blockListLRUSize"`
	BlockListErrorThreshold  int             `yaml:"blockListErrorThreshold"`
	BlockListCleanupInterval time.Duration   `yaml:"blockListCleanupInterval"`
	ConnGracePeriod          time.Duration   `yaml:"connGracePeriod"`
	EnableRateLimit          bool            `yaml:"enableRateLimit"`
	RateLimit                RateLimitConfig `yaml:"rateLimit"`
	PrivateNetworkPSK        string          `yaml:"privateNetworkPSK"`
	GroupID                  string          `yaml:"groupID"`
	MaxPeer                  int             `yaml:"maxPeer"`
	MaxMessageSize           int             `yaml:"maxMessageSize"`
	BlacklistTolerance       int             `yaml:"blacklistTolerance"`
}

Config enumerates the configs required by a host

type HandleBroadcast

type HandleBroadcast func(ctx context.Context, data []byte) error

HandleBroadcast defines the callback function triggered when a broadcast message reaches a host

type HandleUnicast

type HandleUnicast func(context.Context, peer.AddrInfo, []byte) error

HandleUnicast defines the callback function triggered when a unicast message reaches a host

type Host

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

Host is the main struct that represents a host that communicating with the rest of the P2P networks

func NewHost

func NewHost(ctx context.Context, options ...Option) (*Host, error)

NewHost constructs a host struct

func (*Host) AddBootstrap added in v0.3.4

func (h *Host) AddBootstrap(bootNodeAddrs []multiaddr.Multiaddr) error

AddBootstrap adds bootnode into peermanager

func (*Host) AddBroadcastPubSub

func (h *Host) AddBroadcastPubSub(ctx context.Context, topic string, callback HandleBroadcast) error

AddBroadcastPubSub adds a broadcast topic that the host will pay attention to. This need to be called before using Connect/JoinOverlay. Otherwise, pubsub may not be aware of the existing overlay topology

func (*Host) AddUnicastPubSub

func (h *Host) AddUnicastPubSub(topic string, callback HandleUnicast) error

AddUnicastPubSub adds a unicast topic that the host will pay attention to

func (*Host) Addresses

func (h *Host) Addresses() []multiaddr.Multiaddr

Addresses returns the multi address

func (*Host) AdvertiseAsync added in v0.3.5

func (h *Host) AdvertiseAsync() error

AdvertiseAsync publish the groupID of host to the dht network

func (*Host) BlockPeer added in v0.3.4

func (h *Host) BlockPeer(id peer.ID)

BlockPeer add the peer into the blocklist

func (*Host) Broadcast

func (h *Host) Broadcast(ctx context.Context, topic string, data []byte) error

Broadcast sends a message to the hosts who subscribe the topic

func (*Host) ClearBlocklist added in v0.3.0

func (h *Host) ClearBlocklist()

ClearBlocklist clears the blocklist

func (*Host) Close

func (h *Host) Close() error

Close closes the host

func (*Host) Connect

func (h *Host) Connect(ctx context.Context, target core.PeerAddrInfo) error

Connect connects a peer.

func (*Host) ConnectWithMultiaddr

func (h *Host) ConnectWithMultiaddr(ctx context.Context, ma multiaddr.Multiaddr) error

ConnectWithMultiaddr connects a peer given the multi address

func (*Host) ConnectedPeers added in v0.3.4

func (h *Host) ConnectedPeers() []core.PeerAddrInfo

ConnectedPeers returns the connected peers' addrinfo

func (*Host) FindPeersAsync added in v0.3.5

func (h *Host) FindPeersAsync() error

FindPeersAsync connect host to the peers with the same groupID

func (*Host) HostIdentity

func (h *Host) HostIdentity() string

HostIdentity returns the host identity string

func (*Host) Info

func (h *Host) Info() core.PeerAddrInfo

Info returns host's perr info.

func (*Host) JoinOverlay

func (h *Host) JoinOverlay()

JoinOverlay triggers the host to join the DHT overlay

func (*Host) Neighbors

func (h *Host) Neighbors(ctx context.Context) []core.PeerAddrInfo

Neighbors returns the closest peer addresses

func (*Host) OverlayIdentity

func (h *Host) OverlayIdentity() string

OverlayIdentity returns the overlay identity string

func (*Host) Unicast

func (h *Host) Unicast(ctx context.Context, target core.PeerAddrInfo, topic string, data []byte) error

Unicast sends a message to a peer on the given address

type LRUBlacklist added in v0.2.4

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

LRUBlacklist is a blacklist implementation using an LRU cache

func NewLRUBlacklist added in v0.2.4

func NewLRUBlacklist(cap int) (*LRUBlacklist, error)

NewLRUBlacklist creates a new LRUBlacklist with capacity cap

func (*LRUBlacklist) Add added in v0.2.4

func (b *LRUBlacklist) Add(p core.PeerID) bool

Add adds a peer ID

func (*LRUBlacklist) Contains added in v0.2.4

func (b *LRUBlacklist) Contains(p core.PeerID) bool

Contains checks if the peer ID is in LRU

func (*LRUBlacklist) Remove added in v0.2.4

func (b *LRUBlacklist) Remove(p core.PeerID)

Remove removes a peer ID

func (*LRUBlacklist) RemoveOldest added in v0.2.4

func (b *LRUBlacklist) RemoveOldest()

RemoveOldest removes the oldest peer ID

type Option

type Option func(cfg *Config) error

Option defines the option function to modify the config for a host

func ConnectTimeout

func ConnectTimeout(timout time.Duration) Option

ConnectTimeout is the option to override the connect timeout

func DHTGroupID added in v0.3.4

func DHTGroupID(chainID uint32) Option

DHTGroupID helps node finds peers of the same GroupID in dht discovering. MainNet uses "iotex", while other networks use "iotex*"(e.g. "iotex2", "iotex3")

func DHTProtocolID added in v0.3.3

func DHTProtocolID(chainID uint32) Option

DHTProtocolID returns the prefix of dht protocol. MainNet uses "/iotex", while other networks use "/iotex*"(e.g. "/iotex2", "iotex3")

func ExternalHostName

func ExternalHostName(externalHostName string) Option

ExternalHostName is the option to set the host name or IP address seen from external

func ExternalPort

func ExternalPort(externalPort int) Option

ExternalPort is the option to set the port number seen from external

func Gossip

func Gossip() Option

Gossip is to indicate using gossip protocol

func HostName

func HostName(hostName string) Option

HostName is the option to override the host name or IP address

func MasterKey

func MasterKey(masterKey string) Option

MasterKey is to determine network identifier

func Port

func Port(port int) Option

Port is the option to override the port number

func PrivateNetworkPSK added in v0.2.11

func PrivateNetworkPSK(privateNetworkPSK string) Option

PrivateNetworkPSK is to determine network identifier

func SecureIO

func SecureIO() Option

SecureIO is to indicate using secured I/O

func WithConnectionManagerConfig added in v0.2.3

func WithConnectionManagerConfig(lo, hi int, grace time.Duration) Option

WithConnectionManagerConfig set configuration for connection manager.

func WithMaxMessageSize added in v0.3.6

func WithMaxMessageSize(size int) Option

WithMaxMessageSize config MaxMessageSize option.

func WithMaxPeer added in v0.3.4

func WithMaxPeer(num uint32) Option

WithMaxPeer config MaxPeer option.

func WithRateLimit added in v0.2.6

func WithRateLimit(rcfg RateLimitConfig) Option

WithRateLimit is to indicate limiting msg rate from peers

func WithRelay

func WithRelay(relayType string) Option

WithRelay config relay option.

type RateLimitConfig added in v0.2.6

type RateLimitConfig struct {
	GlobalUnicastAvg   int `yaml:"globalUnicastAvg"`
	GlobalUnicastBurst int `yaml:"globalUnicastBurst"`
	PeerAvg            int `yaml:"peerAvg"`
	PeerBurst          int `yaml:"peerBurst"`
}

RateLimitConfig all numbers are per second value.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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