Crypto Academy / Season 3 / Week 7 - Homework Post for @sapwood : Lightning Network

in SteemitCryptoAcademy3 years ago (edited)

In this post, I am here to submit my Crypto Academy S3 Task 7, which I have carried as required by prof. @sapwood.

WhatsApp Image 2021-08-12 at 8.02.58 AM.jpeg

Q. Explain what do you mean by Lightning Network, Lightning Node, Lightning Channels? How does it achieve a near-instant settlement, near-infinite scalability? What are the different uses?

Lightning Network


Bitcoin can be considered as the future of our financial system. But you must be aware of its scalability issues, in which both the fee and time are consumed more in the transaction. So the system came up with an idea of 2nd layer solution, known as the Lightning Network in January 2017.

Lightning Network is a decentralized 2nd layer payment protocol technology on top of Bitcoin Network to enable the possibility of micropayments, which can be as low as 1 satoshi. Spending 1 satoshi in the Bitcoin network makes no sense because the average cost of a transaction is around 5 USD. So the Lightning network came with a solution to first make it possible to make thousand of micropayments without any fee or negligible fee, and secondly, it records off-chain. So a large number of micropayments are offloaded from the main Bitcoin network, which reduces congestion and makes it more scalable. Simply, It makes the Bitcoin Network pretty instant, secure & cheap and allows peer-to-peer payments & micropayments possible.


Lightning Node


screenshot - 2021-08-13T174248.805.png

Lightning nodes are the nodes used in the lightning network and these nodes are connected with each other with lightning channels, and at the same time to validate initial and last transaction, these are also connected with the main blockchain.

These lightning nodes connect the sender and receiver but as we know that there are many ways by which this can be done, so nodes connect the sender and receiver with the shortest way if direct connection is not available between those.


Lightning Channels


screenshot - 2021-08-13T174710.937.png

A channel is a 2of2 multi-sig wallet, so 2 cosigners are needed. The channel connects to the nodes to intersect with the counterparty. So trade is possible if a channel is available and topology is available. An LN consists of thousands of nodes and channels. LN nodes maintain a state of the channel and also interact with other nodes in LN and also report it to the main Bitcoin network. So the topology is always known to a node. Within the channel, all the transactions are off-chain except the opening balance and the closing balance.


Near-instant settlement & Near-infinite scalability


As in the Bitcoin network to complete and validate the transaction, all the nodes of the blockchain have to be involved in this process. So this makes it pretty slow, also consumes a lot of time and energy.

A channel is a 2of2 multi-sig wallet. If I am in trade with a direct peer, I will sign the transaction and my counterparty will approve it, as he is the recipient so it will happen instantly. As I said earlier before you can open a channel a certain amount is first needed to add to it, otherwise, you can not make any transaction within the channel. So if I have already added an amount of 2 mBTC, then anything less than or equal to 2 mBTC is readily available for me to instantly transact with the person with whom I am in the trade. If it is not directly connected to me, I will take the help of the node to locate the shortest possible route and sent money, here there will be some transaction fee, but it will be negligible as compared to on-chain transactions.

So point number one is as I have already added some money during the opening channel I do not need any further approval to spend that money, which is readily available to sign. Second, the node verifies the payment of a channel that is directly connected to it, so all the nodes simultaneously and independently vary a lot of different transactions at the same time, so that exponentially improves the scalability of the LN network.


Uses of Lightning Network


(1). If a vendor or merchant in real-world integrates to LN Network, then I can purchase goods/services directly and instantly without the wait time of on-chain confirmation.

(2). Repetitive payment and small amounts of payments become a reality in Bitcoin with LN.

(3). Hash Lock and TimeLock make it trustless, so it can be applied for other networks too, means cross-chain atomic swap between different blockchain with different consensus rule & governance can also be possible.

(4). LN can be a suitable alternative to cold storage wallets. Generally, centralized exchanges rely on cold storage wallets.

Q. How does a Lightning network enable two peers to make a transaction or recurring or multiple transactions without any transaction fee? Does it happen off-chain or on-chain?


The channel is a 2of 2 multi-sig wallet. The channel can not be created without capital. When I add the capital, I perform an on-chain transaction. That means I have already validated it in the main chain (Bitcoin chain). So LN will allow me to spend that much amount which I have added. I can spend it partially or fully as it pleases me. Since I have already added it by an on-chain transaction, I am free to sign the transactions as many times as I want within the channel.

