Off Chain Explained

State channels

The solution for sending payments outside the main blockchain

State channels are a solution that allows cryptocurrency users to conduct transactions outside the blockchain, thereby bypassing the need for miner confirmations and avoiding the associated time delays and costs. These channels are typically established between two transacting parties, enabling them to execute numerous transactions without involving the blockchain for each one. The blockchain is only used to record the initial opening of the channel and the final settlement. Throughout the life of the state channel, the state of transactions is recorded locally by the two participants, who are the only ones with access to the funds within the channel. 

stateChannel_1.svg

Opening a Channel
The process begins with the opening of a state channel. This is done by locking a certain amount of cryptocurrency into a multi-signature wallet, effectively creating a state channel between the two participants. The initial state of the channel, including the amount of cryptocurrency locked, is recorded on the blockchain. Importantly, only the participants of the state channel have access to the funds in the multi-signature wallet, and both must agree before any funds can be withdrawn, ensuring that neither party can deceive the other.

Off-Chain Transactions
Once the state channel is established, the two participants can conduct multiple transactions inside of the channel. These transactions are signed by both parties and are executed instantly, without the need for blockchain confirmations, thereby avoiding network fees and delays associated with on-chain transactions.

With each transaction, the state of the channel is updated to reflect the new distribution of the channel's balance. This updated state, which indicates each participant's entitlement to a specific portion of the locked funds in the multi-signature wallet, is shared between the two participants. By continuously updating and sharing this state, both parties ensure that the latest agreed-upon balance can be settled on the blockchain whenever they decide to close the channel.

Closing the Channel
A state channel can be closed at any time when both participants agree to it. To close the channel, the latest state is submitted to the blockchain, which then executes the final distribution of the locked funds according to the agreed-upon state. This is the only time the blockchain is involved after the channel is initially opened.

Understanding channel liquidity

Channel liquidity refers to the availability of funds within a state channel that can be either sent or received by the participants. It is a crucial concept because it determines the flow of transactions between the parties involved in the channel. Channel liquidity is split into two key types: inbound liquidity and outbound liquidity, which together determine the channel's overall capacity.

stateChannel_2.svg

Inbound liquidity refers to the amount of cryptocurrency that one participant can receive from the other within the channel. Conversely, outbound liquidity refers to the amount of cryptocurrency that a participant can send to the other party. Notably, one participant's outbound liquidity is identical to the other participant's inbound liquidity, as the total funds available for sending by one party are exactly what the other party can receive.

Inbound liquidity and outbound liquidity are sometimes referred to as "receiving capacity" and "spending balance," or simply as "can receive" and "can send."

A practical example

To better understand how channel liquidity works in practice, let’s walk through a simple example involving two participants, Alice and Bob.

Bob decides to open a state channel with Alice and deposits 8,000 sats. At the start, Bob has 8,000 sats of outbound liquidity, meaning he can send up to 8,000 sats to Alice. Conversely, Alice has 8,000 sats of inbound liquidity, meaning she can receive up to 8,000 sats from Bob. Since Alice didn't deposit any sats into the channel, her outbound liquidity is 0 sats, which means she cannot send any sats to Bob, and Bob cannot receive any sats from her.

stateChannel_3.svg

Bob then sends Alice 5,000 sats, reducing his balance and outbound liquidity to 3,000 sats, while Alice's outbound liquidity increases to 5,000 sats, the amount she just received. This transaction also increases Bob's inbound liquidity from 0 sats to 5,000 sats, while Alice's inbound liquidity decreases to 3,000 sats.

stateChannel_4.svg

Alice, being generous, sends 1,000 sats back to Bob. After this transaction, both Alice and Bob have equal balances of 4,000 sats each. At this point, each of them has 4,000 sats of outbound and inbound liquidity, meaning they can each send or receive up to 4,000 sats.

Finally, Alice and Bob agree to close the channel. The final state is broadcast to the blockchain, which distributes 4,000 sats to each of them accordingly.

stateChannel_5.svg

Payment routing

State channels, while offering a scalable and efficient solution for conducting off-chain transactions, come with the inherent limitation of only allowing transactions between the two parties directly involved in the channel. This means that without additional mechanisms, users can only send transactions to their state channel counterpart, restricting the network's overall flexibility and reach. To overcome this limitation and enable broader connectivity, off-chain networks typically employ mechanisms for payment routing between the network’s users. 

Payment routing involves using intermediary nodes to facilitate transactions between users who are not directly connected by a state channel. Intermediary nodes act as routers, creating a path from the sender to the receiver through a series of interconnected state channels. This path allows any participant in the network to reach any other participant, regardless of whether they share a direct payment channel. 

When a user wants to send a payment to another user outside their direct channel, the network identifies a route through intermediary nodes. Each node along this route temporarily locks the necessary amount of funds, forwarding the payment to the next node until it reaches the intended recipient. This process is secured using cryptographic techniques such as hashed time-locked contracts (HTLCs), which ensure that either the payment is completed within a specified timeframe or the funds are returned to their original owners. This mechanism prevents issues such as stuck or misappropriated funds during the routing process.

To illustrate payment routing, consider the scenario in which Alice wants to send a payment to Charlie, but they do not have a direct state channel between them. However, both Alice and Charlie have open state channels with Bob, who can act as an intermediary node and route the payment between them. 

At this initial state, Alice has an outbound liquidity of 2,000 sats, meaning she can send up to 2,000 sats to Bob. Bob’s channel with Alice shows no outbound liquidity, but he has 4,000 sats in outbound liquidity in his channel with Charlie, meaning he can send up to 4,000 sats to Charlie. Bob also has an equal amount of inbound liquidity with Charlie, indicating he can receive up to 4,000 sats from him.

paymentRouting_1.svg

Alice decides to send 2,000 sats to Charlie using Bob as the intermediary. Alice initiates the transaction by sending 2,000 sats to Bob. Simultaneously, Bob forwards 2,000 sats to Charlie. As a result, Bob’s outbound balance with Alice increases by 2,000 sats, while his outbound balance with Charlie decreases by the same amount. Although Bob facilitates the payment, Bob’s total outbound liquidity remains unchanged; he merely reallocates tokens between his channels with Alice and Charlie.

paymentRouting_2.svg

State channel security

Off-chain transactions, facilitated through state channels, offer significant benefits by increasing transaction speed and reducing costs, as they avoid the need for every transaction to be recorded on the blockchain. However, this approach also requires careful consideration of security, as it involves a different set of risks compared to on-chain transactions. With no blockchain confirming every transaction, users need to be aware of the importance of monitoring their channels to ensure honesty and transparency from their counterparties, especially if the counterparty is unknown to the user.

A key security concern in off-chain transactions is the risk of a counterparty attempting to close the state channel with an outdated or incorrect state. For example, a party might try to broadcast an older channel state to the blockchain that shows a more favorable balance for them. This type of dishonest behavior can lead to disputes and financial loss for the honest part if not properly managed. While vigilance on the part of state channel users generally reduces the risk of encountering dishonest behavior, several mechanisms exist to further address potential security concerns and enhance the safety of off-chain transactions, such as penalty mechanisms and watchtowers.

If one party attempts to cheat by closing a channel with an outdated state, the penalty mechanism allows the honest party to present the most recent channel state, exposing the fraudulent attempt. Upon verification by the blockchain, the dishonest party can be penalized by transferring their balance to the honest party, which serves as a strong deterrent against cheating. However, for this mechanism to be effective, the honest party must be vigilant. They need to be online regularly and monitor every transaction in the blockchain to detect any fraudulent behavior before the arbitration period expires. 

To alleviate the burden of constant monitoring of state channels, especially for users who may not always be able to oversee their channels, "watchtowers" have been developed as a practical solution. Watchtowers are third-party services that observe the blockchain for any channel closure attempts. If they detect an attempt to close a channel with an outdated state, they can automatically broadcast the correct state to the blockchain, thus protecting the user's interests even if they are offline. This service adds an extra layer of security, helping to ensure that users' funds are safeguarded even in their absence.

Another risk when using state channels is the risk of one party becoming unresponsive, for instance, by going offline for a longer period of time. Since closing a channel or withdrawing funds typically requires consensus from both parties involved, complications can occur if one party goes offline or refuses to cooperate, potentially leading to funds being locked in the channel. To mitigate these risks and ensure users can always retrieve their funds, state channels incorporate dispute mechanisms designed to handle such scenarios. 

A common dispute mechanism is the "force-close" procedure, which allows either party to unilaterally initiate the closure of a state channel. This process involves broadcasting the most recent state of the channel to the blockchain, thereby triggering a dispute period. During the dispute period, the other party has the opportunity to present a more recent state if the initially broadcasted state was outdated or incorrect. The dispute period is essential for preventing a malicious closure of a channel using outdated information that may unfairly benefit one party. Once the dispute period concludes, the most recent validated state is finalized, and the funds are released and can be withdrawn by the respective owners.


Copyright © 2024