LIGHTNING NETWORK, NODES AND CHANNELS
LIGHTNING NETWORK
Lightning network is a layer 2 off-chain solution built on the bitcoin network. Imagine the bitcoin network of interconnected nodes as the primary layer, then lightning network will be another network that allows different parties to carry out transactions that would have been done on the bitcoin network.
The transactions carried out on the lightning network are not only cheaper and faster, they are also all broadcasted to the bitcoin network at some point. This implies that the lightning network, though a different network, still connects to the bitcoin network. It is actually built on the bitcoin network as an off-chain second layer solution.
LIGHTNING NETWORK CHANNEL
Lightning network nodes are the softwares that connect together to make up the lightning network. They are the computing components of the network that facilitate the sending and receiving of bitcoins on the lightning network. It is imperative to say that they are the core components and the building blocks of the lightning network.
LIGHTNING NETWORK CHANNEL
A Lightning network channel is a transaction apparatus used to facilitate transactions between parties on the lightning network. It helps to consider the transacting parties to be situated at both ends with the channel in between.
The channel is open when a party locks in a unit of the asset (BTC) in an on-chain transaction, through lightning network. This becomes the channels opening balance. The user who locked in the funds can go ahead to spend the funds until it's exhausted.

It is possible for a node to want to transact with another node they are not channelled to through another node. E.g Node A wants to transact with Node C through Node B but is not channelled to Node C.
The criteria for this possibility is that Node A must have a channel with Node B and Node B must have a channel with Node C. Node B will usually request a fee from Node A in such cases.
HOW LIGHTNING NETWORK ACHIEVES NEAR INSTANT SETTLEMENT AND NEAR INFINITE SCALABILITY
On lightning network, only the opening and closing balances of a channel are recorded on the bitcoin blockchain. Every other transaction on the channel is validated by the node immediate to the peer involved in the transaction unlike bitcoin blockchain transactions that must be verified by every node on the bitcoin network.
This enables the lightning network to carry out transactions faster and achieve near instant settlement. Near infinite scalability is also achieved as more and more nodes can join the network, making validation for more peers possible.
USES OF LIGHTNING NETWORK
LOW TRANSACTION FEES
On lightning network, the miner fee is non-existent. Thus, lightning network is used for bitcoin transactions where low transaction fees are desired
FAST TRANSACTIONS
Lightning network also facilitate fast transactions as just one node is enough to validate a transaction. Thus it can be used for bitcoin transactions where speed is required.
PRIVACY
On the lightning network, nodes don't whisper validated transactions to other nodes. This is enhanced and thus makes the network useful when enhanced privacy is desired.
SCALABILITY
The lightning network is used to achieve scalability on the bitcoin network as it facilitates fast and cheaper transactions and enables the bitcoin network to process more payments.
HOW LIGHTNING NETWORK FACILITATES TRANSACTIONS BETWEEN PEERS
Lightning network facilitates transactions between peers via channels. Each channel is exactly a 2of2 multi-sig wallet and each transaction is validated by just one node on the lightning network.
MAKING A TRANSACTION WITHOUT FEES
A transaction is usually carried out when a channel is opened between two transacting peers. Any of the peer can open the channel by locking-in a certain amount on-chain (ie. on the bitcoin chain).
When this locking-in of the asset on-chain is done, the peer who locked-in the asset can spend it with the other peer or a merchant or an eCommerce store.
The transactions carried out is done with little or no transaction fee. The transaction could also be single, multiple or recurring transactions.
On the lightning network, only the initial locking in of the asset which opens the channel and the final balance of the channel is record on-chain (i.e on the bitcoin blockchain).
The first transaction that binds or locks in a capital on-chain is known as a Funding transaction while every transaction that takes place on the chain after the funding transaction is referred to as Commitment transaction. Usually, the channel is closed with the last commitment transaction which will also reflect the channel's final balance. This last commitment transaction is referred to as the Settlement transaction.
It is only the funding transaction and the last commitment transaction that is recorded on-chain, the rest of the commitment transactions are recorded and carried out off-chain, on the lightning network.
HOW HTLC MAKES ROUTING PAYMENTS THROUGH INTERMEDIATE NODES TRUSTLESS
Lightning network permits transactions between peers through channels. In a case where a peer needs to transact with another peer they are not channelled to, such peer will need an intermediary node.
When using an intermediary node to transact with a distant peer, the transaction is kept trustless through Two mechanisms
Hash lock
Time lock
These two combine to form HTLC (Hashed Timelocked Contract).
When a peer (let's say peer A) is sending a payment to a distant peer (let's say peer C) through an intermediate node (let's say node B), the funds committed to the transaction by peer A is secured by a hash lock and a time lock.
First, Peer A creates a secret and hashes it. Next, a time lock and the hash value generated is then included in the transaction's HTLC script. Only the peer who knows the secret value can redeem the payment and the redemption must be done within the set time lock.
Failure of the receiving peer to redeem the payment within the preset time will invalidate the payment. Also, the sender of the payment cannot retract it until the time lock is over. The sender can however redeem the payment after the time lock.
Thus, the HTLC (Hashed Timelocked Contract) ensure trustless transfer of funds between peers through an intermediate node.
REAL TRANSACTION USING LIGHTNING CHANNEL ON MOBILE
I would have love to use my electrum wallet for this demonstration but..

Lightning can't be used on my mobile version of electrum. I could fix this but I decided to go for another option. I'm choosing phoenix because it runs natively on lightning network.

Once an account is opened on phoenix, there's no lightning network channel opened until you receive BTC in your wallet.
Once you receive BTC in your wallet, a lightning network channel automatically opens. E.g I received 0.00023659 BTC (23,659 sat) and I now have a channel opened with my capacity being 0.00049145 BTC (49,145 sat)


ECOMMERCE TRANSACTION
I decided to top up my airtime using bitrefill

I simply enter the amount I want and proceed to checkout


I had to enter an email so as to be updated on the status of the transaction. Then I selected 'Lightning' as my preferred payment option


I click on 'pay in wallet' and them I'm automatically redirected to my phoenix wallet from my browser


From my wallet I completed the payment.


A short while later, the airtime arrived my mobile number

The fee charged for this bitcoin transaction was 11 sat

Now one significant part of this demonstration was that I went to check a block explorer with the hash of the transaction and with my wallet address but found nothing


Next I closed the channel




Closing the channel incurred fees and it is recorded on-chain. As can be seen below. The fee incurred was 0.00000343BTC.


However, take a closer look at the transaction details, you'll discover that more than my final balance of 0.00010223 BTC was sent.
Now, checking with my binance wallet which the receiving address (1N8pTrDKYbzrGRv7R3ZPjEnrZ6UYq6zGPk) belong to, I received the exact 0.00010223.



This implies that, though the settlement transaction incurred a fee as it was done on-chain, I was excluded from paying the fee.
Note: The transaction where I bought airtime from bitrefill was not found on blockchain explorer. This is because the transaction was carried out of chain, on the lightning network.
SWAP FUNCTION
Swapping on Lightning network is the act of converting on-chain funds to Lightning funds and converting Lightning funds to on-chain funds. This is necessary because in-bound capacity during transactions can get bottlenecked for the merchant.
Also, when opening a channel especially for the first time, conversion from on-chain funds to Lightning funds is required and this is done by swapping.
When on-chain funds are converted to Lightning funds for use on the Lightning network, the sending capacity is improved. This means the user can send more funds or spend more at the ecommerce online store.
When Lightning funds are converted to on-chain funds, possibly to pay provider fees or foot some bills, the receiving capacity is improved. This means that when the user can receive more funds as payments or otherwise.
On phoenix wallet, the swap function is automatated.

HOW SWAP FUNCTION IMPROVES CAPACITY
Converting on-chain funds to lightning funds provides funds to be used on the lightning network, thereby improving the sending capacity.
Converting lightning funds to on-chain funds reduces the in-bound capacity bottleneck thereby improving the receiving capacity.
BACKING UP A WALLET FILE (CHANNEL BACKUP)
Channel backup on phoenix wallet is done automatically. The French company ACINQ who owns the wallet is always the funding peer in every phoenix transaction. They are a node on the lightning network and are always available as they always have assets at stake on the network. Whenever a user needs to restore their channel, all they need is to restore their wallet with their seed phrase and everything returns.

Usually, Lightning channel backup is needed because if the wallet or the device in which the wallet is is lost, the channel opened and the funds in it will be lost.
Lightning channel backup is done when the channel is still on. For phoenix however, backup is done automatically and all the user needs to do is to restore their wallet.
FUNDING TRANSACTION, COMMITMENT TRANSACTION AND SETTLEMENT TRANSACTION
Below, is a screenshot of the funding, commitment and settlement transactions.
Funding transaction is pointed with a green arrow
Commitment transaction is pointed with a yellow arrow
Settlement transaction is pointed with a blue arrow

FUNDING TRANSACTION IN PHOENIX AND IN BLOCKCHAIN EXPLORER


COMMITMENT TRANSACTION IN PHOENIX AND IN BLOCKCHAIN EXPLORER



SETTLEMENT TRANSACTION IN PHOENIX AND IN BLOCKCHAIN EXPLORER



CONCLUSION
It's been a very interesting course beginning from the lecture on multi-sig wallet and now this - lightning network. I've learnt about phoenix wallet, a super easy non-custodial wallet that automates all the processes on the lightning network. You just use it as a normal wallet, the swapping and backing up is being done automatically.
With the lightning work, BTC transactions are not only super fast but also super cheap. These transactions, apart from the funding and settlement transaction are not done on-chain but off-chain, hence they are not accountedin the blockchain explorer. The funding and settlement transactions however are on-chain transactions.
Thanks for reading.
Cc:
@sapwood
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit