This content originally appeared on Level Up Coding - Medium and was authored by Henrique Centieiro
And also the block’s difficulty!
What’s behind all those fields in the Bitcoin block? You probably have already used a block explorer to check the status of a transaction or check wallet transactions or balance (yes, everything is super transparent in the blockchain), but what are all those fields and long numbers, right?
You don’t really need to know what are all those fields and long numbers. Just like an email user doesn't need to know how the internet and email protocols work from the technology perspective, a blockchain or Bitcoin user doesn’t really need to know how does it work to send a transaction. Nevertheless, it’s cool to understand what’s going on under the hood of the blockchain truck!
Firstly, let’s look at how a block is structured in the blockchain and then, we take a deeper look at some of the block’s components.
This is the first Bitcoin block mined in 2021. It was mined 13 minutes after New Year’s Eve. Although we will be looking at a Bitcoin block, most of the blockchains follow very similar principles and have very similar data and fields in their blocks. The Bitcoin block is an excellent place to start studying block architecture. Other Proof of Work blockchains like Dogecoin will use almost the same block structure (basically, Bitcoin is Doge’s grandfather).
You can also check this block or any other block by yourself by visiting one of the many block explorers available online.
Whats’s the meaning behind all the fields in a Bitcoin block structure?
Block height: Also known as block number, it’s basically the block in the blockchain chain until this block, meaning that there were 663852 blocks mined until this block. In Bitcoin, approximately 144 new Bitcoin blocks are mined every day.
Block size: well, the block size is the block size. In this case, the block is around 1.34 MB, and it includes the block header and the transaction information. Each block has a size limit — for example, 1MB, 1.3MB or 8MB depending on the blockchain — and the number of transactions that a block can include is a constraint to the block size.
Block hash: the block hash is the output of the block’s header data hashed through SHA-256 function.
Version: it gives information about the version of the blockchain.
hashMerckeRoot: You may have heard about the Merkle Root and Merkle Tree…. but what is this Merkle thing? The Merkle Root is like a fingerprint of all the transactions in the block. It comes from the Merkle Tree. They are widely used in cryptography, and it was used before blockchains were invented. Merkle Trees were named after Ralph Merkle in 1979. They are a basic method to verify that data shared was not changed, damaged or altered.
The Merkle Tree is also a lightweight way to verify the validity of the data in a blockchain. For example, if you are using a Bitcoin wallet on your mobile phone, your wallet doesn’t need to download all the blockchain transactions to validate them. The Merkle Tree with its Merkle Root will do the work of validating all the previous transactions.
Structure of the Merkle Tree: The Merkle Tree result or Merkle Root is built recursively, i.e. it results from the hash of the leaves in the Merkle Tree, which in turn correspond to transactions.
The Merkle Root is solved by hashing individual transactions, which are called leaves. Additional pairs of leaves are hashed to create additional leaf nodes corresponding to transactions in the blockchain network.
Although a Merkle Tree can be created from a large number of transactions, the Merkle Root always corresponds to a 32-bytes string or 256-bit (SHA256 hashing algorithm, for instance, always outputs a fixed length of 32-bytes, regardless of the size of the input). This way, we can use something as small as 32-bytes to verify that a number of transactions — perhaps thousands — are immutable.
Number of transactions: the number of transactions varies from block to block. It may depend on the block’s maximum capacity in terms of size, how big the transactions are (not in value but how many bytes) and the actual volume of transactions issued by the users. For example, a Bitcoin block can fit up to 1.3MB of data, and the transaction size is on average 500 bytes.
Block reward: the Bitcoin block reward is at the moment 6.25 BTC. This is the miners’ reward as an incentive for them to contribute to the network and create blocks. The block reward started at 50. BTC per block in 2009, and it’s halved every four years or, to be more accurate, the halving occurs every 210,000 blocks. Because of the halving, the block reward will get to zero in 2140.
The reward is contained in each block’s Coinbase transaction, a unique transaction created to pay the block reward. For the block in this example, the 6.25 BTC reward would be equivalent to approximately USD 200 000 00 at June 2021 rates. Not bad, huh?!
Fun fact (especially for those who did it): back in 2009, you could mine bitcoin blocks with your personal computer. If you have mined one single block, the 50 BTC reward would in 2021 be worth 2.3 Million dollars.
Timestamp: The timestamp is the exact time when the hashing of the block took place. Usually, block explorers show the timestamp in human-readable format, but the block's timestamp is actually written in Unix time format.
Transactions: depending on the protocol, transactions can be a transfer of value or any event, i.e. doesn’t need necessarily to be a payment. It can instead be merely a transfer of data. By the way, do you know that some transactions, like the coinbase transaction of the first block, contain a hidden message?
hashPrevBlock, AKA Previous block hash: each block contains the hash of the previous block. This is the way blocks are interconnected in an immutable way. Each block has the hash of the block that comes before. Data in previous blocks cannot be altered because that would change the already recorded block hash.
Bits: it represents the difficulty of the block. It’s represented in hexadecimal notation, and it represents the current target for the difficulty in the block.
Difficulty: the difficulty is determined by the number of zeros that the hash needs to begin with when the hashing process occurs. The genesis block (the first block) had a difficulty=1. The block that we saw at the beginning of this chapter has a difficulty of 18,599,593,048,299.49. The difficulty is always in proportion to the number of miners (i.e. hashing power) in the network. It increases when more miners join the network and decreases when miners leave the network. Miners may join or leave the network according to the mining profitability, which is usually related to the Bitcoin price.
The Bitcoin mining difficulty is actually very similar to the gold mining difficulty. When the gold price goes up, more people and companies usually start mining gold, increasing the competition and increasing the difficulty of finding gold. An excellent example of this was the California Gold Rush. San Francisco grew from barely 100 prospectors in 1846 to more than 30 000 in 1852, while the gold price was also rising. Most of the prospectors were not very lucky in finding gold because the competition made it so difficult. During this period, the people who made most of the money were actually selling mining equipment. A shovel used to be sold for $36, which would be the equivalent to more than $1500 now.
Modern miners, the crypto miners, in this case, join mining pools in order to share efforts and increase profitability.
Let’s go back to the present and dig more in-depth into the difficulty, shall we, dear reader?
You have probably noticed that the block hash starts with several zeros. In the case of our block, it starts with 19 zeros but… why? Why does it need to start with 19 zeros? Where does this come from? Well, it comes from the Bits field and the difficulty. Let’s see how to calculate this in a simplified way.
D = expected/actual
D here would be the difficulty. What is the “expected” value? The Bitcoin protocol expects to add 2016 new blocks in 20160 minutes. This is an average of one block every 10 minutes.
The difficulty re-evaluation occurs every 20160 minutes (approximately 2 weeks), and the expected value is 20160 minutes. Actual = the time it took to mine the last 2016 blocks.
D = 20160/actual
If D > 1, it took less than 20160 min. for the miners to mine 2016 blocks, the difficulty will increase. If D < 1, it means that it took more than 20160 to mine 2016 blocks, meaning that the difficulty needs to decrease because it’s a bit too difficult.
If D > 1
Difficulty increase (0.25, 4)
If D < 1
Difficulty decrease (0.25, 4)
The difficulty increases/decreases at least 0.25 or a maximum of 4, depending on how the difficulty is adjusted.
Now the new difficulty that is going to be calculated every two weeks will be:
newDiff = oldDiff x 20160/actual
Mining difficulty increases according to the hash power in the network to keep the average time of 10 minutes to mine one block. As you can see in the chart, the difficulty will increase or decrease every two weeks based on the average time the blocks were added, and it’s pretty much correlated with the mining power in the network (left y-axis).
Aight! I hope it was useful! If you have any questions, please let me know in the comment section!
? Follow me, and please also check my ? blockchain courses:
? The First Ever Dogecoin Course
?? Fintech, Cloud and Cybersecurity Course
?? Unblockchain Course — The Brain-Friendly Blockchain Course
A Complete Decoding of the Bitcoin Block was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Henrique Centieiro
Henrique Centieiro | Sciencx (2021-06-07T15:48:13+00:00) A Complete Decoding of the Bitcoin Block. Retrieved from https://www.scien.cx/2021/06/07/a-complete-decoding-of-the-bitcoin-block/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.