Crypto Academy Season 3 | Advanced course - Task 7: Lightning Network

in SteemitCryptoAcademy3 years ago (edited)

005-uiy.png

sap204.png

Introduction to Lightning Network

The advent of Blockchain technology, Bitcoin, the first-ever cryptocurrency ushers an era of decentralization. While security and decentralization always takes the center stage when we talk about Bitcoin, the scalability is far away to match the modern-day commerce transactions carried out through a payment network like Visa.

Further to make global commerce a reality, free-flowing on the top of Blockchain we have to have sufficient arrangements to scale like Visa, and in addition to that micropayment, recurring payments should be cost-effective. Privacy is another aspect.

Bitcoin network achieves consensus through Gossip protocol. If we broadcast a transaction, it becomes a global transaction, you need to tell all the nodes participating in the network. Privacy is another issue for many.

Lightning Network is a 2nd layer off-chain solution on the top of the Bitcoin network that aims to address the aforementioned bottleneck in the Bitcoin network which encompasses a network of channels, in which only the net settlement(initial and final) are recorded on-chain. All the micro-transaction within the channels are offloaded from the Bitcoin network without compromising the security & decentralization of the Bitcoin Network.

The peers remain in business within a Lightning channel in a trustless way without counterparty risk, with real Bitcoin transactions(off-chain), low/zero fees, etc.

sap204.png

Lightning Network

We have already discussed Multisig Wallet in the previous week. When you create a channel in Lightning Network, you literally create a 2of2 Multisig wallet with your counterparty. In a Lightning Network, only the opening and closing of the channel are recorded on-chain. All the transactions made within the channel are offloaded from the Bitcoin network, so less network congestion, and better scalability.

  • When you first open a channel, you bind a capital to the channel, this is known as funding transaction(on-chain).
  • All the transactions performed within the channel are known as commitment transactions, commitment transaction keeps on updating the channel state(off-chain).
  • When a channel is closed with the last commitment transaction reflecting the final balance, it is known as a settlement transaction(on-chain).

screenshot (22).png

The payment channels within the Lightning Network are protected with the HTLC contract to make it trustless. For instance, in a particular channel(2of2 Multisig wallet) if the counterparty disappears indefinitely, then the fund of the user will remain locked for an uncertain period.

To mitigate such risks, time-locked contracts are used in the Commitment Transactions.

HTLC

A Lightning Network enables users to create a bidirectional payment channel and trade with each other. Such a structure creates a number of local channels.

A Lightning network becomes a global network when many such local channels are interconnected to each other so that a peer can send payment to the distant peer by routing the payment through intermediaries, yet making it trustless.

Hask Lock and Time Lock make the routing of payment through intermediaries trustless.

HTLC is a combination of Hash Lock and Time Lock. A particular payment can be redeemed only if certain information (secret) is disclosed and as per the defined Time Lock.

First, a secret is created and then hashed, then a timelock and hash value are included in the HTLC payment script. The one who knows the secret value can unlock/redeem the HTLC payment within the defined time period, else the payment becomes invalidated.

Secret=R
H= Hash(R)

Now there will be two scenarios:-

  • The counterparty knows the secret to calculate the H value, produce it and redeem it by broadcasting the transaction within the defined timelock.
  • The sender can not redeem it within the timelock, no matter whatsoever the condition is. So for the sender, the Timelock will always be the governing criteria. Within the Timelock he can not redeem it. If the counterparty fails to produce the secret within the defined timelock, then the HTLC payment becomes invalidated for the counterparty, now the sender can redeem it.

Example,

Let's say A, B, and C are three nodes. A is connected to B and B is connected to C. A & C are not directly connected. A wants to send a payment "x" to C. Then either A has to create a payment channel with C or route the payment through B by utilizing the liquidity of B(in return B can charge a certain fee). Creating a separate channel with C could be cost-prohibitive and will also need additional capital.

screenshot (21).png

