p2putil

package
v1.3.3 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2019 License: MIT Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TimeErrorTolerance   = time.Minute
	DefaultCertTTL       = time.Hour * 6
	DefaultExpireBufTerm = time.Hour * 6
)
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 added in v1.0.0

func CalculateFieldDescSize(varSize int) int

func CheckAndGetV1 added in v1.3.3

func CheckAndGetV1(cert *types.AgentCertificate) (*p2pcommon.AgentCertificateV1, error)

func CheckProtoCert added in v1.3.3

func CheckProtoCert(cert *types.AgentCertificate) (*p2pcommon.AgentCertificateV1, error)

func ComparePeerID added in v1.0.0

func ComparePeerID(pid1, pid2 types.PeerID) int

ComparePeerID do byte-wise compare of two peerIDs,

func ContainsID added in v1.3.3

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

func ConvertCertToProto added in v1.3.3

func ConvertCertToProto(w *p2pcommon.AgentCertificateV1) (*types.AgentCertificate, error)

func ConvertCertsToProto added in v1.3.3

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

func ConvertPKToBTCEC added in v1.3.3

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

ConvertPKToBTCEC return nil if converison is failed

func ConvertPKToLibP2P added in v1.3.3

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

ConvertPKToLibP2P return nil if converison is failed

func ConvertPubKeyToBTCEC added in v1.3.3

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

ConvertPubKeyToBTCEC return nil if converison is failed

func ConvertPubToLibP2P added in v1.3.3

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

ConvertPubToLibP2P return nil if converison is failed

func DebugLogReceive added in v1.3.3

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

func DebugLogReceiveMsg added in v1.0.0

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

Deprecated

func DebugLogReceiveResponse added in v1.3.3

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

func DebugLogReceiveResponseMsg added in v1.0.0

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

Deprecated

func ExternalIP added in v1.0.0

func ExternalIP() (net.IP, error)

func ExtractBlockFromRequest added in v1.0.0

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

func FromMultiAddr added in v1.3.3

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

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

func FromMultiAddrStringWithPID added in v1.3.3

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

FromMultiAddrStringWithPID

func FromMultiAddrToPeerInfo added in v1.3.3

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

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

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

func LoadKeyFile added in v1.3.3

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

func MarshalMessageBody added in v1.3.3

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

func NewAgentCertV1 added in v1.3.3

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

NewAgentCertV1 create certificate object

func PeerMetaToMultiAddr added in v1.3.3

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

PeerMetaToMultiAddr make libp2p compatible Multiaddr object from peermeta @Deprecated

func PrintChainID added in v1.3.3

func PrintChainID(id *types.ChainID) string

func PrintHashList added in v1.0.0

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

func ProtocolIDsToString added in v1.3.3

func ProtocolIDsToString(sli []core.ProtocolID) string

func RandomUUID added in v1.0.0

func RandomUUID() string

RandomUUID generate random UUID and return in form of string

func ReadToLen added in v1.3.3

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

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

func ShortForm added in v0.12.0

func ShortForm(pid types.PeerID) string

ShortForm returns shorthanded types.PeerID.

func ShortMetaForm added in v1.0.0

func ShortMetaForm(m p2pcommon.PeerMeta) string

func SignCert added in v1.3.3

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

func UnmarshalAndReturn added in v1.0.0

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

func UnmarshalMessageBody added in v1.3.3

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

func VerifyCert added in v1.3.3

func VerifyCert(wrap *p2pcommon.AgentCertificateV1) bool

Types

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

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

func NewLogIPNetMarshaller added in v1.3.3

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

func (LogIPNetMarshaller) MarshalZerologArray added in v1.3.3

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

type LogPeerIdsMarshaller added in v1.3.3

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

func NewLogPeerIdsMarshaller added in v1.3.3

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

func (LogPeerIdsMarshaller) MarshalZerologArray added in v1.3.3

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

type LogPeerMetasMarshaller added in v1.3.3

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

func NewLogPeerMetasMarshaller added in v1.3.3

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

func (*LogPeerMetasMarshaller) MarshalZerologArray added in v1.3.3

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

type LogPeersMarshaller added in v1.3.3

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

func NewLogPeersMarshaller added in v1.3.3

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

func (*LogPeersMarshaller) MarshalZerologArray added in v1.3.3

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

type LogStringersMarshaller added in v1.3.3

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

func NewLogStringersMarshaller added in v1.3.3

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

func (*LogStringersMarshaller) MarshalZerologArray added in v1.3.3

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

type LogStringsMarshaller added in v1.3.3

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

func NewLogStringsMarshaller added in v1.3.3

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

func (*LogStringsMarshaller) MarshalZerologArray added in v1.3.3

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