p2p

package
v1.5.0-beta2 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2021 License: MIT Imports: 48 Imported by: 0

Documentation

Index

Constants

View Source
const (
	QlcProtocolID    = "qlc/1.0.0"
	QlcProtocolFOUND = "/qlc/discovery/1.0.0"
	QlcMDnsFOUND     = "/qlc/MDns/1.0.0"
)
View Source
const (
	QlcMessageHeaderLength       = 13
	QlcMessageMagicNumberEndIdx  = 3
	QlcMessageVersionEndIdx      = 4
	QlcMessageTypeEndIdx         = 5
	QlcMessageDataLengthEndIdx   = 9
	QlcMessageDataCheckSumEndIdx = 13
)
View Source
const (
	PublicNet netAttribute = iota
	Intranet
)
View Source
const (
	// Topic is the network pubsub topic identifier on which new messages are announced.
	MsgTopic = "/qlc/msgs"
	// BlockTopic is the pubsub topic identifier on which new blocks are announced.
	BlockTopic = "/qlc/blocks"
)
View Source
const (
	MaxPingTimeOutTimes      = 4
	PingTimeInterval         = 30 * time.Second
	ConnectBootstrapInterval = 20 * time.Second
	PublishConnectPeersInfo  = 20 * time.Second
	PublishOnlinePeersInfo   = 25 * time.Second
	PublishBandWithPeersInfo = 60 * time.Second
)
View Source
const (
	MessagePriorityHigh = iota
	MessagePriorityNormal
	MessagePriorityLow
)

Message Priority.

View Source
const (
	MaxStreamNum         = 50
	MaxPeersNumForRandom = 8
)

Variables

View Source
var (
	ErrInvalidMessageHeaderLength = errors.New("invalid message header length")
	ErrInvalidMessageDataLength   = errors.New("invalid message data length")
	ErrInvalidMagicNumber         = errors.New("invalid magic number")
	ErrInvalidDataCheckSum        = errors.New("invalid data checksum")
)

Error types

View Source
var (
	ErrPeerIsNotConnected = errors.New("peer is not connected")
	ErrNoBootNode         = errors.New("can not get bootNode")
)

Error types

View Source
var (
	ErrStreamIsNotConnected = errors.New("stream is not connected")
	ErrNoStream             = errors.New("no stream")
	ErrCloseStream          = errors.New("stream close error")
)

Stream Errors

View Source
var (
	MagicNumber = []byte{0x51, 0x4C, 0x43} //QLC
)

Functions

func Equal

func Equal(a []byte, b []byte) bool

Equal checks whether byte slice a and b are equal.

func FromUint32

func FromUint32(v uint32) []byte

FromUint32 decodes uint32.

func NewQlcMessage

func NewQlcMessage(data []byte, currentVersion byte, messageType MessageType) []byte

NewQlcMessage new qlc message

func Uint32

func Uint32(data []byte) uint32

Uint32 encodes []byte.

Types

type ConnectionGater added in v1.4.0

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

func NewConnectionGater added in v1.4.0

func NewConnectionGater() *ConnectionGater

func (*ConnectionGater) InterceptAccept added in v1.4.0

func (cg *ConnectionGater) InterceptAccept(network.ConnMultiaddrs) bool

func (*ConnectionGater) InterceptAddrDial added in v1.4.0

func (cg *ConnectionGater) InterceptAddrDial(p peer.ID, addr ma.Multiaddr) bool

func (*ConnectionGater) InterceptPeerDial added in v1.4.0

func (cg *ConnectionGater) InterceptPeerDial(peer.ID) bool

func (*ConnectionGater) InterceptSecured added in v1.4.0

func (*ConnectionGater) InterceptUpgraded added in v1.4.0

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

type DiscoveryOption

type DiscoveryOption func(context.Context, host.Host) (mdns.Service, error)

type Dispatcher

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

Dispatcher a message dispatcher service.

func NewDispatcher

func NewDispatcher() *Dispatcher

NewDispatcher create Dispatcher instance.

func (*Dispatcher) Deregister

func (dp *Dispatcher) Deregister(subscribers *Subscriber)

