P2P网络
p2p
是区块链的网络根基,对于一个可定制化区块链框架而言,yu
允许开发者自由使用P2P网络的功能,开发者可以在ChainEnv
中调用到它。
该接口的底层实现来源于libp2p
接口
代码在这里 ,核心功能如下:
LocalID() peer.ID
GetBootNodes() []peer.ID
ConnectBootNodes() error
LocalID()
返回本地节点的 p2p ID
GetBootNodes()
返回p2p网络中bootnodes
节点们的 p2p ID, bootnodes
在配置文件中配置。
ConnectBootNodes()
连接这些bootnodes
AddTopic(topicName string)
PubP2P(topic string, msg []byte) error
SubP2P(topic string) ([]byte, error)
AddTopic()
在P2P网络中增加一个topic
,用作订阅发布的主题。
PubP2P()
往这个topic
中发送消息。
SubP2P()
从这个topic
中订阅数据。
RequestPeer(peerID peer.ID, code int, request []byte) (response []byte, err error)
SetHandlers(handlers map[int]dev.P2pHandler)
RequestPeer()
往特定的p2p节点上发送请求数据,code
为请求类别,可自定义。(除了100
和101
,这两个是框架预定义的code
,100
是P2P节点连接比对数据的时候用;
101
是同步交易数据的时候用)
SetHandlers()
设置处理来自P2P网络里来的请求,每个code
对应一个handler
, handler
的接口在这里 ,开发者可定制开发这里的逻辑。