The channel is a 2of2 multi-sig wallet. I will pass the invoice to the recipient and sign it, that's all, I do not need to verify it on-chain because my capacity to spend here is already determined by the funding transaction (1st transaction on-chain). And I can do it many times, of course, up to the max of my capacity.

Does it happen off-chain or on-chain?

On-chain-- only first transaction, Off-chain- all other transaction (except the last transaction, in case close the channel).

As I keep spending, my sending capacity will reduce, and my receiving capacity will improve. So with every purchase or trade, the channel state will update accordingly and in real-time.

Now there are two types of transactions that work on the lightning network, one Is direct and indirect transactions. Both these transactions depend on the Lightning channel that is connectivity between two nodes. When two nodes make transactions in between them, then it doesn't require any transaction fee and is known as a Direct transaction. When two nodes are not connected directly, they need to take the help of an intermediate node or peer, there is a small booked reward available for an intermediate node that is deducted as a transaction fee from the user account known as an indirect transaction.

Q. How does the routing of payment through intermediate nodes(or peers) become trustless with HTLC?


Each transaction in the off-chain lightning network is secured by HTLC smart contract. It stands for hash lock time lock Contract. All lightning channels are created on-chain and each channel is inter-connected with each other. Let us try to understand this concept by taking an example-

For instance, I want to purchase from Bitrefill, but I am not directly connected with it. But I am connected with the Trampoline node (intermediate peer). And somehow, the bitrefill is connected to the trampoline node, either directly or indirectly. Now, first of all, I get an invoice from bitrefill which contains a hash (Hash of the preimage). Preimage is a secret that only bitrefill knows. So only Bitrefill can unlock it.

So first, I send it to the trampoline node and included the hash with the condition that, if the secret is known, it can be unlocked, and also it has to be within the time lock. But the trampoline node does not know the secret, so he will create another HTLC payment to bitrefill and include the hash, and put the condition. But He will also add an additional amount as fees i.e. 0.00004 mBTC in my case.

screenshot - 2021-08-14T063802.360.png

Finally, Bitrefill will unlock it within the time window. Now the secret is no more secret, it is revealed. So Trampoline can unlock the fund using the secret value. That is how HTLC makes it trustless. I took the help of the Trampoline node to send the payment to bitrefill, but I need not have trust the trampoline node because of HTLC.

Q. Set up Lighting Wallet(Create a standard wallet, then open a Channel), open a Lightning Channel using Electrum(Trampoline Node), Phoenix, or any other non-custodial wallet that supports Lightning Network, make an eCommerce purchase(or a micro-Transaction) using Lightning Channel, then close the Channel? Does it incur fees? Does it record the transaction on-chain? If yes indicate it, if no, then tell us why it does not record the transaction on-chain? How does Swap Function help to improve sending and receiving capacity? (Screenshots needed)


Opening and setting up a Lightning Channel using Electrum Wallet -

Firstly, open your Electrum Wallet (standard). Now click on the Channels tab. By the way, this tab will be visible in your wallet, if not then simply go to View in the menu bar, then click on Show Channels.

Now click on Open Channel. A pop-up will appear stating that does not put a large amount in Lightning Channel as Lightning Network supports as an experiment in Electrum. Simply hit the Yes button.

screenshot - 2021-08-12T165353.608.png

screenshot - 2021-08-12T165822.327.png


Enter the amount (the minimum amount is 2mBTC or 0.002 BTC for Electrum Trampoline). You cannot open the Lightning Channel on this node if you have less than 2 mBTC in your wallet.

Click on OK.

Then Enter the Password => Hit the Send button.

screenshot - 2021-08-12T170634.691.png

screenshot - 2021-08-12T170945.793.png


Now the channel is established perfectly. You can see the remote peer I'd on the next pop-up.

You can verify this transaction from the blockchain explorer. Here you can see two outputs. As we started the process of opening Lightning Channel with a partial amount, 2 mBTC whereas the total balance in our wallet is 2.4548 mBTC. So Bitcoin network prevents you to send a partial amount, so Electrum will internally create another wallet as output which is known as Change address and the remaining balance will return to the wallet again.

So after deducting the fee, the remaining wallet balance (0.4498 mBTC) is returned to the wallet through the change address.

screenshot - 2021-08-12T171113.816.png

