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.