2

Scenerio 1
Say I have a function f().
Whoever calls the function first gets a reward.
Persons A, B, and C all call the function at roughly the same time.
Who will receive the reward?

Scenerio 2
Say I have a function f()
Whoever calls the function first gets a reward and function f() is no longer callable.
Persons A, B, and C all call the function at roughly the same time.
In actuality Person A calls function f() first.
Will persons B and C still be able to call the function?


For Clarity
I'm building a smart contract where if someone calls a certain function f(), that person and ONLY that person will receive a unique reward. And because that person called the function first, the contract will be paused (function f() will be uncallable) until that block has been mined.

So basically are the two scenarios mentioned possible?
Or am I misunderstanding anything when it comes to the mechanics of the Ethereum blockchain?

I've tested my contract within a private network and the contract is paused immediately once I call the function, and then is unpaused once the block has been mined.

1 Answer 1

0

Every call to the function happens inside a transaction. Whenever a transaction is sent by the user, a miner picks it up and puts it inside a block. The miner decides in what order the transactions are executed. The way the miner decides this, is up to him: they might do it at random, or maybe order the transaction by time of arrival, or maybe use a more optimized way.

Whichever function call is done in the first of the transactions that are put in a block, succeeds in your scenario. The others will fail. It's not possible to 100% accurately predict which transaction will be executed first.

7
  • HAVING AN AHA MOMENT...So a contract's state can only be changed once per block, is that correct? Like let's say I have a variable, i which is initialized to 1 at contract creation. Then let's say I also have a function, f() that allows me to change i to any arbitrary value. Let's say I call function, f() 3 times: f(5), f(7), and f(9). Does the state of i get changed to those three values noticeably to the network WHILE the block is being mined? Or is it just left with one of those values (depending on which order the transactions were mined) ONCE the next block has been mined? Jun 30, 2018 at 20:27
  • May another AHA coming. Blocks disambiguate transaction order. Transactions execute per the most recent state, which may be affected by transactions within the same block. Suppose you have a contrived function that increments a counter and sends ever greater amounts to senders. You can have many people simultaneously send transactions that can all be mined inside one block. The miner will resolve txn order arbitrarily, then 1 to someone, 2 to someone else, 3 to the next person, and so on. Each time the function runs, it's aware of how the contract state has evolved and runs in that context. Jun 30, 2018 at 20:51
  • @JohnDeBord A contracts state can happen more than once per block. In your example, the state will show the last value set in the block. Think of each transaction in a block being an "intermittent state" that the next transaction on the block is run against, but the network will only see the state of the completed block after the transactions are run in the designated order.
    – natewelch_
    Jun 30, 2018 at 23:09
  • @RobHitchensB9lab Ok I see what you're saying. But what if there are two transactions at roughly the same time that both change the contract to a different state? Let's say Person A is the first to make a transaction, followed closely by Person B. Is it the transaction that propagates throughout the network the most first the one that is considered the "first" transaction regardless of which transaction was truly the first? Ughh.. I'm just getting more and more questions. Do you think you could talk with me for a short time through a Google hangout? I think my project might interest you. Jul 1, 2018 at 8:52
  • @flygoing Great explanation; I understand that component now. Jul 1, 2018 at 8:55

Your Answer

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

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