who runs the smart contract in ethereum?

Viewed 199

I am new to Smart Contracts. I want to know who runs the smart contact.

  1. I thought the full node that serves http rpc endpoint runs the smart contracts that node received.

    However solidity supports block attributes like block.coinbase, block.timestamp, block.difficulty. If just full node not miner runs contracts, I think it's impossible. Then the block creator runs the all smart contract execution transactions that in the mined block?

  2. If miner runs smart contracts, can miner pollute timestamp of block?

2 Answers

Contract function invoked by a transaction

Multiple entities run the contract code.

  1. At first, all miners trying to generate a block including a transaction interacting with this contract. In order to calculate state changes (to be included in the block) resulting from the transaction, the miner needs to execute the function.

    Only the winning miner includes these state changes in their block and broadcast the block. So effectively, from the outside view, you see the function being executed just once.

    Values of these block.* global variables are values of the winning miner's metadata. If a different miner published the block, some of these values (such as coinbase and timestamp) would be different.

    If miner runs smart contracts, can miner pollute timestamp of block?

    Yes, they can chose to manipulate the timestamp of the whole block (affecting other transactions in the block as well) to an extent of few seconds. Or they can chose to include the transaction in a later arbitrary block number, or not include it at all.

  2. And then, all validator nodes execute the same function validating if they get the same result as the miner. As they are validators, they don't inject their own metadata and validate against the already published block data - such as the block.coinbase address published in the block.


Contract function invoked by a call

Only the node performing the call invokes the function. Since calls are read-only, they cannot produce any state changes, so the node only reads the data from their local copy of the blockchain.

Example: A web application is connected to a node, and asks the node to perform a call. Only this one node performs the call and returns the response back to the web app. No other nodes are involved.

The smart contract is deployed and then available on the blockchain at its address.

The one who "runs" the smart contract is the caller of a function of a contract. The one who sends the message, interacts with the contract and sends ETH. It is in a contract referred as "msg.sender" It could be an address account or a contract acount (contracts can call others' contracts function).

block.coinbase, block.timestamp, block.difficulty are just referring to the informations of the current block at time of call.

Related