Would this be an acceptable way to produce a random number dependent on the reward? Say if the reward is high enough to entice a miner to manipulate the block then the contract will utilize oraclize.

if (reward < x)
{randNumber = (uint(keccak256(abi.encodePacked(block.difficulty))) % 100;}
else {useOraclize();}

No, a miner will know the current block.difficulty for each block, in real time, at no cost to them. That means whenever reward < x, one can know with 100% certainty what the value of your "random" number will be.

So your random number isn't random at all.

  • what if I add this? Is there anything else you would recomend? {randNumber = (uint(keccak256(abi.encodePacked(block.difficulty, now))) % 100;} – CryptoPuff Feb 20 at 3:31
  • They will also know the current time (or rather, their computer will). To a person, trying to work out cheat-conditions every millisecond is difficult, but it is unlikely that a person will try to cheat your system entirely by hand. They'll write a contract or some other software to monitor your contract, and only execute (automatically) when the conditions are favourable. – AnAllergyToAnalogy Feb 20 at 3:35
  • would using blockhash make things any better? – CryptoPuff Feb 20 at 3:39
  • 1
    No, by definition any variables available to your smart contract's function are available to the miner ahead of time, since they are technically the one who gets to decide whether or not to include the transactions, and they're the one who actually executes the function on their computer. Regardless of how obscure and "noisy" you make your function, in Solidity, always assume the miner can know the result before they execute it. – AnAllergyToAnalogy Feb 20 at 3:43
  • 1
    The miner is going to try to mine the block regardless. But if the conditions are favourable, they will also include a transaction where they (from their account) execute the function on your contract and win the prize, or whatever your random number is for. But whether or not they do the cheat transaction, they're still going to be trying to mine every other transaction on the network and get the reward and fees from that. They won't be shutting down their mining rig depending on your contract unless it has millions of dollars at steak. – AnAllergyToAnalogy Feb 20 at 4:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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