Two transactions in the same time, (Teoric Question)

We have an accountant in a contract that goes up when called.
We have two transactions coming in at the same time from the same state contract.
Contract state: counter = 1
Transaction one: ++;
Transaction two: ++;
Therefore one of them will be chosen according to the gas for the miner.
Assuming they pay the same gas.
What prevents both transactions from being processed at the same time on different nodes?
How do the nodes process sequentially so that the counter always starts from the last contract state update?

In general, transactions get collected by whoever won the "race" to make the next block and they run them in a sequence of their choosing - often whoever paid the highest gas price goes first, but it can be more complicated than that. Transactions are booked at the same time, but "run" in sequence. You can see that order when you examine the block on or the block explorer of the chain you are working with.

For your case, each transaction is going to update the property on-chain as you tell it to increment. That gets committed with your transaction atomically. So whichever one the block-writer chooses to do first will run, increment your value, then the second one later in the list.

The exception to this is when you have re-entrance. That's when function F on contract A calls function G on contract B, which then calls function F on A. That's how the Ethereum DAO got hacked back in the day. Technically this is all one big transaction from a gas payment point of view, but it would mean your code works in a way that you didn't expect. Guarding again these is a matter of blockchain hygiene, and other articles can describe the situation better.

1 Like