Lightning Network
ビットコインのマイクロペイメントに対応したオフチェーンネットワークLightning Networkを試してみました。
https://github.com/lightningnetwork/lnd/tree/master/docker
現在のブロックチェーンはスケーラビリティなど今後問題になってくる技術的な課題があり、それを克服するために(メインチェーン/オンチェーンに負担をかけないような)様々な工夫が用意されています。そのアプローチの仕方が各ブロックチェーンごとに個性的(Layer2、クロスチェーンなどなど)で、技術的好奇心が掻き立てられます。ビットコインではオンチェーンとは独自のしくみのオフチェーンでペイメントチャンネルを経由してコインの売買ができるようです。(ノード構築のインセンティブは手数料)
簡単に言えば取引ごとにオンチェーンに書き込むのではなく、まとまった取引の開始(チャンネルを開く)と終了(チャンネルを閉じる)のみとし、取引自体はオフチェーンで実行されることのようです。
ここでは上記サイトのdockerによる実装を試しました。3つのコンソールを開きメインコンソールではbitcoinデーモン、アリス用のコンソールではdockerで実装されたアリスノード、もう一つはボブ用です。アリスからボブへの送金を試みます。ちょっと長いため最後にまとめて配置しました。
ここ最近、にわかですがブロックチェーンの情報収集をしていて思うことは、ちょっと大げさですが人類があらゆる知恵を絞り出してこの分野に取り組んでいるような気がしてしまいます。ビットコインとならぶ存在のイーサリアム、新しい存在の、ポルカドット、カルダノ、ソラナ、コスモス、ニア、フローなど、様々な背景のある人たちが入り混じって取り組んでいます。エンジニアとして感じたことは暗号通貨のチャートにオープンソースのリンクがあることに、なんという時代になってきたんだ、と思ってしまいます。(資金調達・取引のメカニズムがコードになっていて、バーチャルだけでなくリアルの世界で通用してしまっている) またそれぞれのBlockchainには思想があり、それを支持する人の主義主張をきくのも興味深いです。ネットワークの安定維持(ノード構築のインセンティブ)、分散化(特定の人の影響力が大きくならないようなしくみ)、TPSのような性能をバランスよく最大化させるアイディアは、暗号通貨のことだけでなく、次世代の社会インフラを考える際のヒントになります。次世代ブロックチェーンの議論については、現在のしくみを生かして改善する考え方と、新しいしくみを根本から作ってしまう考え方とわかれる傾向にあるのかなと感じます。(Lightning Networkは前者)
かつてパソコンの黎明期国内外様々なメーカが独自規格のハード、OSを作っていましたが、今のブロックチェーンの規格は、なんかそのころに似ている気がしています。(とても活気があってエキサイティング) そこから思うことは、決してエンジニア目線で良いと思ったものが必ずしも強くないことです。WINTELが勝ち取ってきたようにユーザに近いところが充実しているものが強いのかもしれません。
とはいってもSolidity、Cadence、SimplicityといったBlockchainならではのキーワード、従来のキーワードでもあまり表に出てこなかった、Rust、WebAssembly、Haskellがクローズアップされており、反応してしまいます。今回もdockerが多用されていますが、これまでもテストしたWeb3, React / Node などもあわせて最近のフロントエンドからバックエンドまであらゆるものが活用されているのがわかります。こういったことからも世界中のエンジニアが総力戦で取り組んでいる気がして、前の大げさな表現になりました。
Blockchain、面白すぎです。
環境) Ubuntu20.4/WSL/Windows11
Main Console
$ export NETWORK="simnet"
$ docker volume create simnet_lnd_alice
simnet_lnd_alice
$ docker volume create simnet_lnd_bob
simnet_lnd_bob
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1dd1039ed38b lnd "./start-lnd.sh" 12 seconds ago Up 11 seconds 9735/tcp, 10009/tcp alice
e4fc59546fff btcd "./start-btcd.sh" 15 seconds ago Up 12 seconds 8333-8334/tcp, 18333-18334/tcp, 18555-18556/tcp, 28901-28902/tcp btcd
(alice console)
$ MINING_ADDRESS=rX6B5SfUFCeCDi8mei55RVctaLh8nJUXEX docker-compose up -d btcd
Recreating btcd ... done
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
900848493f15 btcd "./start-btcd.sh" 5 seconds ago Up 4 seconds 8333-8334/tcp, 18333-18334/tcp, 18555-18556/tcp, 28901-28902/tcp btcd
1dd1039ed38b lnd "./start-lnd.sh" 4 minutes ago Up 4 minutes 9735/tcp, 10009/tcp alice
$ docker exec -it btcd /start-btcctl.sh generate 400
[
"30513adbb2ea06a2f41ff6e820f28c0541f74add61db7221288695ca0fedfdf0",
"63e8712d9c82c85c3420747b9d0c2dcb0830054154f39fbbd69b650d6a08d335",
"5d63dd30dba612b8fcb98583439a278b3189240064366c2943fcb5acab7710a4",
"5440ecd7403fd4cbafc54cf2463dc970c774b6cc42a8fd7c3aa7bb511a9937c9",
"60730b05b5ad1b420ed8423a4ba399cf673e1dde3dbb0158558a92424f28f7f7",
"403844d8e249f4977d143dc60ba8bd4954f98e1ef75ec81c2b0650201928cc86",
"78ca8280c6a6de6e18122759459a7f385f290c48d99cfee6a5314fc36508cb6f",
"413968a4d9a6377b8fe791aa3168778ac2525f162078124a0b25423259afdf9c",
"5e4b2583234d35c0669418546593325f08efe450fc469691831aea84c1983ac0",
"19d4d6ec5229152e3b95e0b65d4db5ed3c75dcee23a75c9159025813a490b8dd",
"77d779970fc355f5f6820710907433570a1e9e47e68262a1f280f5913213bcbd",
"781d8f2adc43261023e45562b169a9e3a60d08f68cf858f8ea5afd7e8f804844",
"363204521e8f8e36e0a6be1428958c1a2f43a875156e6623fd60612b99588f76",
"6ae88c7fbe26730e5590df3785ac6758c999a696fa8b1268b828550bf6727a8b",
"1ad55b323913f41fe25d8dd5c284526a446192d43710e3fd4bfe2e1a71920ccd",
...
$ docker exec -it btcd /start-btcctl.sh getblockchaininfo | grep -A 1 segwit
"segwit": {
"status": "active",
$
(bob console)
$ docker inspect bob | grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "",
"IPAddress": "172.20.0.4",
(alice consolからbob-nodeに接続addressddress)
(alice でoepnchannelel)
$ docker exec -it btcd /start-btcctl.sh generate 3
[
"04a28809c867e71fa0c70630cf81885fadddafc2a9759588e5d6dfcdea07a424",
"6e121d9e71bb4ad2e5f35d14bbf3f00e7a943cd18c87b3a4cc531421308a9fb5",
"4c4e90c4e94321fe425d6280535cb26c2068e367577ef3f3acec9507c76bcce6"
]
(alice でclosechannelel)
$ docker exec -it btcd /start-btcctl.sh generate 3
[
"62dab67d832a50f2d69c3f86e88eea425fae796100751e3554845271542001d9",
"7f58bd2257d43be5f4853d73bcf220f2ef6bf65c2a703d6fa9961cd41c81b014",
"538ae071300bfa5f437ccd815d737812d4753dd2bd6cf7b0484b4d6d042362e6"
]
Code language: JavaScript (javascript)
Alice Console
$ docker-compose run -d --name alice --volume simnet_lnd_alice:/root/.lnd lnd
Creating btcd ... done
alice
$ docker exec -i -t alice bash
bash-5.1#
bash-5.1# lncli --network=simnet walletbalance
{
"total_balance": "0",
"confirmed_balance": "0",
"unconfirmed_balance": "0",
"account_balance": {
"default": {
"confirmed_balance": "0",
"unconfirmed_balance": "0"
}
}
}
bash-5.1# lncli --network=simnet channelbalance
{
"balance": "0",
"pending_open_balance": "0",
"local_balance": {
"sat": "0",
"msat": "0"
},
"remote_balance": {
"sat": "0",
"msat": "0"
},
"unsettled_local_balance": {
"sat": "0",
"msat": "0"
},
"unsettled_remote_balance": {
"sat": "0",
"msat": "0"
},
"pending_open_local_balance": {
"sat": "0",
"msat": "0"
},
"pending_open_remote_balance": {
"sat": "0",
"msat": "0"
}
}
bash-5.1# lncli --network=simnet newaddress np2wkh
{
"address": "rX6B5SfUFCeCDi8mei55RVctaLh8nJUXEX"
}
(addressをmainin colsoleのMINING_ADDRESSSS.Recreating btcdするとwalletbalanceが変化nceが変化変化-5.1# lncli --network=simnet walletbalance
{
"total_balance": "1505000000000",
"confirmed_balance": "1505000000000",
"unconfirmed_balance": "0",
"account_balance": {
"default": {
"confirmed_balance": "1505000000000",
"unconfirmed_balance": "0"
}
}
}
(main consoleからaddressress,bob consoleからpubkey所得してbob得してbobてbobob nodeに接続接続-5.1#
bash-5.1# lncli --network=simnet connect 0204e6918ae3239cc8da262715e5ada2d3540cc9fec6db7d6b72e544e2c77f6cac@172.20.0.4
{
}
bash-5.1# lncli --network=simnet listpeers
{
"peers": [
{
"pub_key": "0204e6918ae3239cc8da262715e5ada2d3540cc9fec6db7d6b72e544e2c77f6cac",
"address": "172.20.0.4:9735",
"bytes_sent": "391",
"bytes_recv": "391",
"sat_sent": "0",
"sat_recv": "0",
"inbound": false,
"ping_time": "0",
"sync_type": "ACTIVE_SYNC",
"features": {
"0": {
"name": "data-loss-protect",
"is_required": true,
"is_known": true
},
"5": {
"name": "upfront-shutdown-script",
"is_required": false,
"is_known": true
},
"7": {
"name": "gossip-queries",
"is_required": false,
"is_known": true
},
"9": {
"name": "tlv-onion",
"is_required": false,
"is_known": true
},
"12": {
"name": "static-remote-key",
"is_required": true,
"is_known": true
},
"14": {
"name": "payment-addr",
"is_required": true,
"is_known": true
},
"17": {
"name": "multi-path-payments",
"is_required": false,
"is_known": true
},
"23": {
"name": "anchors-zero-fee-htlc-tx",
"is_required": false,
"is_known": true
},
"31": {
"name": "amp",
"is_required": false,
"is_known": true
},
"45": {
"name": "explicit-commitment-type",
"is_required": false,
"is_known": true
},
"2023": {
"name": "script-enforced-lease",
"is_required": false,
"is_known": true
}
},
"errors": [
],
"flap_count": 1,
"last_flap_ns": "1642158005985969341",
"last_ping_payload": null
}
]
}
bash-5.1#
bash-5.1# lncli --network=simnet openchannel --node_key=0204e6918ae3239cc8da262715e5ada2d3540cc9fec6db7d6b72e544e2c77f6cac --local_amt=1234567
{
"funding_txid": "05b707addfd5aae36fdf62610376e45d4d6859f9e559a5e24957ce173e1a284b"
}
(main coloseでfundingng transaction)
bash-5.1# lncli --network=simnet listchannels
{
"channels": [
{
"active": true,
"remote_pubkey": "0204e6918ae3239cc8da262715e5ada2d3540cc9fec6db7d6b72e544e2c77f6cac",
"channel_point": "05b707addfd5aae36fdf62610376e45d4d6859f9e559a5e24957ce173e1a284b:0",
"chan_id": "887305883680768",
"capacity": "1234567",
"local_balance": "1231097",
"remote_balance": "0",
"commit_fee": "3140",
"commit_weight": "772",
"fee_per_kw": "2500",
"unsettled_balance": "0",
"total_satoshis_sent": "0",
"total_satoshis_received": "0",
"num_updates": "0",
"pending_htlcs": [
],
"csv_delay": 148,
"private": false,
"initiator": true,
"chan_status_flags": "ChanStatusDefault",
"local_chan_reserve_sat": "12345",
"remote_chan_reserve_sat": "12345",
"static_remote_key": false,
"commitment_type": "ANCHORS",
"lifetime": "38",
"uptime": "38",
"close_address": "",
"push_amount_sat": "0",
"thaw_height": 0,
"local_constraints": {
"csv_delay": 148,
"chan_reserve_sat": "12345",
"dust_limit_sat": "354",
"max_pending_amt_msat": "1222222000",
"min_htlc_msat": "1",
"max_accepted_htlcs": 483
},
"remote_constraints": {
"csv_delay": 148,
"chan_reserve_sat": "12345",
"dust_limit_sat": "354",
"max_pending_amt_msat": "1222222000",
"min_htlc_msat": "1",
"max_accepted_htlcs": 483
}
}
]
}
(bob consoleで-5.1# lncli --network=simnet sendpayment --pay_req=lnsb555550n1ps7zkvupp5fd6kx42j5dgd2dc0znmev8et60vqnkcl09nt4al48sjeelf4d76qdqqcqzpgxqyz5vqsp5atu4jx427nlgrwmchg32scz2e3qjdflrxjswcynee34vp3rh3c8s9qyyssqlqfdy6ncp6lm8fs6uyec0yyrqz5aazcum9ehq355x4qpl5c36tj88u5mc636w7aw7n8xjetcgek47hh3u4qp3nprj8k5r6k0cpp837gqusyazn
Payment hash: 4b75635552a350d5370f14f7961f2bd3d809db1f7966baf7f53c259cfd356fb4
Description:
Amount (in satoshis): 55555
Fee limit (in satoshis): 55555
Destination: 0204e6918ae3239cc8da262715e5ada2d3540cc9fec6db7d6b72e544e2c77f6cac
Confirm payment (yes/no): yes
+------------+--------------+--------------+--------------+-----+----------+-----------------+-------+
| HTLC_STATE | ATTEMPT_TIME | RESOLVE_TIME | RECEIVER_AMT | FEE | TIMELOCK | CHAN_OUT | ROUTE |
+------------+--------------+--------------+--------------+-----+----------+-----------------+-------+
| SUCCEEDED | 0.025 | 0.227 | 55555 | 0 | 852 | 887305883680768 | |
+------------+--------------+--------------+--------------+-----+----------+-----------------+-------+
Amount + fee: 55555 + 0 sat
Payment hash: 4b75635552a350d5370f14f7961f2bd3d809db1f7966baf7f53c259cfd356fb4
Payment status: SUCCEEDED, preimage: 2cf46eed79d3aa72f942ea8a2eeb4f7aa50c437670d51e9e98d350fd232ba8ab
bash-5.1#
bash-5.1# lncli --network=simnet channelbalance
{
"balance": "1175542",
"pending_open_balance": "0",
"local_balance": {
"sat": "1175542",
"msat": "1175542000"
},
"remote_balance": {
"sat": "55555",
"msat": "55555000"
},
"unsettled_local_balance": {
"sat": "0",
"msat": "0"
},
"unsettled_remote_balance": {
"sat": "0",
"msat": "0"
},
"pending_open_local_balance": {
"sat": "0",
"msat": "0"
},
"pending_open_remote_balance": {
"sat": "0",
"msat": "0"
}
}
bash-5.1# lncli --network=simnet listchannels
{
"channels": [
{
"active": true,
"remote_pubkey": "0204e6918ae3239cc8da262715e5ada2d3540cc9fec6db7d6b72e544e2c77f6cac",
"channel_point": "05b707addfd5aae36fdf62610376e45d4d6859f9e559a5e24957ce173e1a284b:0",
"chan_id": "887305883680768",
"capacity": "1234567",
"local_balance": "1175542",
"remote_balance": "55555",
"commit_fee": "2810",
"commit_weight": "1116",
"fee_per_kw": "2500",
"unsettled_balance": "0",
"total_satoshis_sent": "55555",
"total_satoshis_received": "0",
"num_updates": "2",
"pending_htlcs": [
],
"csv_delay": 148,
"private": false,
"initiator": true,
"chan_status_flags": "ChanStatusDefault",
"local_chan_reserve_sat": "12345",
"remote_chan_reserve_sat": "12345",
"static_remote_key": false,
"commitment_type": "ANCHORS",
"lifetime": "438",
"uptime": "438",
"close_address": "",
"push_amount_sat": "0",
"thaw_height": 0,
"local_constraints": {
"csv_delay": 148,
"chan_reserve_sat": "12345",
"dust_limit_sat": "354",
"max_pending_amt_msat": "1222222000",
"min_htlc_msat": "1",
"max_accepted_htlcs": 483
},
"remote_constraints": {
"csv_delay": 148,
"chan_reserve_sat": "12345",
"dust_limit_sat": "354",
"max_pending_amt_msat": "1222222000",
"min_htlc_msat": "1",
"max_accepted_htlcs": 483
}
}
]
}
(上記-5.1# lncli --network=simnet closechannel --funding_txid=05b707addfd5aae36fdf62610376e45d4d6859f9e559a5e24957ce173e1a284b --output_index=0
{
"closing_txid": "1b8f588256cedc8e31ed5818bdcd1edc5deff96ced9bfebe59acfc3fca8f7629"
}
(main coloseでfundingng transaction)
bash-5.1# lncli --network=simnet walletbalance
{
"total_balance": "1534999932848",
"confirmed_balance": "1534999932848",
"unconfirmed_balance": "0",
"account_balance": {
"default": {
"confirmed_balance": "1534999932848",
"unconfirmed_balance": "0"
}
}
}
bash-5.1#
Code language: PHP (php)
Bob Console
$ docker-compose run -d --name bob --volume simnet_lnd_bob:/root/.lnd lnd
Starting btcd ... done
bob
$ docker exec -i -t bob bash
bash-5.1# lncli --network=simnet walletbalance
{
"total_balance": "2029999990000",
"confirmed_balance": "2029999990000",
"unconfirmed_balance": "0",
"account_balance": {
"default": {
"confirmed_balance": "2029999990000",
"unconfirmed_balance": "0"
}
}
}
bash-5.1# lncli --network=simnet channelbalance
{
"balance": "0",
"pending_open_balance": "0",
"local_balance": {
"sat": "0",
"msat": "0"
},
"remote_balance": {
"sat": "0",
"msat": "0"
},
"unsettled_local_balance": {
"sat": "0",
"msat": "0"
},
"unsettled_remote_balance": {
"sat": "0",
"msat": "0"
},
"pending_open_local_balance": {
"sat": "0",
"msat": "0"
},
"pending_open_remote_balance": {
"sat": "0",
"msat": "0"
}
}
bash-5.1# lncli --network=simnet getinfo
{
"version": "0.14.1-beta commit=v0.14.1-beta-dirty",
"commit_hash": "6042004edaaa5b3cad0a0808ff23dba4716f7178",
"identity_pubkey": "0204e6918ae3239cc8da262715e5ada2d3540cc9fec6db7d6b72e544e2c77f6cac",
"alias": "0204e6918ae3239cc8da",
"color": "#3399ff",
"num_pending_channels": 0,
"num_active_channels": 0,
"num_inactive_channels": 0,
"num_peers": 0,
"block_height": 806,
"block_hash": "56e1d3d993da053d4b50cdd08b8101f72d91fac1a42845cef06eebc3a4293d2d",
"best_header_timestamp": "1642157626",
"synced_to_chain": true,
"synced_to_graph": false,
"testnet": false,
"chains": [
{
"chain": "bitcoin",
"network": "simnet"
}
],
"uris": [
],
"features": {
"0": {
"name": "data-loss-protect",
"is_required": true,
"is_known": true
},
"5": {
"name": "upfront-shutdown-script",
"is_required": false,
"is_known": true
},
"7": {
"name": "gossip-queries",
"is_required": false,
"is_known": true
},
"9": {
"name": "tlv-onion",
"is_required": false,
"is_known": true
},
"12": {
"name": "static-remote-key",
"is_required": true,
"is_known": true
},
"14": {
"name": "payment-addr",
"is_required": true,
"is_known": true
},
"17": {
"name": "multi-path-payments",
"is_required": false,
"is_known": true
},
"23": {
"name": "anchors-zero-fee-htlc-tx",
"is_required": false,
"is_known": true
},
"30": {
"name": "amp",
"is_required": true,
"is_known": true
},
"31": {
"name": "amp",
"is_required": false,
"is_known": true
},
"45": {
"name": "explicit-commitment-type",
"is_required": false,
"is_known": true
},
"2023": {
"name": "script-enforced-lease",
"is_required": false,
"is_known": true
}
}
}
(pubkey->alice console)
bash-5.1#
bash-5.1# lncli --network=simnet listpeers
{
"peers": [
{
"pub_key": "035a928e2fdb16f8356735d189028bd2a424f10d6dea8b997b66ac27811c6e996a",
"address": "172.20.0.3:44330",
"bytes_sent": "391",
"bytes_recv": "391",
"sat_sent": "0",
"sat_recv": "0",
"inbound": true,
"ping_time": "0",
"sync_type": "ACTIVE_SYNC",
"features": {
"0": {
"name": "data-loss-protect",
"is_required": true,
"is_known": true
},
"5": {
"name": "upfront-shutdown-script",
"is_required": false,
"is_known": true
},
"7": {
"name": "gossip-queries",
"is_required": false,
"is_known": true
},
"9": {
"name": "tlv-onion",
"is_required": false,
"is_known": true
},
"12": {
"name": "static-remote-key",
"is_required": true,
"is_known": true
},
"14": {
"name": "payment-addr",
"is_required": true,
"is_known": true
},
"17": {
"name": "multi-path-payments",
"is_required": false,
"is_known": true
},
"23": {
"name": "anchors-zero-fee-htlc-tx",
"is_required": false,
"is_known": true
},
"31": {
"name": "amp",
"is_required": false,
"is_known": true
},
"45": {
"name": "explicit-commitment-type",
"is_required": false,
"is_known": true
},
"2023": {
"name": "script-enforced-lease",
"is_required": false,
"is_known": true
}
},
"errors": [
],
"flap_count": 1,
"last_flap_ns": "1642158005986281830",
"last_ping_payload": null
}
]
}
(alice consoleでopenen channel)
bash-5.1#
bash-5.1# lncli --network=simnet addinvoice --amt=55555
{
"r_hash": "4b75635552a350d5370f14f7961f2bd3d809db1f7966baf7f53c259cfd356fb4",
"payment_request": "lnsb555550n1ps7zkvupp5fd6kx42j5dgd2dc0znmev8et60vqnkcl09nt4al48sjeelf4d76qdqqcqzpgxqyz5vqsp5atu4jx427nlgrwmchg32scz2e3qjdflrxjswcynee34vp3rh3c8s9qyyssqlqfdy6ncp6lm8fs6uyec0yyrqz5aazcum9ehq355x4qpl5c36tj88u5mc636w7aw7n8xjetcgek47hh3u4qp3nprj8k5r6k0cpp837gqusyazn",
"add_index": "1",
"payment_addr": "eaf9591aaaf4fe81bb78ba22a8604acc4126a7e334a0ec1279cc6ac0c4778e0f"
}
(payment_request->alice console.alice console: send payment from alice to bob)
bash-5.1# lncli --network=simnet channelbalance
{
"balance": "55555",
"pending_open_balance": "0",
"local_balance": {
"sat": "55555",
"msat": "55555000"
},
"remote_balance": {
"sat": "1175542",
"msat": "1175542000"
},
"unsettled_local_balance": {
"sat": "0",
"msat": "0"
},
"unsettled_remote_balance": {
"sat": "0",
"msat": "0"
},
"pending_open_local_balance": {
"sat": "0",
"msat": "0"
},
"pending_open_remote_balance": {
"sat": "0",
"msat": "0"
}
}
bash-5.1#
bash-5.1# lncli --network=simnet walletbalance
{
"total_balance": "2030000045555",
"confirmed_balance": "2030000045555",
"unconfirmed_balance": "0",
"account_balance": {
"default": {
"confirmed_balance": "2030000045555",
"unconfirmed_balance": "0"
}
}
}
bash-5.1#
Code language: PHP (php)
※code中、漢字がまざると文字化けが・・まだこのテーマになれない・・