p2putil

package
v2.5.2 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2024 License: MIT Imports: 33 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LogPeerID     = "peer_id"
	LogFullID     = "full_id" // LogFullID is Full qualified peer id
	LogPeerName   = "peer_nm"
	LogProtoID    = "protocol_id"
	LogMsgID      = "msg_id"
	LogOrgReqID   = "req_id" // LogOrgReqID is msgid of request from remote peer
	LogBlkHash    = types.LogBlkHash
	LogBlkNo      = types.LogBlkNo
	LogBlkCount   = "blk_cnt"
	LogTxHash     = "tx_hash"
	LogTxCount    = "tx_cnt"
	LogRespStatus = types.LogRespStatus
	LogRaftMsg    = "raftMsg"
)

frequently used constants for indicating p2p log category

Variables

View Source
var (
	UseFullID bool
)

Functions

func CalculateFieldDescSize

func CalculateFieldDescSize(varSize int) int

func ComparePeerID

func ComparePeerID(pid1, pid2 types.PeerID) int

ComparePeerID do byte-wise compare of two peerIDs,

func ContainsID

func ContainsID(pool []types.PeerID, id types.PeerID) bool

func ConvertCertsToProto

func ConvertCertsToProto(cs []*p2pcommon.AgentCertificateV1) ([]*types.AgentCertificate, error)

func ConvertPKToBTCEC

func ConvertPKToBTCEC(pk crypto.PrivKey) *btcec.PrivateKey

ConvertPKToBTCEC return nil if converison is failed

func ConvertPKToLibP2P

func ConvertPKToLibP2P(pk *btcec.PrivateKey) crypto.PrivKey

ConvertPKToLibP2P return nil if converison is failed

func ConvertPubKeyToBTCEC

func ConvertPubKeyToBTCEC(pk crypto.PubKey) *btcec.PublicKey

ConvertPubKeyToBTCEC return nil if converison is failed

func ConvertPubToLibP2P

func ConvertPubToLibP2P(pk *btcec.PublicKey) crypto.PubKey

ConvertPubToLibP2P return nil if converison is failed

func DebugLogReceive

func DebugLogReceive(logger *log.Logger, protocol p2pcommon.SubProtocol, msgID string, peer p2pcommon.RemotePeer, additional zerolog.LogObjectMarshaler)

DebugLogReceive log received remote message with debug level.

func DebugLogReceiveMsg

func DebugLogReceiveMsg(logger *log.Logger, protocol p2pcommon.SubProtocol, msgID string, peer p2pcommon.RemotePeer, additional interface{})

Deprecated

func DebugLogReceiveResponse

func DebugLogReceiveResponse(logger *log.Logger, protocol p2pcommon.SubProtocol, msgID string, reqID string, peer p2pcommon.RemotePeer, additional zerolog.LogObjectMarshaler)

DebugLogReceive log received remote response message with debug level.

func DebugLogReceiveResponseMsg

func DebugLogReceiveResponseMsg(logger *log.Logger, protocol p2pcommon.SubProtocol, msgID string, reqID string, peer p2pcommon.RemotePeer, additional interface{})

Deprecated

func ExternalIP

func ExternalIP() (net.IP, error)

func ExtractBlockFromRequest

func ExtractBlockFromRequest(rawResponse interface{}, err error) (*types.Block, error)

func FromMultiAddr

func FromMultiAddr(targetAddr multiaddr.Multiaddr) (p2pcommon.PeerMeta, error)

FromMultiAddr returns PeerMeta from multiaddr. the multiaddr must contain address(ip or domain name), port and peer id.

func FromMultiAddrString

func FromMultiAddrString(str string) (p2pcommon.PeerMeta, error)

func FromMultiAddrStringWithPID

func FromMultiAddrStringWithPID(str string, id types.PeerID) (p2pcommon.PeerMeta, error)

FromMultiAddrStringWithPID

func FromMultiAddrToPeerInfo

func FromMultiAddrToPeerInfo(ma types.Multiaddr) (p2pcommon.PeerMeta, error)

FromMultiAddrToPeerInfo create PeerStaticInfo from qualified multiaddr which contains if and only if address, port and peerID

func GenerateKeyFile

func GenerateKeyFile(dir, prefix string) (crypto.PrivKey, crypto.PubKey, error)

