What is the lighting network, the lighting network, or LN for short is a layer 2 scaling solution which can lower the cost of bitcoin transactions to less than a cent while at the same time increase, the transaction volume.
When you want to connect to the LN you must open up a payment channel between you and another node connected to the network known as your channel partner. This connection is done using an HTLC or a Hashed Time-Locked Contract, and a Multisignature script which is implemented using bitcoin script. Bitcoin script is a simple stack-based language that consists of two stacks, the main stack and the alt stack, which takes in instructions that are pushed on the main stack and are executed in chronological order FIFO (first in first out) this language also consist of no loops.
When you open up a payment channel you must connect to a lighting node, you can do this by either running your own node or connecting to a third-party node, next you must deposit funds you'd like to use on the LN. The initial deposit and final withdraw to and from your lighting node is the only deposit that will be recorded on the bitcoin blockchain and will be the most expensive transaction incurred, the rest of the transactions you make on the LN will be recorded in an offline private and trustless matter. Your connected node will lock your bitcoin in a multi-signature and HTLC script, you and your channel peer will share keys controlling the funds, and the HTLC will lock up your bitcoin for around 2,016 blocks which is approximately two weeks. This time can be renegotiated with your channel partner for either longer or shorter. A block is a group of transactions that are validated and confirmed on the bitcoin network.
Nodes connect to a randomly selected group of nodes through a peer-to-peer gossip protocol, this is typically done over TCP/IP. Once your node is connected to the other nodes you can decide to open up a payment channel via channel announcement, with one of the connected nodes known as your peer. This payment channel can be validated on the bitcoin network, payments can be routed through nodes on the network and you can collect small transaction fees for helping route transactions. Public channels are used to create a web of connected nodes that help route transactions. If you intend to use the LN often it's best to avoid closing your channel as you will run into higher transaction fees since it gets recorded on the bitcoin blockchain.
Most payments on the lighting channel start as an invoice, which is generated by the recipient. An invoice consists of an optional payment hash, recipient, amount, and text description. For payments to route successfully throughout the LN, the payment travels across multiple channels in an atomic way, meaning in a trustless way, it will either complete successfully or fail. First, the merchant will provide a QR code encoding a bech-32 address, (This address format is also used for native segwit versions 0 P2WPKH - Pay 2 Witness Public Key Hash and P2WSH - Pay 2 Witness Script Hash). Note that in the bitcoin network the recipient passes an address to the sender whereas in the lighting network the sender pays the invoice which is routed through the network in other words Bitcoin creates transactions whereas Lighting sends transactions.
When routing payments through the LN it's important to keep in mind that nodes can only forward as much BTC as they own and only you and your channel partner keep track of your transactions by calculating the payment hash of a preimage that is signed by your channel partner and you. If this calculation is done successfully it is known as a committed transaction. Since transactions aren't monitored and stored by the whole network payments can be kept anonymous while at the same time allowing the network to scale easily. To route a payment we must consider two problems pathfinding - finding a path from the source to the destination and routing - sending funds from source to destination. There is still active research for finding an optimal pathfinding solution, There are many pathfinding strategies used on LN which different nodes implement. A very basic implementation would be to keep trying to route through nodes until a valid route can be reached but this approach is less than optimal. The next issue to consider is routing the actual payment, LN uses an onion routing protocol and blinded shnorr signatures to accomplish this.
Thinking of an onion as analogous to what onion routing is. After a path is found the sender encrypts a payment hash for every payment channel that will be used, creating layers of encryption. As the payment travels from channel peer to peer, each peer will decrypt a layer of the message and then forward the message to the appropriate peer as well as add some randomness to the message, this is meant to help obfuscate the position of the message in the path. This improves privacy as nodes only know who forwarded the previous message and where to forward that message too. The payment message is small enough to fit into a single TCP packet and even a link-layer Ethernet frame helping further improve privacy and security. Recall that in the pathfinding strategy we were unable to derive the balance of the other nodes in our payment channel path. Since nodes must have a sufficient balance to help route payments, this means while our payment is being routed it is possible that one of the nodes has an insufficient balance to support the transaction or one of the nodes might go offline or occur some other error. In this case, an error will be sent back from the erroring node which will be routed back in the reverse direction until it reaches the sender. The error message would be indistinguishable from routing nodes to the external observer and intermediary nodes. This trial-and-error "probing" method is used to help route a payment successfully. This framework for decrypting and encrypting payment messages can be more formally known as Noise Protocol Framework which offers authentication encryption, forward secrets, and identity privacy.
Reference
Mastering the Lighting Network by Andreas M. Antonopolos, Olaouwa Osuntokun, and Rene Pickhardt (O'Reilly). Copyright 2022 aantonop Books LLC, Rene Pickhardt, and uuddlrlrhas LLC, ISBN 978-1-492-05486-3