As the liquidity of B is being utilized in this case, a transaction fee will be paid to B.

  • First C will create a secret and then hash it. As A & C are in a trade, C hands over the hash to A. At this point, the one who knows the secret can unlock the HTLC payment. Here C is the intended recipient. So only C knows it.
  • A will initiate an HTLC payment utilizing the payment channel with B. B does not know the secret, but C knows it and B & C are direct peers. So B will initiate another HTLC payment to C with a condition that the payment can be claimed if the secret value is disclosed.
  • C will redeem it as C knows the secret, as the secret has been disclosed, utilizing that B can redeem the payment from A.

All the HTLC payments are generated with a timelock, so the counterparty has to claim within that time interval otherwise it will timeout.

sap204.png

Lightning Node & Lightning Channel

Lightning Network is basically a 2nd layer off-chain solution on the top of Bitcoin Network. So the very first impression is that the Lightning network in itself is a network consisting of nodes and lightning channels.

Lightning channels basically allow the two peers on either side to send/receive money. The channels can be created by either side by binding a certain capital on-chain, in other words, you can say lock an amount "x" on-chain and spend the same/part amount of money(up to a max of "x") in the Lightning network with no fees/low fees with another peer/merchant/eCommerce store, etc.

screenshot (23).png

Further, a merchant or peer may not be directly connected with you, but you can still make payment in the lightning network by routing the payment through intermediary nodes. The only thing is that it is required to establish a loop to connect to the merchant. That's the job of a node to connect the origin & destination.

So the creation of the channel will require an on-chain transaction, and then you can do multiple transactions(subject to in-bound & out-bound capacity). When you want to close the channel the final balance will be recorded again on-chain.

Just think of it like a contract where you bind a capital to perform multiple micro-transactions off-chain with another peer using Lightning Network. Sine, it is off-chain, you are saved from the miner fee, waiting time(at least 10 mins) for confirmation on-chain. That is how it enables micropayments, saves time, and transaction cost.

A lightning node interacts with the 1st layer(i.e. the Bitcoin Network) as well as the second layer(i.e. the nodes of Lightning Network). The peer-to-peer establishment should remain intact so that anyone can join(decentralization), in general, the node should be able to interact with a Bitcoin node independently in a trustless way(state).

The lightning node together with Lightning channels constitute the Lightning network, a 2nd layer solution for lightning-fast payment. You can spend as low as 1 satoshi with zero fees, and instant settlement, you do not have to wait like on-chain transactions.

That's possible in the lightning network because a lightning node validates payment of its immediate peer(direct payment), not the entire network. In the case of the Bitcoin network, a node verifies the transaction of the entire network. That is why the on-chain Bitcoin network is slow and not scalable, whereas the Lightning network is lightning fast. It also offers better privacy, it does not whisper a financial transaction to all the nodes.

Requirement of an LN Node

When you require to send a payment to a distant peer who is not directly connected with you, you need the help of a node who can route the payment and help it to reach the destination, a node can help you to establish the optimal route. To make the route trustless HTLC is used.

The biggest issue with a lightning network is that as the network grows, it suffers from higher bandwidth, memory, CPU which does impact the user experience, imagine with a mobile you want to send payment over a lightning channel you need to update to the network graph every time. Because lightning channel in itself is a dynamic thing, on a daily basis new channels are being created and closed. So an optimal loop today may not hold good tomorrow. So it does impact the user experience, may not look attractive from business/enterprise-grade implementation.

So pre-identified Lightning Network Node like Trampoline & many others implement the protocol, equipped with the necessary resources to remain in sync with the Network graph offers optimal solutions to the general users. So they are basically the ideal pre-identified network nodes to open a channel for the general users. In other words, if you remain in sync with the pre-identified LN node, you remain in sync with the entire network without fulfilling necessary requirement of bandwidth/CPU. The trampoline node will bear that responsibility, so basically they make it a light version for the general users.

htl3.png

