base

package
v0.0.0-...-b124b1e Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2022 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultModule            = "network"
	DefaultPort              = 47101 // p2p port
	DefaultAddress           = "/ip4/127.0.0.1/tcp/47101"
	DefaultNetKeyPath        = "netkeys" // node private key path
	DefaultNetIsNat          = true      // use NAT
	DefaultNetIsTls          = false     // use tls secure transport
	DefaultNetIsHidden       = false
	DefaultMaxStreamLimits   = 1024
	DefaultMaxMessageSize    = 128
	DefaultTimeout           = 30
	DefaultStreamIPLimitSize = 10
	DefaultMaxBroadcastPeers = 20
	DefaultServiceName       = "localhost"
	DefaultIsBroadCast       = true
)

default settings

View Source
const (
	DefaultStrategy           FilterStrategy = "DefaultStrategy"
	BucketsStrategy                          = "BucketsStrategy"
	NearestBucketStrategy                    = "NearestBucketStrategy"
	BucketsWithFactorStrategy                = "BucketsWithFactorStrategy"
	CorePeersStrategy                        = "CorePeersStrategy"
)

supported filter strategies

View Source
const (
	MessageVersion  = "1.0.0"
	SubModName      = "network"
	BlockChain      = "corechain"
	ProtocolVersion = "1.0"
	Retry           = 10

	BucketsFilterFactor = 0.5
	Namespace           = "core"
)

Variables

View Source
var (
	MaxBroadCast = 20
)

Functions

This section is empty.

Types

type Dispatcher

type Dispatcher interface {
	Register(sub Subscriber) error
	UnRegister(sub Subscriber) error
	Dispatch(*protos.CoreMessage, Stream) error
}

type FilterStrategy

type FilterStrategy string

FilterStrategy defines the supported filter strategies

type NetConf

type NetConf struct {
	// Module is the name of p2p module plugin
	Module string `yaml:"module,omitempty"`
	// Port the p2p network listened for p2p
	Port int32 `yaml:"port,omitempty"`
	// Address multiaddr string, /ip4/127.0.0.1/tcp/8080
	Address string `yaml:"address,omitempty"`
	// keyPath is the node private key path
	KeyPath string `yaml:"keyPath,omitempty"`
	// isNat envconfig whether the node use NAT manager
	IsNat bool `yaml:"isNat,omitempty"`
	// isHidden envconfig whether the node can be found
	IsHidden bool `yaml:"isHidden,omitempty"`
	// bootNodes envconfig the bootNodes the node to connect
	BootNodes []string `yaml:"bootNodes,omitempty"`
	// staticNodes envconfig the nodes which you trust
	StaticNodes map[string][]string `yaml:"staticNodes,omitempty"`
	// isBroadCast envconfig whether broadcast to all StaticNodes
	IsBroadCast bool `yaml:"isBroadCast,omitempty"`
	// maxStreamLimits envconfig the max stream num
	MaxStreamLimits int32 `yaml:"maxStreamLimits,omitempty"`
	// maxMessageSize envconfig the max message size
	MaxMessageSize int64 `yaml:"maxMessageSize,omitempty"`
	// timeout envconfig the timeout of Request with response
	Timeout int64 `yaml:"timeout,omitempty"`
	// StreamIPLimitSize set the limitation size for same ip
	StreamIPLimitSize int64 `yaml:"streamIPLimitSize,omitempty"`
	// MaxBroadcastPeers limit the number of common peers in a broadcast,
	// this number do not include MaxBroadcastCorePeers.
	MaxBroadcastPeers int `yaml:"maxBroadcastPeers,omitempty"`
	// isTls envconfig the node use tls secure transparent
	IsTls bool `yaml:"isTls,omitempty"`
	// ServiceName
	ServiceName string `yaml:"serviceName,omitempty"`
}

Config is the envconfig of p2p server. Attention, envconfig of dht are not expose

func GetDefP2PConf

func GetDefP2PConf() *NetConf

func LoadP2PConf

func LoadP2PConf(cfgFile string) (*NetConf, error)

type NetCtx

type NetCtx struct {
	// 基础上下文
	xctx.BaseCtx
	// 运行环境配置
	EnvCfg *xconf.EnvConf
	// 网络组件配置
	P2PConf *NetConf
}

网络组件运行上下文环境

func NewNetCtx

func NewNetCtx(envCfg *xconf.EnvConf) (*NetCtx, error)

type Network

type Network interface {
	Init(*NetCtx) error
	Start()
	Stop()

	NewSubscriber(protos.CoreMessage_MessageType, interface{}, ...SubscriberOption) Subscriber
	Register(Subscriber) error
	UnRegister(Subscriber) error

	SendMessage(xctx.Context, *protos.CoreMessage, ...OptionFunc) error
	SendMessageWithResponse(xctx.Context, *protos.CoreMessage, ...OptionFunc) ([]*protos.CoreMessage, error)

	Context() *NetCtx
	PeerInfo() protos.PeerInfo
}

type Option

type Option struct {
	Filters   []FilterStrategy
	Addresses []string
	PeerIDs   []string
	Accounts  []string

	WhiteList map[string]bool

	Percent float32 // percent wait for return

	Factor float64
}

func Apply

func Apply(optFunc []OptionFunc) *Option

Apply apply OptionFunc

type OptionFunc

type OptionFunc func(*Option)

OptionFunc define single Option function for send message

func WithAccounts

func WithAccounts(accounts []string) OptionFunc

func WithAddresses

func WithAddresses(addresses []string) OptionFunc

WithAddresses add target peer addresses to message Option

func WithFactor

func WithFactor(factor float64) OptionFunc

func WithFilter

func WithFilter(filters []FilterStrategy) OptionFunc

WithFilter add filter strategies to message Option

func WithPeerIDs

func WithPeerIDs(peerIDs []string) OptionFunc

WithPeerIDs add target peer IDs to message Option

func WithPercent

func WithPercent(percent float32) OptionFunc

WithPercent add percentage to message Option

func WithWhiteList

func WithWhiteList(whiteList map[string]bool) OptionFunc

WithWhiteList add whiteList

type Stream

type Stream interface {
	Send(*protos.CoreMessage) error
}

Stream send p2p response message

type Subscriber

type Subscriber interface {
	GetMessageType() protos.CoreMessage_MessageType
	Match(*protos.CoreMessage) bool
	HandleMessage(xctx.Context, *protos.CoreMessage, Stream) error
}

Subscriber is the interface for p2p message SubscriberImpl

type SubscriberOption

type SubscriberOption func(Subscriber)

Jump to

Keyboard shortcuts

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