BTC
ETH
HTX
SOL
BNB
View Market
简中
繁中
English
日本語
한국어
ภาษาไทย
Tiếng Việt

How does EKT multi-chain technology solve the storage and design problems of smart contract data

EKT多链技术
特邀专栏作者
This article is about 1711 words, reading the full article takes about 3 minutes
How to solve the storage and design problems of smart contract data
AI Summary
Expand
How to solve the storage and design problems of smart contract data

Since the game CtyptoKitties (crypto cat) on Ethereum became popular last year, many people have discovered a new way to play smart contracts - making games! In the mobile game market, chess and card games occupy a large market, such as Fight the Landlord and Texas Hold'em. This type of game has a common feature: before the start of each round of the game, it is necessary to shuffle the cards by generating random numbers to ensure the randomness of the cards in each round. One of the problems encountered in implementing this kind of chess and card games through smart contracts is: because smart contracts, including the data on the blockchain, are open source, once the random numbers used in this round of shuffling are recorded in the block before the end of the current round of games On the chain, the player can calculate the cards of all other players based on this random number, then the shuffling will lose its original meaning, and the game will not continue. How can this situation be solved on the blockchain? In this article, we will discuss the data storage problem of smart contracts together.

1. The shuffling algorithm of chess and card games

Everyone may have played chess and card games such as Doudizhu or Texas Hold'em. In these games, at the beginning of each round, the cards in the players' hands are different. How is this achieved? This problem is called "shuffling algorithm" in the computer field. Abstractly speaking, the problem it describes is how to generate a random permutation algorithm (array random sorting) for a finite set. The random permutations generated by this algorithm are equally probable, and the algorithm needs to be very efficient. Let's take a look at the solution to the popular Fisher–Yates shuffle algorithm:

1-N cards are stored in the original list list1

The randomly shuffled cards are stored in the new list list2

Randomly generate a number i (1 to the remaining numbers (including this number))

Starting from the low bit, get the i-th number (this number has not been taken out), take it out, and store it in list2

Repeat the third step to regenerate i until all numbers are taken out

The list taken out is a randomly sorted list of the original list, and the algorithm is completed

The above algorithm is expressed in pseudocode as follows:

In this algorithm, the random number i is a very important role, which directly determines the final cards in the hands of each player. If this number is exposed, it is easy to guess the final shuffling result.

2. The random number problem of smart contracts

Due to its openness, transparency and non-tamperable characteristics, the blockchain does not leave room for the dealer to cheat. Therefore, the implementation of a chess and card game based on blockchain smart contracts can very well guarantee the interests of players, and it is also a very promising application. The question is how to generate and save random numbers through smart contracts to ensure the fairness of the game and the interests of players? We first analyze the current situation of game contracts on Ethereum, and then put forward our thinking.

1. Insecure random number generation method

based on certain variables within the block

A lot of meta information about the block is recorded in the block, such as block.coinbase, block.difficulty, block.gaslimit, block.number, block.timestamp, etc. Many people think that random numbers can be generated based on these meta-information, but in fact this is not safe. First of all, if this random number can generate enough benefits, then the miners who maintain the operation of the blockchain have the motivation to modify these data. Secondly, if the attacker wants to attack, the random number can also be generated at the same time based on this information.

based on the blockhash of some generated blocks

Each block has its own blockhash, and EVM also provides an interface to obtain blockhash through block.blockhash(). In many current smart contracts, the blockhash of the previous block, that is, block.blockhash(block.number - 1) is often used to generate random numbers. The disadvantage of this scheme is also very obvious: if an attacker wants to attack, he can also generate a smart contract with the same random number based on the blockhash of the previous block.

2. How to generate and save random numbers more securely

We have explained the problem of the oracle machine in the previous article. The oracle machine is a bridge connecting the blockchain world and the real world information. We can implement a random number generator by ourselves, and then import the smart contract through the oracle machine, or directly call a website like Random.org to obtain a real random number. Regarding this aspect, with the continuous improvement of EKT, we will also provide a good mechanism to solve this problem.

Going back to the chess and card game example we mentioned at the beginning, even if the problem of random number generation is solved, when will the random number be written into the blockchain? We can think of each game as a "session". The beginning of each session is based on the generation of random numbers, but after the random numbers are generated, they are not immediately written to the blockchain. This information will first be recorded in its own "private" persistent space. After each round of the game, this data will be written into the blockchain from its own private space, which not only ensures the fairness of the game, but also does not disclose the information of the game in advance.


安全
Welcome to Join Odaily Official Community