30

This question was asked on Reddit a while ago:

When a node sends a transaction to the network and has the receiver as a contract, does every node execute the contract bytecode with the inputs to confirm the hash? Or does the first node receiving the transaction execute and create the transaction hash that's then distributed and is what's used for consensus. Basically, is the contract code executed once or many times for each transaction? I assume the it's the former if every node is running the EVM. Reddit: Basic Questions About the Ethereum EVM and State Storage

0

4 Answers 4

21

Yes, the answer is quite logic. Every node has to verify the results of a transaction which invokes a smart contract. The result is that at least every full node will execute the code.

The hash of the transaction isn't relevant until it is being stuffed into a merkle tree. When running the execution, all we care about is the amount of gas, the data being passed in, and the code of the contract that's being called. When the execution completes, the storage tree of the contract may be updated, so we recompute the merkle root hash of that tree (and any other contract which may have been called by that one!)

Source.

5
  • 3
    what does a non-mining node do with the result of the execution of an arbitrary contract? A mining node would inherently attempt to verify the transaction and add the result to the blockchain, but that does not apply for a non-mining node.
    – zanzu
    Commented Feb 19, 2016 at 12:16
  • 2
    I'm surprised. It doesn't sound very scalable if every single node must run the same code. I would think that one node would run it (perhaps at random) and then sign the transaction as ready to be added to the next block. What happens when the code demand outstrips the processing ability of each individual node?
    – Brain2000
    Commented Jul 21, 2016 at 13:05
  • 1
    @Brain2000 I updated details to a question about scalability: ethereum.stackexchange.com/questions/133/…
    – eth
    Commented Aug 8, 2016 at 12:09
  • 1
    @Brain2000 If only one node were to run the EVM and then publish the results for all the other nodes to blindly trust that would raise a security issue. On the other hand, if only 51% of the nodes were to run it, conserving the other 49% nodes' resources, the security issue would probably not arise. Commented Oct 3, 2022 at 11:03
  • @Brain2000 that's why the gas fees are so high!
    – user20574
    Commented Apr 4, 2023 at 19:24
13

Every full node processes every transaction. The miners choose which transactions to execute, based on gasprice. Once a block is mined, every node must rerun all of the contract executions in order to verify the block.

2
  • 5
    So does that mean that every node runs all mined contract twice? Once when it is unconfirmed and second when it is mined.
    – Jus12
    Commented Feb 17, 2017 at 7:45
  • The way I read it only the miner runs the code when the block is mined. All other nodes only run it once when the block is being verified.
    – pooya13
    Commented Mar 23, 2023 at 1:54
10

Yes, every (full) node executes the contract code for each transaction.

Since executing contract code is relatively expensive, nodes only do this when they need to. When a node gets a transaction, it only does a few basic checks first, such as:

  • is the signature valid?
  • does the sending account have enough Ether to pay for the gas?
  • is the gas below the block gas limit?

If the basic checks pass, the node relays the transaction. Mining nodes then perform the relatively expensive job of executing the transaction, include it in a block and collect the transaction fee. When a (full) node gets the block, it then executes the transactions in the block to independently verify the security and integrity of the blockchain that it builds.

2
  • 1
    is there a flow diagram/sequence diagram for this?
    – Nathan Aw
    Commented Jul 31, 2018 at 6:11
  • 1
    @NathanAw Not that I know, and it's possible that node implementations may have differences.
    – eth
    Commented Aug 12, 2018 at 2:37
7

In the current version every node runs every contract. Since this is not scalable it is highly likely that "sharding" will be implemented, where some states are only on certain nodes. They will then be accessed as needed asynchronously. This is What Vitalik is currently working on

1
  • 1
    sharding is coming to Ethereum.. Commented May 1, 2018 at 8:25

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.