20

What happens when a smart contract gets several similar calls in the same block?

Consider for instance this greeter:

contract greeter {
    string name;

    function setName(string name_) {
        name = name_;
    }

    function getName() constant returns(string) {
        return name;
    }
}

What happens if Bob, Alice and Eve all call the Greeter at the same time and all three calls are in the same block? Does everyone get the proper response with their own name? Does the contract get run three times?

2
  • This is likely a duplicate of ethereum.stackexchange.com/questions/1405/…. However StackExchange isn't call this out as related. Moderator will need to fix.
    – Paul S
    Commented Apr 10, 2016 at 22:02
  • My opinion is that the answer to this question will add a little more to my understanding of transaction execution order in mined blocks, compared to the link by Paul S. I had to think one step further. Commented Apr 10, 2016 at 22:25

2 Answers 2

18

What happens if Bob, Alice and Eve all call the Greeter at the same time and all three calls are in the same block?

The three transactions would get called, not necessarily in sequence, and the last transaction's name will be persisted in the mined block.

Does everyone get the proper response with their own name?

No. The getName() method would return the persisted name in the mined block - this is set by the last transaction.

Does the contract get run three times?

Yes. The setName() would be run three times.



The Details

Referring to What is the order and concurrency behavior of multiple calls to a contract in a single transaction?:

  • The winning miner of the block gets to decide the order that the transactions are included in the block.
  • Transactions from different addresses can be executed in an arbitrary sequence.
  • Transactions from the same address will always be executed in order of their transaction nonces.

So:

  • The virtual machine would call setName(...) three times, but the sequence of which transaction gets executed first is arbitrary assuming that the transactions have different From: addresses (different accounts).
  • The name field would be changed three times but the name in the final transaction would be persisted in the mined block.
  • Calls to getName() will return the name persisted in the mined block, after the block has been mined.
  • Calls to getName() from a smart contract executed in the same block would get the getName() value from any previously run transaction of the Greeter contract. Thanks @Tjaden for clarifying this issue.

Here is an example of the last two points above:

  • Transactions are sent in the order Alice, Bob and Eve
  • The winning miner sequences the transaction in the order Eve, Bob and Alice
  • The smart contract runs between the transaction of Bob and Alice
  • The smart contract's call to getName() would return Bob.
  • After the block has been mined, the name persisted in the block is Alice. Calls to getName() will now return Alice.
6
  • 1
    If there is another contract that calls getName() in the same block, it may not conform to your last bullet point Commented Apr 10, 2016 at 23:19
  • @Tjaden, doesn't the call to getName() just return the persisted data in the mined block? Commented Apr 11, 2016 at 0:02
  • 2
    Only if it is called from a node/web3.js. If another contract calls it, it will receive the most recently updated value, which could be any of the three, depending on the ordering within the block. Commented Apr 11, 2016 at 0:05
  • "The winning miner sequences the transaction", how is this done usually by the timestamp of the transaction? How does this work with multiple block confirmations if the results are diff. depending on the executing order? So the subsequent miners are bound to the original order?
    – Andi Giga
    Commented Nov 30, 2017 at 6:40
  • @TheOfficiousBokkyPooBah Your answer is huge for my understanding of ethereum transactions. With respect to “The name field would be changed three times but the name in the final transaction would be persisted in the mined block.”, something I’m struggling with is can that value persisted multiple times within the same block? Meaning, can the tx be executed yet again, following the first heee concurrent runs, resulting in the value being stored multiple times In the same block? If not, why?
    – Howiecamp
    Commented Aug 18, 2018 at 17:21
4

Short Answer: Both transactions will be executed in the same block. Important: The miner decide the order of the transactions within a block.

Expanded: In Ethereum, every transaction triggers a code execution that updates the entire world state. For each transaction in a block, the output of the world state of the n-th transaction becomes the input state of the n-th + 1 transactions. There is no assumption regarding the order of transaction execution within a block. When a block is mined, it's the miner that decide the order. No contract should rely on transactions ordering in it's code execution.

1
  • Good principle: "No contract should rely on transactions ordering in its code execution. " Commented Nov 27, 2017 at 19:06

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.