Lightning NetworkのBig Endian

Noise Protocolによるハンドシェイク後は、Lightning Messageのやり取りが始まる。そして、すべてのメッセージはBig Endianでのやり取りとなる。(a 2-byte big-endian)

Endian

エンディアン(英: endianness)は、複数のバイトなどを並べる順序の種類である。一般的な用語による表現ではバイトオーダ(英: byte order)、ないしそれを一部訳して日本語ではバイト順とも言う。

この中でBig Endianは、例えば、十六進法で表現すると 1234ABCD という1ワードが4バイトのデータを、バイト毎に上位側から「12 34 AB CD」のように並べる順序のことになると。

これを基礎として、Lightning Networkにおけるメッセージを解析してみる。

Lightning Message Format

  • type: a 2-byte big-endian field indicating the type of message
  • payload: a variable-length payload that comprises the remainder of the message and that conforms to a format matching the type

図にするとこんな感じか。

ということで、一旦typeとしてsetup、そのinit messageを見る。

The init Message

まず、定義から確認すると、

type: 16 (init)  
data:  
[2:gflen]
[gflen:globalfeatures]
[2:lflen]
[lflen:localfeatures]

となる。だからこの情報だけで左は埋まる。

あーややこしい、けれど、

MUST set any undefined feature bits to 0. SHOULD use the minimum lengths required to represent the feature fields.

なので、現状定義が存在しないglobalfeaturesについては考えることが出きる。

残るはlocal feature。LNDのソースを見ると、「optionupfrontshutdown_script」は設定が存在していない。なんでだろう。まあそれは置いといて。

DataLossProtectRequiredとGossipQueriesOptionalが有効化されている。ので、こんな感じか?

なんとなく雰囲気はわかってきた。あとはこれをどうやってjsでbig endianとやらで扱うのか。考察する。