0.0.1 • Published 6 years ago

ethereum_rps v0.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
6 years ago

Ethereum_RPS

This project is a game of Rock, Paper, Scissors over the Ethereum blockchain to demonstrated the Ethereum-Easy library Ethereum-Easy. Every interaction with the Ethereum blockchain in this game is done with the Ethereum-Easy library. In this README I will show you how to run the game and how it works under the hood.

Running the Game

After you git a copy of the game, open three terminal windows. The first is for RabbitMQ, which you will install and run with the directions under Requirements. Navigate below the src directory in the Ethereum_RPS folder with the other two windows. Start the game by entering:

node app.js <player_entry> <throw_entry>

<player_entry> : string This is either p1 or p2 for player 1 and player 2.
It doesn't matter which player you decide to be.

<throw_entry> : string This is the rock, paper, scissors throw you want to select. You can choose from 'rock', 'paper', or 'scissor'.

Note: It is helpful to start the nodes at relatively the same time.

Requirements

In order to run this game, you will need to have a working installation of RabbitMQ on your machine. This Rock, Paper, Scissors game uses Rabbit for passing AMQP messages between players in different Node windows. If you would like to run this game yourself, download RabbitMQ from the link provided and then run a broker on a terminal window with the instructions on their website under 'Getting Started'..

How it Works

Each node of this game enters different states as it progresses along its commitment scheme. The states are Ready, Deploying, Submitting Answer, Confirming Submissions, and Reveal. These follow a commitment scheme where each node checks the liveness of one another, a smart contract for the game is deployed, answers which have been run through a caesar cipher are committed to the blockchain, each node checks that the other has committed an answer, and finally the key to the caesar cipher is shared and each answer is decrypted. The details of each state are as follows:

Ready

In the Ready state, each node sends a message to each other verifying the other is ready to start the game.

BLOCKCHAIN
 -----        -----        -----        -----        -----
|     | ---> |     | ---> |     | ---> |     | ---> |     |
 -----        -----        -----        -----        -----


 --------------                    --------------
|              |     MESSAGE      |              |
|              |  ------------>   |              |
|   PLAYER 1   |     MESSAGE      |   PLAYER 2   |
|    READY!    |  <------------   |    READY!    |
|              |                  |              |
 --------------                    --------------

Deploying

Once the Ready state has been confirmed, PLAYER 1 will deploy a smart contract to facilitate the Rock, Paper, Scissors game. While PLAYER 1 is deploying, PLAYER 2 is waiting for confirmation of the deployed smart contract. Once the smart contract is deployed, PLAYER 1 will send a confirmation message to PLAYER 2.

BLOCKCHAIN
 -----        -----        -----        -----        -----
|     | ---> |     | ---> |     | ---> |     | ---> |     |
 -----        -----        -----        -----        -----
        ^                                   
        |                                   
 --------------                    --------------
|              |                  |              |
|              |                  |              |
|   PLAYER 1   |                  |   PLAYER 2   |
|   DEPLOYING  |                  |    WAITING   |
|              |                  |              |
 --------------                    --------------

Once PLAYER 1 gets confirmation from the blockchain that the smart contract is deployed...

BLOCKCHAIN
 -----        -----        -----        -----        -----
|     | ---> |     | ---> |     | ---> |     | ---> |     |
 -----        -----        -----        -----        -----


 --------------                    --------------
|              |     MESSAGE      |              |
|              |  ------------>   |              |
|   PLAYER 1   |                  |   PLAYER 2   |
|   DEPLOYED!  |                  |  NEXT STATE! |
|              |                  |              |
 --------------                    --------------

Submitting Answer

Once the contract is deployed and PLAYER 2 has confirmation of this, then both players submit their answers to the smart contract on the blockchain. The answers they submit are run through a caesar cipher to keep the answer secret to avoid cheating.

BLOCKCHAIN
 -----        -----        -----        -----        -----
|     | ---> |     | ---> |     | ---> |     | ---> |     |
 -----        -----        -----        -----        -----
        ^                                ^
        |                                |
 --------------                    --------------
|              |                  |              |
|              |                  |              |
|   PLAYER 1   |                  |   PLAYER 2   |
|  SUBMITTING  |                  |  SUBMITTING  |
|              |                  |              |
 --------------                    --------------

Once each player gets a confirmation from the blockchain that their answer is submitted, they inform the other player that they have answered.

BLOCKCHAIN
 -----        -----        -----        -----        -----
|     | ---> |     | ---> |     | ---> |     | ---> |     |
 -----        -----        -----        -----        -----


 --------------                    --------------
|              |     MESSAGE      |              |
|              |  ------------>   |              |
|   PLAYER 1   |     MESSAGE      |   PLAYER 2   |
|  SUBMITTED!  |  <------------   |  SUBMITTED!  |
|              |                  |              |
 --------------                    --------------

Confirming Submissions

Following the commitment scheme, each player now calls into the smart contract and ensures that the opponent has submitted an answer. Keep in mind, the only information they will find is an encoded answer, but it proves the other player has submitted their response. Once a player verifies their opponent has submitted an answer, they send them a message that they are ready to move on to the final state.

BLOCKCHAIN
 -----        -----        -----        -----        -----
|     | ---> |     | ---> |     | ---> |     | ---> |     |
 -----        -----        -----        -----        -----
        ^                                ^
        |                                |
 --------------                    --------------
|              |                  |              |
|              |                  |              |
|   PLAYER 1   |                  |   PLAYER 2   |
|   CHECKING   |                  |   CHECKING   |
|  SUBMISSION  |                  |  SUBMISSION  |
 --------------                    --------------

Now that each player knows the opponent has submitted an encoded answer and thus cannot cheat...

BLOCKCHAIN
 -----        -----        -----        -----        -----
|     | ---> |     | ---> |     | ---> |     | ---> |     |
 -----        -----        -----        -----        -----


 --------------                    --------------
|              |     MESSAGE      |              |
|              |  ------------>   |              |
|   PLAYER 1   |     MESSAGE      |   PLAYER 2   |
|  CONFIRMED!  |  <------------   |  CONFIRMED!  |
|              |                  |              |
 --------------                    --------------

Reveal

Now that each player know that their opponent has submitted an answer, they are ready to exchange cipher keys. The keys are sent in a message between each player and then the answer submitted to the blockchain can be decoded. From there, each player uses their own logic to decide if they won, lost, or if the game was a draw.

BLOCKCHAIN
 -----        -----        -----        -----        -----
|     | ---> |     | ---> |     | ---> |     | ---> |     |
 -----        -----        -----        -----        -----


 --------------                    --------------
|              |  MESSAGE (KEY)   |              |
|              |  ------------>   |              |
|   PLAYER 1   |  MESSAGE (KEY)   |   PLAYER 2   |
|    WINNER!   |  <------------   |    LOSER!    |
|              |                  |              |
 --------------                    --------------

I hope you enjoyed playing! Thanks for looking at this demo and let me know if you have any question at jon.heinecke@gmail.com.