Blockchain Semantics Insights
Business Case | Deep Tech | Announcements | Blockchain Glossary |
State Channels- A Way To Think About Blockchain TransactionsBy Viswanath Kapavarapu | June 13, 2018, 3:03 p.m. GMT
A fundamental limitation of blockchain applications is that blockchains are expensive. Each transaction on blockchain requires high fees and needs time to get mined into a block. Now, if I want to build a 2-player game on Ethereum, executing each move on-chain will result in a slow and expensive user experience. This is where state channels come.
What are State Channels?
“State channels are a very broad and simple way to think about blockchain interactions which could occur on the blockchain, but instead get conducted off of the blockchain, without significantly increasing the risk of any participant” - Jeff Coleman.
Consider the game of chess to be implemented on blockchain.
Chess is a 1-1 game, players take turns, and the rules are clearly defined so it’s a natural fit.
At each turn, the following conditions need to be determined:
- Is it the turn of the player who’s posted the action?
- Is the move legally allowed?
- Is the game finished yet?
Of course, the above logic can be coded entirely in Solidity. However, updating the state on the chain for each turn will result in an expensive user experience. By using State Channels, we can avoid some on-chain transactions without losing trustlessness. With a state channel in place, instead of updating the smart contract for each move, the players shall be exchanging signed messages indicating their moves to the opponents.
The participants need to publish the state to the blockchain only when the game is complete or when the player stops sending messages, thus stalling a potential loss.
Let’s look at how the chess game can be implemented in real world using state channels. The main reason that the game needs to be on-chain in the first place is because the players are competing for something of value. Prior to the start of the game, a channel can be simply opened taking funds from both players and locking them until the channel is closed. Apart from creating a channel, a web-based application needs to be created to enable communications between the two players.
Once the application is created, both the players can take turns updating the state channel and then pass to the next player. Each time a move is carried out, the active player signs a message with the action and the updated state resulting from the move. The passive player can then run the same action against the previous state and compare if it results as the same new state. If not, a passive player can immediately fall back to the smart contract that the active player is trying to cheat.
Once the game is complete, an on-chain transaction to close the state channel can be invoked. The smart contract function takes the signed state, verifies the signatures, and validates each player moves to determine the winner.
State channels facilitate the exchange of signed messages replacing expensive on-chain transactions.
It is important that the participants should be able to update the smart contract in case the off-chain communication breaks down.
In a later post, we shall explore on the how the end-users can interact with a game using state channels.