The pre-identified nodes(Trampoline) take care of the necessary storage to update the network graph, they will find the optimal route for the users to help the payment reach destination. They will also make it private(cryptography).

In general, think of the Trampoline node as a payment gateway for a user who wants to purchase from an eCommerce store, the Trampoline node in the backend will do the rest to help the payment reach the merchant.

sap204.png

Open a Lightning Channel in Electrum

Open your Electrum Wallet(assuming you have already downloaded and installed your wallet(standard)). You can see a separate tab-- "Channels". Click on it. If you don't see it, Go to View, then click on Show Channel.

Next, click on Open Channel. A pop-up will appear, click on Yes.

screenshot (4).png

screenshot (5).png

Then enter the amount(the default node is Electrum Trampoline), the min amount is 0.002 BTC(or 2 mBTC), hit the OK button.

screenshot (6).png

Enter the Password, Click on Send.

screenshot (7).png

Now the Channel is established(Remote Peer ID indicated). You can also verify the transaction from Block Explorer.

screenshot (8).png

screenshot (10).png

You will notice the Short Channel ID showing as Not Yet Available, after three confirmations, it will display the Short Channel ID. Further, you can also notice the Channel Capacity is 2 mBTC, I can send 1.962 mBTC to another peer. I can receive 0 because I have not made any transactions yet using Lightning Channel. Receive amount is equal to the difference between my capacity and the Available Balance. So I need to spend first before being able to receive.

screenshot (9).png

Generally opening a Channel means binding a capital into it which becomes the capacity of the Channel. Here, in this case, 2mBTC is the capital and it is an on-chain transaction that you can see.

So here I will only tell the Bitcoin network/nodes that a channel has been created and for any subsequent transactions within the channel, I need not have to tell the node, if I close the channel, there will be another on-chain transaction, which will be the final settlement. Put simply, opening and closing of a Channel will incur transaction costs, any transaction within the Channel won't incur any transaction fee.

sap204.png

Backup a Channel

Backing up a Channel will help you close your channel and send the fund on-chain. Please note that with Channel backup you can not restore a Channel. If you have lost your Channel Backup file then you need the help of the peer on the other side of your channel to close the channel.

To export Channel Backup, right-click on the Node, then select Export Backup, it will display the QR code, you can also copy the text. Then click on Save.

screenshot (1).png

screenshot (2).png

screenshot (3).png

sap204.png

Make a transaction using Lightning Channel

You can only make a transaction in the Lightning Network when someone is directly or indirectly connected with you. In case the two peers under consideration are not directly connected, the payment will hop over different intermediaries/nodes to reach the destination.

Now I will make an e-commerce transaction using Lightning Channel.

Go to https://www.bitrefill.com/buy. Select any product of your choice. Here I chose to recharge my Data pack(Jio). Fill in all the necessary information. Then check out and select the payment method as Lightning(BTC). Now it will display the QR code, Alternatively, you can copy the invoice, now return to your Electrum Wallet.

screenshot (12).png

screenshot (13).png

screenshot (14).png

screenshot (15).png

screenshot (16).png

Click on the Send tab, then enter the invoice(or simply paste the invoice) in "Pay to", then click on Pay. Confirm it by clicking on Yes. It is indeed lightning fast. You do not have to wait. It's instant. Now the Channel capacity is updated. You can receive equal to the amount you have spent so far.

screenshot (17).png

screenshot (18).png

screenshot (19).png

I will make another e-commerce purchase using Bitrefill. As I go on spending my receiving capacity will increase.

Click on History to check the entire history of Transactions you have made so far.

screenshot (20).png

sap204.png

Swap function

You can also notice a Swap button under the Channel tab, next to Open Channel. This is another useful inbuilt feature in Electrum Wallet.

What generally happens in Lightning Channel, it always requires an in-bound capital to be able to make a transaction with another peer using Lightning Channel. When you wish to realize it on-chain you have to close the Channel. Closing the Channel means closing the business for a Merchant and getting disconnected from the customer base.

