p2p

package
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: May 22, 2019 License: MIT Imports: 40 Imported by: 0

README

Stuffs for p2p network

Documentation

Index

Constants

View Source
const (

	// time to determine which the remote designated peer is not working.
	DesignatedNodeTTL = time.Minute * 60
	// time to determine which the remote peer is not working.
	DefaultNodeTTL = time.Minute * 10
)

constants for peer communicating

View Source
const (
	SyncWorkTTL        = time.Second * 30
	AddBlockCheckpoint = 100
	AddBlockWaitTime   = time.Second * 10
)

constants for legacy sync algorithm. DEPRECATED newer sync loging in syncer package is used now.

View Source
const (
	DiscoveryQueryInterval = time.Minute * 5

	MaxAddrListSizePolaris = 200
	MaxAddrListSizePeer    = 50
)

constants for node discovery

View Source
const (
	DefaultGlobalBlockCacheSize = 300
	DefaultPeerBlockCacheSize   = 100

	DefaultGlobalTxCacheSize = 50000
	DefaultPeerTxCacheSize   = 10000
	// DefaultPeerTxQueueSize is maximum size of hashes in a single tx notice message
	DefaultPeerTxQueueSize = 2000
)

constants for caching TODO this value better related to max peer and block produce interval, not constant

View Source
const (
	DefaultPkKeyPrefix = "aergo-peer"
	DefaultPkKeyExt    = ".key"
	DefaultPubKeyExt   = ".pub"
	DefaultPeerIDExt   = ".id"
)

constants about private key

View Source
const ClientVersion = "0.2.0"

ClientVersion is the version of p2p protocol to which this codes are built FIXME version should be defined in more general ways

View Source
const (
	// other actor
	DefaultActorMsgTTL = time.Second * 4
)

constants for inter-communication of aergosvr

Variables

View Source
var InvalidArgument = fmt.Errorf("invalid argument")
View Source
var TimeoutError error

Functions

func FromMultiAddr added in v0.9.4

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

func GenerateKeyFile added in v0.11.0

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

func InitNodeInfo

func InitNodeInfo(baseCfg *config.BaseConfig, p2pCfg *config.P2PConfig, logger *log.Logger)

InitNodeInfo initializes node-specific informations like node id. Caution: this must be called before all the goroutines are started.

func LoadKeyFile added in v0.11.0

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

func NewNetworkTransport added in v0.9.4

func NewNetworkTransport(conf *cfg.P2PConfig, logger *log.Logger) *networkTransport

func NewPeerManager

NewPeerManager creates a peer manager object.

func NodeID

func NodeID() peer.ID

NodeID returns the node id.

func NodePrivKey

func NodePrivKey() crypto.PrivKey

NodePrivKey returns the private key of the node.

func NodePubKey

func NodePubKey() crypto.PubKey

NodePubKey returns the public key of the node.

func NodeSID

func NodeSID() string

NodeSID returns the string representation of the node id.

func P2PTxHashArrToString

func P2PTxHashArrToString(bbarray []types.TxID) string

bytesArrToString converts array of byte array to json array of b58 encoded string.

func P2PTxHashArrToStringWithLimit

func P2PTxHashArrToStringWithLimit(bbarray []types.TxID, limit int) string

func ParseMultiAddrString added in v0.10.0

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

func ParseMultiaddrWithResolve added in v0.9.4

func ParseMultiaddrWithResolve(str string) (multiaddr.Multiaddr, error)

ParseMultiaddrWithResolve parse string to multiaddr, additionally accept domain name with protocol /dns NOTE: this function is temporarilly use until go-multiaddr start to support dns.

func PeerMetaToMultiAddr added in v0.10.0

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

PeerMetaToMultiAddr make libp2p compatible Multiaddr object from peermeta

func ToMultiAddr added in v0.10.0

func ToMultiAddr(ipAddr net.IP, port uint32) (multiaddr.Multiaddr, error)

ToMultiAddr make libp2p compatible Multiaddr object

Types

type AncestorReceiver added in v1.0.0

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

BlocksChunkReceiver is send p2p getBlocksRequest to target peer and receive p2p responses till all requestes blocks are received It will send response actor message if all blocks are received or failed to receive, but not send response if timeout expired.

