4

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.

5

You're mostly correct. Here's the actual format of a block: enter image description here

Ethereum is a little more unsure of what belongs in a block. Generally, when doing a regular full sync, the blocks you download are what above is shown as the header, and the transactions that belong in the transaction root. You then run all the transactions and generate your own state that should, after running all transactions, belong to the state root of the latest block.

If you do a fast sync, you'll download all block headers, as well as the transactions for the last x blocks (I think Geth does 256 or 1024), and also the full state at the last block that you don't download so that you have a state to run the transactions downloaded on.

This all means that the state isn't actually included in the block, and all miners and nodes have to run all transactions to verify the blockchain and get the latest state. Of course nodes (like fast sync and light clients) can download a more recent state tree if they want to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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