Deregister deregister subscribers.

func (*Dispatcher) PutMessage

func (dp *Dispatcher) PutMessage(msg *Message)

PutMessage put new message to chan, then subscribers will be notified to process.

func (*Dispatcher) PutSyncMessage added in v1.3.0

func (dp *Dispatcher) PutSyncMessage(msg *Message)

PutMessage put new message to chan, then subscribers will be notified to process.

func (*Dispatcher) Register

func (dp *Dispatcher) Register(subscribers ...*Subscriber)

Register register subscribers.

func (*Dispatcher) Start

func (dp *Dispatcher) Start()

Start start message dispatch goroutine.

func (*Dispatcher) Stop

func (dp *Dispatcher) Stop()

Stop stop goroutine.

type EventBroadcastMsg added in v1.4.0

type EventBroadcastMsg struct {
	Type    MessageType
	Message interface{}
}

type EventConfirmAckMsg added in v1.4.0

type EventConfirmAckMsg struct {
	Block *protos.ConfirmAckBlock
	From  string
}

type EventFrontiersReqMsg added in v1.4.0

type EventFrontiersReqMsg struct {
	PeerID string
}

type EventPovBulkPullReqMsg added in v1.4.0

type EventPovBulkPullReqMsg struct {
	Req  *protos.PovBulkPullReq
	From string
}

type EventPovBulkPullRspMsg added in v1.4.0

type EventPovBulkPullRspMsg struct {
	Resp *protos.PovBulkPullRsp
	From string
}

type EventPovPeerStatusMsg added in v1.4.0

type EventPovPeerStatusMsg struct {
	Status *protos.PovStatus
	From   string
}

type EventSendMsgToSingleMsg added in v1.4.0

type EventSendMsgToSingleMsg struct {
	Type    MessageType
	Message interface{}
	PeerID  string
}

type Message

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

Message struct

func NewMessage added in v1.1.0

func NewMessage(messageType MessageType, from string, data []byte, content []byte) *Message

NewBaseMessage new base message

func (*Message) Content added in v0.0.9

func (msg *Message) Content() []byte

Content get the message content

func (*Message) Data

func (msg *Message) Data() []byte

Data get the message data

func (*Message) Hash

func (msg *Message) Hash() types.Hash

Hash return the message hash

func (*Message) MessageFrom

func (msg *Message) MessageFrom() string

MessageFrom get message who send

func (*Message) MessageType

func (msg *Message) MessageType() MessageType

MessageType get message type

func (*Message) String added in v1.1.0

func (msg *Message) String() string

String get the message to string

type MessageService

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

func NewMessageService

func NewMessageService(netService *QlcService, ledger ledger.Store) *MessageService

NewService return new Service.

func (*MessageService) Start

func (ms *MessageService) Start()

Start start message service.

func (*MessageService) Stop

func (ms *MessageService) Stop()

type MessageType

type MessageType byte

MessageType a string for message type.

const (
	PublishReq      MessageType = iota //PublishReq
	ConfirmReq                         //ConfirmReq
	ConfirmAck                         //ConfirmAck
	FrontierRequest                    //FrontierReq
	FrontierRsp                        //FrontierRsp
	BulkPullRequest                    //BulkPullRequest
	BulkPullRsp                        //BulkPullRsp
	BulkPushBlock                      //BulkPushBlock
	MessageResponse                    //MessageResponse
	PovStatus
	PovPublishReq
	PovBulkPullReq
	PovBulkPullRsp
)

Message Type

type PeersSlice

type PeersSlice []interface{}

PeersSlice is a slice which contains peers

type QlcMessage

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

func ParseQlcMessage

func ParseQlcMessage(data []byte) (*QlcMessage, error)

ParseqlcMessage parse qlc message

func (*QlcMessage) DataCheckSum

func (message *QlcMessage) DataCheckSum() uint32

DataCheckSum return data checkSum

func (*QlcMessage) DataLength

func (message *QlcMessage) DataLength() uint32

DataLength return dataLength

func (*QlcMessage) HeaderData

func (message *QlcMessage) HeaderData() []byte

HeaderData return HeaderData

