Updating the balance of a Payment Channel
Tags: commitmenttransactio, lightningnetwork, paymentchannels, bitcoin
Published: 2021-03-10 13:35:50

---
title: Updating the balance of a Payment Channel
published: true
date: 2021-03-09 13:54:12 UTC
tags: commitmenttransactio,lightningnetwork,paymentchannels,bitcoin
canonical_url: https://medium.com/coinmonks/updating-the-balance-of-a-payment-channel-944a713a8408
---

In this article we will look in detail at how the balance of a Lighting Network channel gets updated without any of the participants risking their funds.

Lightning Network is an adversarial network, participants do not trust the other side. In fact they _expect_ that the “others” will try to cheat them.   
A participant signs **only** what it knows to be safe. At **any moment** it must have the option to take out the money and leave the agreement (close the channel).

For this article we assume that the **payment channel has already been open** and it has a balance of 0.10 BTC for Alice and 0.0 BTC for Bob.

### Commitment Transactions

After the payment channel has been open, Alice wants to pay Bob 0.02 BTC. The payments between the two parties are executed using a special type of transaction called a **Commitment Transaction**.  
These transactions are special because they:

- can be revoked (canceled) if both parties agree to do so
- always spend from the 2-of-2 multisig Output of the Funding Transaction

The Commitment Transactions are intended to be used inside the payment channel, without hitting the main-chain. However, they are fully valid Bitcoin transactions and can be broadcast to the main-chain if one of the parties decides to do so.