Through a Lightning channel, a merchant and customers connect to each other and make an instant transaction. In general, from a business perspective, cash flow and rotation are important aspects. So lightning network's inbound capacity becomes a bottleneck for many merchants. But a Swap function allows a merchant to swap the lightning fund to on-chain without affecting the inbound capacity. Similarly, a customer/user equally gets befitted from such a thing.

Further, swapping also helps you to improve the capacity of the channel. For instance, if you swap your on-chain fund for a lightning fund, it will improve your sending capacity. Similarly, if you swap your lightning fund for an on-chain fund then it will improve your receiving capacity.

Click on Swap, enter the desired details, and hit the OK button. It will then update the balance, sending capacity, and receiving capacity.

screenshot.png

sap204.png

Close the Channel

You can close the channel any time you wish to. The last commitment transaction reflecting the final Blanace will be recorded on-chain in that case.

Now right-click on the channel. Select Close Channel. Then Click on Yes.

screenshot (24).png

screenshot (25).png

screenshot (26).png

screenshot (27).png

Now the channel is closed. The final Balance of the Channel(as per the last settlement) has been returned to my wallet and recorded on-chain.

Click on Addresses to check the Balance and the address bearing that Balance(returned after closing the Channel). Right Click on that particular Address(bearing the balance) and check it on Block Explorer.

screenshot (28).png

screenshot (29).png

sap204.png

Homework Task (Season 3/ Week-7)

(1) 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? (within 300 words)

(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? (within 200 words)

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

(4) 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)

(5) 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? (Screenshots needed)

Important Note-

  • Please note that BTC transaction fee is not that high(that is expensive only when you use Centralized Exchanges), So I would suggest you using ChanngeNOW to exchange your Altcoin to BTC if you don't have BTC(It is indeed very cost-effective, costs less than $2 for 100 USD worth exchange).

  • Do not write the steps of how to set up a standard wallet, do not write an unnecessarily lengthy post, avoid redundant words/sentences.

Guidelines

  • Your article should be at least 300 words.
  • It is always better to gain sufficient user experience before submitting your article.
  • Refrain from spam/plagiarism. This task requires screenshot(s) of your own experience. Use images from copyright-free sources and showcase the source, if any.
  • This homework task will run until 14th Aug'2021, Time- 11:59 PM UTC.
  • Users having a reputation of 60 or above, and having a minimum SP of 500(excluding any delegated-in SP) are eligible to partake in this Task. (Must not be powering it down)
  • Add tag #sapwood-s3week7 #cryptoacademy in your post and should be among the first five tags. You can also use other relevant tags like #blockchain #lightning #bitcoin #trampoline or any other relevant tag. And also make sure you post in the Steemit Crypto Academy community.
  • Those who include the real transactions/screenshots will score better.
  • Always hide sensitive info.

(Please feel free to join the comment section if you have any doubt on Homework-Task)

Thank you.

sap204.png

Cc:-
@steemitblog
@steemcurator01
@steemcurator02

sap204.png

Unless otherwise stated, all the images, infographics, charts, tables, etc(if any) are my original works. The screenshots, logos are taken from Electrum.

sap204.png

Sort:  

IMG_20210810_123754.jpg

I think A & C should be in highlighted line.

Corrected now. Thank you so much for the proof-reading.

Professor @sapwood,

(1) 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? (within 300 words)

Do you mean we must write about all topics (Lightning Network, Lightning Node, Lightning Channels, and two other questions) within 300 words, or do we should write for each within 300 words?

Thanks, Professor.

 3 years ago (edited)

Do you mean we must write about all topics (Lightning Network, Lightning Node, Lightning Channels, and two other questions) within 300 words, or do we should write for each within 300 words?

No.

Not all within 300 words, that is specific to a sub-part or Q.

For the Q.1 - within 300 words, it can be even 200 words too.

Q.2- 200 Words

Q.3- 300 words

Q.4 and Q.5 do not have restrictions, but you should not make it too lenghty.

