p2pcommon

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: May 2, 2019 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// this magic number is useful only in handshaking
	MAGICMain uint32 = 0x47416841
	MAGICTest uint32 = 0x2e415429

	P2PVersion030 uint32 = 0x00000300

	SigLength = 16

	MaxPayloadLength = 1 << 23 // 8MB

	MaxBlockHeaderResponseCount = 10000
	MaxBlockResponseCount       = 2000
)

constants of p2p protocol since v0.3

View Source
const (
	AergoP2PSub protocol.ID = "/aergop2p/0.3"
)

context of multiaddr, as higher type of p2p message

View Source
const IDLength = 16

Variables

View Source
var (
	EmptyID = MsgID(uuid.Nil)
)

Functions

This section is empty.

Types

type ActorService

type ActorService interface {
	// TellRequest send actor request, which does not need to get return value, and forget it.
	TellRequest(actor string, msg interface{})
	// SendRequest send actor request, and the response is expected to go back asynchronously.
	SendRequest(actor string, msg interface{})
	// CallRequest send actor request and wait the handling of that message to finished,
	// and get return value.
	CallRequest(actor string, msg interface{}, timeout time.Duration) (interface{}, error)
	// CallRequestDefaultTimeout is CallRequest with default timeout
	CallRequestDefaultTimeout(actor string, msg interface{}) (interface{}, error)

	// FutureRequest send actor reqeust and get the Future object to get the state and return value of message
	FutureRequest(actor string, msg interface{}, timeout time.Duration) *actor.Future
	// FutureRequestDefaultTimeout is FutureRequest with default timeout
	FutureRequestDefaultTimeout(actor string, msg interface{}) *actor.Future

	GetChainAccessor() types.ChainAccessor
}

ActorService is collection of helper methods to use actor FIXME move to more general package. it used in p2p and rpc

type Message

type Message interface {
	Subprotocol() SubProtocol

	// Length is lenght of payload
	Length() uint32
	Timestamp() int64
	// ID is 16 bytes unique identifier
	ID() MsgID
	// OriginalID is message id of request which trigger this message. it will be all zero, if message is request or notice.
	OriginalID() MsgID

	// marshaled by google protocol buffer v3. object is determined by Subprotocol
	Payload() []byte
}

type MessageHandler

type MessageHandler interface {
	ParsePayload([]byte) (proto.Message, error)
	CheckAuth(msgHeader Message, msgBody proto.Message) error
	Handle(msgHeader Message, msgBody proto.Message)
	PreHandle()
	PostHandle(msgHeader Message, msgBody proto.Message)
}

MessageHandler handle incoming subprotocol message

type MoFactory

type MoFactory interface {
	NewMsgRequestOrder(expecteResponse bool, protocolID SubProtocol, message PbMessage) MsgOrder
	NewMsgBlockRequestOrder(respReceiver ResponseReceiver, protocolID SubProtocol, message PbMessage) MsgOrder
	NewMsgResponseOrder(reqID MsgID, protocolID SubProtocol, message PbMessage) MsgOrder
	NewMsgBlkBroadcastOrder(noticeMsg *types.NewBlockNotice) MsgOrder
	NewMsgTxBroadcastOrder(noticeMsg *types.NewTransactionsNotice) MsgOrder
	NewMsgBPBroadcastOrder(noticeMsg *types.BlockProducedNotice) MsgOrder
}

type MsgID

type MsgID [IDLength]byte

MsgID is

func MustParseBytes

func MustParseBytes(b []byte) MsgID

MustParseBytes return msgid from byte slice

func NewMsgID

func NewMsgID() (m MsgID)

NewMsgID return random id

func ParseBytesToMsgID

func ParseBytesToMsgID(b []byte) (MsgID, error)

func (MsgID) String

func (id MsgID) String() string

func (MsgID) UUID

func (id MsgID) UUID() uuid.UUID

type MsgOrder

type MsgOrder interface {
	GetMsgID() MsgID
	// Timestamp is unit time value
	Timestamp() int64
	IsRequest() bool
	IsNeedSign() bool
	GetProtocolID() SubProtocol

	// SendTo send message to remote peer. it return err if write fails, or nil if write is successful or ignored.
	SendTo(p RemotePeer) error
}

msgOrder is abstraction information about the message that will be sent to peer some type of msgOrder, such as notice mo, should thread-safe and re-entrant

type MsgReadWriter

type MsgReadWriter interface {
	MsgReader
	MsgWriter
}

type MsgReader

type MsgReader interface {
	// ReadMsg return types.MsgHeader as header, proto.Message as data
	// The header and/or data can be nil if error is not nil
	ReadMsg() (Message, error)
}

MsgReader read stream and return message object

type MsgSigner

type MsgSigner interface {
	// signMsg calulate signature and fill related fields in msg(peerid, pubkey, signature or etc)
	SignMsg(msg *types.P2PMessage) error
	// verifyMsg check signature is valid
	VerifyMsg(msg *types.P2PMessage, senderID peer.ID) error
}

signHandler sign or verify p2p message

type MsgWriter

type MsgWriter interface {
	WriteMsg(msg Message) error
}

