Home

Awesome

State Channels

State Channels allow entities to communicate with each other with the goal of collectively computing some function f. This f can be any agreement expressed in Smart Contract logic and just like any legal contract, we need an arbiter in case one party tries to act maliciously. This arbiter is the blockchain. For more information visit here

State Channel Demo

A demo use case of æternity's State Channels. Users can play a thousand+ rounds of rock-paper-scissors games through the æternity blockchain by deploying a game-rules Smart Contract into State Channels.

<img width="1411" alt="image" src="https://user-images.githubusercontent.com/10965573/201765249-c71b0d18-4fce-4b03-a65b-5ee1f4ae18d9.png">

Table of Contents

  1. Installation
  2. Running Options
  3. Channel Communication between Apps
  4. Helpful links

Coding Tutorial: Step by step channel guide

Installation

cd contract && npm install
cd ../client && npm install
cd ../server && npm install

The subsequent steps require docker compose and NodeJS >= 16

💚 Interested in the vue.js version? You can find it here

Running options

Local Node and not on connected with an æternity network

Terminal #numberExplanationCommand
#1Start nodedocker-compose up
#2Start servercd server && npm run dev
#3Start clientcd client && npm run dev

Deployed node and on testnet

Terminal #numberExplanationCommand
#1Start servercd server && npm run dev:testnet
#2Start clientcd client && npm run dev:testnet

Services

nameport
frontend8000
backend - nodejs3000
æternity node3013
Sophia Compiler3080
Websocket server3014

Channel Communication between Apps

Communication Diagram

Introduction

Except the funding & channel initialization phase (further explained below) where the demo accounts are funded and the required information for channel initialization is exchanged, the client application (frontend) and the server application (backend) always communicate via WebSocket to their configured node.

The æternity node runs 3 processes for each State Channel that a user initializes:

Note:

Server application / Backend

The server application (backend) can be seen as the game session manager. It is responsible for:

Client application / Frontend

On the other hand, the client application (frontend) is responsible to do the following:

Note:

FAQ

How do I build my own State Channel application?

You can find the steps at the complete State Channel Tutorial Guide.

Is State Channel Demo provably fair?

Short answer: yes!

State Channels offer an innovative solution, where off-chain transactions are executed on top of blockchain technology. The State Channel protocol inherently offers security to the demo!

State Channel transactions are ruled by co-signed Smart Contracts. Each party shall verify the content of a transaction before signing it. Off-chain transactions are performed by applying a list of operations on the off-chain state trees, then generating a new ChannelOffchainTx with an incremented round and updated state tree root hash. The updates themselves are not included in the ChannelOffchainTx for privacy reasons. The updates supported are:

Note: Serializations described here and here

State Channel Demo Game verifies counterparty transactions. At each transaction, calldata content is decoded utilizing æternity's calldata lib which is integrated in the SDK. Each party can utilize this library in order to verify that the opponent is following the anticipated flow of the game (calling the right contract methods). If they accept opponent's transaction, then they can co-sign it, otherwise they will refuse to co-sign.

How do State Channel Demo Game transactions take place?

Rock, paper, scissors expects the players to make their moves simultaneously, but in the State Channels implementation, it needs to be implemented as a turn based game, where the move of each player needs to be co-signed by the opponent in order to be executed. In order to prevent cheating, the move of the initial player is hashed. The other party receives the off-chain transaction with calldata containing the hash, where he/she can only confirm the function the opponent called (e.g. opponent picked a move) but not see the actual move.

After co-signing the opponent's hashed move, the next required step is for the second player to make his/her move. This is achieved by calling the corresponding contract method which does not hash the pick (the opponent's move is already sealed in the channel's state tree).

Now that both players have picked, the contract requires from the first player to reveal his move. Revealing is required as only the first player has the key to the hash. With the execution of it, the winning party receives the stake. In the case of a draw, the stake is returned to both players.

If one party refuses to reveal their move, how this can be disputed?

At first player's side, he/she can inspect the calldata (second player non-hashed move), examine the move the opponent picked and choose to make an inappropriate action (e.g. not revealing his move, because he/she found out that will lose after inspecting opponent's move).

The RockPaperScissors Demo Smart Contract provides disputing methods to use on-chain with ForceProgressTx, such as player1_dispute_no_reveal which can be used in cases where the first player did not reveal his move.

In that case, the second player can raise an on-chain dispute utilizing transaction force progress mechanism ForceProgressTx`.

With a force progress transaction, the contract state is broadcast on-chain where the game move is confirmed by on-chain computation.

Note:

Helpful Links