> For the complete documentation index, see [llms.txt](https://counterfactual.gitbook.io/specs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://counterfactual.gitbook.io/specs/ldct-withdraw/06-update-protocol.md).

# Update Protocol

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

Once an application has been installed into the state channel, the multisignature wallet has transferred control over the installed amount from the free balance to the application's `resolve` function, a mapping from application state to funds distribution. For example, in the case of Tic-Tac-Toe, a possible payout function is: if X wins, Alice gets 2 ETH, else if O wins Bob gets 2 ETH, else send 1 ETH to Alice and Bob.

As the underlying state of the application changes, the result of the payout function changes. It is the job of the Update Protocol to mutate this state, independently of the rest of the counterfactual structure.

Using our Tic-Tac-Toe example, if Alice decides to place an X on the board, Alice would run the Update Protocol, transitioning our state to what is represented by the figure above. Notice how both the board changes and the *local* nonce for the app is bumped from 0 to 1. To play out the game, we can continuously run the update protocol, making one move at a time.

## Messages

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

For the below messages, the digest that is signed is represented as the following:

```typescript
keccak256(
  ["bytes1", "bytes32", "bytes32", "uint256",
  [
    0x19,
    keccak256(encode(
      [address, address[], address, bytes32, uint256 ],
      [owner, signingKeys, appDefinitionAddress, termsHash, defaultTimeout]
    )),
    0,
    TIMEOUT
  ]
);
```

**Type:** `UpdateParams`

| Field           | Type                   | Description         |
| --------------- | ---------------------- | ------------------- |
| `appInstanceId` | `bytes32`              | Hashed App Identity |
| `newState`      | `AppState / JSON-like` | New state to set to |

### The `SetState` Message

| Field         | Description                      |
| ------------- | -------------------------------- |
| `protocol`    | `"update"`                       |
| `params`      | An `UpdateParams` object         |
| `fromAddress` | The address of Alice             |
| `toAddress`   | The address of Bob               |
| `seq`         | `1`                              |
| `signature`   | Alice's signed commitment digest |

> TODO: Add a field for the encoded action

### The `SetStateAck` Message

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

## Commitments

**Commitment for** `SetState` **and** `SetStateAck`:

The commitment can be visually represented like:

![](https://3695485484-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LOsdTNChcpbTmOtYFXM%2F-LaLLJLKv4VA0_zZTbTy%2F-LZ8uyDh9WuE6YRX_pnI%2Fsetstate-protocol-commitment.png?generation=1553005733796385\&alt=media)

This transaction invoke the `setState` function with the signatures exchanged during the protocol.
