0

If I have several transactions in the same function, can I be sure that they will be executed one just after the other.

Example with a dummy code:

Contract store {
    constructor() public returns(bool){
        return true;
    }
    function withdraw(){
        msg.sender.transfer(address(this).balance)
    }
    function() payable(){}
}

contract Exploit  {

    function execute(uint amount) public {
        store s = store();
        s.tranfer(amount);
        // Other code
        s.withdraw();
    }
}

If we take this example: if I send money to a contract, execute some code, and call the withdraw function to retrieve money from the contract.

Can I be sure that nobody will be able to call withdraw() before I do ? ie:

  • Can a miner put his transaction in the middle in hope of mining the block ?
  • Can someone listening to an Ethereum node, send a withdraw transaction with a high gas as soon as he sees a payment to the store contract?

Regards,

1

Can a miner put his transaction in the middle in hope of mining the block ?

If your deposit and withdrawal are two separate transactions, then yes, it can be put in between them. However, if your Exploit example is accurate, and they are both done in a single contract method, this is not possible.

Transactions are executed serially in ethereum, there is no parallel processing. A second transaction while not start executing while another one is halfway through its execution.

Can someone listening to an Ethereum node, send a withdraw transaction with a high gas as soon as he sees a payment to the store contract?

If they are separate transactions, yes.

In most such scenarios, you use modifiers to restrict access to certain methods to only authorized callers.

If the method needs to be public, you would add a mapping of addresses to balances, and ensure an address is only allowed to withdraw as much as your usecase allows them, and only after certain other conditions (that might be other methods you run in between the deposit and withdrawal) are met.

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.