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

in hive-108451 •  4 years ago  (edited)

Introduction

It's a great pleasure to be back at the academy again to learn more about cryptocurrency. This time, I've attended the lecture presented by professor @sapwood where the professor taught about the Lightning Network, of course, it was a great lecture. I will be attending to the task given by the professor without wasting time.

0001-5899740094_20210813_194812_0000.png
Designed in Canva with a free image- Image Source

1. What is Lighting Network, Lightning Node and Lightning Channel?

Lightning Network

Lightning network is the second layer of the Bitcoin network which is aimed to address the shortcomings of the Bitcoin network. Bitcoin network is associated with delayed transactions up to 10mins as miners carry out confirmations before adding a block, this is quite a time-wasting process.

To address the aforementioned shortcomings, Lightning network comes as the 2nd layer network of Bitcoin which is aimed at enabling instant transaction and improved scalability of the network. In Lightning network, we have the lighting channel which enables lightning nodes interaction and has only the first and last transaction recorded on-chain while other transactions that take place in the channel are kept off-chain.

Lightning Node

Remember, nodes are devices connected to share transaction data on the blockchain, likewise, the lightning node is seen as the device that establishes connection of sending and receiving of payments between peers on the lightning network through nodes interaction. Furthermore, the interaction of the nodes form the channels for sending and receiving funds, which are the lightning channels and the combination of all forms of the lightning network.

In addition, the lightning node has the capability to interact with the on-chain Bitcoin network to reveal the status of the Lightning network, the lightning node carries out validations of the transactions of the node they are directly interacting with to enhance fast transactions.

Lightning Channel

This is a network of Lightning nodes, that is, the combination of the interaction between the lightning nodes forms the lightning channel which enables peer-peer sending/receiving of funds in a decentralized way and the transactions within the channels are kept off-chain thereby enabling fast transactions.

How does it achieve a near-instant settlement, near-infinite scalability?

Near-instant settlement is made possible in the lightning network whereby nodes that are directly connected in the channel do the job of transaction validation without waiting for other nodes and this results in swiftness of transactions. Likewise, near-infinite scalability is achievable through the off-chain transaction that takes place in the channel which eliminates congestion of network, it improves scalability as numerous transactions are done effectively without the existence of 10 mins confirmation waiting time of the Bitcoin network seen in on-chain transactions.

