1) Both miners included the same transaction from their mempool, both mind the block in the same time, both blocks are on blockchain (until one block "dies").
So basically, I can create two transactions, one that references "first" duplicate transaction, and the second that references the "second"? (in theory of course)
It's called a block CHAIN: there can only be ONE valid chain.
The other chain will become orphaned (the chain with less proof of work)
That's why you're told to wait for x confirmations (usually 6),
A "confirmation" means that a miner has built upon the tip of that particular block, and the more confirmations a transaction has, the more secure it is.
After a few confirmations you can be rest assured that there was no double spend.
2) I could "spend" first transaction, and that block could be cancelled (because it ends up in the "dead chain) and my transaction "terminated" - but I already received goods for that transaction and the "retailer" lost his money? Right or wrong?
That's the thing though; you can't know which block will be orphaned unless you're the miner that mined (both) the transaction.
When a block is orphaned, its transactions will be anulled and added back to "the" mempool of unconfirmed transactions, and since there's a conflicting UTXO that has been spent, that transaction will never make it into a block.
So it's equally possible that in the event of a reorg, that your payment to the merchant will be in the valid block, and your double spend attempt will be orphaned.
But theoretically, if your double spend transaction was included in the valid block and the other transaction that paid the retailer was in the orphaned block and the retailer already released the goods, then yes, the retailer lost the bitcoin you were supposed to pay.
Which is why you're required to wait for a certain amount of confirmations before you accept a transaction as valid, depending on your risk exposure -- or aversion.
The higher the risk, the more confirmations you need to wait for.
A merchant selling gift cards or so can even accept 0 confirmations even though it isn't secure because his risk isn't high.
However, someone selling a house worth millions of dollars can choose to wait for 144 blocks (~ 1 day) because the probability of a chain reorg that deep is negligible.
Although there isn't deterministic finality in bitcoin transactions, the probability of a chain reorg or rewrite reduces exponentially as the number of confirmations increase.
3) When does the miner get the fees for including transactions in the block (output-input)? Is there a different "maturity" for transactions or it is the same as for Block?
I am asking in regards if he collects the fee right away, and that block gets "killed" and transactions returned back into "pool", someone has to pay the second miner who will include that transaction again in his pool.
The maturation time applies to the block reward; the block reward includes the block subsidy(the newly minted coins) and transaction fees.
One other thing I missed on google (nobody shows that example), where is the transaction for "fee"? Is it included in the coinbase transaction (reward for block + sum of all fees for transactions in the block) or are transactions fee separate? If they are separate who will create a "record" about that fee (it has to be written as a transaction so that the miner can reference it when he wants to spend it)
Big thank you once again for all your efforts to go in this border line cases with me
KR
The Coinbase transaction includes the transaction fees for all the transactions in the block, plus the block subsidy.
However, if a miner mines an empty block (a block without any other transactions apart from the Coinbase transaction -- for a block to be valid, the Coinbase transaction must always be present), then the Coinbase transaction contains only the block subsidy.
For example
in this block you can see that the block had fees of 0.4739
BTC and if you check the
Coinbase transaction you can see that the output of the transaction is 12.9739
BTC which is the block subsidy of 12.5
BTC + the total transaction fees of ~0.4739
BTCCompare it to the
first block that Satoshi mined that had no other transactions.
You can clearly see that the
Coinbase transaction only has an output of 50BTC (which was the block subsidy at that time)