![](https://cdn-images-1.medium.com/max/483/1*ANBUn70eAzsTHIjGsV00ow.gif)<figcaption>Multiple Commitment Transactions inside a Payment Channel</figcaption>

In order to pay Bob, Alice must create two transactions, **both of which** will pay 0.02 BTC to Bob, but the outputs have different conditions for spending.

We will refer to these transactions as the **Pay-Me-First** transaction and the **Pay-Other-First** transaction. Remember that **both transactions are created by Alice** without any direct input from Bob. Alice got all the information she needed when Bob sent the accept\_channel message.

#### Pay-Me-First Transaction

In the Pay-Me-First transaction Alice can spend her output immediately, while Bob has to comply with the [**RSM Contrac**](https://motorina0.medium.com/rsm-contracts-f690cfdf04e8) **t**. A Revocable Sequence Maturity Contract puts several restrictions on spending the Output:

- the beneficiary of the funds can only spend them after a “maturation” period has passed (is delayed)
- the contract can be revoked (by the beneficiary)
- if the contract was revoked and the beneficiary still tries to spend the funds then it risks penalty (loosing all the funds)

More details about Revocable Sequence Maturity Contracts [here](https://motorina0.medium.com/rsm-contracts-f690cfdf04e8).

This transaction is more advantageous for Alice. You can view a sample of this transaction [here](https://bitcoincore.tech/apps/bitcoinjs-ui/index.html?&tx_note=Sample%20of%20a%20Commitment%20Transaction%20used%20in%20Lightning%20Network.&tx_input_0_note=Spend%20from%20the%20Funding%20Transaction&tx_input_0_script_note=This%20script%20unlocks%20a%20P2WSH.%20Click%20the%20%27Edit%27%20button%20and%20manually%20set%20the%20type%20to%20P2WSH%20in%20order%20to%20view%20the%202-of-2%20multisig%20script.&tx_output_0_note=%5BTo%20Local%5D%20Output%20sends%20funds%20back%20to%20the%20owner%20(Alice)%20of%20this%20commitment%20transaction%20and%20thus%20must%20be%20timelocked%20using%20%60OP_CHECKSEQUENCEVERIFY%60.%20It%20can%20be%20claimed,%20without%20delay,%20by%20the%20other%20party%20if%20they%20know%20the%20revocation%20private%20key.&tx_output_0_script_note=This%20is%20the%20%22naked%22%20unwrapped%20Revocable%20Sequence%20Maturity%20Contract%20(RSMC).%20In%20order%20to%20see%20the%20final%20form,%20click%20the%20%27Edit%27%20button%20and%20%27Wrap%20in%20P2WSH%27.&tx_output_1_note=%5BTo%20Remote%5D%20Output%20sends%20funds%20to%20the%20other%20party.%20If%20%60option_anchor_outputs%60%20applies%20to%20the%20commitment%20transaction,%20the%20%60to_remote%60%20output%20is%20encumbered%20by%20a%20one%20block%20csv%20lock.%20Otherwise,%20this%20output%20is%20a%20simple%20P2WPKH%20to%20remotepubkey.&tx_output_1_script_note=This%20is%20the%20%22naked%22%20unwrapped%20Contract%20that%20pays%20Bob%20with%20no%20restrictions.%20In%20order%20to%20see%20the%20final%20form,%20click%20the%20%27Edit%27%20button%20and%20%27Wrap%20in%20P2WSH%27.&tx_output_2_note=%5BLocal%20Anchor%5D%20Output%20can%20be%20spent%20by%20the%20local%20node%20to%20provide%20incentive%20to%20mine%20the%20transaction,%20using%20child-pays-for-parent.&tx_output_3_note=%5BRemote%20Anchor%5D%20Output%20can%20be%20spent%20by%20the%20remote%20node%20to%20provide%20incentive%20to%20mine%20the%20transaction,%20using%20child-pays-for-parent.&tx_hex=02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a4884890000000000ffffffff04e48d6a00000000004d63210212a140cd0c6539d07cd08dfe09984dec3251ea808b892efeac3ede9402bf2b1967029000b2752103fd5960528dc152014952efdb702a88f71e3c1653b2314431701ec77e57fde83c68acc0c62d00000000002521032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991ad51b24a010000000000002200202b1b5854183c12d3316565972c4668929d314d81c5dcdbb21cb45fe8a9a8114f4a01000000000000220020e9e86e4823faa62e222ebc858a226636856158f07e69898da3b0d1af0ddb39940340ca3e2b815f7bf548fa59a79002d8093ee786b38803704130a589f749c33552145a36440a0d7d407dee948c85c2f20e4892479075cf2f166e94c2e6309907971440f89034eba16b2be0e5581f750a0a6309192b75cce0f202f0ee2b4ec0cc39485076c65dc507fe42276152b7a3d90e961e678adbe966e916ecfe85e64d430e75f3475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae00000000).

![](https://cdn-images-1.medium.com/max/722/1*O9F0Ggmmn8vsU0AkwZ2zpg.png)<figcaption>The <strong>Pay-Me-First</strong> transaction created by Alice</figcaption>

#### Pay-Other-First Transaction

In the Pay-Other-First transaction Bob can spend his output immediately, while Alice has to comply with the [**RSM Contract**](https://motorina0.medium.com/rsm-contracts-f690cfdf04e8). This transaction is more advantageous for Bob. You can view a sample of this transaction [here](https://bitcoincore.tech/apps/bitcoinjs-ui/index.html?&tx_note=Sample%20of%20a%20Commitment%20Transaction%20used%20in%20Lightning%20Network.&tx_input_0_note=Spend%20from%20the%20Funding%20Transaction&tx_input_0_script_note=This%20script%20unlocks%20a%20P2WSH.%20Click%20the%20%27Edit%27%20button%20and%20manually%20set%20the%20type%20to%20P2WSH%20in%20order%20to%20view%20the%202-of-2%20multisig%20script.&tx_output_0_note=%5BTo%20Local%5D%20Output%20sends%20funds%20back%20to%20the%20owner%20(Bob)%20of%20this%20commitment%20transaction%20and%20thus%20must%20be%20timelocked%20using%20%60OP_CHECKSEQUENCEVERIFY%60.%20It%20can%20be%20claimed,%20without%20delay,%20by%20the%20other%20party%20if%20they%20know%20the%20revocation%20private%20key.&tx_output_0_script_note=This%20is%20the%20%22naked%22%20unwrapped%20Revocable%20Sequence%20Maturity%20Contract%20(RSMC).%20In%20order%20to%20see%20the%20final%20form,%20click%20the%20%27Edit%27%20button%20and%20%27Wrap%20in%20P2WSH%27.&tx_output_1_note=%5BTo%20Remote%5D%20Output%20sends%20funds%20to%20the%20other%20party.%20If%20%60option_anchor_outputs%60%20applies%20to%20the%20commitment%20transaction,%20the%20%60to_remote%60%20output%20is%20encumbered%20by%20a%20one%20block%20csv%20lock.%20Otherwise,%20this%20output%20is%20a%20simple%20P2WPKH%20to%20remotepubkey.&tx_output_1_script_note=This%20is%20the%20%22naked%22%20unwrapped%20Contract%20that%20pays%20Bob%20with%20no%20restrictions.%20In%20order%20to%20see%20the%20final%20form,%20click%20the%20%27Edit%27%20button%20and%20%27Wrap%20in%20P2WSH%27.&tx_output_2_note=%5BLocal%20Anchor%5D%20Output%20can%20be%20spent%20by%20the%20local%20node%20to%20provide%20incentive%20to%20mine%20the%20transaction,%20using%20child-pays-for-parent.&tx_output_3_note=%5BRemote%20Anchor%5D%20Output%20can%20be%20spent%20by%20the%20remote%20node%20to%20provide%20incentive%20to%20mine%20the%20transaction,%20using%20child-pays-for-parent.&tx_hex=02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a4884890000000000ffffffff04e48d6a00000000004d632103f9b004476ef731bac15887882b99c8756d2451800674e4b9f42d2effde4bebc567029000b27521034e0ec6dc63301141448af127eb2d8eba26e8c2bb24d4bfdf8e29afa7bb02657b68acc0c62d00000000002521034f355bdcb7cc0af728ef3cceb9615d90684bb5b2ca5f859ab0f0b704075871aaad51b24a010000000000002200202b1b5854183c12d3316565972c4668929d314d81c5dcdbb21cb45fe8a9a8114f4a01000000000000220020e9e86e4823faa62e222ebc858a226636856158f07e69898da3b0d1af0ddb39940340ca3e2b815f7bf548fa59a79002d8093ee786b38803704130a589f749c33552145a36440a0d7d407dee948c85c2f20e4892479075cf2f166e94c2e6309907971440f89034eba16b2be0e5581f750a0a6309192b75cce0f202f0ee2b4ec0cc39485076c65dc507fe42276152b7a3d90e961e678adbe966e916ecfe85e64d430e75f3475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae00000000).

![The Pay-Other-First transaction created by Alice](https://cdn-images-1.medium.com/max/722/1*VCFKJzFqiUNcrC_FTV5tcQ.png)<figcaption>The <strong>Pay-Other-First</strong> transaction created by Alice</figcaption>

**Note** : Transaction outputs are [sorted in ascending order](https://github.com/bitcoin/bips/blob/master/bip-0069.mediawiki) by amount.

Alice will **only send** signatures to Bob for the Pay-Me-First transaction (me=Alice). She will **never**  **send** signatures directly to Bob for the Pay-Other-First transaction (other=Bob).

The **only time** Alice will sign the Pay-Other-First transaction is if she intends to broadcast that transaction to the Bitcoin network (without sending it to Bob).

![](https://cdn-images-1.medium.com/max/761/1*EdYugowRmYkVMoQnGsZsrw.gif)

The outputs of the Pay-Other-First transaction are called [**to\_local**](https://github.com/lightningnetwork/lightning-rfc/blob/master/03-transactions.md#to_local-output) (the one that pays Alice) and [**to\_remote**](https://github.com/lightningnetwork/lightning-rfc/blob/master/03-transactions.md#to_remote-output) (the one that pays Bob).

In a similar manner, Bob will create a Pay-Me-First transaction and a Pay-Other-First transaction.

#### Channel State Update — Step By Step

Next we will take a look in detail at what happens when Alice pays Bob 0.02 BTC using a Lightning Network payment channel.

Below are the steps taken by Alice and Bob to update the balance from Alice 0.10 BTC and Bob 0.0 BTC ( **State01** ) to Alice 0.08 BTC and Bob 0.02 BTC ( **State02** ).

The party making the payment initiates the flow, in this case Alice.

#### Alice Builds and Signs

![Alice builds and signs her Pay-Me-First transaction](https://cdn-images-1.medium.com/max/682/1*6iE3Kp-5h8bbc13ZREN9PA.png)<figcaption>Alice builds and signs her Pay-Me-First transaction</figcaption>

1. BuildPayMeFirstTx( alice) — Alice builds the transaction that pays her 0.08 BTC **with no restrictions** and 0.02 BTC to Bob **under the** [**RSM Contract**](https://motorina0.medium.com/rsm-contracts-f690cfdf04e8). We name this transaction payAliceFirstTx.
2. Sign(payAliceFirstTx) — Alice signs payAliceFirstTx transaction and creates a signature named payAliceFirstSignature.
3. Send(payAliceFirstSignature) — Alice sends the payAliceFirstSignature to Bob. There is no risk for her sending this signature. The worst case scenario is that Bob will close the channel and will broadcast payAliceFirstTx, which is fine for Alice since she can spend her output right away, while Bob has to wait.   
By broadcasting payAliceFirstTx, Bob also discloses that he received the 0.02 BTC from Alice. If instead of closing the channel Bob becomes unresponsive then Alice can safely broadcast the previous transaction where she has a larger amount ( **State01** ) since she has not revoked the current commitment transaction yet.

#### Bob checks and revokes

![](https://cdn-images-1.medium.com/max/682/1*TJ1anRzIMJ1L5AKBto2IRQ.png)

![Bob checks the signature from Alice and revokes the previous state](https://cdn-images-1.medium.com/max/682/1*_Ii2NIDDxY3CDStToHsj1w.png)<figcaption>Bob checks the signature from Alice and revokes the previous state</figcaption>

4. Receive(payAliceFirstSignature) — Bob receives the payAliceFirstSignature message. Note that Alice sends only the signature and not the full transaction. Bob can reconstruct the exact same transaction.

5. BuildPayOtherFirstTx(alice) — Bob builds the transaction that pays him 0.02 BTC under the [RSM Contract](https://motorina0.medium.com/rsm-contracts-f690cfdf04e8) and 0.08 BTC to Alice with no restrictions. We name this transaction payOtherFirstTx. It is **identical** with the payAliceFirstTx (that Alice built).

6. CheckSignature(payOtherFirstTx, payAliceFirstSignature) — Bob checks if the signature received from Alice (payAliceFirstSignature) is valid for the transaction that he just built (payAliceFirstTx). If the signature from Alice is valid then Bob has a valid new commitment transaction ( **State02** ). He can continue to use the channel or he can sign and broadcast the transaction, thus closing the channel. However, if Bob broadcasts the transaction he will be restricted in spending it by the [RSM Contract](https://motorina0.medium.com/rsm-contracts-f690cfdf04e8), while Alice will be able to spend her funds immediately.

7. Store(payAliceFirstSignature) — Bob persists the signature together with the data that allows him to reconstruct payAliceFirstTx. He might need this data if Alice becomes unresponsive.

8. BuildRevokeCommitmentMsg() — Bob does not need the older state (where he had less money) so he wants to make **State02** (payAliceFirstTx) the current state and revoke (invalidate) the old one ( **State01** ). He builds the revokeMessage message that invalidates **State01**.

9. Send(revokeMessage) — Bob sends the revokeMessage to Alice. There is no risk for him to send this message which revokes the previous state ( **State01** ). The worst case scenario is that Alice becomes unresponsive and Bob has to broadcast the payAliceFirstTx transaction.  
By revoking the previous state ( **State01** ), Bob assures Alice that it is OK for her to take the next steps.

![](https://cdn-images-1.medium.com/max/682/1*TJ1anRzIMJ1L5AKBto2IRQ.png)

![Alice receives the revocation message for State01](https://cdn-images-1.medium.com/max/682/1*ipXIw4Sa8HW4Pvz4jLEywQ.png)<figcaption>Alice receives the revocation message for State01</figcaption>

10. ReceiveRevokeMsg(revokeMessage) — Alice receives the revocation message from Bob. Alice now has the possibility to penalize Bob if he broadcasts the revoked commitment transaction ( **State01** ).  
She will not revoke her current state yet since she has not received a signature from Bob for  **State02**.

#### Bob builds and signs

![](https://cdn-images-1.medium.com/max/682/1*TJ1anRzIMJ1L5AKBto2IRQ.png)

![Bob builds and signs the Pay-Me-First transaction](https://cdn-images-1.medium.com/max/682/1*1lQ9yezVm3UE7pzcidRAdQ.png)<figcaption>Bob builds and signs the Pay-Me-First transaction</figcaption>

11. BuildPayMeFirstTx(bob) — Bob **does not simply sign** the payAliceFirstTx transaction and sends it to Alice. This transaction is **unfavorable to him** since he has to comply with the [RSM Contract](https://motorina0.medium.com/rsm-contracts-f690cfdf04e8) while Alice is free to spend her funds immediately. But most importantly, he will have **no power to penalize** Alice if she decides to broadcast the payAliceFirstTx transaction sometime in the future when this old transaction becomes favorable to her (more money).  
Bob builds a transaction that pays him **with no restrictions** , but pays Alice under the [RSM Contract](https://motorina0.medium.com/rsm-contracts-f690cfdf04e8) restrictions. This is the payBobFirstTx transaction.

12. Sign(payBobFirstTx) — Bob creates the payBobFirstSignature by signing the payBobFirstTx transaction.

13. Send(payBobFirstSignature) — Bob sends the payBobFirstSignature message to Alice.

#### Alice checks and revokes

![](https://cdn-images-1.medium.com/max/682/1*TJ1anRzIMJ1L5AKBto2IRQ.png)

![](https://cdn-images-1.medium.com/max/682/1*zeE8oYVicXRBG236gy0eFQ.png)

14. Receive(signatureBob) — Alice receives the payBobFirstSignature message. Note that Bob sends only the signature and not the full transaction. Alice can reconstruct the exact same transaction.

15. BuildPayOtherFirstTx(bob) — Alice builds the transaction that pays her 0.08 BTC under the [RSM Contract](https://motorina0.medium.com/rsm-contracts-f690cfdf04e8) and pays Bob 0.02 with no restrictions. We name this transaction payOtherFirstTx. It must be identical with the payBobFirstTx (built by Bob).

16. CheckSignature(payOtherFirstTx, signatureBob) — Alice checks if the signature received from Bob (payBobFirstSignature) is valid for the transaction that she just built (payBobFirstTx). If the signature from Bob is valid then Alice has a valid new commitment transaction ( **State02** ). She can continue to use the channel or she can sign and broadcast the transaction therefore closing the channel. If Alice chooses to broadcast this transaction, Bob will be able to spend his funds immediately while she has to comply with the [RSM Contract](https://motorina0.medium.com/rsm-contracts-f690cfdf04e8).

17. Store(signatureBob) — Alice persists the signatureBob message together with the data that allows her to reconstruct payBobFirstTx. She can use this data if Bob becomes unresponsive.

18. BuildRevokeCommitmentMsg() — Alice builds the revokeMessage, that will penalize her if she tries to broadcast the old state ( **State01** ) where she had 0.10 BTC.

19. Send(revokeMessage) — Alice sends the revokeMessage to Bob. She can now consider the payment complete. The payment channel has moved to  **State02**.

![](https://cdn-images-1.medium.com/max/682/1*TJ1anRzIMJ1L5AKBto2IRQ.png)

![](https://cdn-images-1.medium.com/max/682/1*2Pdmt12_XlgGzmH8kezLyA.png)

20. Receive(revokeMessage) — Bob receives the revocationMessage from Alice. He now can consider the payment complete. The payment channel has moved to  **State02**.

* * *