0

Imagine I have a contract that allows people to buy tokens.

After some time, only one token left, but two persons try to buy this token at nearly the exact same time from the same node (a web server).

What happens? Does the first person who ask it get the token and the second does not even if the block has not been mined yet?

Does the block needs to be mined so that the amount of token available in the contract is updated?

More generaly, where is the state of a contract saved between each block?

2 Answers 2

0

Coin will belong to the user whose transaction is mined first. As far as I understand the miners will include first transaction to the block and then will not include the second one since it makes the block invalid.

It's actually even possible that person B gets the coin even if he submitted his transaction a bit later, but with higher fee. In that case miners will include this transaction and leave out transaction from person A (note that user A won't get any coin in that case).

In general all new transaction are in "pending" state and are not considered "real" until included into some block. You might have actually see this behavior during attack on ropsten test network: you could submit transaction and it'll actually be visible on etherscan but it'll be in a pending state. After about 20-25 minutes without the confirmation transaction is dropped.

The last paragraph of "Blockchain and mining" of ethereum white paper explains it:

A commonly asked question is "where" contract code is executed, in terms of physical hardware. This has a simple answer: the process of executing contract code is part of the definition of the state transition function, which is part of the block validation algorithm, so if a transaction is added into block B the code execution spawned by that transaction will be executed by all nodes, now and in the future, that download and validate block B

If you think of it it makes sense: nothing could be declared as "happened" until it's recorded on a blockchain (and even then it could be "reverted" if longer alternate chain will appear).

So

Does the block needs to be mined so that the amount of token available in the contract is updated?

The answer is "yes": transfer of tokens is contract execution and it happens while mining and by each block after block is recorded on a blockchain.

More generaly, where is the state of a contract saved between each block?

As far as I can tell there is no "state between each block". There is a pool of pending transactions and each transaction changes state. So change of state happens only when new block is added.

5
  • Thanks for this detailed answer. However, you say in first paragraph that tx from B will be invalid. But in second paragraph, you say that tx from B might get inserted in the block and tx from A ignored... That does not make sense for me. Commented Nov 13, 2017 at 6:03
  • 1
    When person A and person B submit their transaction they both are in "pending" state. Whichever is included first becomes valid and second is discarded. What I was trying to say is that even if you submit transaction later you have a higher chance of getting the coin if you specify higher fee (since miners would be interested in including higher fee transactions first)
    – yname
    Commented Nov 13, 2017 at 6:27
  • 1
    @yname The first part of your statement is not correct. Both transaction can be included in the blockchain, only the first one will be included with a successful status, the second one will be included with a failed status, ie the sender will pay for the gas consumption.
    – Ismael
    Commented Nov 13, 2017 at 6:51
  • @Ismael this is a new information for me. I though transaction could be included into block only if it's valid. Could you please provide some docs where I can find more about inclusion of failed transactions?
    – yname
    Commented Nov 15, 2017 at 17:44
  • 1
    @yname Look at for example at block 4558810 in Etherscan, which includes transaction 0x2823d59ff96646e47d9a4ff26f2b8ae4e23709f42e8750b1b6ef178453849922 which has failed status. The transaction is part of the blockchain, and it is used to recoup the gas used, any other change is discarded because of the failed status.
    – Ismael
    Commented Nov 15, 2017 at 18:43
1

If both transactions for the token saved in your contract are mined, it will act by whoever is higher up in the transaction pool (whoever paid more).

There is only one token and two people, so only one person will get the token and the other will be thrown an error and have most of their gas sent back since they're not actually executing a transaction.

Yes the block must be mined so that "state" can be managed in the contract.

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.