func InvokeWithTimer

func InvokeWithTimer(m Callable, timer *time.Timer) (interface{}, error)

InvokeWithTimer call DoCall method of m and return if m is finished or return error if timer fires.

func IsContainedIP

func IsContainedIP(ip net.IP, nets []*net.IPNet) bool

func LoadKeyFile

func LoadKeyFile(keyFile string) (crypto.PrivKey, crypto.PubKey, error)

func MarshalMessageBody

func MarshalMessageBody(message p2pcommon.MessageBody) ([]byte, error)

func NewAgentCertV1

func NewAgentCertV1(bpID, agentID types.PeerID, bpKey *btcec.PrivateKey, addrs []string, ttl time.Duration) (*p2pcommon.AgentCertificateV1, error)

NewAgentCertV1 create certificate object

func PeerMetaToMultiAddr

func PeerMetaToMultiAddr(m p2pcommon.PeerMeta) (types.Multiaddr, error)

PeerMetaToMultiAddr make libp2p compatible Multiaddr object from peermeta @Deprecated

func PrintChainID

func PrintChainID(id *types.ChainID) string

func PrintHashList

func PrintHashList(blocks []*types.Block) string

func ProtocolIDsToString

func ProtocolIDsToString(sli []core.ProtocolID) string

func RandomUUID

func RandomUUID() string

RandomUUID generate random UUID and return in form of string

func ReadToLen

func ReadToLen(rd io.Reader, bf []byte) (int, error)

ReadToLen read bytes from io.Reader until bf is filled.

func ResolveMultiAddress added in v2.5.0

func ResolveMultiAddress(peerAddr types.Multiaddr) ([]multiaddr.Multiaddr, error)

ResolveMultiAddress resolve address of dns name to ip address, It is needed to connect tcp stream since tcp protocol requires ip4 or ip6 after 2021

func ResolveToBestIp4Address added in v2.5.0

func ResolveToBestIp4Address(peerAddr types.Multiaddr) (multiaddr.Multiaddr, error)

ResolveToBestIp4Address resolve multi address and then choose one with ip4

func ShortForm

func ShortForm(pid types.PeerID) string

ShortForm returns shorthanded types.PeerID.

func ShortMetaForm

func ShortMetaForm(m p2pcommon.PeerMeta) string

func SignCert

func SignCert(key *btcec.PrivateKey, wrap *p2pcommon.AgentCertificateV1) error

func UnmarshalAndReturn

func UnmarshalAndReturn(data []byte, msgData p2pcommon.MessageBody) (p2pcommon.MessageBody, error)

func UnmarshalMessageBody

func UnmarshalMessageBody(data []byte, msgData p2pcommon.MessageBody) error

func VerifyCert

func VerifyCert(wrap *p2pcommon.AgentCertificateV1) bool

Types

type AgentCertMarshaller

type AgentCertMarshaller struct {
	*p2pcommon.AgentCertificateV1
}

func (AgentCertMarshaller) MarshalZerologObject

func (a AgentCertMarshaller) MarshalZerologObject(e *zerolog.Event)

type Callable

type Callable interface {
	// DoCall run function. it should put result anything if call is over. It also stop  function if Cancel was called as soon as possible
	DoCall(done chan<- interface{})
	// Cancel should return instantly
	Cancel()
}

Callable

type ChannelPipe

type ChannelPipe interface {
	// Put item to pipe. it should be used after Open() method is called.
	// It always returns true and guaranty that item is queued.
	Put(item interface{}) bool
	Out() <-chan interface{}
	// Done should be called after get item from out channel
	Done()

	Open()
	Close()
}

ChannelPipe serve non blocking limited size channel. It preserve input ordering, and not block caller unless it is Closed() Tt must be called Open before using it, and Close for dispose resource.

func NewDefaultChannelPipe

func NewDefaultChannelPipe(bufSize int, listener PipeEventListener) ChannelPipe

NewDefaultChannelPipe create pipe to output channel out

type LogIPNetMarshaller

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

func NewLogIPNetMarshaller

func NewLogIPNetMarshaller(arr []*net.IPNet, limit int) *LogIPNetMarshaller

func (LogIPNetMarshaller) MarshalZerologArray

func (m LogIPNetMarshaller) MarshalZerologArray(a *zerolog.Array)

