0

https://github.com/axiomzen/eth-random

They have :

"We ultimately recommend not using this module, or this pattern in general. The reason is that it's fundamentally flawed and we can't recover from. A smart contract can call the random function and it can decide wether or not the result is beneficial, and in case it isn't then revert the operation."

How the revert works in code/solidity ? How the contract caller knows if reverts or not, he checks the balance or make other call to check the outcome?

Thanks in advance.

1 Answer 1

1

Let's suppose we have a contract V, it has a roll() function that will generate a random number and pay you double if you guess it correctly.

contract V {
    Random api = Random(0xAddress);

    function roll(uint guess) public payable {
        if (api.random(6) == guess) {
            msg.sender.transfer(msg.value * 2);
        }
    }
}

In this contract is easy to determine if you won by checking the balance. For other contracts you have to get the result calling a function, or by measuring the gas consumption.

The attacker can call roll() and if it lost it will revert the whole transaction, meaning it will not have to paid for the lost only the transaction fee.

contract A {
    V v = V(0xAddress);

    function bet(uint guess) public payable {
        uint balance = address(this).balance;
        v.roll.value(msg.value)(guess);
        if (address(this).balance < balance) {
            // We lost, rollback all changes
            // The state will be as we never made the bet
            revert();
        }
    }
}

A revert() it will cause all previous changes to be discarded, even modifications done to other contracts through calls will be discarded.

2
  • Hi Ismael and thanks for your answer, but it possible to use tx.origin to block all players to use contracts ? thanks in advance
    – hugofreire
    Jun 2, 2018 at 10:25
  • Using tx.origin is deprecated, it is better if you get codezise() of the sender it is non-zer for contracts. Doing that will prevent multisig wallets from using your contract. Also in the future with account abstraction every account will be a contract. But that will not solve the problem for example a miner can evaluate your transaction and discard it if they didn't win, and it will cost them very little.
    – Ismael
    Jun 2, 2018 at 17:35

Your Answer

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

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