diff --git a/18_3_Setting_Up_a_Channel.md b/18_3_Setting_Up_a_Channel.md index 92598c1..f0643fb 100644 --- a/18_3_Setting_Up_a_Channel.md +++ b/18_3_Setting_Up_a_Channel.md @@ -2,11 +2,11 @@ > :information_source: **NOTE:** This is a draft in progress, so that I can get some feedback from early reviewers. It is not yet ready for learning. -You're now ready to your first Lightning Network channel. To begin with, you'll need to understand what is, and how it's created using c-lightning. +You now understand the basics of your Lightning setup, and hopefully have either created or been given info on a second Lightning node. You're ready to create your first Lightning Network channel. Of course, you'll need to understand what is, and how it's created using c-lightning. > :book: ***What is a Lighting Channel?*** Simply, a lightning channel is a money tube that allows fast, cheap and private transfers of money without sending transactions to the blockchain. More technically a channel is a 2-of-2 multisignature on-chain Bitcoin transaction that establishes a financial relationship between two people or two agents without requiring trust. The channel mantains a local database with bitcoin balance for both parties, keeping track of how much money they each have. The two users can then exchange bitcoins through their Lightning channel without ever writing to the Bitcoin blockchain. Only when they want to close out their channel do they settle their bitcoins, based on the final division of coins. -> :book: ***What is the Lightning Network (Take Two)?*** Although a Lightning channel only allows payment between two users, channels can be connected together to form a network that allows payments between members that doesn't have a direct channel between them. This creates a network among multiple people built from pairwise channels. +> :book: ***How do Lightning Channels Create a Lightning Network?*** Although a Lightning channel only allows payment between two users, channels can be connected together to form a network that allows payments between members that doesn't have a direct channel between them. This creates a network among multiple people built from pairwise connections. In this section, we will continue using our c-lightning setup as our primary node. @@ -22,9 +22,9 @@ Creating a Lightning channel requires the following steps: In order to move funds to a Lightning channel first requires funding your c-lightning wallet. -> :book: ***What is a c-lightning wallet?*** C-lightning's standard implementation comes with a integrated bitcoin wallet that allows you send and receive on-chain bitcoin transactions. This wallet will be used to create new channels. +> :book: ***What is a c-lightning wallet?*** C-lightning's standard implementation comes with a integrated Bitcoin wallet that allows you send and receive on-chain bitcoin transactions. This wallet will be used to create new channels. -The first thing you need to do is send some satoshis to your c-lightning wallet. You can create a new address using `lightning-cli newaddr` command. This generates a new address that can subsequently be used to fund channels managed by the c-lightning node. You can specify the type of address wanted; if not specified the address generated will be a bech32. +The first thing you need to do is send some satoshis to your c-lightning wallet. You can create a new address using `lightning-cli newaddr` command. This generates a new address that can subsequently be used to fund channels managed by the c-lightning node. You can specify the type of address wanted; if not specified, the address generated will be a bech32. ``` $ lightning-cli --testnet newaddr @@ -33,7 +33,9 @@ $ lightning-cli --testnet newaddr "bech32": "tb1qefule33u7ukfuzkmxpz02kwejl8j8dt5jpgtu6" } ``` -You can then send funds to this address using `bitcoin-cli sendtoaddress` (or any other methodlogy you prefer). For this example, we have done so in the transaction [11094bb9ac29ce5af9f1e5a0e4aac2066ae132f25b72bff90fcddf64bf2feb02](https://blockstream.info/testnet/tx/11094bb9ac29ce5af9f1e5a0e4aac2066ae132f25b72bff90fcddf64bf2feb02) This transaction is called the [funding transaction](https://github.com/lightningnetwork/lightning-rfc/blob/master/03-transactions.md#funding-transaction-output), and it needs to be confirmed before funds can be used. +You can then send funds to this address using `bitcoin-cli sendtoaddress` (or any other methodlogy you prefer). For this example, we have done so in the transaction [11094bb9ac29ce5af9f1e5a0e4aac2066ae132f25b72bff90fcddf64bf2feb02](https://blockstream.info/testnet/tx/11094bb9ac29ce5af9f1e5a0e4aac2066ae132f25b72bff90fcddf64bf2feb02). + +This transaction is called the [funding transaction](https://github.com/lightningnetwork/lightning-rfc/blob/master/03-transactions.md#funding-transaction-output), and it needs to be confirmed before funds can be used. To check you local balance you should use `lightning-cli listfunds` command: @@ -45,7 +47,7 @@ c$ lightning-cli --testnet listfunds } ``` -Since the funds do not yet have 6 confirmations, there is no balance available. After six confirmations you should see a balance: +Since the funds do not yet have six confirmations, there is no balance available. After six confirmations you should see a balance: ``` c$ lightning-cli --testnet listfunds { @@ -65,170 +67,106 @@ c$ lightning-cli --testnet listfunds "channels": [] } -``` +``` +Note that the value is listed in satoshis or microsatoshis, not Bitcoin! -Now that you have funded our c-lightning wallet you will need information about a remote node to start creating channel process. +> :book: ***What are satoshis and msat?*** You already met satoshis way back in [§3.4](03_4_Receiving_a_Transaction.md). One satoshi is one hundred millionth of a bitcoin, so 300,000 satoshi = 0.003 BTC. A satoshi is the smallest unit of currency on the Bitcoin network. But, the Lightning network can go smaller, so 1,000 msat, or millisatoshis, equal one satoshi. That means that 1 msat is one hundred billionth of a bitcoin, and 300,000,000 msat = 0.003 BTC. -#### Connect to remote node +Now that you have funded your c-lightning wallet you will need information about a remote node to start creating channel process. -The first thing you need to do is connect your node to a peer. This is done with the `lightning-cli connect` command. Remember that if you want more information on this command, you should type `lightning-cli help connect`. The connect RPC command establishes a new connection with another node in the Lightning Network. +### Connecting to remote node -You have two options here, you could set up a Lightning node on a second machine using the Standup script and choose LND implementation or search a node to connect to. +The next thing you need to do is connect your node to a peer. This is done with the `lightning-cli connect` command. Remember that if you want more information on this command, you should type `lightning-cli help connect`. -To connect your node to a remote peer you need it's id that represents the target node’s public key. As a convenience, id may be of the form id@host or id@host:port. Using `lightning-cli listnodes` command you obtain all nodes available on the network and choose one. +To connect your node to a remote peer you need its id, which represents the target node’s public key. As a convenience, `id` may be of the form `id@host` or `id@host:port`. You may have retrieved this with `lightning-cli getinfo` (on c-lightning) or `lncli --network=testnet getinfo` (on LND) as discussed in the [previous interlude](18_2__Interlude_Accessing_a_Second_Lightning_Node.md). + +We've selected the LND node, `032a7572dc013b6382cde391d79f292ced27305aa4162ec3906279fc4334602543`, which is located at IP address `45.33.35.151`, which we're going to connect to from our c-lightning node: ``` -c$ lightning-cli --network testnet listnodes -``` - -``` - { - "nodeid": "033c2c5eb5cc514b54264a838048b4e7194281e2dcd4ad03bab0198259df2dcbc7", - "alias": "shangoa0c8225d-d86b-4", - "color": "e20f00", - "last_timestamp": 1533416062, - "features": "", - "addresses": [ - { - "type": "ipv4", - "address": "54.158.207.111", - "port": 9735 - } - ] - }, - { - "nodeid": "03dc1ad7b657c4d7a042f1847ffcb953cd353bcfe818bce008c35abdcdc25a5257" - }, - { - "nodeid": "030b3a8efb847f1c267172d7afbfe93bf501c44a76c6ac6294a8b6b59335d5cdcd", - "alias": "030b3a8efb847f1c2671", - "color": "3399ff", - "last_timestamp": 1558873572, - "features": "", - "addresses": [ - { - "type": "ipv4", - "address": "167.99.231.18", - "port": 9735 - } - ] - }, - { - **"nodeid": "0302d48972ba7eef8b40696102ad114090fd4c146e381f18c7932a2a1d73566f84",** - "alias": "0302d48972ba7eef8b40", - "color": "3399ff", - "last_timestamp": 1594828492, - "features": "02a2a1", - "addresses": [] - }, - -``` -We've selected node with public key 0302d48972ba7eef8b40696102ad114090fd4c146e381f18c7932a2a1d73566f84 and we'll connect it with `lightning-cli connect` command: - -``` -c$ lightning-cli --network=testnet connect 0302d48972ba7eef8b40696102ad114090fd4c146e381f18c7932a2a1d73566f84@127.0.0.1:9736 +$ lightning-cli --network=testnet connect 032a7572dc013b6382cde391d79f292ced27305aa4162ec3906279fc4334602543@45.33.35.151 { - "id": "0302d48972ba7eef8b40696102ad114090fd4c146e381f18c7932a2a1d73566f84", + "id": "032a7572dc013b6382cde391d79f292ced27305aa4162ec3906279fc4334602543", "features": "02a2a1" } -``` +``` -To check out: +### Opening a channel -``` -c$ lightning-cli --network=testnet listpeers -{ - "peers": [ - { - "id": "0302d48972ba7eef8b40696102ad114090fd4c146e381f18c7932a2a1d73566f84", - "connected": true, - "netaddr": [ - "127.0.0.1:9736" - ], - "features": "02a2a1", - "channels": [] - } - ] -} -``` -On success, an object with a “peers” key is returned containing a list distinct objects. Object features are bit flags showing supported features. - -#### Open a channel - -The fundchannel RPC command opens a payment channel with a peer by committing a funding transaction to the blockchain. You should use `lightning-cli fundchannel` command that receives this parameters: - -* **id** is the peer id obtained from connect. +The fundchannel RPC command opens a payment channel with a peer by committing a funding transaction to the blockchain. You should use `lightning-cli fundchannel` command to do so, with the following parameters: +* **id** is the peer id return from connect. * **amount** is the amount in satoshis taken from the internal wallet to fund the channel. The value cannot be less than the dust limit, currently set to 546, nor more than 16.777.215 satoshi (unless large channels were negotiated with the peer). - * **feerate** is an optional feerate used for the opening transaction and as initial feerate for commitment and HTLC transactions. - -* **announce** is an optional flag that triggers whether to announce this channel or not. Defaults to true. If you want to create an unannounced private channel put to false. - +* **announce** is an optional flag that triggers whether to announce this channel or not. It defaults to true. If you want to create an unannounced private channel set it to false. * **minconf** specifies the minimum number of confirmations that used outputs on the channel opening processe should have. Default is 1. - * **utxos** specifies the utxos to be used to fund the channel, as an array of “txid:vout”. -Now we open the channel like this: +Now you can open the channel like this: ``` -c$ lightning-cli --network=testnet fundchannel 0302d48972ba7eef8b40696102ad114090fd4c146e381f18c7932a2a1d73566f84 280000 urgent true 1 +$ lightning-cli --testnet fundchannel 032a7572dc013b6382cde391d79f292ced27305aa4162ec3906279fc4334602543 100000 urgent true 1 { - "tx": "0200000000010102eb2fbf64dfcd0ff9bf725bf232e16a06c2aae4a0e5f1f95ace29acb94b09110000000000feffffff02264b000000000000160014aa572371f29310cd677d039cdcd054156c1a9545c045040000000000220020c1ebc407d32cd1fdcd7c0deb6817243b2b982cdaf3c70413f9d3ead29c36f11f024730440220676592b102ee659dfe5ac3acddbe35885140a8476ae7dbbb2f53a939cc815ac0022057a66de1ea16644008791d5cd510439bac00def02cbaa46d04febfe1d5e7e1e001210284368eca82346929a1c3ee2625077571b434b4c8111b81a715dfce5ea86dce1f1f2c1b00", - "txid": "9843c037f54a4660b297a9f2454e11d26d8659f084a284a5740bb15cb1d97aa6", - "channel_id": "a67ad9b15cb10b74a584a284f059866dd2114e45f2a997b260464af537c04399" + "tx": "0200000000010193dc3337837f091718f47b71f2eae8b745ec307231471f6a6aab953c3ea0e3b50100000000fdffffff02a0860100000000002200202e30365fe321a435e5f66962492163302f118c13e215ea8928de88cc46666c1d07860100000000001600142fe02e5be9283e8c5bcb93ae61421baf8cb64f9c024730440220668a7c253c9fd83fc1b45e4a52823fb6bc5fad30da36240d4604f0d6981a6f4502202aeb1da5fbbc8790791ef72b3378005fe98d485d22ffeb35e54a6fbc73178fb2012103b3efe051712e9fa6d90008186e96320491cfe1ef1922d74af5bc6d3307843327c76c1c00", + "txid": "66694d23ca15efe379e5f4a71d9be1a2d65e383b89ee3abe126ee36a12f23c1d", + "channel_id": "1d3cf2126ae36e12be3aee893b385ed6a2e19b1da7f4e579e3ef15ca234d6966", + "outnum": 0 } ``` To confirm channel status use `lightning-cli listfunds` command: ``` -c$ lightning-cli --network=testnet listfunds +c$ lightning-cli --testnet listfunds { "outputs": [ { - "txid": "9843c037f54a4660b297a9f2454e11d26d8659f084a284a5740bb15cb1d97aa6", - "output": 0, - "value": 19238, - "amount_msat": "19238000msat", - "scriptpubkey": "0014aa572371f29310cd677d039cdcd054156c1a9545", - "address": "tb1q4ftjxu0jjvgv6emaqwwde5z5z4kp49299gmdpd", - "status": "confirmed", - "blockheight": 1780768, + "txid": "66694d23ca15efe379e5f4a71d9be1a2d65e383b89ee3abe126ee36a12f23c1d", + "output": 1, + "value": 99847, + "amount_msat": "99847000msat", + "scriptpubkey": "00142fe02e5be9283e8c5bcb93ae61421baf8cb64f9c", + "address": "tb1q9lszuklf9qlgck7tjwhxzssm47xtvnuu4jslf8", + "status": "unconfirmed", "reserved": false + }, + { + "txid": "b5e3a03e3c95ab6a6a1f47317230ec45b7e8eaf2717bf41817097f833733dc93", + "output": 1, + "value": 200000, + "amount_msat": "200000000msat", + "scriptpubkey": "0014ed54b65eae3da99b23a48bf8827c9acd78079469", + "address": "tb1qa42tvh4w8k5ekgay30ugyly6e4uq09rfpqf9md", + "status": "confirmed", + "blockheight": 1862831, + "reserved": true } ], "channels": [ { - "peer_id": "0302d48972ba7eef8b40696102ad114090fd4c146e381f18c7932a2a1d73566f84", + "peer_id": "032a7572dc013b6382cde391d79f292ced27305aa4162ec3906279fc4334602543", "connected": true, "state": "CHANNELD_AWAITING_LOCKIN", - "channel_sat": 280000, - "our_amount_msat": "280000000msat", - "channel_total_sat": 280000, - "amount_msat": "280000000msat", - "funding_txid": "9843c037f54a4660b297a9f2454e11d26d8659f084a284a5740bb15cb1d97aa6", - "funding_output": 1 + "channel_sat": 100000, + "our_amount_msat": "100000000msat", + "channel_total_sat": 100000, + "amount_msat": "100000000msat", + "funding_txid": "66694d23ca15efe379e5f4a71d9be1a2d65e383b89ee3abe126ee36a12f23c1d", + "funding_output": 0 } ] } + ``` -While the channel with 280.000 satoshis (Channel capacity) is confirmed its state will be CHANNELD_AWAITING_LOCKIN and we got an change output with 19.238 sats. After all 6 confirmations are completed, channel will change to CHANNED_NORMAL state, that it will be it's permanent state. -As we're using testnet network these values are used as an example to show the different states that a channel can have. In a channel with real funds, the actual mining circumstances of the mainnet network must be taken into account. +While this new channel with 100,000 satoshis is unconfirmed, its state will be `CHANNELD_AWAITING_LOCKIN`. Note that unconfirmed change of `99847` satoshis is also showing as a new transaction in the wallet. After all six confirmations are completed, the channel will change to `CHANNED_NORMAL` state, which will be its permanent state. -The funding_txid onchain is [9843c037f54a4660b297a9f2454e11d26d8659f084a284a5740bb15cb1d97aa6](https://blockstream.info/testnet/tx/9843c037f54a4660b297a9f2454e11d26d8659f084a284a5740bb15cb1d97aa6) - -#### Channel capacity - -As we said before both sides of the channel own a portion of its capacity. The amount on your side of the channel is called *local balance* and the amount on your peer’s side is called *remote balance*. Both balances can be updated many times without closing the channel (sending final balance to the blockchain), but the channel capacity cannot change without closing or splicing it. The total capacity of a channel is the sum of the balance held by each participant in the channel. -Next chapter we will deep creating and paying invoices. +Meanwhile, the funding transaction can be found onchain at [66694d23ca15efe379e5f4a71d9be1a2d65e383b89ee3abe126ee36a12f23c1d](https://blockstream.info/testnet/tx/66694d23ca15efe379e5f4a71d9be1a2d65e383b89ee3abe126ee36a12f23c1d) +> :book: ***What is Channel Capacity?*** In a Lightning Channel, both sides of the channel own a portion of its capacity. The amount on your side of the channel is called *local balance* and the amount on your peer’s side is called *remote balance*. Both balances can be updated many times without closing the channel (when the final balance is sent to the blockchain), but the channel capacity cannot change without closing or splicing it. The total capacity of a channel is the sum of the balance held by each participant in the channel. ## Summary: Setting up a channel -You need to create a channel with remote nodes to be able to receive and send money over the lightning network. Further a maintenance task is required to the channels to keep them balanced. +You need to create a channel with a remote node to be able to receive and send money over the Lightning Network. ## What's Next? -Continue "Understanding Your Lightning Setup" with [§19.1: Generate a Payment request](19_1_Generate_a_Payment_Request.md). - +You're ready to go! Move on to [Chapter Nineteen: Using Lightning](19_0_Understanding_Your_Lightning_Setup.md).