Uses of Lightning Network

  • It is used to perform micro-transactions off-chain which is associated with high scalability as it eliminates the confirmation waiting time in on-chain transactions.

  • It allow users to connect with a merchant (which could be an e-commerce) for purchase of goods at no transaction fee because it is done off-chain, only intermediate nodes charge for their routing job. Therefore, it's cost-effective.
  • Fred Br.png

    2. 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?

    On the Lightning network, to perform a transaction or recurring or multiple transactions without any transaction fee starts from the user intending to spend on the Lightning network binding an amount to the channel and such transaction is recorded on-chain. Furthermore, all transactions that take place within the Lightning network channel are off-chain and at no transaction fee.

    As a sender or receiver, they are may be connected directly or indirectly connected through nodes and the network of the nodes forms the channel, the sender can create a channel through a node to be routed to the another node to connect with another peers. In the process of creating a channel, a certain amount is bound and this is done on-chain. After the successful opening of the channel, the peers are connected through the Lightning nodes and can make transactions with no transaction fee off-chain (only indirect connection cost charges) at a very high speed since there is a need for waiting confirmations, nodes that are directly involved carry out transaction validations.

    In short, the binding of the capital to the channel is done on-chain with transaction fee while all micro-transactions that are done within the channel are off-chain that can be done swiftly numerous times with no transaction fee or with infinitesimal charges by the intermediate node through which the payment is routed.

    Fred Br.png

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

    Let's talk briefly about the HTLC, Hashed Timelock Contract is a smart contract-based payment in the crypto ecosystem to make payment with conditions routed through intermediaries whereby the funds are redeemed within the timelock by supplying the hash secret.

    This payment type comes with the innovation to solve problems that might occur between two individuals that set up 2 of 2 multi-sig wallets to carry out a trade but one of them might be away indefinitely while the funds in the wallet is hanging for an unpredictable period, with the HTLC payment, this problem is solved as payment are to be redeemed within the timelock in a trustless manner. In short, payment through the HTLC can only be redeemed by a user who knows the hash secret and it must be redeemed within the timelock deadline. Let's see how it works below.

    Assuming there are 3 nodes, Fred, PanWorld and Jane, having Fred and Jane to be the ones that have business dealings and PanWorld is the intermediary through which the payment is routed. Let's see the steps below

  • The intended recipient Jane creates a secret and hash it. The output hash is then revealed to Fred with whom they are in trade together.

  • Now that Fred has the secret, Fred creates a HTLC payment routed through PanWorld, PanWorld and Jane are directly connected and Jane has the secret.

  • In this case, PanWorld creates another HTLC payment with a condition, such that Jane can redeem the payment only if she knows and disclose the secret value to PanWorld to redeem the payment. Of course, Jane knows the secret.

  • Jane then will redeem the payment by disclosing the secret value to PanWorld who redeem the payment from Fred. And that completes a successful payment between sender Fred and receiver Jane routed through the intermediary PanWorld.

  • Note that, payment must be redeemed within the timelock.

    Fred Br.png

    4. Setting up a Lightning Wallet

    How to open a Channel

    I already have a Standard wallet on Electrum, such that the process of setting up a Lightning wallet began. Let's see the steps below.

  • I launched my Electrum standard wallet. Let's see the wallet below.
  • iMarkup_20210813_135417.jpg

  • Now, I clicked on Channels at the top of the page and selected the Open channel.
  • iMarkup_20210813_140621.jpg

  • I received a prompt message asking if I want to open a channel such that I clicked OK.
  • iMarkup_20210813_141243.jpg

  • I inputted the amount I am binding to the channel which is 2mBTC through the Electrum Trampoline which is the default routing node on Electrum and clicked OK. On the next interface to confirm the transaction, I inputted my password and clicked Send. The mining fee cost 0.0054mBTC.
  • iMarkup_20210813_140420.jpg

    iMarkup_20210813_140300.jpg

  • The channel is established and I clicked OK. It requires 3 confirmations before the channel can become usable.
  • iMarkup_20210813_140034.jpg

  • The transaction is waiting for confirmations such that the Short Channel ID is Not yet available.
  • iMarkup_20210813_135931.jpg

  • Now, the channel is fully opened. I have the channel capacity to be 1.94736 mBTC and it's the amount I can send and 0 as the amount I can receive. The receive remains zero because I have not used the channel for sending before.
  • iMarkup_20210813_135604.jpg

    My channel opening transaction is on-chain. Now, let's check the transaction details on the Blockchain explorer.

    iMarkup_20210813_142725.jpg

    Above is the details of my channel as recorded on-chain. The transaction hash is; 68ab5d402365436e03baf39bdbab93240de3ab2bcc158b72b02222758b26296e.

    Making a e-commerce purchase using the Lightning channel

    For this section of the task I will be using the e-commerce platform https://www.bitrefill.com/buy to make a purchase using the Lightning channel. Let's see it in a few steps below.

  • I visited the website https://www.bitrefill.com/buy. And from the landing page, I selected MTN voucher as the product I am interested to purchase.
  • iMarkup_20210813_151428.jpg

  • On the next page, I inputted the amount of the MTN voucher I am purchasing which is 500 NGN and clicked the purchase button. Then I Checked out to proceed to payment.
  • iMarkup_20210813_151350.jpg

    iMarkup_20210813_151231.jpg

  • I am required to input my email address to get notification as regards my transaction status. I inputted my email, agreed to Terms of Use and Privacy policy then clicked Continue.
  • iMarkup_20210813_151146.jpg

  • Now, it's time to select payment method, I selected Lightning BTC and the payment cost 3118 sats.
  • iMarkup_20210813_151026.jpg

  • On the next interface, I clicked Copy to get the Lightning invoice, and it was copied.
  • iMarkup_20210813_150941.jpg

    iMarkup_20210813_150820.jpg

  • Now that I have gotten the Lightning invoice, I opened my Electrum Standard wallet and clicked Send at the top of the page. I inputted the Lightning invoice, clicked the Pay button and prompt message to confirm the process surfaced to send 0.03139 mBTC, then I clicked Yes. The transaction status changed to in progress.
  • iMarkup_20210813_150653.jpg

    iMarkup_20210813_150531.jpg

  • In a few seconds, the transaction was completed and I received the airtime on my phone.
  • iMarkup_20210813_155104.jpg


    How to close the channel

  • To close the channel, I right-clicked on the channel and selected the Close channel button.
  • iMarkup_20210813_163204.jpg

  • A window popped up for confirmation of the process and I clicked Yes. Now I have successfully closed the channel and I clicked OK.
  • iMarkup_20210813_163235.jpg

    iMarkup_20210813_163312.jpg

  • I checked on the addresses and the closing balance from the channel which is 1.9604 mBTC has been routed to my wallet through a change address.
  • iMarkup_20210813_163345.jpg


    Is there any fee incurred?

    For the Open channel transaction, a transaction fee of 0.0054 mBTC was incurred because it's an on-chain transaction. The e-commerce purchase would have been completely without fees at all due to the fact it was done off-chain but the intermediate node involved charged an infinitesimal fee of 0.00007 mBTC to have the transaction completed.

    iMarkup_20210813_150353.jpg
    e-commerce transaction fee

    The close channel transaction comes with the transaction fee of 0.00815mBTC because it happened on-chain.

    iMarkup_20210813_165408.jpg
    Close channel transaction fee


    Does it record the transaction on-chain?

    The e-commerce purchase was not recorded on-chain, such that the answer to this question is NO. The reason it does not record the transaction on-chain is that the transaction took place within the Lightning network channel that has all transactions carried out within the channel recorded off-chain. Remember, the Lightning Network is a second layer network on top of the Bitcoin network whereby transactions within its channels are kept off-chain to improve the speed of transactions and enhance scalability.

    How does Swap Function help to improve sending and receiving capacity?

    It's important to open a basis to the swap function, it allow users to swap funds from on-chain for lightning funds and vice-versa, without closing the channel. In the process, it increases the sending capacity of a user if the user swap the on-chain funds for lightning funds, likewise, the receiving capacity of a user is increased as he/she uses the swap function to swap lightning funds for on-chain funds.

    iMarkup_20210813_163119.jpg
    On-chain funds <---> lightning funds interface

    iMarkup_20210813_163048.jpg
    Lightning funds <---> on-chain funds interface

    Fred Br.png

    5. How to backup a wallet file (Channel backup)

  • To backup the wallet file, I right-clicked on the channel and selected Export backup from the options.
  • iMarkup_20210813_162824.jpg

  • Now on the next interface, I selected the Save button to proceed.
  • iMarkup_20210813_162911.jpg

  • On my computer, I leave the filename as qrcode then clicked the save button and that completes the process of backup.
  • iMarkup_20210813_162955.jpg


    Why and When the Backup file is needed

    The backup file is used by the user to close the channel. The backup file is needed when one intends to close the channel and move the funds from the Lightning network to on-chain. Users should understand that the backup file cannot be used to restore a channel rather it is used to close a channel.

    Indication of funding transaction, commitment transaction and settlement transaction in the wallet and block explorer

    iMarkup_20210813_163428.jpg

    Above is the screenshot of the history of funding, commitment and settlement transactions on the wallet. Let's see each of them below.

    Funding transaction: This transaction is the first transaction that took place when opening a channel through which the user binds a certain capital to a channel and it's recorded on-chain. Let's see my funding transaction both in wallet and blockchain explorer.

    iMarkup_20210813_180013.jpg
    Funding transaction on the wallet

    iMarkup_20210813_142725.jpg
    Funding transaction on the blockchain explorer

    The screenshots above has the indication of my funding transaction of 2 mBTC and transaction fee 0.0054 mBTC shown both on wallet and the blockchain explorer.

    Commitment transaction: This is the transaction that takes place within the Lightning channels and they are recorded off-chain. Let's see it on my wallet below.

    iMarkup_20210813_182124.jpg
    Commitment transaction on my wallet

    Above is the transaction I made within the channel which is 0.03139 mBTC which comes with a charges of 0.00007 mBTC by the intermediate node. It has the transaction hash, preimage and lightning invoice in the record.

    Settlement transaction: Settlement transaction is the final balance on a channel when it was closed and routed back to the wallet. This transaction is recorded on-chain. Let's see my transaction as indicated on the wallet and blockchain explorer.

    iMarkup_20210813_180103.jpg
    Settlement transaction on the wallet

    iMarkup_20210813_165408.jpg
    Settlement transaction on the blockchain explorer

    The screenshots above is the indication of the settlement transaction that has 1.9604 mBTC routed back my wallet with a transaction fee of 0.00815 mBTC.

    Fred Br.png

    Conclusion

    In conclusion, the Lightning network is the second layer network that is aimed at correcting the shortcomings of the main Bitcoin network. It improves the speed of transactions and scalability by keeping transactions that happen within the channel off-chain such that transactions are processed faster and at no transaction fee (intermediate nodes may charge and it's always infinitesimal).

    On the Lightning network, HTLC payment is utilized to transact between peer-peer in a trustless manner whereby the only one that knows the secret value can redeem the funds in a transaction. Thanks to professor @sapwood for this great lecture.

    Fred Br.png

    Cc: @sapwood

    Fred Br.png

    Written by;
    @fredquantum

    Authors get paid when people like you upvote their post.
    If you enjoyed what you read here, create your account today and start earning FREE STEEM!
    Sort Order:  
    Loading...