You should not make it long. You can even manage it within 200 words(for the sub-parts) if you are good at making it concise and clear.

Thank you.

Thank, Professor. Going back to work.

 3 years ago 

So if I understand correctly sir all of the Q1 answers should be in an answer less than 300 words?

Yes.

 3 years ago 

Woow Interesting lesson. I'm going to participate professor 😁

👍❤️

Is making real transactions necessary professor @sapwood?

Not mandatory, but you can score better, you can check Q No-4 that asks for practical exploration.

Thank you.

Ok sir

No logro adquiere lo mbtc para realizar esta pregunta. donde puedo cambiarlos o hacer swap, ya que son mas de 50 dolares para poder enviar btc a las direcciones que nos da Lightning

La comision de retiro es de 0.0054 que ya son 40 y algo de dolares. ya que los btc los tengo en Binance. Es algo costosa esta tarea :C

Not at all, it will cost only $1 roughly as an exchange fee, exchange some altcoins in ChnageNOW, I have already mentioned it.

vgtr.png

para acctivar un canal pide minimo 200.000 satoshis jjejeje lo intente hasta ese punto tuve que borrar la tarea jajaja no iva a poder concluirla :c

ya habia logrado depositsr en la wallet desde atomic wallet que tiene un swap y todo. pero al llegar a la parte de abrie un canal, me pidio minimo 200.000 satoshis al cambio eran 80 y algo de dolares. aunque parezca mentira la velocidad en aceptación de la transaccion y el posterior retiro duro mas de 40 minutos en ambos casos. la.red e BTC si esta lenta jajaja las confirmaciones duran muuuucho.. experiencias que viví hoy jaja

There are many other non-custodial wallets, too, you could try. e.g. Phoenix.

Thank you.

 3 years ago 

Hello professor @sapwood, I have a total SP of 598 and wants to delegate 250 to my community.. Please after doing so, am I still eligible to take on advantace courses??? Because I want to start doing your Homework now

Yes, delegating out will not affect your eligibility.

You are eligible.

Thank you.

 3 years ago 

Okay thanks alot for the clarification professor

 3 years ago 

@sapwood, in the fee rate can we put Mempool or Static, instead of ETA? As I have 1.4 mBTC in my account and whenever I try to make the channel they say Funding sat too low.

 3 years ago (edited)

If you are using Electrum, then the min amount to open a channel is 2 mBTC. That is why you are not being able to open the channel.

Thank you.

 3 years ago 

Yeah I was trying to use the Electrum, now I get it. Thanks.

0.002 btc is equal to 100$. That is too much. In my view, people are not interested in making homework post due to this fact. First we will have to convert sbd to steem then steem to btc and then ot will be sent in Electrum wallet. Practical work is required to complete this homework post and it is quite difficult.

I was writing the post and now I realized this fact and stopped my post.

First read this Question once again:-

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?

It is not mandatory to explore an LN wallet in Electrum only. You can choose any non-custodial wallet.

Have you tried Phoenix??

You can explore with Phoenix with as little as 10000 satoshis which is less than $5.

In the Advanced tier, the average reward is more than 50 USD. So you can not spend even $5 for a Task and that too u can withdraw it back and use it for other purposes. So in reality, if we take fees into account, then it will only incur a total cost of 2 USD at max, not more than that. I think that makes sense.

Thank you.

 3 years ago (edited)

I honestly experienced the same 100$ hitch... I used up to 30$ worth of Bitcoin and it kept saying sat too low.. Will try again with the Phoenix wallet, most of us assumed since it was too high with the electreum wallet it will be the same with other wallet. Thank you for the clarification sir

Thanks for suggestion of Phoenix and I have used this wallet for this homework post. Here is my post link for your kind consideration.

https://steemit.com/hive-108451/@mawattoo8/crypto-academy-season-3-week-7-homework-post-for-professor-sapwood

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.034
BTC 63900.40
ETH 3140.82
USDT 1.00
SBD 3.98