2

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?

1 Answer 1

3

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.

6
  • Sorry I need a bit more clarification, 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
    – johnny 5
    Nov 5, 2021 at 22:19
  • Do you have a link to where I can find this info so I can read more?
    – johnny 5
    Nov 8, 2021 at 5:28
  • 2
    How can they reorder transactions on a block? You mean they can peek at the results determine if a transactions will be profitable and front run it?
    – johnny 5
    Nov 8, 2021 at 5:33
  • 1
    Absolutely. If you're a miner, you can do exactly that. Its called "MEV", Miner Extractable Value. Have a look at medium.com/@danrobinson/ethereum-is-a-dark-forest-ecc5f0505dff Nov 8, 2021 at 8:26
  • If you want more details on Ethereum Virtual Machine Execution, have a look at Ethereum Yellow Paper. It's a tough read though, so don't read it from cover to cover, instead try to pick the relevant sections and only dive into those. Nov 8, 2021 at 8:28

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.