lightning nodeにおけるpeerとserverの関係の整理

LNDでは、かなりシンプルにpeerの管理をしている。

    inboundPeers  map[string]*peer
    outboundPeers map[string]*peer

serverの持つstructとして、ここに保持しているだけだ。他のブロックチェーンプログラムを見ると、poolモジュールもあって、そこで管理してるものも多い。この場合は、serverがpoolを管理していると言えるかもしれない。

    const peer = Peer.fromOutbound(this.options, addr);

    this.hosts.markAttempt(addr.hostname);

    this.bindPeer(peer);

    this.logger.debug('Connecting to %s.', peer.hostname());

    peer.tryOpen();

    return peer;

poolという用語自体はおそらくそれほど一般的に分散型システムで用いられるものではなくて、便箋上つけているだけと思われる。

例えば、hsdの場合は下記の流れで処理が動く。 まずfull nodeで、

  /**
   * Connect to the network.
   * @returns {Promise}
   */

  connect() {
    return this.pool.connect();
  }

poolでは、

  /**
   * Attempt to refill the pool with peers (no lock).
   * @private
   */

  fillOutbound() {
    const need = this.options.maxOutbound - this.peers.outbound;

    if (!this.peers.load)
      this.addLoader();

    if (need <= 0)
      return;

    this.logger.debug('Refilling peers (%d/%d).',
      this.peers.outbound,
      this.options.maxOutbound);

    for (let i = 0; i < need; i++)
      this.addOutbound();
  }
  /**
   * Create an outbound peer with no special purpose.
   * @private
   * @param {NetAddress} addr
   * @returns {Peer}
   */

  createOutbound(addr) {
    const peer = Peer.fromOutbound(this.options, addr);

    this.hosts.markAttempt(addr.hostname);

    this.bindPeer(peer);

    this.logger.debug('Connecting to %s.', peer.hostname());

    peer.tryOpen();

    return peer;
  }

そしてpeerで、

  /**
   * Open and perform initial handshake.
   * @method
   * @returns {Promise}
   */

  async _open() {
    this.opened = true;

    // Connect to peer.
    await this.initConnect();
    await this.initStall();
    await this.initVersion();
    await this.finalize();

    assert(!this.destroyed);

    // Finally we can let the pool know
    // that this peer is ready to go.
    this.emit('open');
  }

まあざっくりこんな感じになる。 ミニマム実装は、peerが一つだけのときだろうか。そこから考えていこうか。