Created on: Aug 26 2017 | Last change: Aug 26 2017

Example State Channel

Jeff Coleman edited this page on 8 Nov 2016 · 3 revisions

A simple example for the use of state channels can be conceived if two parties, Alice and Bob, decide to play a game of Tic Tac Toe together for money, while using a blockchain-based "smart contract" or "smart agent" to ensure the winner and payouts are determined fairly.

In the direct blockchain paradigm, the on-blockchain agent they create must be able to understand the rules of Tic Tac Toe so that it can act as a referee to determine the winner, and it will track the state of the game as it progresses in order to determine the outcome. First, Alice and Bob submit their bets. Next, they take turns submitting a new move, with the game board updating in the blockchain each time. Finally, when the blockchain agent determines that the game has been won or drawn, the bets are distributed in the manner previously agreed. Each "move" or action consists of submitting a transaction to the blockchain, and waiting for that transaction to be confirmed by an appropriate number of blocks. These transactions have associated fees, with more complex logic on the part of the "agent" resulting in higher costs.

In the state channel paradigm, however, this situation can be substantially improved. Instead of creating an agent that follows the game move by move, Alice and Bob could instead create a sort of "judge" with the following rules:

  1. Accept any state of the Tic Tac Toe game signed by both Alice and Bob as the "starting state".
  2. Wait a small amount of time to see if any later states or moves are submitted, as denoted by a sequence number called the "nonce". Wait again any time more state or moves are submitted.
  3. When the waiting period expires, use the rules of Tic Tac Toe to determine a winner based on the latest information received, and distribute the bets accordingly.

Now, Alice and Bob need only to create this new "judge" program, and supply it with their initial bets. Their actual moves, instead of being submitted to the blockchain as transactions, can be signed and sent directly to the other player. When the player receives the move, they check if the move is valid and sign off on the updated version of the game "state", returning it to the other player. Or, if the move is invalid, they submit the most recent agreed upon state to the judge: followed by the other player's signed move, so that the judge can use the rules to determine an appropriate penalty. Each new move increments the sequence number, so the other player will not be able to submit an old version of the state to the judge. If they do so, the other player will simply respond with a more recent version during the waiting period. Similarly, if the other player refuses to return a signed copy of the updated state, the judge can also be called upon to adjudicate. But as long as the other player follows the rules, no recourse to the judge is yet needed, as illustrated in Figure 1.

Figure 1: Playing Tic Tac Toe on State Channels

Diagram of Tic Tac Toe on State Channels

This type of play is substantially cheaper, because transaction fees do not have to be paid unless one player misbehaves and the judge's services are needed. It can also proceed much faster, because the time to exchange and sign a new move is much smaller than the time needed to have a transaction confirmed by the blockchain. And finally, unless one player misbehaves the moves can remain private from other blockchain users.

When the game is finished, the players can both sign an update that simply distributes the bets according to how the game ended, and none of the judge's complex logic will even be needed, reducing costs yet again. In fact, with some further optimisation even the adjudicator's logic and the rules of the game itself can be put into the signed "state" the players exchange amongst themselves. Only a simple consent contract must be created to store the channel participants' assets, following the rule that any action performed must be agreed to by all parties. This one blockchain operation allows the entire game to be created and played offchain, even between parties who are not directly connected but who have a chain of connected state channels).

All this might seem to be overkill just for the simple example of a game of Tic Tac Toe. But the state channels paradigm can also be applied to most types of "smart contracts" that involve a specific pair or set of people, and prove to be extremely powerful. Whether it's making Tic Tac Toe games cheaper and faster, or accelerating large scale commerce across an entire network, state channels are a powerful technique for creating scalable, responsive, blockchain-based applications.