Let's say I sent a transaction to invoke a smart contract (to its address). At which point will the smart contract get executed? Is it during the transaction validation process?

Also, is there any limit on how long a smart contract can run on? For example, the block generation time in Ethereum is about 12 seconds. If there is a smart contract which takes a long time to run and if it gets executed during the transaction validation process, how would that impact on the block time?

1 Answer 1


When does it run?

The execution for a smart contract transaction occurs when the mining node includes the transaction in a block it generates. The transaction and smart contract code is re-run by every validating node upon receipt of the block.

Mining nodes, to generate a block:

  1. Receive unconfirmed transactions from the network (p2p protocol)
  2. Validate each unconfirmed transaction, including running associated code
  3. Include valid transactions to fill the block in a way that maximizes profitability

Once the block is generated, it's distributed to the network, and any node to receive that block then proceeds to run through the list of transactions, ensuring each transaction is valid (including running associated code). When a new node connects to the network, it downloads every block in history and re-validates every transaction in each.

So when does smart contract code run?

  1. Lots of times, repeatedly and redundantly. By design.
  2. But the "official" execution point is the point at which the transaction occurs in the blockchain. If it's transaction #5 in block #3000000, then the transaction's "point of execution" could perhaps only be described as immediately following transaction #4, preceding transaction #6.

The repeated validation is possible because smart contract transactions are deterministic. They may depend on factors such as the block number itself, current storage values for a contract, or the result of another smart contract's computation, but that information is constant and can be recomputed perfectly by stepping through transactions from the start of the chain.

N.B. This pertains to transactions specifically. The EVM also defines the notion of a call. This distinction is well-explained elsewhere on this forum.

What limits the execution time?

The limits are provided by means of the "gas" system. Gas is computed based on amount of storage used, plus individual gas costs for each machine instruction (opcode).

Gas is limited by two mechanisms:

  1. The sender of a transaction specifies the gas limit they're willing to spend for the transaction. (This is usually computed automatically based on an estimate)

  2. Mining nodes specify a block total gas limit, theoretically optimizing for profitability based on network performance. Transactions that exceed the block gas limit will not get included in any block.

During validation, the transaction code execution can safely be aborted if either of these limits is reached. This prevents nodes from spending excess computing time performing this validation, and prevents the block time from growing as the number of transactions increases.

  • Thanks for the answer. Can you please elaborate 'The execution for a smart contract transaction occurs when the mining node includes the transaction in a block it generates. ' ? Is it during the process of validating the transaction before adding into a block? and my other question is if it running for a long time - how would that affect the block time - or does the miner have to wait till the contract completes to add the block to the chain? Jul 9, 2017 at 12:43
  • I expanded my answer a bit, I hope it clears things up better!
    – gnidan
    Jul 9, 2017 at 22:09
  • Thanks this helps! Also can I assume if the transaction has enough gas then the contract will run for a longer time and that will impact the block time? For example if the average block time is 12 secs then it may even go into minutes based on the time it takes to run a contract? Jul 9, 2017 at 22:47
  • Not quite - the block time is designed to be relatively constant; the total block gas limit (#2 in answer) stops execution from exceeding a certain amount. (Let me know how I can reflect this more clearly in my answer!)
    – gnidan
    Jul 9, 2017 at 23:17
  • 1
    So, what would happen if a contract takes 1 minute to execute and whoever invokes the transaction willing to provide the required gas..? Would this be dropped by the miner if the gas limit exceeds what is set by the miner per block? Jul 9, 2017 at 23:27

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.