Documentation
¶
Index ¶
- Constants
- Variables
- func ComparePeerID(pid1, pid2 peer.ID) int
- func InitNodeInfo(cfg *config.P2PConfig, logger *log.Logger)
- func NodeID() peer.ID
- func NodePrivKey() crypto.PrivKey
- func NodePubKey() crypto.PubKey
- func NodeSID() string
- func P2PTxHashArrToString(bbarray []TxHash) string
- func P2PTxHashArrToStringWithLimit(bbarray []TxHash, limit int) string
- func RandomUUID() string
- func SendProtoMessage(data proto.Message, rw *bufio.Writer) error
- type ActorService
- type BaseMsgHandler
- type BlkHash
- type BlockHashesReceiver
- type BlocksChunkReceiver
- type HSHeader
- type HandlerFactory
- type Message
- type MessageHandler
- type MsgID
- type MsgReadWriter
- type MsgReader
- type MsgWriter
- type P2P
- func (p2ps *P2P) AfterStart()
- func (p2ps *P2P) BeforeStart()
- func (p2ps *P2P) BeforeStop()
- func (p2ps *P2P) CallRequest(actor string, msg interface{}, timeout time.Duration) (interface{}, error)
- func (p2ps *P2P) CallRequestDefaultTimeout(actor string, msg interface{}) (interface{}, error)
- func (p2ps *P2P) FutureRequest(actor string, msg interface{}, timeout time.Duration) *actor.Future
- func (p2ps *P2P) FutureRequestDefaultTimeout(actor string, msg interface{}) *actor.Future
- func (p2ps *P2P) GetAddresses(peerID peer.ID, size uint32) bool
- func (p2ps *P2P) GetBlockHashes(context actor.Context, msg *message.GetHashes)
- func (p2ps *P2P) GetBlockHeaders(msg *message.GetBlockHeaders) bool
- func (p2ps *P2P) GetBlocks(peerID peer.ID, blockHashes []message.BlockHash) bool
- func (p2ps *P2P) GetBlocksChunk(context actor.Context, msg *message.GetBlockChunks)
- func (p2ps *P2P) GetChainAccessor() types.ChainAccessor
- func (p2ps *P2P) GetMissingBlocks(peerID peer.ID, hashes []message.BlockHash) bool
- func (p2ps *P2P) GetSyncAncestor(peerID peer.ID, hashes [][]byte) bool
- func (p2ps *P2P) GetTXs(peerID peer.ID, txHashes []message.TXHash) bool
- func (p2ps *P2P) NotifyNewBlock(newBlock message.NotifyNewBlock) bool
- func (p2ps *P2P) NotifyNewTX(newTXs message.NotifyNewTransactions) bool
- func (p2ps *P2P) Receive(context actor.Context)
- func (p2ps *P2P) SendRequest(actor string, msg interface{})
- func (p2ps *P2P) Statistics() *map[string]interface{}
- func (p2ps *P2P) TellRequest(actor string, msg interface{})
- type PeerEventListener
- type PeerHandshaker
- type PeerManager
- type PeerMeta
- type PeerPoolManager
- type ReconnectManager
- type RemotePeer
- type ResponseReceiver
- type SubProtocol
- type SubProtocolMeta
- type SyncManager
- type TxHash
- type V020Wrapper
- type V030HSMessage
- type V030Handshaker
- type V030Message
- type V030ReadWriter
- type V030Reader
- type V030Writer
Constants ¶
const ( DefaultGlobalBlockCacheSize = 300 DefaultPeerBlockCacheSize = 100 DefaultGlobalTxCacheSize = 50000 DefaultPeerTxCacheSize = 2000 // DefaultPeerTxQueueSize is maximum size of hashes in a single tx notice message DefaultPeerTxQueueSize = 40000 )
TODO this value better related to max peer and block produce interval, not constant
const ( DesignatedNodeTTL time.Duration = time.Minute * 60 DefaultNodeTTL time.Duration = time.Minute * 10 )
TTLs are node ttl
const ( // this magic number is useful only in handshaking MAGICMain uint32 = 0x47416841 MAGICTest uint32 = 0x2e415429 P2PVersion030 uint32 = 0x00000300 SigLength = 16 IDLength = 16 MaxPayloadLength = 1 << 23 // 8MB MaxBlockHeaderResponseCount = 10000 MaxBlockResponseCount = 2000 MaxResponseSplitCount = 5 SyncWorkTTL = time.Second * 30 AddBlockCheckpoint = 100 AddBlockWaitTime = time.Second * 10 )
constants of p2p protocol since v0.3
const ( LogPeerID = "peer_id" LogProtoID = "protocol_id" LogMsgID = "msg_id" LogBlkHash = "blk_hash" LogBlkCount = "blk_cnt" LogTxHash = "tx_hash" LogTxCount = "tx_cnt" )
constants for indicating logitem of p2p
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
const (
EmptyGetBlockResponseSize = 12 // roughly estimated maximum size if element is full
)
Variables ¶
var TimeoutError error
Functions ¶
func ComparePeerID ¶
ComparePeerID do byte-wise compare of two peerIDs,
func InitNodeInfo ¶
InitNodeInfo initializes node-specific informations like node id. Caution: this must be called before all the goroutines are started.
func NodePrivKey ¶
func NodePrivKey() crypto.PrivKey
NodePrivKey returns the private key of the node.
func P2PTxHashArrToString ¶
bytesArrToString converts array of byte array to json array of b58 encoded string.
func RandomUUID ¶
func RandomUUID() string
RandomUUID generate random UUID and return in form of string
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 BaseMsgHandler ¶
type BaseMsgHandler struct {
// contains filtered or unexported fields
}
BaseMsgHandler contains common attributes of MessageHandler
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(context actor.Context, peer RemotePeer, req *message.GetHashes, ttl time.Duration) *BlockHashesReceiver
func (*BlockHashesReceiver) ReceiveResp ¶ added in v0.8.1
func (br *BlockHashesReceiver) ReceiveResp(msg 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.
func NewBlockReceiver ¶ added in v0.8.1
func NewBlockReceiver(context actor.Context, peer RemotePeer, blockHashes []message.BlockHash, ttl time.Duration) *BlocksChunkReceiver
func (*BlocksChunkReceiver) ReceiveResp ¶ added in v0.8.1
func (br *BlocksChunkReceiver) ReceiveResp(msg 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 HandlerFactory ¶
type HandlerFactory interface {
// contains filtered or unexported methods
}
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 {
// contains filtered or unexported methods
}
MessageHandler handle incoming subprotocol message
type MsgID ¶
MsgID is
func MustParseBytes ¶
MustParseBytes return msgid from byte slice
func ParseBytesToMsgID ¶
type MsgReadWriter ¶
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 P2P ¶
type P2P struct { *component.BaseComponent // contains filtered or unexported fields }
P2P is actor component for p2p
func NewP2P ¶
func NewP2P(hub *component.ComponentHub, cfg *config.Config, chainsvc *chain.ChainService) *P2P
NewP2P create a new ActorService for p2p
func (*P2P) AfterStart ¶
func (p2ps *P2P) AfterStart()
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 ¶
CallRequest implement interface method of ActorService
func (*P2P) FutureRequest ¶
FutureRequest implement interface method of ActorService
func (*P2P) FutureRequestDefaultTimeout ¶
FutureRequestDefaultTimeout implement interface method of ActorService
func (*P2P) GetAddresses ¶
GetAddresses send getAddress request to other peer
func (*P2P) GetBlockHashes ¶ added in v0.8.1
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) 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) GetMissingBlocks ¶
GetMissingBlocks send request message to peer about blocks which my local peer doesn't have
func (*P2P) GetSyncAncestor ¶ added in v0.8.1
Syncer.finder request remote peer to find ancestor
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) SendRequest ¶
SendRequest implement interface method of ActorService
func (*P2P) Statistics ¶
Statistics show statistic information of p2p module. NOTE: It it not implemented yet
func (*P2P) TellRequest ¶
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 PeerManager ¶
type PeerManager interface { host.Host Start() error Stop() error PrivateKey() crypto.PrivKey PublicKey() crypto.PubKey SelfMeta() PeerMeta SelfNodeID() peer.ID AddNewPeer(peer PeerMeta) RemovePeer(peerID peer.ID) // 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() ([]*types.PeerAddress, []*types.NewBlockNotice, []types.PeerState) }
PeerManager is internal service that provide peer management
func NewPeerManager ¶
func NewPeerManager(handlerFactory HandlerFactory, iServ ActorService, cfg *cfg.Config, signer msgSigner, rm ReconnectManager, logger *log.Logger, mf moFactory) PeerManager
NewPeerManager creates a peer manager object.
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 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 (PeerMeta) ToPeerAddress ¶
func (m PeerMeta) ToPeerAddress() types.PeerAddress
ToPeerAddress convert PeerMeta to PeerAddress
type PeerPoolManager ¶
type PeerPoolManager struct { }
type ReconnectManager ¶
type ReconnectManager interface { AddJob(meta PeerMeta) // CancelJob cancel from outer module to reconnectRunner CancelJob(pid peer.ID) Stop() // contains filtered or unexported methods }
ReconnectManager manage reconnect job schedule
type RemotePeer ¶
type RemotePeer interface { ID() peer.ID Meta() PeerMeta State() types.PeerState LastNotice() *types.NewBlockNotice GetReceiver(id MsgID) ResponseReceiver // TODO MF() moFactory // contains filtered or unexported methods }
type ResponseReceiver ¶ added in v0.8.1
ResponseReceiver returns true when receiver handled it, or false if this receiver is not the expected handler. NOTE: the return value is temporal works for old implementation and will be remove later.
type SubProtocol ¶
type SubProtocol uint32
SubProtocol identifies the type of p2p message
const ( StatusRequest SubProtocol PingRequest PingResponse GoAway AddressesRequest AddressesResponse )
const ( GetBlocksRequest SubProtocol = 0x010 + iota GetBlocksResponse GetBlockHeadersRequest GetBlockHeadersResponse GetMissingRequest GetMissingResponse NewBlockNotice GetAncestorRequest GetAncestorResponse GetHashesRequest GetHashesResponse )
const ( GetTXsRequest SubProtocol = 0x020 + iota GetTxsResponse NewTxNotice )
func (SubProtocol) String ¶
func (i SubProtocol) String() string
func (SubProtocol) Uint32 ¶
func (sp SubProtocol) Uint32() uint32
type SubProtocolMeta ¶
type SubProtocolMeta struct { SubProtocol // contains filtered or unexported fields }
type SyncManager ¶
type SyncManager interface { HandleNewBlockNotice(peer RemotePeer, hash BlkHash, data *types.NewBlockNotice) HandleGetBlockResponse(peer RemotePeer, msg Message, resp *types.GetBlockResponse) HandleNewTxNotice(peer RemotePeer, hashes []TxHash, data *types.NewTransactionsNotice) DoSync(peer RemotePeer, hashes []message.BlockHash, stopHash message.BlockHash) }
type V020Wrapper ¶
type V020Wrapper struct { *types.P2PMessage // contains filtered or unexported fields }
func NewV020Wrapper ¶
func NewV020Wrapper(message *types.P2PMessage, originalID string) *V020Wrapper
func (*V020Wrapper) ID ¶
func (m *V020Wrapper) ID() MsgID
func (*V020Wrapper) Length ¶
func (m *V020Wrapper) Length() uint32
func (*V020Wrapper) OriginalID ¶
func (m *V020Wrapper) OriginalID() MsgID
func (*V020Wrapper) Payload ¶
func (m *V020Wrapper) Payload() []byte
func (*V020Wrapper) Subprotocol ¶
func (m *V020Wrapper) Subprotocol() SubProtocol
func (*V020Wrapper) Timestamp ¶
func (m *V020Wrapper) Timestamp() int64
type V030HSMessage ¶
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() MsgReadWriter
type V030Message ¶
type V030Message struct {
// contains filtered or unexported fields
}
V030Message is basic form of p2p message v0.3
func (*V030Message) ID ¶
func (m *V030Message) ID() MsgID
func (*V030Message) Length ¶
func (m *V030Message) Length() uint32
func (*V030Message) OriginalID ¶
func (m *V030Message) OriginalID() MsgID
func (*V030Message) Payload ¶
func (m *V030Message) Payload() []byte
func (*V030Message) Subprotocol ¶
func (m *V030Message) Subprotocol() 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() (Message, error)
func (*V030ReadWriter) WriteMsg ¶
func (rw *V030ReadWriter) WriteMsg(msg 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() (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 Message) error
WriteMsg() must be used in single thread
Source Files
¶
- actorwork.go
- blkreceiver.go
- handshake.go
- hashreceiver.go
- message.go
- messagehandler.go
- msgio.go
- msgorder.go
- p2p.go
- peermanager.go
- peermeta.go
- peerpool.go
- protobuf.go
- protobufHelper.go
- protobufrw.go
- protocols.go
- reconnect.go
- reconnectmanager.go
- remotepeer.go
- signature.go
- subprotocol_string.go
- subprotocoladdrs.go
- subprotocolblock.go
- subprotocolblockhash.go
- subprotocolgetblock.go
- subprotocolping.go
- subprotocoltx.go
- syncmanager.go
- syncworker.go
- util.go
- v020wrapper.go
- v030handshake.go
- v030io.go
- v030mofactory.go
- v030msg.go