In ethereum, when a smart-contract runs, does it run on all nodes? How is concensus achieved?

If a smart-contract is executed on noda A, does node B,C,D,E, ect, get the same data input and will every node in the p2p-network run the smart-contract ?

if not, what prevents a node from tampering with a smart-contract ?

Comments

  • SmithgiftSmithgift Posts: 64Member
    Yes, every node will run the same contract with the same input.

    More accurately, when a mining node includes a contract execution in a block, each peer will run the contract for themselves to see if the execution happened as the miner says it did. If not, the block is invalid and rejected.
  • PranayPranay Posts: 58Member
    that is the main thing for blockchain and mining. So each node when it doing mining it gather the information for as many as contract.
  • tym4ttym4t Nagata-ku, Kobe-shi, Hyogo-ken, JapanPosts: 71Member ✭✭✭
    Ethereum has two types of accounts. Externally owned accounts (accounts owned by people) and contract accounts which contain the EVM bytecode which are encoded VM instructions that can change its contract storage contents through Ethereum's state transition function. Once you upload a contract's EVM bytecode (which are a set of rules that change the contract's storage data) and it gets included into the blockchain then you can't alter its contents. You can execute the contract it by sending encoded (which contain the pointer to the function in the bytecode that you want to call with the data that you want to pass it.

    Nodes are not "executing" any code like it would an application that is running on your computer. They are verifying blocks which contain transactions which change the state of the Ethereum Virtual Machine. Each new block contains the SHA3 signatures of each transactions' header that are included in it. The transactions change the state of the EVM through the aforementioned state transition function. Every node that is in agreement will have the same EVM state.


    Please refer to the whitepaper or the yellow paper for more details.

    https://github.com/ethereum/wiki/wiki/White-Paper

    http://gavwood.com/paper.pdf
  • ikar0sikar0s Posts: 6Member
    @tym4t +1 for the answer !

    "They are verifying blocks which contain transactions which change the state of the Ethereum Virtual Machine."
  • AjoyBhatiaAjoyBhatia Posts: 7Member
    I do not agree with the following statement from tym4t, but I am still a beginner in Ethereum:
    "Nodes are not "executing" any code like it would an application that is running on your computer. They are verifying blocks which contain transactions which change the state of the Ethereum Virtual Machine."

    Here is another quote from the White Paper that is linked in the same post (from the section: https://github.com/ethereum/wiki/wiki/White-Paper#ethereum-state-transition-function - Step 4 in the State Transition Function):
    "4. Transfer the transaction value from the sender's account to the receiving account. If the receiving account does not yet exist, create it. If the receiving account is a contract, run the contract's code either to completion or until the execution runs out of gas."

    Note - "..., run the contract's code ...". I interpret this as that the code being executed is a part of the block verification process. The post does not explain where the Ethereum Virtual Machine is running. The EVM is what runs the contract code (which is compiled to low-level EVM code) on every node that verifies the block containing a transaction in which the receiving account is a contract.
  • o0ragman0oo0ragman0o Posts: 1,251Member, Moderator mod
    @AjoyBhatia Yes, the EVM (Ethereum Virtual Machine) is run independently on each node. It 'executes code' of pending transactions which may result in change to state. The node can chose which transactions to run from a pool of pending transactions that it sees on the network. Nodes may have different views of the pending transactions at any moment due to different network horizons. Due to these different views and choices, different nodes would produce different blocks if mined at the same moment (A moment here being 'block number', both historic and current). This is the Byzantine problem regarding consensus that blockchain in general attempts to solve.

    When a node sees a new block on the network it must validate that block by rerunning the transactions (again, running code) and verify the hash.
Sign In or Register to comment.