BOLTに準拠してinit
brontideでnoise protocolが完了すると、そのpeerはoutbound peerリストに登録される。
// OutboundPeerConnected initializes a new peer in response to a new outbound
// connection.
// NOTE: This function is safe for concurrent access.
func (s *server) OutboundPeerConnected(connReq *connmgr.ConnReq, conn net.Conn) {
// Exit early if we have already been instructed to shutdown, this
// prevents any delayed callbacks from accidentally registering peers.
if s.Stopped() {
return
}
そのpeerとの接続が存在しない場合、接続を開始する。
// Check to see if we already have a connection with this peer. If so,
// we may need to drop our existing connection. This prevents us from
// having duplicate connections to the same peer. We forgo adding a
// default case as we expect these to be the only error values returned
// from findPeerByPubStr.
connectedPeer, err := s.findPeerByPubStr(pubStr)
switch err {
case ErrPeerNotConnected:
// We were unable to locate an existing connection with the
// target peer, proceed to connect.
s.cancelConnReqs(pubStr, nil)
s.peerConnected(conn, nil, true)
s.peerConnected(conn, nil, true)では、 様々なpeerの初期化処理が実施されるが、initもここで行われる。
初期化処理はpeerオブジェクトのstart methodで実施される。
// Start starts all helper goroutines the peer needs for normal operations. In
// the case this peer has already been started, then this function is a loop.
func (p *peer) Start() error {
if atomic.AddInt32(&p.started, 1) != 1 {
return nil
}
peerLog.Tracef("Peer %v starting", p)
// Exchange local and global features, the init message should be very
// first between two nodes.
if err := p.sendInitMsg(); err != nil {
return fmt.Errorf("unable to send init msg: %v", err)
}
ln messageの定義はln wireで実施される。
ln wire自体も、brontideと同様にスタンドアローンのpackageである。
// Init is the first message reveals the features supported or required by this
// node. Nodes wait for receipt of the other's features to simplify error
// diagnosis where features are incompatible. Each node MUST wait to receive
// init before sending any other messages.
type Init struct {
// GlobalFeatures is feature vector which affects HTLCs and thus are
// also advertised to other nodes.
GlobalFeatures *RawFeatureVector
// LocalFeatures is feature vector which only affect the protocol
// between two nodes.
LocalFeatures *RawFeatureVector
}
ここのメッセージ定義は、BOLT1で定義される。 見てもよく理解出来ない部分が多いが、少しづつ噛み砕く。