screenshot - 2021-08-14T132341.143.png


Now you can see that the channel has been established but the Short Channel I'd is still showing as Not yet Available and the status is as Opening. Apart from this, you can also see the channel information such as channel capacity (2mBTC), can send as 1.95556 mBTC, and can receive as 0, because I haven't made any transaction yet.

Basically, the channel is established but is not usable. The channel will be usable after three confirmations.

screenshot - 2021-08-12T171330.270.png


Now you can notice that the channel status changed as Open, which means 3 confirmations have been completed. Also, Short Channel I'd is displayed. Lightning Channel is now usable and transactional.

screenshot - 2021-08-14T134817.511.png

As this is a valid Lightning Channel, transactions can be made on it. And as the process of creating the channel took place on the on-chain network, simply this transaction can be viewed on the Blockchain Explorer.

newwww line.png

Making an e-commerce transaction using Lightning Channel -

Simply visit https://www.bitrefill.com, and click on Browse Products. On bitrefill, A total of 4000+ products are available.

Now select any product, gift card, as you like. Here I selected VI (network operator), for my data recharge. Now fill in the required information in the given fields e.g. Phone Number & Amount. Hit the Purchase button.

screenshot - 2021-08-11T223843.706.png

screenshot - 2021-08-11T224030.217.png

screenshot - 2021-08-12T172017.192.png


Now a pop-up will appear asking you to keep shopping or checkout. Simply click on Check Out to confirm the order.

Enter your email I'd for order status updates => Hit the Confirm button.

screenshot - 2021-08-12T172126.531.png

screenshot - 2021-08-12T172351.731.png


Now choose a payment method as Lightning Network (From the requirement of the task).

Now a new interface will appear displaying you all the order details & a QR code. Leave the QR code as it is. Simply click on the Copy button below the Qr Code. It will show you the Lightning Invoice on a popup. Copy it and return back to the Electrum wallet.

screenshot - 2021-08-12T172532.728.png

screenshot - 2021-08-12T172708.623.png

screenshot - 2021-08-12T172914.529.png


Now on your Electrum wallet, Go to the Send button. Enter/Paste the invoice of your order in the Pay to field. The amount and description will be filled in automatically.

Hit the Pay button. Then Click on Yes to confirm the transaction. The order is in progress.

screenshot - 2021-08-12T173422.007.png

screenshot - 2021-08-12T173611.611.png

screenshot - 2021-08-12T173737.361.png


Simply, The order gets completed in no time. It is surely lightning. Now Go to the Channel tab => You may notice that the receiving capacity of the channel has been updated as you spent so far.

screenshot - 2021-08-14T143144.913.png


Click on the History tab to check the history and status of the transaction you made. You can notice here that it is showing the history of two e-commerce purchases in my wallet.

Yes, it sure is right, As I have made two e-commerce purchases so far to increase the channel receiving capacity. And also because of the receiving capacity was not updated/displayed due to the small amount in the first purchase.

screenshot - 2021-08-14T143352.005.png


And here are the eMails of the two e-commerce purchases from Bitrefill. As I have shopped properly with it twice.

screenshot - 2021-08-12T174545.697.png

newwww line.png

Close the channel


Go to the Channel tab. Your Lightning Channel is displayed. Right-click on the channel => Click on the Close Channel .

Now it will take you to the next interface, Simply hit the Yes button to close the channel.

A pop-up will appear stating that the wallet has been closed,

screenshot - 2021-08-12T174945.231.png

screenshot - 2021-08-13T210142.011.png

screenshot - 2021-08-14T140354.353.png


Now you can notice that the status has changed to Shutdown/Closing, This means that the process of closing the wallet is still going on. After the required confirmations, the wallet will be completely closed.

Now you can see the status it has changed to Closed.

screenshot - 2021-08-14T143759.527.png

Now the final balance of the channel, as per the last transaction, is routed back to my wallet through the change address and it is recorded on-chain.

To check, go to the Addresses tab. And check the Change Address (highlighted in yellow).

And Simply go to the History tab. Right-click on the settlement transaction (Close Channel). Click on View Transaction. Now copy the transaction I'd.

Visit blockchain explorer => Paste the transaction I'd. Now you can see all the information about this transaction on Blockchain Explorer (on-chain).

screenshot - 2021-08-14T144448.227.png

