pBitcoin QA: Empty blocks, orphan blocks, and valid chainsp p Are the miners simultaneously verifying the solution of the previous blocks cryptographic function while competing to find the puzzle solution for the next new block? Yes, they are.
It takes about 1520 seconds at least to validate a block. Depending on how complicated the block is, how complicated the scripts in the block are, some blocks may take less than a second. Other blocks take a lot longer because they may contain a lot of complex scripts in them, a lot of signature operations, etc. While miners are validating the block, they generally start mining immediately. Validationless mining, as its called.
They start mining the next block without validation. However, in order to do that they must start by mining a new block that is empty. The reason for that is because theyre going to be expending all of this energy trying to find a proofofwork.
They cant include any transactions in the new block because they havent yet validated the previous block and therefore they dont know which transactions may have already been included and which ones have to still be included. If they mine a block with a transaction in it, and theres a possibility that transaction was already included in the previous block. If that block is valid, then all the energy they put into mining the new block is wasted because that block is then a doublespend and therefore invalid. The safest way to ensure that the block theyre mining while theyre still validating the previous block is itself valid, is to make it an empty block which is why you see empty blocks being mined.
The thing is, because this is a probabilistic game, it doesnt make any difference if you change the block youre mining halfway through or thirty seconds into it. Usually what would happen from a miner strategy is that while theyre validating the previous block, they mine an empty block. As soon as the previous block has been validated, and the mempool has been cleared out and reconciled with what transactions are still available, then they can create a new candidate block that contains transactions, so they can get the extra profit from the fees. And then start mining against that, to put energy in. It doesnt make any difference, because each attempt to find a hash is probabilistically distinct.
It has exactly the same probability as the previous event, whether you change the underlying block or not, whether you started the proofofwork nonce from the beginning or not. It doesnt make any difference. You have just as much chance at finding valid proofofwork in the very next hash as you did in the previous one. The probability doesnt change.
Therefore, miners will start mining an empty block and then thirty seconds in once theyve validated the previous block they can add some transactions, and theyre going to just switch to mining a block with transactions in it and continue their proofofwork. How do you stop miners from creating empty blocks when they create a regular one? Well, you dont.
In fact, you dont want to. Theres no reason to stop miners from creating empty blocks. If a miner creates an empty block, that doesnt reduce the capacity of the network. Thats really important to understand.
If a miner creates an empty block, that doesnt reduce for anybody else the probability of finding a block immediately afterwards. It increases the security of the network with every block mined, regardless of whether it is empty or not. If you have a block that is found now, that doesnt change the probability of finding a block in the next second.
These are probabilistically independent. Miners creating empty blocks simply forces everyone to restart their calculation but doesnt change the probability that they will find a block within the next second. In general, you should think of empty blocks as adding to the cumulative proofofwork and difficulty of a chain, and therefore increasing its security and increasing its immutability.
Theres also good reasons why miners create empty blocks. When a miner first receives a winning block from the network, they have to validate it, validate every transaction within that block, remove those transactions from the mempool, and create a new candidate block. That takes time it takes a few seconds, maybe just a few hundred milliseconds.
But in mining, a few hundred milliseconds is time. Why would you stop mining? You dont stop mining. What miners do is they start mining for an empty candidate block while theyre doing the validation. If they find one during that time, theyll publish it and continue to do the validation for a block that has transactions.
They cant put transactions inside a block until they have validated all of the previous blocks transactions, to ensure theyre not doublespending. Otherwise theyre wasting their mining power. Thats one of the reasons why empty blocks get created. Empty blocks are not a bad thing theyre part of the protocol. What happens if two miners solve the next block at the same time?
BTC clients trust the longest chain. If two blocks are mined at the same time, its probably up to the miners to decide which is going to be accepted. But how do they decide ? This is the fundamental aspect of mining. Were going to talk about it in the future sessions when we talk more specifically about mining and forks, but for the time being let me just . Approximately once every week, two blocks are mined nearly at the same time.
Miners and nodes decide which one to accept based on which one they see and validate first. This may cause the network to have two different perspectives on which is the next correct, longest chain. That is a fork of sorts, it happens once a week on average, and it is resolved as long as the next block that comes in doesnt also come in simultaneously which is a very low probability. Eventually, that fork is resolved because one of the two chains gets extended first the other one has its tip orphaned.
The block is no longer considered valid, all of the transactions are replayed. In the competition, it is normal for blocks to be mined nearly at the same time. The winner is the one who remains on a chain that gets extended in the next round.
Is a satoshi the smallest unit of bitcoin that can be mined and tracked? Yes. A satoshi is the smallest unit of bitcoin that can be mined and tracked.
Let me be more precise though: a satoshi is the only unit of bitcoin that can be mined and tracked. On the Bitcoin network, there is only one unit and thats satoshis. Everything is denominated in satoshis.
When your wallet shows you, all it is doing is taking the number of satoshis and dividing by 100 million. Everything on Bitcoin is in satoshis. There is no other unit, just satoshis. Where is the history of each bitcoin fraction stored and how can it be traced on a blockchain?
The blockchain itself is a history of every bitcoin fraction. Every transaction takes a bitcoin fraction or more than one bitcoin fraction as its input, and produces one or more bitcoin fractions as its output. Therefore, the blockchain is a history of the fractions of bitcoin moving from one owner to the next.
When calculating your total amount of coin balance, must your wallet go through the entire blockchain from the genesis block? What are the calculations on the blockchain the wallet must make? What verification does it perform?
Are there any shortcuts to doing this? If you are running a wallet that is a fullnode wallet, in order for it to calculate your balance and to do that authoritatively, you have to process the entire blockchain from the genesis block. That doesnt mean you have to store it, but you have to at least process and validate everything once. Your wallet will have to go through the entire blockchain, which means downloading the entire blockchain, even if youre not storing the entire blockchain.
The calculations the wallet does looking at each block and looking for any addresses that it knows it controls, from the private keys it has. Its looking for any transactions involving those addresses. Its tracking unspent transaction outputs, or UTXOs which are basically coins, and adding them up. When theyre spent, it removes them from the calculation. When new ones are created with your addresses, then it adds them to your balance calculation until it reaches the end of the blockchain.
Now it has an accurate picture of your balance. Are there any shortcuts to doing this? Yes.
You can use what is known as Simplified Payment Verification SPV or lightweight wallets. What they do is ask a fullnode, validating the answer by looking for proofofwork and the position of the relevant transactions in the Merkle tree. Finally, the least preferred option which is the ultimate shortcut is to use a block explorer or website, and ask it what your balance is by typing in your addresses. This reduces your privacy, but it also means that you have no idea if that block explorer is telling you the truth. Youre taking a thirdpartys word for what your balance is.
That breaks the security model of a decentralized cryptocurrency. An astute reader might say, But you have not yet solved the Byzantine Generals Problem, just moved it to the miners. What if two miners send out blocks with conflicting information?
How do the clients choose? Again, as I said before, they choose based on their own appreciation of the consensus rules. If both blocks are fully valid by the consensus rules, by all of the clients, then timing matters. The first block to be seen in each part of the network gets chosen. If more than one block is seen in more than one part of the network, then the network finds itself with two competing realities.
The next block will resolve that, because the longest chain is something that evolves over time. When the next block is found, it will settle the dispute between the two competing versions of reality by making one of those chains the one it builds upon longer. The longest cumulative difficulty, valid chain wins, but each one of those components is important. Longest. Cumulative difficulty.
Valid chain. Which chain is valid? Whichever one your node thinks is valid. If the network is big enough, thats the valid chain.
Which is the longest? The one that has the most proofofwork in it. These things all work together. The Byzantine Generals Problem is not solved in one place its solved through the collaborative action of every participant in this system who has a small role to play. All of these add up.
Is the longest chain the chain with the most blocks? No. It was briefly, but the algorithm was changed very early on, during the very early experimental days of Bitcoin.
But the correct definition of which chain wins in Bitcoin, is the greatest cumulative difficulty, valid chain. The longest does not mean the most blocks. It means most difficulty.
How do you do that? How do you calculate most difficulty? You add up the amount of proofofwork that has existed.
You add up, for each block, how much difficulty is expressed by the proofofwork how many bits of difficulty are involved in the proofofwork for that particular block. The cumulative difficulty is the measure. The next word thats really important, valid chain, means that the chain is valid because every block in it is valid and the blocks are valid because every transaction in them is valid. It doesnt matter if you have the greatest cumulative difficulty if one of your transactions or blocks is invalid.
Its only in the context of the chain being valid, under the consensus rules, that you win. To answer : the longest chain is a short way of saying longest cumulative difficulty, valid chain.p