P2P network
p2p
is the network foundation of the blockchain. For a customizable blockchain framework, yu
allows developers to freely use the functions of the P2P network, and developers can call it in ChainEnv
.
The underlying implementation of this interface comes from libp2p
Interface
Code is here ,the core functions are as follows:
LocalID() peer.ID
GetBootNodes() []peer.ID
ConnectBootNodes() error
LocalID()
returns local p2p ID
GetBootNodes()
returns the p2p IDs of the bootnodes
nodes in the p2p network, bootnodes
are configured in the configuration file.
ConnectBootNodes()
connects these bootnodes
AddTopic(topicName string)
PubP2P(topic string, msg []byte) error
SubP2P(topic string) ([]byte, error)
AddTopic()
adds a topic
to the P2P network, which is used as a topic for subscription publishing.
PubP2P()
publishes messages into the topic
SubP2P()
subscribes messages from the topic
RequestPeer(peerID peer.ID, code int, request []byte) (response []byte, err error)
SetHandlers(handlers map[int]dev.P2pHandler)
RequestPeer()
sends request data to a specific p2p node, code
is the request category, which can be customized. (except 100
and 101
, these two are predefined code
of the framework, 100
is used when P2P node connection comparison data;
101
is used when synchronizing transaction data)
SetHandlers()
is set to handle requests from the P2P network, each code
corresponds to a handler
, the interface of handler
is here , developers can customize the logic here.