func NewAncestorReceiver added in v1.0.0

func NewAncestorReceiver(actor p2pcommon.ActorService, peer p2pcommon.RemotePeer, seq uint64, hashes [][]byte, ttl time.Duration) *AncestorReceiver

func (*AncestorReceiver) ReceiveResp added in v1.0.0

func (br *AncestorReceiver) ReceiveResp(msg p2pcommon.Message, msgBody proto.Message) (ret bool)

ReceiveResp must be called just in read go routine

func (*AncestorReceiver) StartGet added in v1.0.0

func (br *AncestorReceiver) StartGet()

type BlockHashByNoReceiver added in v0.8.2

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

BlocksChunkReceiver is send p2p getBlocksRequest to target peer and receive p2p responses till all requestes blocks are received It will send response actor message if all blocks are received or failed to receive, but not send response if timeout expired.

func NewBlockHashByNoReceiver added in v0.8.2

func NewBlockHashByNoReceiver(actor p2pcommon.ActorService, peer p2pcommon.RemotePeer, seq uint64, blockNo types.BlockNo, ttl time.Duration) *BlockHashByNoReceiver

func (*BlockHashByNoReceiver) ReceiveResp added in v0.8.2

func (br *BlockHashByNoReceiver) ReceiveResp(msg p2pcommon.Message, msgBody proto.Message) (ret bool)

ReceiveResp must be called just in read go routine

func (*BlockHashByNoReceiver) StartGet added in v0.8.2

func (br *BlockHashByNoReceiver) StartGet()

type BlockHashesReceiver added in v0.8.1

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

BlocksChunkReceiver is send p2p getBlocksRequest to target peer and receive p2p responses till all requestes blocks are received It will send response actor message if all blocks are received or failed to receive, but not send response if timeout expired.

func NewBlockHashesReceiver added in v0.8.1

func NewBlockHashesReceiver(actor p2pcommon.ActorService, peer p2pcommon.RemotePeer, seq uint64, req *message.GetHashes, ttl time.Duration) *BlockHashesReceiver

func (*BlockHashesReceiver) ReceiveResp added in v0.8.1

func (br *BlockHashesReceiver) ReceiveResp(msg p2pcommon.Message, msgBody proto.Message) (ret bool)

ReceiveResp must be called just in read go routine

func (*BlockHashesReceiver) StartGet added in v0.8.1

func (br *BlockHashesReceiver) StartGet()

type BlocksChunkReceiver added in v0.8.1

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

BlocksChunkReceiver is send p2p getBlocksRequest to target peer and receive p2p responses till all requestes blocks are received It will send response actor message if all blocks are received or failed to receive, but not send response if timeout expired, since syncer actor already dropped wait before.

func NewBlockReceiver added in v0.8.1

func NewBlockReceiver(actor p2pcommon.ActorService, peer p2pcommon.RemotePeer, seq uint64, blockHashes []message.BlockHash, ttl time.Duration) *BlocksChunkReceiver

func (*BlocksChunkReceiver) ReceiveResp added in v0.8.1

func (br *BlocksChunkReceiver) ReceiveResp(msg p2pcommon.Message, msgBody proto.Message) (ret bool)

ReceiveResp must be called just in read go routine

func (*BlocksChunkReceiver) StartGet added in v0.8.1

func (br *BlocksChunkReceiver) StartGet()

type HSHandler added in v0.9.4

type HSHandler interface {
	// Handle peer handshake till ttl, and return msgrw for this connection, and status of remote peer.
	Handle(r io.Reader, w io.Writer, ttl time.Duration) (p2pcommon.MsgReadWriter, *types.Status, error)
}

HSHandler will do handshake with remote peer

type HSHandlerFactory added in v0.9.4

type HSHandlerFactory interface {
	CreateHSHandler(outbound bool, pm p2pcommon.PeerManager, actor p2pcommon.ActorService, log *log.Logger, pid peer.ID) HSHandler
}

HSHandlerFactory is creator of HSHandler

type HSHeader

type HSHeader struct {
	Magic   uint32
	Version uint32
}

func (HSHeader) Marshal

func (h HSHeader) Marshal() []byte

func (*HSHeader) Unmarshal

