# Cleanup Protocol

![](https://2551238251-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LOsdTNChcpbTmOtYFXM%2F-LaLLJLKv4VA0_zZTbTy%2F-LZ8uyB6cT3CVryT4NQ1%2Fcleanup-protocol-state.png?generation=1553005722759191\&alt=media)

> NOTE: Notice that the `stale-invalid state` object has been removed from the previous figure shown in the [Uninstall Protocol](#uninstall-protocol) representing the effective "garbage collection" phenomena of the cleanup protocol

The cleanup protocol is a protocol that is periodically run to update the dependency of every active application to a newer root nonce version. In effect, it achieves the goal of O(1) constant time invalidation of outdated state put on-chain by resetting the state such that regardless of the number of historical / outdated apps that have been uninstalled, refuting their validitity on chain requires a single transaction.

## Messages

![](https://2551238251-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LOsdTNChcpbTmOtYFXM%2F-LaLLJLKv4VA0_zZTbTy%2F-LZ8uyBAI1yWTBgNaVFN%2Fcleanup-protocol-exchange.png?generation=1553005721085031\&alt=media)

> NOTE: The dependency in the message exchange is important; it is not safe to sign the root nonce commitment without possession of all the active app commitments.

### The `CleanupInstall` Message

| Field         | Description                      |
| ------------- | -------------------------------- |
| `protocol`    | `"cleanup"`                      |
| `fromAddress` | The address of Alice             |
| `toAddress`   | The address of Bob               |
| `seq`         | `0`                              |
| `signature`   | Alice's signed commitment digest |

### The `CleanupInstallAck` Message

| Field         | Description                    |
| ------------- | ------------------------------ |
| `protocol`    | `"cleanup`                     |
| `fromAddress` | The address of Alice           |
| `toAddress`   | The address of Bob             |
| `seq`         | `1`                            |
| `signature`   | Bob's signed commitment digest |

## Commitments

**Commitments for** `CleanupInstall` **and** `CleanupInstallAck`:

For each active application, a similar commitment to the one described in the [Install Protocol](#install-protocol) must be generated. The commitment calls `executeAppConditionalTransaction` with a limit of `c_1 + c_2` and a expected root nonce key of `r + 1`. Note that this is different from the install commitment in that it is not a multisend and does not set the free balance. Note that the free balance is also considered an active app. Here is an example of a commitment for a given app:

![](https://2551238251-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LOsdTNChcpbTmOtYFXM%2F-LaLLJLKv4VA0_zZTbTy%2F-LZ8uyBDB25YKCFYK1BX%2Fcleanup-protocol-commitment1.png?generation=1553005703525544\&alt=media)

Then, finally, the commitment update the root nonce is simply:

![](https://2551238251-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LOsdTNChcpbTmOtYFXM%2F-LaLLJLKv4VA0_zZTbTy%2F-LZ8uyBFkg_xPyo0mIYD%2Fcleanup-protocol-commitment2.png?generation=1553005727462076\&alt=media)