screenshot - 2021-08-14T145336.705.png

newwww line.png

Does it incur fees?


Yes, it does incur charges. But it depends on the transaction how much fee it will charge for that transaction. For example, As I said earlier (in ques 3), I purchased products from Bitrefill. And as I was not directly connected to the bitrefill, the trampoline node was involved as an intermediate peer in this case. So the Trampoline node will charge an additional amount as a fee for making this payment. And as this transaction is recorded off-chain, it charges negligible fees.

So in two transactions (Commitment Transaction) from bitrefill, the fees are as follows-

TxnTransaction Fees (mBTC)
Bitrefill Txn 10.00003 mBTC
Bitrefill Txn 20.00004 mBTC

screenshot - 2021-08-14T153854.762.png

Bitrefill Txn 1

screenshot - 2021-08-14T153947.179.png

Bitrefill Txn 2


On the other side, It charges certain fees in the form of mining fees for funding transactions and settlement transactions, which are recorded on-chain. You can adjust the fee by setting the rate per byte that you are willing to pay for this transaction.


Does it record the transaction on-chain?


No, Everything does not get the record in on-chain. Only initial and final transactions are recorded on-chain. The transactions that we perform on the lightning network are recorded off-chain, that is all these are recorded in the history of the lightning wallet. Also, these transactions don't have a record on the main chain. We can only check these payments in the lightning wallet. When you close a channel except for the last commitment transaction all other becomes null and void for Bitcoin network. The final state of the channel (closing the channel) is recognized by a Bitcoin network.

newwww line.png

Swap Function


Swap Function is another useful feature of the Electrum wallet. It surely is a useful feature, it allows the merchant to move the fund on-chain without closing the channel. In LN Channel you can not bring the fund back to your wallet on-chain if you don't close it. Having a swap function allows you to do without the requirement to close a channel, so the channel will still remain there which establishes a connection with the customer base. And there is always positive cash flow for business as a merchant can easily swap the LN fund for an on-chain fund.

The swap function doesn't have any effect on the channel, rather it improves the sending and receiving capacity of the channel.

There are two types of swapping:

  • LN to On-chain-- the inbound capacity
  • On-chain to LN-- will improve outbound capacity

1. Swap lightning funds for on-chain funds -


This function improves the receiving capacity of the Lightning channel.

Go to the Channel tab. Click on the Swap button.

On the next interface, Enter the amount you want to swap. Then click on OK. For this swap, I am sending 0.1 mBTC for which I will receive 0.08971 mBTC.

Note, the receiving capacity before the swap is 0.05071 mBTC and the total wallet balance is 0.4498 mBTC.

screenshot - 2021-08-12T183120.703.png

screenshot - 2021-08-12T175917.790.png


After the required confirmations, The swap of lightning funds for on-chain funds will be completed. Now you can notice that the receiving capacity of the channel as 0.15079 mBTC and wallet balance as 0.53951 mBTC have changed.

screenshot - 2021-08-14T145740.743.png


Also, you can check the information of swapping (transaction) on blockchain explorer as it records On-chain.

Now Click on the History tab to check the history and status of the swapping ( transaction ). Swapping of 0.1 mBTC has been completed and the capacity has also been updated.

screenshot - 2021-08-14T145916.030.png

screenshot - 2021-08-12T214151.675.png


2. Swap on-chain funds for lightning funds -


This function improves the sending capacity of the Lightning channel.

Go to the Channel tab. Click on the Swap button. Now it will take you to the interface of Swap lightning funds for on-chain funds first. So simply click on the Double arrow button to change the interface for swap on-chain funds for lightning funds.

Now the interface changes. Enter the amount you want to swap. Then click on OK. For this swap, I am sending 0.11 mBTC for which I will receive 0.10268 mBTC.

Note, the sending capacity before this swap is 1.78482 mBTC and the total wallet balance is 0.53951 mBTC.

Enter the Password to proceed further.

screenshot - 2021-08-12T214318.745.png

screenshot - 2021-08-12T214733.785.png

screenshot - 2021-08-12T214842.437.png


After the required confirmations, The swap of on-chain funds for lightning funds will be completed. Now you can notice that the sending capacity of the channel as 1.8875 mBTC and wallet balance as 0.42571 mBTC have changed.

screenshot - 2021-08-14T150146.663.png


Also, you can check the information of swapping (transaction) on blockchain explorer as it records On-chain.

