How does the EVM execution work

evmsolidity

I'm trying to understand how the EVM works a bit better.
Imagine I have a smart contract which is calling a uniswap to swap ETH for tokens and then on the Next line I swap back those Tokens for ETH.

uniswap.swapExactETHForTokens{value: amount}(
    amountOutMin,
    path,
    SELF,
    deadline);

uniswap.swapExactTokensForETH(
    amount,
    amountOutMin,
    path,
    SELF,
    deadline);

Does the EVM Execute transactions in an asynchronous manner,

  1. is it possible that, the transactions on the block chain can affect the price in between those calls?
  2. OR does the state of a contract "read committed" in the sense that the variable in blocks that are trying to be written are updated
  3. OR is the state a combination of both, other people who are in the same block of transactions experience the contract as "read commited" but it's a race to see which block gets written first

Edit:
How does execution of the code work, is the state of my entire function local to my method call, or is the state local to the block?

Best Answer

The transactions and calls into other smart contracts are executed synchronously. So the execution of the command calling into another contract will wait for the other contract to return before moving the program counter to the next line.

Transactions are also atomic, and cannot be interrupted by other transactions. This also means that a call into a function in a smart contract is atomic, even if this function call calls other smart contracts, the whole execution is atomic and cannot be interspersed or interrupted by other parties' or persons' transactions.

However, miners can rearrange transactions (choose the ordering) within a block to benefit them -- and include their own transactions anywhere within a block. But they can do so only before and after other transactions, not in the middle of the execution of another transaction, even if that transaction has calls to other smart contracts.

If a price is contingent on the amount of a token in the contract, then the price will be updated throughout the execution of the program, as each call into an external contract are seperate from the callee's perspective.

In other words: If the transaction you are executing updates state within the callee's memory, then these updates will be reflected throughout the execution of the transaction. But these separate calls into the callee cannot be interrupted by other transactions.

The execution takes place in the VM which has both ephemeral memory and persistent memory.

Ephemeral memory is for "stack" and "memory", and persistent memory is account nonce, eth balances, and something called "storage" where e.g. ERC20 balances are stored. The ephemeral memory is not visible outside your transaction but persistent storage is. So the state of your function call is only visible within the transaction you are making, but the persistent changes it makes are visible outside the transaction.

Related Topic