func (*QlcMessage) MagicNumber

func (message *QlcMessage) MagicNumber() []byte

MagicNumber return magicNumber

func (*QlcMessage) MessageData

func (message *QlcMessage) MessageData() []byte

func (*QlcMessage) MessageType

func (message *QlcMessage) MessageType() MessageType

func (*QlcMessage) ParseMessageData

func (message *QlcMessage) ParseMessageData(data []byte) error

ParseMessageData parse qlc message data

func (*QlcMessage) VerifyData

func (message *QlcMessage) VerifyData() error

VerifyData verify qlc message data

func (*QlcMessage) VerifyHeader

func (message *QlcMessage) VerifyHeader() error

VerifyHeader verify qlc message header

func (*QlcMessage) Version

func (message *QlcMessage) Version() byte

type QlcNode

type QlcNode struct {
	ID peer.ID

	MessageTopic *pubsub.Topic

	MessageSub pubsub.Subscription
	// contains filtered or unexported fields
}

func NewNode

func NewNode(config *config.Config) (*QlcNode, error)

NewNode return new QlcNode according to the config.

func (*QlcNode) BroadcastMessage

func (node *QlcNode) BroadcastMessage(messageName MessageType, value interface{})

BroadcastMessage broadcast message.

func (*QlcNode) GetBandwidthStats added in v1.3.0

func (node *QlcNode) GetBandwidthStats(stats *p2pmetrics.Stats)

func (*QlcNode) GetID

func (node *QlcNode) GetID() string

ID return node ID.

func (*QlcNode) HandlePeerFound

func (node *QlcNode) HandlePeerFound(p peer.AddrInfo)

HandlePeerFound attempts to connect to peer from `PeerInfo`.

func (*QlcNode) SendMessageToPeer

func (node *QlcNode) SendMessageToPeer(messageName MessageType, value interface{}, peerID string) error

SendMessageToPeer send message to a peer.

func (*QlcNode) SetQlcService

func (node *QlcNode) SetQlcService(ns *QlcService)

SetQlcService set netService

func (*QlcNode) StartServices

func (node *QlcNode) StartServices() error

func (*QlcNode) Stop

func (node *QlcNode) Stop() error

Stop stop a node.

func (*QlcNode) StreamManager

func (node *QlcNode) StreamManager() *StreamManager

ID return node ID.

type QlcService

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

QlcService service for qlc p2p network

func NewQlcService

func NewQlcService(cfgFile string) (*QlcService, error)

NewQlcService create netService

func (*QlcService) Broadcast

func (ns *QlcService) Broadcast(name MessageType, value interface{})

Broadcast message.

func (*QlcService) Deregister

func (ns *QlcService) Deregister(subscribers *Subscriber)

Deregister Deregister the subscribers.

func (*QlcService) MessageEvent

func (ns *QlcService) MessageEvent() event.EventBus

EventQueue return EventQueue

func (*QlcService) Node

func (ns *QlcService) Node() *QlcNode

Node return the peer node

func (*QlcService) PutMessage

func (ns *QlcService) PutMessage(msg *Message)

PutMessage put dpos message to dispatcher.

func (*QlcService) PutSyncMessage added in v1.3.0

func (ns *QlcService) PutSyncMessage(msg *Message)

PutMessage put snyc message to dispatcher.

func (*QlcService) Register

func (ns *QlcService) Register(subscribers ...*Subscriber)

Register register the subscribers.

func (*QlcService) SendMessageToPeer

func (ns *QlcService) SendMessageToPeer(messageName MessageType, value interface{}, peerID string) error

SendMessageToPeer send message to a peer.

func (*QlcService) Start

func (ns *QlcService) Start() error

Start start p2p manager.

func (*QlcService) Stop

func (ns *QlcService) Stop() error

Stop stop p2p manager.

type ServiceSync

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

Service manage sync tasks

func NewSyncService

func NewSyncService(netService *QlcService, ledger ledger.Store) *ServiceSync

NewService return new Service.

func (*ServiceSync) Start

func (ss *ServiceSync) Start()

func (*ServiceSync) Stop

func (ss *ServiceSync) Stop()

