Lightning Network — how does it work?

A snapshot of the Lightning Network

Wondering what all the buzz is about the Lightning Network? Read on to find out.

The Lightning Network (LN) is a second-layer payment network, meaning it operates on top of a blockchain. Most notably, LN is compatible with Bitcoin and Litecoin.

Currently, cryptocurrencies cannot achieve a high enough transaction throughput to handle any hypothetical scenario of mass adoption in the future, without sacrificing decentralization. There have been improvements made to reduce the size of individual transactions and many more are in the works. Some cryptocurrencies have increased their block size but this is considered a short-sighted decision, as blocks would have to be thousands of megabytes in size to scale up to the levels of a typical credit card processor.

LN intends to provide improved micropayment functionality, allowing a cryptocurrency to scale while keeping the base blockchain sufficiently decentralized.

Payment channels

LN uses a network of payment channels powered by smart contracts.

Two parties can initiate a payment channel by creating a multi-signature transaction on the blockchain. At least one party must commit funds to this transaction. Once this channel is open and a payment channel is established on the LN, the participants can transfer funds between each other, instantly and with extremely low fees. Each transaction updates a “temporary” balance sheet, signed by each party’s private key. This balance sheet can be broadcast as a Bitcoin transaction at any time if either or both parties wants to close the channel. LN payments are done peer-to-peer, with no intermediary.

For example, let’s say Alice sets up a payment channel with Bob. The capacity of this channel is 1 BTC, with Alice and Bob each committing half of that capacity. They can then transact as many times as they want over the LN. If Alice sends 0.05 BTC to Bob, the “temporary” balance sheet is signed by each person and now states that 0.45 BTC belongs to Alice and 0.55 belongs to Bob (minus any small amount of fees). If Bob sends 0.03 to Alice, the balance sheet now reads 0.48 BTC for Alice and 0.52 BTC for Bob. They can do this for a theoretical unlimited amount of times. LN payments are instant (limited only by the speed of the internet) and with sub-satoshi fees being possible.

When either or both party decides to close the payment channel, the most recent balance sheet is published to the public Bitcoin blockchain, with the updated values going to their respective owners. Cryptography ensures that neither party can steal from the other.

Fraudulent transactions

In the event of one party trying to be dishonest, there is recourse for the other party. If Alice tries to publish any balance sheet other than the most recent one (e.g., one in which she owns more BTC than in the most recent balance sheet), her funds will be locked while Bob will be given the opportunity to publish a “penalty” transaction that will send the entire capacity of the payment channel to himself. This serves as a deterrent to cheating.

To do this, Bob would have to be online. In case he cannot access the internet, there are “watchtowers”, which are LN nodes that look out for dishonest transactions being broadcast. If the watchtowers detect that Alice tried to publish a dishonest transaction, they can sign the penalty transaction for Bob and be paid a small proportion of the penalty transaction as a reward for keeping users honest. Bob himself could run a watchtower on a separate machine that is always connected to the internet.

Payment routing

So far, we’ve seen what can be done with a payment channel between two parties. But we can’t expect someone to open up a channel with every single party they want to interact with. Since LN is a network of payment channels (where each user is called a node), every node is connected to another node and payments can be forwarded to another node (called a “hop”) until the payment gets to its intended recipient. In this way, someone can send funds to another user without having a direct payment channel with them.

Let’s say Alice and Bob have a payment channel with each other, Bob has a payment channel with Carol, and Carol and Dave have a payment channel with each other, with each participant contributing 1 BTC.

Bob wants to pay Dave but does not want to go through the hassle of opening a payment channel with him. It would be extremely impractical if Bob had to establish a direct payment channel with everyone he wants to transact with. Instead, he can use his indirect connection with Dave (through Carol) and “route” his payment to Dave.

Bob’s LN wallet will find an optimal route to Dave, which is dependent on a few things:

each node on the route having sufficient funds,

the fee that each node charges to “forward” the payment,

and the number of hops it takes to reach Dave.

Each payment channel along the route is updated to reflect the payment. If Bob wanted to send 0.1 BTC to Dave, the network would now look like this:

Note the changes in the balance of each channel. If the payment channels were to close, Bob would receive 0.9 BTC from closing his channel with Carol.

Carol would receive 1.1 BTC from this channel-closing transaction. This is because she took the 0.1 BTC that Bob sent her and gave it to Dave, by giving 0.1 BTC from her payment channel with Dave. So Carol has not lost any money; she would receive 1.1 BTC from closing her channel with Bob and 0.9 BTC from closing her channel with Dave. The smart contract-powered payment channel ensures that Carol can only receive the 0.1 BTC from Bob if she has already forwarded the 0.1 BTC to Dave.

Dave would receive 1.1 BTC from closing his channel with Carol because he was paid by Bob.

The balances of all the payment channels along the route were simply updated to reflect Bob’s payment.

LN payments either go to completion or fail, meaning Bob’s payment either reaches Dave or it doesn’t. Bob’s payment cannot get stuck or stolen along the way. Even though Carol acted as an intermediary, Bob does not have to trust her. He knows that if she is a part of the LN, she must be an honest user and he can be assured that his funds will get to Dave or be returned to him.

Other info

LN is essentially a network of user-established payment channels. Participants can effectively transact using Bitcoin, with minimal fees and instant confirmations and without counterparty risk. In theory, with enough users and assuming each user has several payment channels open, almost every user in the LN can be indirectly connected with each other. LN also brings improved privacy to BTC, as LN transactions are done off-chain and are not recorded onto the Bitcoin blockchain until a payment channel is closed.

Like the Bitcoin network, LN is decentralized and has no single point of attack or failure, payments cannot be censored and are done entirely peer-to-peer.

Each party of a payment channel is said to be a “node” of the LN. Nodes are usually compared to miners on the Bitcoin network. They help process transactions but do not have any control over the network or funds of any user. Nodes can even get paid a very small fee for every payment they forward successfully.

If a party in a payment channel ever goes offline, the payment channel can be unilaterally closed, ensuring that users can always access their funds.

Besides micropayments, other functionality is also made possible using LN. For example, LN can be used to directly transfer Bitcoin for Litecoin, in a process called an “atomic swap”. Alice and Bob can create a LN smart contract where Alice will send Bob 5 BTC when Bob sends her 500 LTC. Assets stored on different blockchains can be transferred without using an intermediary such as an exchange.

Conclusion

Despite the promising potential of LN, it is still in the early days of development and remains difficult to use. In particular, a lot of work is being done to improve routing performance and user experience. Many people believe a second layer network is crucial in scaling Bitcoin while keeping the base protocol decentralized and secure. In reality, the solution will most likely be a combination of protocol improvements such as Segwit, second layers such as LN and further increasing the block size. LN is likely to not go away anytime soon and if it succeeds as intended, it could fundamentally change the cryptocurrency landscape.

Interested in running your own LN node? Check out this easy guide on how to set up a Lightning Network node on Windows: https://medium.com/coinmonks/guide-setup-a-lightning-network-node-on-windows-8475206807f