func (h *HSHeader) Unmarshal(b []byte)

type HandlerFactory

type HandlerFactory interface {
	// contains filtered or unexported methods
}

type InboundHSHandler added in v0.9.4

type InboundHSHandler struct {
	*PeerHandshaker
}

func (*InboundHSHandler) Handle added in v0.9.4

type OutboundHSHandler added in v0.9.4

type OutboundHSHandler struct {
	*PeerHandshaker
}

func (*OutboundHSHandler) Handle added in v0.9.4

type P2P

type P2P struct {
	*component.BaseComponent
	// contains filtered or unexported fields
}

P2P is actor component for p2p

func NewP2P

func NewP2P(cfg *config.Config, chainsvc *chain.ChainService) *P2P

NewP2P create a new ActorService for p2p

func (*P2P) AfterStart

func (p2ps *P2P) AfterStart()

func (*P2P) BeforeStart

func (p2ps *P2P) BeforeStart()

BeforeStart starts p2p service.

func (*P2P) BeforeStop

func (p2ps *P2P) BeforeStop()

BeforeStop is called before actor hub stops. it finishes underlying peer manager

func (*P2P) CallRequest

func (p2ps *P2P) CallRequest(actor string, msg interface{}, timeout time.Duration) (interface{}, error)

CallRequest implement interface method of ActorService

func (*P2P) CallRequestDefaultTimeout

func (p2ps *P2P) CallRequestDefaultTimeout(actor string, msg interface{}) (interface{}, error)

CallRequest implement interface method of ActorService

func (*P2P) ChainID added in v0.9.4

func (p2ps *P2P) ChainID() *types.ChainID

func (*P2P) CreateHSHandler added in v0.9.4

func (p2ps *P2P) CreateHSHandler(outbound bool, pm p2pcommon.PeerManager, actor p2pcommon.ActorService, log *log.Logger, pid peer.ID) HSHandler

func (*P2P) FutureRequest

func (p2ps *P2P) FutureRequest(actor string, msg interface{}, timeout time.Duration) *actor.Future

FutureRequest implement interface method of ActorService

func (*P2P) FutureRequestDefaultTimeout

func (p2ps *P2P) FutureRequestDefaultTimeout(actor string, msg interface{}) *actor.Future

FutureRequestDefaultTimeout implement interface method of ActorService

func (*P2P) GetAddresses

func (p2ps *P2P) GetAddresses(peerID peer.ID, size uint32) bool

GetAddresses send getAddress request to other peer

func (*P2P) GetBlockHashByNo added in v0.8.2

func (p2ps *P2P) GetBlockHashByNo(context actor.Context, msg *message.GetHashByNo)

GetBlockHashes send request message to peer and make response message for block hashes

func (*P2P) GetBlockHashes added in v0.8.1

func (p2ps *P2P) GetBlockHashes(context actor.Context, msg *message.GetHashes)

GetBlockHashes send request message to peer and make response message for block hashes

func (*P2P) GetBlockHeaders

func (p2ps *P2P) GetBlockHeaders(msg *message.GetBlockHeaders) bool

GetBlockHeaders send request message to peer and

func (*P2P) GetBlocks

func (p2ps *P2P) GetBlocks(peerID peer.ID, blockHashes []message.BlockHash) bool

GetBlocks send request message to peer and

func (*P2P) GetBlocksChunk added in v0.8.1

func (p2ps *P2P) GetBlocksChunk(context actor.Context, msg *message.GetBlockChunks)

GetBlocksChunk send request message to peer and

func (*P2P) GetChainAccessor

func (p2ps *P2P) GetChainAccessor() types.ChainAccessor

GetChainAccessor implment interface method of ActorService

func (*P2P) GetNetworkTransport added in v0.9.4

func (p2ps *P2P) GetNetworkTransport() p2pcommon.NetworkTransport

func (*P2P) GetPeerAccessor added in v1.0.0

func (p2ps *P2P) GetPeerAccessor() types.PeerAccessor

func (*P2P) GetSyncAncestor added in v0.8.1

func (p2ps *P2P) GetSyncAncestor(context actor.Context, msg *message.GetSyncAncestor)

Syncer.finder request remote peer to find ancestor

