2

I am just starting with all these dapp things and one thing is not completely clear for me.

Ok, so I will just start from simple example. Let's say I want to create simple "Guess a number" dapp. The whole idea is very simple. I want to have 100 participants in my game who are guessing number from 1 to 1000. One guess costs some ETH. And when 100th guess is made I want to find closest guess and transfer all ETH, which were collected thought the game, to the winner(or winners).

As I understand, all that logic should be written in contract. And all information about participants(addresses, numbers and etc.) should also be stored there(Please correct me if I am wrong). And finally my question is, is it possible to kind of erase/reset all the previous participants related data from contract? I mean, I want this game to be kind of looped. This is the whole idea step by step:

  1. generate random number on first iteration
  2. collect data from 100 participants
  3. on 100th guess choose winner/winners(closest to actual number)
  4. transfer money to winner/winners
  5. remove all current participants related data
  6. start again from step 1.

Is this achievable? Or is this just me misunderstanding the whole idea of contracts and dapps? :)

So, maybe someone could correct me if I am wrong and point me to the right direction of creating dapp for my example? :)

Thanks a lot in advance :)

2 Answers 2

1

First, the other answer is correct, you can't store private data in your smart contract, but you can store an obfuscated version of the answer and an agreed upon (in the code) method by which to reveal it later. So you do this:

  1. Send in an obfuscated answer
  2. Allow people to the game and make their guesses
  3. Send in the piece of data that reveals the answer (maybe you call this salt or something)

The code of the smart contract, given the missing data, reveals the previously entered answer so all participants are convinced that you didn't cheat. (There's a problem with this--how do they know you didn't make a guess under a different account--i.e. Sybil attack).

Concerning resetting for the next game:

Don't create new data each time and leave 'cruft' laying around--that's not good for the community--if everyone did that, the hard drives of those of us who are running full nodes would fill up--(the fact that we should all be running full nodes is a different argument that I won't go into).

Just keep a pointer pointing to the nextAvailableSlot. As the game is ongoing and new entrants make their guesses increment the pointer. To reset the game, point the pointer back to the start of the array. In this way, you get fixed size storage for the life of the contract as opposed to a continually growing 'cruft machine.' (At the expense of history of games--but I would argue that the history is in the transactions. You can always get the history if you need it by scanning the chain. You don't need to store history on-chain).

0

It might be quite difficult. It's not possible to store any secret data in a smart contract at the moment, so the number that everyone is trying to guess will be public unless you send it in after everyone's guess (but in that case, the participants have to trust that you're not favouring any of them).

You can use something like Oraclize to generate the random number after the final guess, and then nobody has to trust you.

You could reset the data simply by having a 2d mapping, so your rounds of guesses go something like guesses[roundNumber][guessNumber] and you increment the roundNumber each time. That way you're not paying for resetting all the data each round.

2
  • thanks for answer :) well my concern wasn't about the secret data. I was worried about storing data and resetting it. And I think I found the solution. I just realized I can have structure something like this: array[counter] = { sender: someHash, guessedNumber: 123 } so, on each 100th guess, I could just start from 0 and rewrite old data with new one. As I understand this would be the right way to go :) Or maybe someone have some other opinions? :)
    – tdk
    Sep 21, 2017 at 11:40
  • Yeah - that shouldn't be a problem, just make sure you're not accessing data until you have at least 100 guesses, otherwise you could be pulling data from previous rounds. Sep 26, 2017 at 1:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.