📜 ⬆️ ⬇️

How we played the game “Stone - Scissors - Paper” on the Ethereum blockchain

In April 2018, inside our company, the idea came to release our game on the smart contract of the blockchain Ethereum. After thinking for a few days and sorting through several options, we stopped at the game “Stone, Scissors, Paper” well known to all of us since childhood.
I will not disclose what other options we considered in the discussion, but there were a lot of disputes, debates and fun. Some were too absurd, others were quite complex, others did not at all fit the blockchain technology. The discussion was a couple of quite serious options that, quite possibly, will see the light in the near future.

The idea to create the game "rock, paper, scissors" at the beginning was heavily criticized by our security specialists. Their main argument was that it is impossible to hide anything in the Ethereum blockchain, all information is publicly available. That is, an experienced person can see what move his opponent made before he made his return move. Even the fact that the variable in which the move is stored is not publicly accessible does not remain an obstacle to its detection. The obvious solution was to use sha3 data encryption with two-step key disclosure.

At the first stage, the client sends his move, encrypted with a key, randomly generated by him. Then, waiting for confirmation from the smart contract, that the opponent also did and sent his turn. In the second stage, the opponents send their keys to the smart contract to decrypt their moves. After that, the result of the game can be deciphered by a smart contract and a winner is determined.
')
Circuit logic in the studio!




Three days after starting work, the prototype of the game was ready. Inspired, we all tested the game in the office, without realizing what difficulties we will face in the future. The prototype allowed you to send encrypted moves, listen to the arrival of an opponent's move and send private keys to determine the winner. Thus, we have seen in practice that the idea is viable and can be implemented within a reasonable time.

How wrong we were ...



The next three weeks went less smoothly. It turned out that in order to carry out any game actions, it is necessary to form signed transactions in block Ethereum. There are ready-made solutions for this, in the form of a browser plugin Metamask – ethereum wallet. Raise your own wallet inside the game did not. Because of this, the mobile version of the game for mobile browsers immediately fell off. There is no possibility to install plugins on them yet. The big problem was, and still partially remains, the determination of the game state at a single point in time, when the page is reloaded or when switching between tables. Do not forget that we are not working with a server on which such a problem is solved elementarily, but with a blockchain, where to get information from is rather exotic.

We constantly listen to a number of game events, such as the joining of an opponent, the beginning of moves, the completion of moves and others. We also constantly send requests for some game information, for example, a current score, the start time of the round and others. Moreover, the majority of game states are determined not by one event, and not by one variable, but by the superposition of several events with some variables at once — the results of direct data acquisition from the blockchain. For example, the situation when the game goes to two or more victories, and when the third round is the time to send private keys. We have to track that the game has begun, the opponent has connected, three rounds have passed, and the encrypted moves have been sent from both sides. Try to reload the page at this moment and you have to restore all interconnections with the blockchain.

Each request or receipt of information occurs asynchronously. Delays overlap one another and as a result, the application runs significantly slower than the server version. God forbid with any problems with a stable connection. If you miss something, the state of the game is not restored at all, and the most annoying thing is that in such a situation nothing can be done to help the player, to clarify the situation. Only reloading the page, most often, can solve such inconsistencies on the client. At some point, the fate of the project in its originally planned form was threatened with failure. Thoughts appeared to cut the game, abandoning more than one game round. We thought that if the game always went on until one victory, it would be easier not to get confused and not to miss anything. Another possible solution would be to close the possibility of switching between several tables. Such an opportunity was made to fill the tedious waiting time for the opponent's move - you can play on several tables in parallel. Make a move on one, while on the other there is a wait.

Fortunately, we managed not to take these necessary measures - we found the reason, a buggy wifi router in the office, and the situation improved more or less.



As a result, the project took place almost in its original form. The main goal of the project is to popularize blockchain technology and entertainment. To participate in it, a person has to master for himself the basics of the world of cryptocurrency - creating a wallet, receiving a test broadcast, performing transactions, encryption, wallet addresses, types of networks, and so on. In addition, the process of playing the blockchain itself is slightly more negative in its user experience - the waiting time is sometimes tedious. With a large commission of the test "gas", the course takes from 15 to 20 seconds, while with a small commission, the turn may take from 7 to 11 hours. But we are optimistic about this, because We believe in the further improvement of this technology and the expansion of opportunities for its widespread use. How timely is the appearance of this project? On the basis of the stated goal - quite timely, based on the level of technology development - rather prematurely.

Link to the second part

Source: https://habr.com/ru/post/358576/


All Articles