Crypto Academy season 3:- Week 7 - Homework Post for professor @sapwood
So let's get started:-
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:-
As we know that being one of the largest blockchains, it is used by most people for transactions. But there are many problems related to bitcoin blockchain In which the problem of low scalability is most important.
As we know that because of the limitations of bitcoin protocols, the maximum size of a block in the bitcoin blockchain is 1 MB and also the average time of generating a new block is 10 minutes which puts limitations on its scalability.
To solve this problem, the idea of the Lightning network was put forward which is a 2nd layer payment protocol.
It allows instant payment and less congestion in the main blockchain by linking the nodes with channels to perform off-chain transactions which don't require validation from the main blockchain that is the bitcoin blockchain in this case. Thus not every transaction will be needed to get validation from blockchain thus it will reduce congestion and will increase the scalability of the blockchain.
In this network smart contracts named hashed timelock contract (HTLC) protocol are used to control the transactions. In this initially, the initial transaction is made on the main blockchain. Which is transparent and everyone can see it. But after this, all the transactions except the last, are made off-chain and are controlled by a smart contract. After the last transaction is made, and that is validated by off-chain, it is validated by the main blockchain and is updated on the main blockchain.
Lightning nodes
Lightning nodes are the nodes which by connecting with one another creates the Lightning network and allows the off-chain transactions but at the same time, these nodes are also connected with the main blockchain.
The main purpose of Lightning nodes is to establish a connection between the sender and the receiver via the shortest route available which consumes minimum fees.
Lightning channels
Lightning channels are the links that connect nodes with each other. These channels by connecting all nodes ensure the transaction using the Lightning network.
How does it achieve near-instant settlements
As we know that, in the bitcoin blockchain, any transaction has to be verified by all the nodes connected with the blockchain. And every transaction has to be validated. This makes blockchain congested. And it took much time to validate the transactions. Thus scalability is low.
On the other hand, in the Lightning Network, Lightning nodes themselves validate the transactions and no main nodes are involved in any off-chain transactions. Only the initial and last transaction is validated by the main blockchain. This way it gives us near-instant settlements.
Near- infinite scalability::- As in Lightning network, not on-chain nodes validates the commitment transactions only initial and the last transaction is validated by the main blockchain. Thus by validating the initial Transaction, you can make hundreds of transactions without validation by the main blockchain and when you validate your last transaction, all the remaining transactions are validated as a single transaction and thus in achieve near-infinite scalability by validating hundreds of transactions at the same time.
Uses of Lightning network:-
The major uses of the Lightning network are to make micropayment for buying something from an e-commerce website and at stores that accept these types of payment. If we use the main blockchain to make micropayment then the fees levied are quite high as compared to these micropayments.
But by using the Lightning network we can make hundreds of transactions without fees if the nodes are connected directly and even if nodes are connected indirectly then too the levied fees are quite low.
Other uses are the reduction of congestion and increasing the scalability of the main blockchain by reducing the number of payments on the main blockchain.
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?
To make a transaction in the Lightning network, first of all, a link is created between nodes, and the initiator deposits the fund in that link, and this transaction is done on the main chain so everyone can see this transaction. This process is called a funding transaction.
After the fund is deposited, access keys to access that fund is provided to both nodes. These access keys are unique and both keys are required to make a transaction. And no single node with only an access key can make a transaction. This acts as a 2 of 2 cosigner wallet in the multi-sign wallet.
Now as the link between nodes is established, now transactions between these nodes can be made but these transactions are not needed to get validated by the main blockchain. Instead of getting validated from the main blockchain, these transactions are validated by nodes themselves by entering the access keys to access the fund and for the security of funds, a smart contract hashed timelock contract is used.
These transactions are called commitment transactions.
Now between two nodes, multiple transactions can be made without any limitations and without any transaction fees. The transaction fees are not levied in this case as these transactions are not being broadcasted to the main blockchain.
After every commitment transaction, the wallet ledger is updated with the updated balance and sign of both parties.
When the link between these nodes is to be closed then the last copy of the transaction signed by both parties is provided to the main blockchain. And all the transaction made is considered as one single transaction and the main blockchain validates that as one single transaction.
Thus we can conclude that in the Lightning network only the first transaction which is a funding transaction and the last transaction is broadcasted to the main blockchain and other transactions known as commitment transactions are kept off-chain.
How does the routing of payment through intermediate nodes(or peers) become trustless with HTLC?
Peer to peer transaction that takes place in the Lightning network is secure and safe as HTLC which is an abbreviation for hashed timelock contract which is a combination of hashed function along with time is used to secure the transaction.
Now we know that there are many nodes in the Lightning network and all are interconnected with each other with Lightning channels. And any user A can send funds/money to user B through various channels.
Now commitment Transactions are secured with hashed timelock contract. And funds can only be unlocked by entering the secret hash. But along with this, these transactions are also associated with time, and funds can only be unlocked or redeemed in a prescribed time.
If the receiver couldn't enter the secret hash in the prescribed time then funds will be refunded to the sender. And funds are refunded to the sender only in this case as transactions are secured with HTLC and after the allocation of funds or initiation of a transaction, the sender can't alter it to cheat the receiver.
In the Lightning network, if the nodes are not connected directly with each other, in that case, intermediary nodes are used to complete transactions. And like in the directly connected nodes, these transactions are secured with HTLC protocol.
In this sender or receiver creates a secret and hash it and uses it to initiate the transaction. And it uses HTLC protocol to secure the funds and to use time lock. Then thus fund is transferred to the intermediate node but as this node doesn't have a secret hash, it can't unlock the funds.
The fund can only be unlocked with a secret hash.
The intermediate node also initiates another transaction secured with HTLC protocol that requires the receiver to prove that funds belong to him by entering the secret hash before the prescribed time.
When the receiver proves this, then all the intermediate nodes can not unlock the fund in the sequence. And then this fund is transferred to the receiver wallet.
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)
To create a channel in the Lightning network, I am here using the Electrum wallet because it supports the Lightning transaction. You can go for any wallet which does the same.
First of all go to your electrum wallet, and select the channel section and then click on an open channel
Now it will ask for your confirmation that you really want to open your first channel. Click on ok on this
Now select the node you want to connect with. Here I am going for the default node.
Also here enter the amount of mBTC. Here I am entering 2mBTC.
Now enter the account password to verify and then click on send to broadcast your message to the main blockchain for verification.
after the transaction is successful, a pop-up will be shown stating that your channel has been established.
Now this will show you that channel status as an opening because it is still not ready. Here you can see your short Channel ID, your Lightning node, capacity and can send and can receive. These all values are visible on the screenshot.
Now as we discussed earlier and also in this question we broadcasted the initial Transaction on the main blockchain, it can be traced or explored using a blockchain explorer.
Here you can see that my transaction is still unconfirmed and as soon as it is confirmed by miners, you will see my account status as open.
Now my initial Transaction is confirmed and you can see my short channel ID and also my channel status open
Now as my initial Transaction is validated by the main blockchain and now all transactions expect last can be done using a Lightning network that is done off-chain and confirmed by nodes itself rather than the Main blockchain.
E-commerce purchasing using Lightning Network:
To make an e-commerce purchase, visit any e-commerce site which supports the purchase using the Lightning network. For example, here I am using the Bitrefill website.
Go to Bitrefill website and the home page will look like this.
Now click on browse 4000+ products to browse your desired product.
Now I am going for recharging my mobile and as I am using reliance jio sim, I clicked on that section
now choose your desired plan that you want to purchase after entering your mobile number. And then click on purchase. It also will show you your plan worth In bitcoin
Now it will ask you to check out or continue shopping. If you want to buy more, click on keep shopping otherwise click on checkout
Now, enter your e-mail ID but it will not ask you to confirm email it will just send you a notification of your purchase.
Also, click on terms and conditions and then click on continue
Now click on your desired payment methods as here we are using a Lightning network to pay, click on Lightning BTC
now a QR code will be shown to make payment. But click on the copy option below to copy Lightning invoice ID.
Copy this Lightning invoice ID
Lightning invoice ID is the secret hash that is generated by the receiver. Now I as a sender will use this hashed secret to make payment.
Now go to your electrum wallet as a sender and click on send and then enter the pay to address, description, and amount in the designated fields. And then click on pay
Now it will ask you to confirm the transaction. Click on ok to confirm.
Here you can see the progress of the payment it is still in progress. As soon as it is confirmed, your order will be purchased.
After your payment is confirmed, your order will be completed.
You can see the history of your order by clicking on the history section. Here you can see one more order which I made for recharging my another number for 10 rupees.
Using the same strategy, you can purchase more than 4000 products using this website as claimed by this website.
Does it incur fees?
The answer to this question is yes. It does incur fees but this fee is quite low. This fee is for using intermediate nodes to move transaction after the receiver claim the fund by entering the secret hash. This process was discussed already in this task.
So yes fees are changed and you can see how much fees are changed by going to the history section and then by clicking on order.
These are the two orders I purchased. You can see that on these orders, I was charged 0.00006 and 0.00007 mBTC respectively which is 6 SAT and 7 SAT respectively.
Does it record the transaction on-chain?
Does it record the transaction on-chain? The answer is no. As we have purchased our order using the Lightning network, these transactions are done off-chain and these transactions are not yet validated by the main blockchain thus are not recorded.
By doing this we have reduced the burden of the main blockchain and thus it reduced the congestion and increased the scalability of the blockchain.
Only on-chain transactions can be seen using blockchain explorer and thus these two transactions can't be explored using the block explorer.
Swap function:
As we discussed that when we want to record transactions on-chain then we have to close our channel as the last transaction is to be validated and recorded by the main blockchain.
But the closing channel is not a good idea when you want to record the transaction but don't want to close the channel.
In this scenario, the swapping feature helps us. The swapping feature allows sending funds to the main blockchain from the Lightning network and vice versa and in this process Channel is not needed to be closed. On the other hand, it also increases the sending and receiving capacity of the channel.
When on-chain funds are swapped with Lightning funds, it increases the sending capacity and when Lightning funds are swapped with on-chain funds, it increases the receiving capacity.
swapping is done
Swapping your Lightning fund for on-chain funds will increase your receiving capacity. In this example, I will show you how you can increase your receiving capacity by doing this swapping.
Here you can see before swapping my receiving capacity and sending capacity are 0.02481 and 1.893 respectively. Now:-
Go to channel option and click on swap
Nowhere enter the amount you want to swap for on-chain funds. Here for example I am entering 0.1 mBTC and I am receiving 0.08 mBTC on-chain. and click on ok to confirm swapping
After some time after this transaction is validated, you can see my receiving capacity has increased to around 0.12mBTC
Here you can see my swapping history by clicking on history and here all records are available.
as swapping is done on-chain as you are sending off-chain funds to on-chain, you can trace it using a block explorer. Here is the screenshot of the block explorer.
Similarly you can increase your sending capacity by swapping yours on-chain funds for off chain fund. The process is similar. Just enter the amount and click on swap and the process will be completed.
How to close channel?
To close a channel
Right-click on the channel you want to close. And click on close
Now it will ask for your confirmation to close the channel. Click ok on this
Now a pop-up will show us that our channel has closed but in the background, you can see the status as shut down this means that process is not completed yet.
After some time you can see the status of the channel is now changed to closed thus channel is now closed
Q 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)
To back up your Lightning channel,
Go to your channel by clicking on the channel section. And click on export backup
Now a QR code will be shown. Click on save to save that QR code
Now select the destination and name for that QR code to save on your computer. And your backup will be saved to your computer.
Now the question arises why we need back up of our channel. Back up is used to close our channel in case we lost our password and the fund is stuck in the channel. Then by using backup we can close our channel and all the fund will be transferred to our on-chain address.
Funding transaction :
Funding transaction is the first stage when funds are added to the channel when it was being created. In funding transactions, the node on which I created a channel will broadcast the transaction to the main blockchain and when the transaction is validated by the main blockchain then my channel was ready to use.
And as the transaction was recorded on the main blockchain thus we can see this using block explorer.
As adding funds was my first transaction thus to see this I clicked on the first transaction. And at the same time, we can see this transaction on block explorer with the copied transaction ID from the history.
Commitment Transaction
Commitment transactions are the ones that take place in the Lightning network that means off-chain.
In this nodes are connected to make transactions and nodes themselves validate the transactions and no data is broadcasted to the main blockchain.
Now nodes may be connected directly or indirectly. If nodes are connected directly then no transaction fees will be deducted but if nodes are connected indirectly through an intermediary node In that case transaction fees are levied to award the intermediate node.
And as these take place off-chain, you can see these transactions using a block explorer.
To see the payment and fees, you can see this on the history section by clicking on the payment
Payment 1.
Payment 2.:-
Settlement transaction:
Settlement transactions are the one which occurs when we close the channel and remaining fund is transferred to on-chain address. As the funds are being transferred to a chain address, it is broadcasted and recorded on the main blockchain and thus can be seen using a block explorer.
To see the details about settlement transaction:-
First, we need to close the channel which we have illustrated in the previous question. I don't think it should be repeated here.
After closing the channel as shown, Go to history and click on channel closed transaction and right-click on that and copy the transaction ID
And now this transaction ID can be used to see the transaction on block explorer. Just copy thus transaction I'd on block explorer.
Conclusion
Now a days we can see that the role of crypto currency is increasing and many people are accepting these for payments and a number of people are using this for making payments. But as the number of transactions are increasing, the scalability of the blockchain especially the bitcoin blockchain is low as compared to the conventional payment system.
Now payment system VISA can handle more than 65000 transactions in a second but at the same time bitcoin can only about 7.
Also we can't use bitcoin for micro payment as for paying at a coffee shop or at a store as when we compare fees levied with the payment, it's quite high.
By using the Lightning network we can resolve both of these problems. As in Lightning network the transaction completes fast and the fees charged is nill when nodes are connected directly and quite low when these are connected indirectly.
And also this reduces the congestion on Main blockchain as it doesn't require validation from blockchain and thus Increases the scalability of the main blockchain.
But at the same time Lightning network also have some challenges as it requires to be in sync with main blockchain all the time, thus it requires bandwidth and memory to do this. As the network grows the required bandwidth and memory increases. And this is a challenge before Lightning network
That's all from my side professor. Thank you for such a good homework full of knowledge.
Cc-
@sapwood
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