[Ethereum] When is a new block created

blockchainblocksminingproof-of-work

I am currently reading an interesting book about solidity, and I'm reading about block creation. This gives a good overview:

enter image description here

The book mentions that the maximum size of a block is determined by the upper gas limit and block size.

I am a bit confused as for when a new block needs to be created.
In another example, they give an example in which the gas value does not even reach the gasUsed property of a block doesn't even come close to the gasLimit.

So in the example added, Sam wants to add $$ to Mark. But it isn't clear to me how it is decided that a new block needs to be created.
I mean, a block consists of multiple transactions, so why wouldn't this transaction just be appended to an already existing block?

Related, everywhere I read that a new block gets created by the miner who 'solves the puzzle' based on a changing nonce. So who creates this puzzle?
Also related, they mention the 'genesis' block, which is the first block in a blockchain, but there is no puzzle for this, so who creates the first block? And in a blockchain, how is it decided that we need a new genesis block? Is the ' Ethereum blockchain' therefore a combination of blockchains (each having their own genesis block)?

I know, quite a few questions…
If you prefer me to split up these questions, I will. But as they're closely related, I would have hoped that a single answer could clarify these questions at once.

Best Answer

Answering your questions in a bit of a different order. I think I covered everything, but feel free to ask follow-ups in the comments.

  1. The genesis block is simply agreed upon, and there's only ever one. Anyone working from the same genesis block is part of the same blockchain.
  2. To add a block to the chain, miners "solve a puzzle" as you said. This is called mining. They construct a block out of some set of transactions. It's totally up to them, but they typically use all the transactions they know about that will fit. This new block has a bunch of headers, including a reference to the previous block in the chain. They then try to find a nonce such that hashing that nonce together with all the other headers yields a value smaller than a difficulty threshold.
  3. Once a miner finds a valid nonce, they have mined a new block. This block is sent out to the rest of the network. Now that there's a new block, everyone switches to mining on top of that. So "Who creates the puzzle?" is sort of "everyone". The puzzle is defined by the hash of the previous block and the contents of the new block being mined.
  4. The answer to why not just add a transaction to an existing block is that blocks are immutable. Changing one would mean changing its hash, and that would mean it would have to be mined again. A miner could try to do that, but in the meantime, all the other miners have moved on to the next block, so they will have a longer blockchain, and the longest blockchain is always the "correct" blockchain.

It should be noted that the difficulty is automatically adjusted according to a formula every so often with the goal being that blocks are mined at a certain pace. If blocks are being mined too fast, then the difficulty is adjusted to make mining harder and vice versa.

Related Topic