Bitcoin’s implementation of Blockchain
We have already covered the basics of Blockchain in our previous post. Blockchain is a digital distributed ledger where data in each block is immutable and the blocks are ordered by timestamp. Today, the concept of Blockchain is being applied to not just hundreds of crypto-currencies but also to many use-cases in the Banking and Finance industry and each has its own implementation. In fact Blockchain has been extended to every industry -supply chain management, real estate, retail to name a few. In my opinion, one should always start with Bitcoin, to learn a practical implementation of Blockchain. Bitcoin is the first and time-tested implementation used by millions. Rest all blockchain implementations are still in proof of concept phase.
Bitcoin’s Block Structure
To learn more on Bitcoin read one of my initial post. Every block in the Bitcoin network has the exact same structure as per the above diagram. Each newly created block is ‘chained’ to the last added block of the blockchain and stores its digital finger print. Let us examine the fields of a Block —
- Magic number (4 bytes): This is an identifier for the Blockchain network. It has a constant value of 0xD9B4BEF9. It indicates a) Start of the block b) Data is from production network. You can read more on this concept on wiki.
- Block size(4 bytes): Indicates how large the block is. Since the very beginning till as of today (Dec 2016) each block is fixed to 1 MB. However a proposal might soon have the consensus of the core development team (who can change protocol rules) and this will be increased to 2 MB. The maximum capacity is 2 GB so scalability factor has already been taken care of.
- Version (4 bytes): Each node running the Bitcoin protocol has to implement the same version and it is mentioned in this field.
- Previous block hash (32 bytes): This is a digital fingerprint (hash) of the block header of the previous (last added) block of the blockchain. It is calculated by taking all the fields of the header (version, nonce etc) together and applying a cryptographic function (SHA-256) twice by rearranging the bytes of the individual fields (Little-endian format). You can check the technical details at Bitcoin wiki.
- Merkle Root (32 bytes): We will see this in the next section of the blog post
- Timestamp (4 bytes), Difficulty Target (4 bytes), Nonce (4 bytes): We will see them in the next article on Bitcoin mining
The Block header is composed of the fields from Version to Nonce
- Transaction Counter (Variable: 1–9 bytes) : This is the count of transactions that are included with the block
- Transaction List (Variable: Total block size is 1 MB): Stores the digital finger-print of all the transactions in that block. Each individual transaction has its own structure but we will cover Bitcoin Transactions as a separate topic in future.
You can check the fields mentioned in human readable format for a random block (# 442424 where 442424 is the height of the block aka count of block since the first block was created) and genesis block (i.e the first block that was mined)
Merkle Root
As we have already seen in the above section, each block contains a list of summary of all transactions. Once the block is part of the blockchain it is an immutable record i.e the transaction entry in it is permanent. It also means that if one transaction is present in one block it will not be present in any other block of the blockchain. The transactions are listed as merkle tree or a binary hash tree. It is a very popular data structure used in programming languages.
The root of the tree is the topmost node and hence this tree is represented upside down. The bottommost nodes are called as leaf nodes. Each node is simply a cryptographic hash of a transaction. In the above diagram, Transaction A,B,C,C form the leaves of the tree.
You might be wondering that why is Transaction C repeated. Well, 2 consecutive nodes form 1 parent node. Hence the total number of leaves should always be even and if that is not the case then the last leaf is repeated twice.
The merkle tree does not contain a list of all the transactions, rather a hash (digital fingerprint) of all transactions as a tree structure.
Hash of Transaction A = Hash[Tx(A)] = SHA256 (SHA 256 (Transaction A))
Each hash is calculated by applying the SHA256 algorithm twice.
Similarly to construct a parent node Hash(AB). The 32 byte Hash[Tx(A)] and 32 byte Hash[Tx(B)] is concatenated as a 64 byte hash string and then SHA256 is applied twice to give a 32 byte Hash(AB).
This concept can be further expanded to any size. The biggest advantage is that it is very easy and highly efficient to determine whether a particular transaction has been included within a block (since the block contains the merkle root — which is digital fingerprint of all transactions contained in it)
What is SHA256?
SHA stands for Secure Hash Algorithm. This is used to prove data integrity. The same input(s) will always produce the exact same output. This output is always 256 bits or 32 bytes in length regardless of the length of the input (even if input is millions of bytes).
As an analogy consider this. We know that 2 atoms of Hydrogen and 1 atom of Oxygen always gives one molecule of water. Now assume that from different chemical processes, we obtain 2 atoms of Hydrogen and 1 atom of Oxygen separately. If we have to prove that indeed we have obtained the expected result then we just have to mix the atoms and if we get 1 molecule of water, then our results were right.
Any change in the input(s) will result in a change of output. The same output can never be derived from different input(s).
However, from the output we can never determine the inputs. That is why this is the highly secure. As an example consider you have 3 jars of paint — red, blue and green. The combination of them will create countless colors. However, if you have been given single jar which has a color formed after mixing the 3 jars then there is no way you can find out the exact proportion of red, blue and green by just looking at it.
You can test it yourself at a few online SHA-256 tools. (Link 1 and Link 2) The input can be any string (which you have obtained from even concatenating many strings). Even if you copy-paste the entire of Bible, still the output remains 256 bits. Few examples —
Bitcoin’s Blockchain
I have been stressing on how Bitcoin implements Blockchain. The reason is that the concepts of Blockchain can be implemented in different ways. Blockchain implementations can be categorized based on many parameters some of which are — consensus mechanism (i.e how non-trusted peers validate each other), public/private, mining involved (Yes/No), permission (anyone can join Y/N), authority (all peers have same level of authority) etc.
Bitcoin has its own implementation method.
We have already covered the structure of each individual Block in the Bitcoin Blockchain. The very first block created is called as the Genesis block. You can find its details here. The field of ‘previous block hash’ is 32 byte string of zeros. It has just 1 transaction. However, as the blocks continue to add up one by one the transaction list will increase till it reaches the maximum size of 1 MB. Then all blocks will have variable transactions but approximately block size will be 1 MB. Every time a new block is generated then it is appended to the last added block of the blockchain. Of course a crucial step of ‘Mining’ is still pending and we will cover it in our next blog.
Forking
There always exists one and only one path from the last added block to the genesis block (first block) in the network. The reverse is also true, but multiple paths exists of which only 1 is valid. When 2 blocks are created around the exact same time then only 1 is accepted by the network . In the above figure, Block with height = 2 has 2 children. But finally only 1 is accepted. So a block will always have only 1 parent but may have multiple children (temporarily). Eventually ,the ‘orphan’ blocks are found out and its transactions are picked up for another block and hence each transaction gets a chance to be included in the network.
If you are an enthusiast in Bitcoin, Distributed Ledgers/ Blockchain and similar crypto-technologies then do join my Signal group, where we as a community, will answer all your questions. Join by clicking here
I will definitely reply to your comments and clarifications. Do share your thoughts 🙂
Источник
Block
Enjoyed the article? Share:
Block is a permanently recorded file at Bitcoin containing information on occurred transactions. Block is the record of the every recent transaction or its part that has not been recorded in the previous blocks. Practically in all cases blocks are added to the end of the chain, which contains all transactions and is called blockchain. When a block is added to the end of the chain, it cannot be changed. Each block contains information about everything that happened in previous blocks before it was created.
Contents
Transaction data is permanently recorded in files called blocks. They can be thought of as the individual pages of a city recorder’s recordbook (where changes to title to real estate are recorded) or a stock transaction ledger. Blocks are organized into a linear sequence over time (also known as the blockchain). New transactions are constantly being processes by miners into new blocks which are added to the end of the chain and can never be changed or removed once accepted by the network (although some software will remove orphaned blocks).
Block Review [ edit ]
Each block contains, among other things, a record of some or all recent transactions, and a reference to the block that came immediately before it. It also contains an answer to a difficult-to-solve mathematical puzzle — the answer to which is unique to each block. New blocks cannot be submitted to the network without the correct answer — the process of «mining» is essentially the process of competing to be the next to find the answer that «solves» the current block. The mathematical problem in each block is extremely difficult to solve, but once a valid solution is found, it is very easy for the rest of the network to confirm that the solution is correct. There are multiple valid solutions for any given block — only one of the solutions needs to be found for the block to be solved.
Block reward [ edit ]
Because there is a reward of brand new bitcoins for solving each block, every block also contains a record of which Bitcoin addresses or scripts are entitled to receive the reward. This record is known as a generation transaction, or a coinbase transaction, and is always the first transaction appearing in every block. The number of Bitcoins generated per block starts at 50 and is halved every 210,000 blocks (about four years).
Block generation [ edit ]
Bitcoin transactions are broadcast to the network by the sender, and all peers trying to solve blocks collect the transaction records and add them to the block they are working to solve. Miners get incentive to include transactions in their blocks because of attached transaction fees.
The difficulty of the mathematical problem is automatically adjusted by the network, such that it targets a goal of solving an average of 6 blocks per hour. Every 2016 blocks (solved in about two weeks), all Bitcoin clients compare the actual number created with this goal and modify the target by the percentage that it varied. The network comes to a consensus and automatically increases (or decreases) the difficulty of generating blocks.
Because each block contains a reference to the prior block, the collection of all blocks in existence can be said to form a chain. However, it’s possible for the chain to have temporary splits — for example, if two miners arrive at two different valid solutions for the same block at the same time, unbeknownst to one another. The peer-to-peer network is designed to resolve these splits within a short period of time, so that only one branch of the chain survives.
The client accepts the ‘longest’ chain of blocks as valid. The ‘length’ of the entire block chain refers to the chain with the most combined difficulty, not the one with the most blocks. This prevents someone from forking the chain and creating a large number of low-difficulty blocks, and having it accepted by the network as ‘longest’.
Bitcoin block structure [ edit ]
Field | Description | Bitcoin Block Size |
---|---|---|
Magic no. | Value is always 0xD9B4BEF9 | 4 bytes |
Blocksize | The bytes number should be added to the end of the block | 4 bytes |
Blockheader | Consists of 6 components | 80 bytes |
Transaction counter | Positive integer VI = VarInt | 1-9 bytes |
Transactions | Transaction list | Multiple transactions |
Complexity [ edit ]
The complexity of the task is regulated by Bitcoin so 6 blocks could be found per hour on average (1 block within 10 minutes). Every 2016 blocks (approximately every 2 weeks) the complexity in the network is changing — every Bitcoin client compares how much faster (slower) blocks have been found in comparison with standard values and, based on these data, the complexity is regulated to higher (lower) side.
Fork [ edit ]
Since each block has a reference in its heading to the previous one, we can say that they form a chain. There is a possibility that the chain can be divided, for example, if 2 miners simultaneously find a solution for a new block. Bitcoin system is configured to solve such chain branching as soon as possible, leaving only one branch. Reward for the block, which has been abandoned, is not charged.
Bitcoin client perceives the longest blockchain as the right one. «Length» of the entire blockchain is calculated as the sum of the complexities of all blocks in the chain, but not the sum of the blocks. Such calculation method does not allow anyone to make the «master» chain with a large number of blocks with low difficulty that prevents transactions forgery.
Blocks FAQ [ edit ]
How many blocks are there?
Current block count
What is the maximum number of blocks?
There is no maximum number, blocks just keep getting added to the end of the chain at an average rate of one every 10 minutes.
Even when all 21 million coins have been generated?
Yes. The blocks are for proving that transactions existed at a bitcoin block time. Transactions will still occur once all the coins have been generated, so blocks will still be created as long as people are trading Bitcoins.
How long will it take me to generate a block?
No one can say exactly. There is a generation calculator that will tell you how long it might take.
What if I’m 1% towards calculating a block and.
There’s no such thing as being 1% towards solving a block. You don’t make progress towards solving it. After working on it for 24 hours, your chances of solving it are equal to what your chances were at the start or at any moment. Believing otherwise is what’s known as the Gambler’s fallacy.
It’s like trying to flip 53 coins at once and have them all come up heads. Each time you try, your chances of success are the same.
Where can I find more technical detail?
There is more technical detail on the block hashing algorithm page.
Источник