type LogPeerIdsMarshaller

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

func NewLogPeerIdsMarshaller

func NewLogPeerIdsMarshaller(arr []types.PeerID, limit int) *LogPeerIdsMarshaller

func (LogPeerIdsMarshaller) MarshalZerologArray

func (m LogPeerIdsMarshaller) MarshalZerologArray(a *zerolog.Array)

type LogPeerMetasMarshaller

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

func NewLogPeerMetasMarshaller

func NewLogPeerMetasMarshaller(metas []p2pcommon.PeerMeta, limit int) *LogPeerMetasMarshaller

func (*LogPeerMetasMarshaller) MarshalZerologArray

func (m *LogPeerMetasMarshaller) MarshalZerologArray(a *zerolog.Array)

type LogPeersMarshaller

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

func NewLogPeersMarshaller

func NewLogPeersMarshaller(metas []p2pcommon.RemotePeer, limit int) *LogPeersMarshaller

func (*LogPeersMarshaller) MarshalZerologArray

func (m *LogPeersMarshaller) MarshalZerologArray(a *zerolog.Array)

type LogStringersMarshaller

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

func NewLogStringersMarshaller

func NewLogStringersMarshaller(arr []fmt.Stringer, limit int) *LogStringersMarshaller

func (*LogStringersMarshaller) MarshalZerologArray

func (m *LogStringersMarshaller) MarshalZerologArray(a *zerolog.Array)

type LogStringsMarshaller

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

func NewLogStringsMarshaller

func NewLogStringsMarshaller(strs []string, limit int) *LogStringsMarshaller

func (*LogStringsMarshaller) MarshalZerologArray

func (m *LogStringsMarshaller) MarshalZerologArray(a *zerolog.Array)

type MultiListener

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

MultiListener can contain multiple unit listeners and toss events

func NewMultiListener

func NewMultiListener(ls ...PipeEventListener) *MultiListener

func (*MultiListener) AppendListener

func (ml *MultiListener) AppendListener(l PipeEventListener)

func (*MultiListener) OnDrop

func (ml *MultiListener) OnDrop(element interface{})

func (*MultiListener) OnIn

func (ml *MultiListener) OnIn(element interface{})

func (*MultiListener) OnOut

func (ml *MultiListener) OnOut(element interface{})

type PipeEventListener

type PipeEventListener interface {
	// OnIn is called when item is queued
	OnIn(element interface{})
	// OnDrop is called when queued item is dropped and not out to channel receiver
	OnDrop(element interface{})
	// OnOut is called when queued item went to out channel (and will be sent to receiver)
	OnOut(element interface{})
}

PipeEventListener listen event of ChannelPipe

type PressableQueue

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

PressableQueue non-threadsafe fixed size queue, implemented like circular queue

func NewPressableQueue

func NewPressableQueue(capacity int) *PressableQueue

NewPressableQueue create a new queue

func (*PressableQueue) Empty

func (q *PressableQueue) Empty() bool

Empty returns true if queue has no element, or false if not

func (*PressableQueue) Full

func (q *PressableQueue) Full() bool

Full returns true if queue has maximum number of elements, or false if not

func (*PressableQueue) Offer

func (q *PressableQueue) Offer(e interface{}) bool

Offer is adding element to queue, it returns true if add success, or false if queue if add fail.

func (*PressableQueue) Peek

func (q *PressableQueue) Peek() interface{}

Peek return first element but not delete in queue. It returns nil if queue is empty

func (*PressableQueue) Poll

func (q *PressableQueue) Poll() interface{}

Poll return first element and remove it in queue. It returns nil if queue is empty

func (*PressableQueue) Press

func (q *PressableQueue) Press(e interface{}) interface{}

Press is adding element to queue and return nil fi queue is not full, or drop first element and return dropped element if queue is full.

func (*PressableQueue) Size

func (q *PressableQueue) Size() int

Size return the number of element queue has

type StatListener

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

StatListener make summation

func NewStatLister

func NewStatLister() *StatListener

func (*StatListener) OnDrop

func (l *StatListener) OnDrop(element interface{})

func (*StatListener) OnIn

func (l *StatListener) OnIn(element interface{})

func (*StatListener) OnOut

func (l *StatListener) OnOut(element interface{})

Jump to

Keyboard shortcuts

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