Now Click on the History tab to check the history and status of the swapping ( transaction ). Swapping of 0.11 mBTC has been successfully completed and the capacity has also been updated.

screenshot - 2021-08-14T150341.049.png

screenshot - 2021-08-13T063050.802.png

Q. How do you back up a wallet file(Channel Backup), why and when is it needed? Indicate the funding Transaction, Commitment Transaction, and Settlement Transaction both in your wallet and Block Explorer?


Steps to back up a wallet file -

Go to the Channel tab. Your Lightning Channel is displayed, Right-click on the channel => Click on Export Backup.

It will take you to the next interface, where it shows the QR code. Hit the Save button to save the QR code or alternatively, you also can copy the text & copy the image.

screenshot - 2021-08-12T174749.630.png

screenshot - 2021-08-12T211046.655.png


Enter the file name as you want. Here I am saving the file with the default name itself as qrcode. Click on Save.

The backup file was successfully exported.

screenshot - 2021-08-12T211247.575.png

screenshot - 2021-08-12T211420.903.png


Why and when is backup needed?


Backing up a file comes in handy in case we lose our wallet file by requesting another peer to close the channel and sending funds on-chain. So we can easily transfer our funds from off-chain to on-chain by scanning the QR code. The point to be noted is that we back something up so that we can restore it in case we lose it, but the channel backup can't restore our channel.

newwww line.png

Funding Transaction


Funding transaction is nothing but refers to that transaction when we open the Lightning channel for the first time and bind a fund to that channel. This transaction takes place On-chain and is stored in the Blockchain. Basically, this transaction happens when we open Lightning Channel by adding funds.

screenshot - 2021-08-13T141800.498.png


Simply Go to the History tab. Right-click on the funding transaction (Open Channel). Click on View Transaction. Now copy the transaction I'd.

Visit Blockchain Explorer => Paste the transaction I'd. Now you can see all the information about this transaction on Blockchain Explorer (on-chain).

screenshot - 2021-08-14T150559.472.png


Commitment Transaction


Commitment Transaction refers to all the transactions that are performed on the Lightning Channel. As in Lighting Network, only 2 nodes participate in validating the transaction, and it is also off-chain. So this transaction will be stored on Lightning Channel instead of being stored on the Blockchain. Also, this transaction incurs negligible fees.

As I have made two e-commerce purchases (Vi & Paytm) so far to increase the channel receiving capacity. So these transactions will be known as Commitment Transaction. And simply these are displayed on Lightning Channel (off-chain) itself.

Commitment Transaction 1 - In this transaction, I did a data recharge on my VI's number.

screenshot - 2021-08-13T142831.331.png

To know all the details and fees of this transaction, Simply Go to the History tab. Right-click on the Commitment transaction. Click on View Payment.

screenshot - 2021-08-14T150907.534.png


Commitment Transaction 2 - For this transaction, I did a recharge on my Paytm number.

screenshot - 2021-08-13T143405.428.png

To know all the details and fees of this transaction, Simply Go to the History tab. Right-click on the Commitment transaction. Click on View Payment.

screenshot - 2021-08-14T150955.373.png


Settlement Transaction


Settlement Transaction refers to the transaction when the channel is closed with the last commitment transaction showing the closing balance. The total balance is routed back to the main wallet through the change address. And this transaction is recorded on-chain.

screenshot - 2021-08-14T153459.310.png


Simply Go to the History tab. Right-click on the Settlement transaction (Close Channel). Click on View Transaction. Now copy the transaction I'd.

Visit Blockchain Explorer => Paste the transaction I'd. Now you can see all the information about this transaction on Blockchain Explorer (on-chain).

screenshot - 2021-08-14T150746.297.png

newwww line.png

Channel State, Outbound Capacity, Inbound Capacity


screenshot - 2021-08-14T151409.881.png

Thank You

Sort:  
Loading...

You have been upvoted by @sapwood, a Country Representative from INDIA. We are voting with the Steemit Community Curator @steemcurator07 account to support the newcomers coming into Steemit.

Engagement is essential to foster a sense of community. Therefore we would request our members to visit each other's posts and make insightful comments.

Thank you

Thank you

Coin Marketplace

STEEM 0.18
TRX 0.13
JST 0.028
BTC 64479.05
ETH 3181.85
USDT 1.00
SBD 2.57