BIZ

Go言語によるビットコインのフルノード実装btcdを調べる(3)

今日はピアとの握手の仕方を確認して、スクラッチで実装してみたいと思います。握手して最初のブロックをもらうところまでいければいい感じだと思ってますが、とりあえずやってみます。最初のピアを教えてもらう方法も大体わかったし、TCP接続してversionメッセージ以降のやり取りの仕方がわかれば、スクラッチで握手できるはずです。握手して、ブロック状態を確認して、必要なブロックを確認して、このブロックくださいメッセージをブロードキャストします。そうすると誰かがくれます。

他のサーバを探して接続できたら、アウトバウンドピアを作成します。アウトバウンドピアの作成は下記で行われます。

// outboundPeerConnected is invoked by the connection manager when a new
// outbound connection is established. It initializes a new outbound server
// peer instance, associates it with the relevant state such as the connection
// request instance and the connection itself, and finally notifies the address
// manager of the attempt.
func (s *server) outboundPeerConnected(c *connmgr.ConnReq, conn net.Conn) {
sp := newServerPeer(s, c.Permanent)
p, err := peer.NewOutboundPeer(newPeerConfig(sp), c.Addr.String())
if err != nil {
srvrLog.Debugf("Cannot create outbound peer %s: %v", c.Addr, err)
s.connManager.Disconnect(c.ID())
}
sp.Peer = p
sp.connReq = c
sp.AssociateConnection(conn)
go s.peerDoneHandler(sp)
s.addrManager.Attempt(sp.NA())
}