Stop sync service

type Stream

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

Stream define the structure of a stream in p2p network

func NewStream

func NewStream(stream network.Stream, node *QlcNode) *Stream

NewStream return a new Stream

func NewStreamFromPID

func NewStreamFromPID(pid peer.ID, node *QlcNode) *Stream

NewStreamFromPID return a new Stream based on the pid

func (*Stream) Connect

func (s *Stream) Connect() error

Connect to the stream

func (*Stream) IsConnected

func (s *Stream) IsConnected() bool

IsConnected return if the stream is connected

func (*Stream) SendMessageToChan added in v1.2.3

func (s *Stream) SendMessageToChan(message *QlcMessage) error

SendMessage send msg to buffer

func (*Stream) SendMessageToPeer added in v0.0.9

func (s *Stream) SendMessageToPeer(messageType MessageType, data []byte) error

SendMessage send msg to peer

func (*Stream) StartLoop

func (s *Stream) StartLoop()

StartLoop start stream ping loop.

func (*Stream) String

func (s *Stream) String() string

func (*Stream) Write

func (s *Stream) Write(data []byte) error

func (*Stream) WriteQlcMessage

func (s *Stream) WriteQlcMessage(message *QlcMessage) error

WriteQlcMessage write qlc msg in the stream

type StreamManager

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

StreamManager manages all streams

func NewStreamManager

func NewStreamManager() *StreamManager

NewStreamManager return a new stream manager

func (*StreamManager) Add

func (sm *StreamManager) Add(s network.Stream)

Add a new stream into the stream manager

func (*StreamManager) AddOnlineInfo added in v1.3.2

func (sm *StreamManager) AddOnlineInfo(info *types.PeerInfo)

func (*StreamManager) AddOrUpdateOnlineInfo added in v1.3.2

func (sm *StreamManager) AddOrUpdateOnlineInfo(info *types.PeerInfo)

func (*StreamManager) AddStream

func (sm *StreamManager) AddStream(stream *Stream)

AddStream into the stream manager

func (*StreamManager) BroadcastMessage

func (sm *StreamManager) BroadcastMessage(messageName MessageType, v interface{})

BroadcastMessage broadcast the message

func (*StreamManager) CloseStream

func (sm *StreamManager) CloseStream(peerID string) error

CloseStream with the given pid and reason

func (*StreamManager) FindByPeerID

func (sm *StreamManager) FindByPeerID(peerID string) *Stream

FindByPeerID find the stream with the given peerID

func (*StreamManager) GetAllConnectPeersInfo added in v1.1.1

func (sm *StreamManager) GetAllConnectPeersInfo(pr *[]*types.PeerInfo)

func (*StreamManager) GetOnlinePeersInfo added in v1.3.1

func (sm *StreamManager) GetOnlinePeersInfo(pr *[]*types.PeerInfo)

func (*StreamManager) IsConnectWithPeerId added in v1.2.3

func (sm *StreamManager) IsConnectWithPeerId(peerID string) bool

func (*StreamManager) PeerCounts added in v0.0.4

func (sm *StreamManager) PeerCounts() int

func (*StreamManager) RandomPeer

func (sm *StreamManager) RandomPeer() (string, error)

func (*StreamManager) RemoveStream

func (sm *StreamManager) RemoveStream(s *Stream)

RemoveStream from the stream manager

func (*StreamManager) SetQlcNodeAndMaxStreamNum added in v1.3.0

func (sm *StreamManager) SetQlcNodeAndMaxStreamNum(node *QlcNode)

SetQlcService set netService

type Subscriber

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

Subscriber subscriber.

func NewSubscriber

func NewSubscriber(msgChan chan *Message, msgType MessageType) *Subscriber

NewSubscriber return new Subscriber instance.

func (*Subscriber) MessageChan

func (s *Subscriber) MessageChan() chan *Message

MessageChan return msgChan.

func (*Subscriber) MessageType

func (s *Subscriber) MessageType() MessageType

MessageType return msgTypes.

type WhiteList added in v1.4.0

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

Directories

Path Synopsis
pb

Jump to

Keyboard shortcuts

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