26

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

19

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.

  • 2
    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 Feb 19 '16 at 12:16
  • 1
    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 Jul 21 '16 at 13:05
  • 1
    @Brain2000 I updated details to a question about scalability: ethereum.stackexchange.com/questions/133/… – eth Aug 8 '16 at 12:09
11

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.

  • 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 Feb 17 '17 at 7:45
7

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.

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

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
    sharding is coming to Ethereum.. – eugene_sunic May 1 '18 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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