How Does the Blockchain Work?
A blockchain is a distributed database that stores information electronically in a digital format and is shared among the nodes of a computer network. A typical difference between a blockchain and a database is how data is structured. A blockchain is a shared, immutable ledger as the name suggests structures data into chunks or blocks, and a database structures data into tables. A blockchain is a chain of blocks. Once a block is filled with data and it is chained to the previous blocks. Different types of information can be stored on the blockchain network but the most important is transactions.
How Does a Blockchain Work?
The transaction process in a blockchain can be summarized as follows:
1. Facilitating a transaction: A new transaction enters the blockchain network. All the information that needs to be transmitted is doubly encrypted using public and private keys.
2. Verification of transaction: The transaction is then transmitted to the network of peer-to-peer computers distributed across the world. All the nodes on the network will check for the validity of the transaction like if a sufficient balance is available for carrying out the transaction.
3. Formation of a new block: In a typical blockchain network there are many nodes and many transactions get verified at a time. Once the transaction is verified and declared a legitimate transaction, it will be added to the mem pool. All the verified transactions at a particular node form a mem pool and such multiple mem pools form a block.
4. Consensus Algorithm: The nodes that form a block will try to add the block to the blockchain network to make it permanent. But if every node is allowed to add blocks in this manner then it will disrupt the working of the blockchain network. To solve this problem, the nodes use a consensus mechanism to ensure that every new block that is added to the Blockchain is the one and only version of the truth that is agreed upon by all the nodes in the Blockchain, and only a valid block is securely attached to the blockchain. The node that is selected to add a block to the blockchain will get a reward and hence we call them “miners”. The consensus algorithm creates a hash code for that block which is required to add the block to the blockchain.
5. Addition of the new block to the blockchain: After the newly created block has got its hash value and is authenticated, now it is ready to be added to the blockchain. In every block, there is a hash value of the previous block and that is how the blocks are cryptographically linked to each other to form a blockchain. A new block gets added to the open end of the blockchain.
6. Transaction complete: As soon as the block is added to the blockchain the transaction is completed and the details of this transaction are permanently stored in the blockchain. Anyone can fetch the details of the transaction and confirm the transaction.
Let’s understand this working of blockchain with the help of an example:
Let’s say Jack and Phil are two nodes on the bitcoin blockchain network who wants to carry out a transaction between them.
Step 1: Facilitating the transaction: Jack wants to send 20 BTC to Phil via the Blockchain network.
Step 2: Verification of transaction: The message for verification will be sent to all the nodes on the network. All the nodes will check the important parameters related to the transaction like Does Jack has sufficient balance i.e. at least 20BTC to perform the transaction. Is Jack a registered node? Is Phil a registered node? After checking the parameters the transaction is verified.
Step 3: Formation of a new block: A number of verified transactions stack up in mem pools and get stored in a block. This verified transaction will also get stored in a block.
Step 4: Consensus algorithm: Since here we are talking about bitcoins so the Proof-of-Work consensus algorithm will be used for block verification. In proof-of-work, the system assigns the target hash value to a node, and according to this, it must come up with a hash for the new block. The node has to calculate the hash value for the new block that is less than the target value. If two or more miners mine the same block at the same time, the block with more difficulty is selected. The others are known as stale blocks. Mining usually rewards miners with blockchain currency. In this case, the blockchain currency is bitcoin.
Step 5: Addition of the new block in the blockchain: After the newly created block has got the hash value and authentication through proof-of-work only then it will be added to the network and the transaction will mark as complete. Phil will receive 20 BTC from Jack.
The new block will be linked to the open end of the blockchain.
Step 6: Transaction complete: As soon as the block is added to the blockchain, the transaction will take place and 20 BTCs will get transferred from Jack’s wallet to Phil’s wallet. The details of the transaction are permanently secured on the blockchain.
Anyone on the network can fetch the information and confirm the transaction. This will help to keep track of all the transactions and to verify whether any user is trying to double spend. For example, if Jack tries to carry out a transaction in the future, the rest of the nodes can check Jack’s past transaction records to check whether Jack has enough balance to carry out the current transaction. If there is enough balance then the transaction will be approved.
Is Blockchain Secure?
In the most basic way, one can think of a blockchain as a linked list. Each of the next items in the list is dependent on the previous item, except for the first block, also known as the genesis block, which is hardcoded into the blockchain. In the blockchain, each block contains the hash of the previous block’s header and a hash of the transactions in the Merkle tree of the current block. In this way, each block is cryptographically chained to the previous block. Let’s understand with an example what happens when someone attempts to change a transaction or block data in a blockchain network.
- Suppose, there is a chain of 10 blocks, where the 10th block depends on the 9th block, the 9th block depends on the 8th block, and so on.
- In this way, the 10th block depends on all the previous blocks and the genesis block as well.
- If someone tries to change data on the 2nd block, then the attacker will have to change data on all the later blocks as well, otherwise, the blockchain will become invalid since the later blocks depend on the hash value present in the 2nd block and the 2nd block has changed, but not the later blocks.
- Thus, as the blocks are added, immutability increases as changing the block is an expensive operation.
- Also, to add/change a block in a blockchain, a consensus algorithm is used by nodes in the blockchain network. In order to compensate for the change in one block, one must have to recalculate the hash of every block to update the hash value of the block header in the next block. This will involve a lot of time and computational resources.
- In order to succeed with such kind of attack, the hacker has to simultaneously control and change 51% or more copies of the blockchain so that their new copy becomes the majority copy and thus the agreed-upon chain.
- Thus, requiring an immense amount of time, money, and computational resources.