MsgWriter write message to stream

type NTContainer

type NTContainer interface {
	GetNetworkTransport() NetworkTransport

	// ChainID return id of current chain.
	ChainID() *types.ChainID
}

NTContainer can provide NetworkTransport interface.

type NetworkTransport

type NetworkTransport interface {
	host.Host
	Start() error
	Stop() error

	PrivateKey() crypto.PrivKey
	PublicKey() crypto.PubKey
	SelfMeta() PeerMeta
	SelfNodeID() peer.ID

	GetAddressesOfPeer(peerID peer.ID) []string

	// AddStreamHandler wrapper function which call host.SetStreamHandler after transport is initialized, this method is for preventing nil error.
	AddStreamHandler(pid protocol.ID, handler inet.StreamHandler)

	GetOrCreateStream(meta PeerMeta, protocolID protocol.ID) (inet.Stream, error)
	GetOrCreateStreamWithTTL(meta PeerMeta, protocolID protocol.ID, ttl time.Duration) (inet.Stream, error)

	FindPeer(peerID peer.ID) bool
	ClosePeerConnection(peerID peer.ID) bool
}

NetworkTransport do manager network connection TODO need refactoring. it has other role, pk management of self peer

type PbMessage

type PbMessage interface {
	proto.Message
}

type PeerManager

type PeerManager interface {
	Start() error
	Stop() error

	//NetworkTransport
	SelfMeta() PeerMeta
	SelfNodeID() peer.ID

	AddNewPeer(peer PeerMeta)
	// Remove peer from peer list. Peer dispose relative resources and stop itself, and then call RemovePeer to peermanager
	RemovePeer(peer RemotePeer)
	// NotifyPeerHandshake is called after remote peer is completed handshake and ready to receive or send
	NotifyPeerHandshake(peerID peer.ID)
	NotifyPeerAddressReceived([]PeerMeta)

	// GetPeer return registered(handshaked) remote peer object
	GetPeer(ID peer.ID) (RemotePeer, bool)
	GetPeers() []RemotePeer
	GetPeerAddresses(noHidden bool, showSelf bool) []*message.PeerInfo

	GetPeerBlockInfos() []types.PeerBlockInfo
}

PeerManager is internal service that provide peer management

type PeerMeta

type PeerMeta struct {
	ID peer.ID
	// IPAddress is human readable form of ip address such as "192.168.0.1" or "2001:0db8:0a0b:12f0:33:1"
	IPAddress  string
	Port       uint32
	Designated bool // Designated means this peer is designated in config file and connect to in startup phase

	Hidden   bool // Hidden means that meta info of this peer will not be sent to other peers when getting peer list
	Outbound bool
}

PeerMeta contains non changeable information of peer node during connected state TODO: PeerMeta is almost same as PeerAddress, so TODO to unify them.

func FromPeerAddress

func FromPeerAddress(addr *types.PeerAddress) PeerMeta

FromPeerAddress convert PeerAddress to PeerMeta

func NewMetaFromStatus

func NewMetaFromStatus(status *types.Status, outbound bool) PeerMeta

FromStatusToMeta create peerMeta from Status message

func (PeerMeta) ToPeerAddress

func (m PeerMeta) ToPeerAddress() types.PeerAddress

ToPeerAddress convert PeerMeta to PeerAddress

type RemotePeer

type RemotePeer interface {
	ID() peer.ID
	Meta() PeerMeta
	ManageNumber() uint32
	Name() string

	State() types.PeerState
	LastNotice() *types.LastBlockStatus

	RunPeer()
	Stop()

	SendMessage(msg MsgOrder)
	SendAndWaitMessage(msg MsgOrder, ttl time.Duration) error

	PushTxsNotice(txHashes []types.TxID)

	ConsumeRequest(msgID MsgID)
	GetReceiver(id MsgID) ResponseReceiver

	// updateBlkCache add hash to block cache and return true if this hash already exists.
	UpdateBlkCache(blkHash []byte, blkNumber uint64) bool
	// updateTxCache add hashes to transaction cache and return newly added hashes.
	UpdateTxCache(hashes []types.TxID) []types.TxID
	// updateLastNotice change estimate of the last status of remote peer
	UpdateLastNotice(blkHash []byte, blkNumber uint64)

	// TODO
	MF() MoFactory
}

type ResponseReceiver

type ResponseReceiver func(Message, proto.Message) bool

type SubProtocol

type SubProtocol uint32

SubProtocol identifies the lower type of p2p message

func (SubProtocol) String

func (i SubProtocol) String() string

func (SubProtocol) Uint32

func (i SubProtocol) Uint32() uint32

type SyncManager

type SyncManager interface {
	// handle notice from bp
	HandleBlockProducedNotice(peer RemotePeer, block *types.Block)
	// handle notice from other node
	HandleNewBlockNotice(peer RemotePeer, data *types.NewBlockNotice)
	HandleGetBlockResponse(peer RemotePeer, msg Message, resp *types.GetBlockResponse)
	HandleNewTxNotice(peer RemotePeer, hashes []types.TxID, data *types.NewTransactionsNotice)
}

Jump to

Keyboard shortcuts

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