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 Answer
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.)
-
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?– SliccDec 23, 2017 at 19:23
-
2The 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.) Dec 24, 2017 at 1:51