func (*P2P) GetTXs

func (p2ps *P2P) GetTXs(peerID peer.ID, txHashes []message.TXHash) bool

GetTXs send request message to peer and

func (*P2P) NotifyBlockProduced added in v0.9.4

func (p2ps *P2P) NotifyBlockProduced(newBlock message.NotifyNewBlock) bool

NotifyNewBlock send notice message of new block to a peer

func (*P2P) NotifyNewBlock

func (p2ps *P2P) NotifyNewBlock(newBlock message.NotifyNewBlock) bool

NotifyNewBlock send notice message of new block to a peer

func (*P2P) NotifyNewTX

func (p2ps *P2P) NotifyNewTX(newTXs message.NotifyNewTransactions) bool

NotifyNewTX notice tx(s) id created

func (*P2P) Receive

func (p2ps *P2P) Receive(context actor.Context)

Receive got actor message and then handle it.

func (*P2P) SendRequest

func (p2ps *P2P) SendRequest(actor string, msg interface{})

SendRequest implement interface method of ActorService

func (*P2P) Statistics

func (p2ps *P2P) Statistics() *map[string]interface{}

Statistics show statistic information of p2p module. NOTE: It it not implemented yet

func (*P2P) TellRequest

func (p2ps *P2P) TellRequest(actor string, msg interface{})

TellRequest implement interface method of ActorService

type PeerEventListener

type PeerEventListener interface {
	// OnAddPeer is called just after the peer is added.
	OnAddPeer(peerID peer.ID)

	// OnRemovePeer is called just before the peer is removed
	OnRemovePeer(peerID peer.ID)
}

PeerEventListener listen peer manage event

type PeerHandshaker

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

PeerHandshaker works to handshake to just connected peer, it detect chain networks and protocol versions, and then select InnerHandshaker for that protocol version.

type V030HSMessage

type V030HSMessage struct {
	HSHeader
	Sigature  [p2pcommon.SigLength]byte
	PubKeyB   []byte
	Timestamp uint64
	Nonce     uint16
}

type V030Handshaker

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

V030Handshaker exchange status data over protocol version .0.3.0

func (*V030Handshaker) GetMsgRW

func (h *V030Handshaker) GetMsgRW() p2pcommon.MsgReadWriter

type V030Message

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

V030Message is basic form of p2p message v0.3

func NewV030Message added in v0.9.4

func NewV030Message(msgID, originalID p2pcommon.MsgID, timestamp int64, protocol p2pcommon.SubProtocol, payload []byte) *V030Message

NewV030Message create a new object

func (*V030Message) ID

func (m *V030Message) ID() p2pcommon.MsgID

func (*V030Message) Length

func (m *V030Message) Length() uint32

func (*V030Message) OriginalID

func (m *V030Message) OriginalID() p2pcommon.MsgID

func (*V030Message) Payload

func (m *V030Message) Payload() []byte

func (*V030Message) Subprotocol

func (m *V030Message) Subprotocol() p2pcommon.SubProtocol

func (*V030Message) Timestamp

func (m *V030Message) Timestamp() int64

type V030ReadWriter

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

func NewV030ReadWriter

func NewV030ReadWriter(r *bufio.Reader, w *bufio.Writer) *V030ReadWriter

func (*V030ReadWriter) ReadMsg

func (rw *V030ReadWriter) ReadMsg() (p2pcommon.Message, error)

func (*V030ReadWriter) WriteMsg

func (rw *V030ReadWriter) WriteMsg(msg p2pcommon.Message) error

type V030Reader

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

func NewV030Reader

func NewV030Reader(rd *bufio.Reader) *V030Reader

func (*V030Reader) ReadMsg

func (r *V030Reader) ReadMsg() (p2pcommon.Message, error)

ReadMsg() must be used in single thread

type V030Writer

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

func NewV030Writer

func NewV030Writer(wr *bufio.Writer) *V030Writer

func (*V030Writer) WriteMsg

func (w *V030Writer) WriteMsg(msg p2pcommon.Message) error

WriteMsg() must be used in single thread

Directories

Path Synopsis
Package p2pmock is a generated GoMock package.
Package p2pmock is a generated GoMock package.

Jump to

Keyboard shortcuts

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