Or in other words how smart contracts are mined?
I need help with filing some blanks to really understand that issue. Let me start with what I know.
The bitcoin approach (level easy)
In the standard blockchain, the miner composes the block of all transactions.
In the high abstract view it could look like this:
[PREVIOUS BLOCK HASH]
addr1 sends X to addr2
addr4 sends Y to addr3
.
.
.
nonce
The miner finds a nonce and publish the result to the network.
Then all other can check if the block is valid. A miner can, of course, ignore it, and mine but risks ending up with a smaller chain that everyone ignores.
The Ethereum approach
It is said that smart contracts are executed on each node, so I imagine it like this:
- each miner get's a list of transactions and smart contract functions call, run those and create a block with results and try to solve it.
- does the miners have deterministic way of choosing transactions/contracts' calls or every miner composes their own mix, and one wins?
- If any creates different mix, so we cant think of each miner as running the same contracts (the order may differ, and the results would have been different, and those which were not solved on time are discarded)
- one of the miner wins, and publishes the block which has transactions and run opcode, so it can be reproduced. The block includes also the results (like storage variables)
- in order to confirm the block, other miners have to get the block, run the opcode, check for results and check the hash if correct
- as I understand this is the part we mean, when we talk that code is run by every node (is that correct?)
I have a feeling I can have some wrong concepts about how it all works, so I need someone to show me where I'm wrong.
Is my idea of and Ethereum block correct (high abstract):
[PREVIOUS BLOCK HASH]
addr1 sends X to addr 2
addr4 calls function foo on contract Z
.
.
.
nonce
(probably the addr4 calls function foo on contract Z
would be expanded to the actual opcode, so there's no room for interpretation anymore by other miners)
Hashing the whole thing gives a special hash, and the rest of the story is like with bitcoin - some other miner could ignore it, but risks being left with a chain that no one else accepts.