Note
If you are new to the field of information technology, taking our Free Information Technology Course for Beginners and Cloud Technology Course for Free with Certification is highly recommended. Also, for a comprehensive coverage of blockchain concepts and its use cases, taking our Free Blockchain Course with Certification is highly recommended.
Recap
In our previous article (How Decentralized Peer-To-Peer Network Works), we learned about how decentralized peer-to-peer networks work and in what ways they outperform traditional centralized networks.
In this article, we discuss how a new block of data is added to the blockchain system.
One-to-One Live Blockchain Classes
Coding Bootcamps school offers One-to-One Live Blockchain Classes for Beginners.
How does blockchain work?
Using the previous example, as shown in the following diagram, let’s assume Alice wants to buy something from Bob and she agrees to pay Bob $10 or 10 Bitcoins (BTC):
Let’s walk through the high-level processes step-by-step, demonstrating how the blockchain works:
- Create blockchain transactions: A transaction is a value transfer between two parties. When Alice sends $10 or 10 BTC to Bob, it will create a transaction with one or more inputs and two or more outputs, where the inputs reflect Alice’s account, and the outputs reflect which account(s) Alice intends to transfer to. The transaction is then digitally signed with Alice’s private key and broadcasted to the P2P network. The receiver will use the digital signature to verify the ownership of Alice’s funds. We will discuss digital signatures and cryptographic hash functions in detail in later articles.
- Validate the transactions and add to the transaction pool: Once the transaction is submitted to the blockchain network, the bookkeeper node (usually a full node in a P2P network that receives the transactions) will validate it according to the business and technical rules defined by the blockchain network. If the transaction is valid, the bookkeeper will add it to the transaction pool and relay the transaction to the peers in the network.
- Create the candidate blocks: Transactions in the transaction pool are collected into the block periodically. In a Bitcoin network, every 10 minutes, a subset of network nodes, called mining nodes or miners, will collect all valid transactions from the transaction pool and create the candidate blocks. The following diagram shows the structure of a candidate block:
As illustrated in the previous diagram, the high-level processes are as follows:
- The candidate block packages recent valid transactions into the block structure based on block specifications.
- For each transaction in the package, it creates a cryptographic hash of the transaction data, recursively calculates the hash out of existing hashes, and creates a Merkle root of all transactions, as depicted in the following diagram:
The miner node looks for the latest block on the blockchain and adds its hash to the block header of the candidate block as the reference from the candidate block it intends to link to.
- Mine the new block: Once the candidate block is created, the race starts for the chance to add new blocks and win the rewards. The process for such a race is called mining. The winning of the race is determined by the consensus mechanism. We will discuss different consensus mechanisms in later articles. In blockchain systems such as Bitcoin or Ethereum, the PoW consensus mechanism is applied to mining. Miners will keep trying to find a random number, the nonce in the block header structure, until the hash meets certain challenging conditions. For example, one such challenging condition is, the resulting block hash is smaller than a target number, or in some cases, the hash has a few leading zeros. In practice, every random number has the same chance to win the race, so practically, you can just start a loop through from 1 to 232 until it finds such a nonce. It requires huge CPU hashing power to find such a nonce. The challenging condition, called difficulty, can be adjusted based on the target number or bits in the block header structure. The difficulty in winning the race grows exponentially the smaller the target number is or the fewer bits are in the block header structure.
- Add a new block to the blockchain: The first winning node will announce the new block to the rest of the network for verification. Once the block is verified and approved by the majority of the network miners, it will be accepted and becomes the new tip of the chain. Since all blocks are chained together by linking the hash to the previous block, any tampering with the ledger becomes impossible since it will require PoW on all previous transitions.
All miners have the chance to solve the puzzle, but only the winning miner has the authority to add the block to the chains and claim the bounty. Once the new block is added to the blockchain, all in-progress miners will stop their mining efforts on the newly added block and start the race again on a new block.
The following diagram summarizes the step-by-step process when new transactions are submitted to the blockchain network:
Now you know how blockchain works. Cryptography plays a critical role in maintaining the transaction state in the blockchain and ensuring immutability. Cryptography is not new.
Next Article
In the next article (How Public and Private Keys of Cryptography Work), we move on discussing how cryptography works and what role it plays in a blockchain system. We also go over differences between public and private keys of cryptography.
This article is written in collaboration with Brian Wu who is a leading author of “Learn Ethereum: Build your own decentralized applications with Ethereum and smart contracts” book. He has written 7 books on blockchain development.
Resources
coming soon