2

Can someone tell me where smart contracts are actually executed? Is it on the miners or the nodes? Is it run in all miners/nodes, or just one? Also if I declare a variable where is that state persisted? On the blockchain or other storage?

1

1 Answer 1

6

Every full node (those participating in mining and those not) runs the smart contract code involved in every transaction.

Your second question is answered here: Where is the state data stored?. Essentially, because that state is computed from the transactions that make up the blockchain, that storage doesn't need to itself be part of the actual blockchain structure. But in practice, it doesn't make much of a difference. (Nodes store it, just like they store the blockchain.)

2
  • Thanks, does running on all nodes cause problems with the order in which transactions are received by the contract? If one transaction request adds 10 to a value and a second transaction squares a value, the order these are received will have different outcomes. Or is this type of operation not allowed?
    – Slicc
    Dec 23, 2017 at 19:23
  • 2
    The order of the transactions is determined by the miner who mined the block. All the other nodes are just validating that the block broadcast by the miner is valid, so they process the included transactions in the order determined by the miner. (Additionally, multiple transactions originating from the same address do have a defined order, so they can never appear out of order.)
    – user19510
    Dec 24, 2017 at 1:51

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