DATA CONVERGENCE — BLOCKCHAIN TECHNOLOGY-INTRODUCTION TO BLOCKCHAIN — Part 2
Introduction to blockchain part -2:
Types of blockchains
Peer-to-Peer Network
51% Attack
Def: Blockchain:
Demystifying blockchains:
Index:
Timestamp:
Hash:
Valid hash:
Block hash calculation:
Previous hash:
Data:
Nonce:
Mining Process:
Adding block to the blockchain:
Adding valid blocks:
Types of blockchains
A blockchain can be both permissionless (e.g., bitcoin and Ethereum) or permissioned, like the different Hyperledger blockchain frameworks. The choice between permissionless and permissioned blockchains is driven by the particular use case.
A permissionless blockchain is also known as a public blockchain, because anyone can join the network. A permissioned blockchain, or private blockchain, requires pre-verification of the participants within the network, who are usually known to each other.
Peer-to-Peer Network
A global network of computers work together to keep the blockchain secure, correct, and consistent.
Peers have three states:
• Currently active
• Connected
• Disconnected
Peers ask for each other’s blocks to determine who has the most up-to-date blockchain.
51% Attack
If a participant has more than 51% of the network, he could out-mine the network and hack the blockchain.
When there are more miners in the network, the processing power becomes more distributed and no one has majority power. This leads to a more secure blockchain.
Def: Blockchain:
Lets take the destination of blockchain from the wikipedia
• A blockchain,originally block chain, is a growing list of records, called blocks, that are linked using cryptography. Each block contains a cryptography hash** of the previous block, a timestamp and transaction data (generally represented as a Merkle tree).
records — a record is a basic data structure. Records in a database are usually called “rows”.
cryptography — is the practice and study of techniques for secure communication in the presence of third parties called adversaries.
crypto hash — is a hash function that is suitable for use in cryptography.
timestamping — is the process of securely keeping track of the creation and modification time of a document.
merkle tree — is a tree in which every leaf node is labelled with the cryptographic hash of a data block, and every non leaf node is labelled with the hash of the labels of its child nodes.
Demystifying blockchains:
A blockchain, in itself, is a distributed ledger and an interconnected chain of individual blocks of data, where each block can be a transaction, or a group of transactions. In order to explain the concepts of the blockchain, let’s look at a code example in JavaScript.
Block: A block in a blockchain is a combination of the transaction data along with the hash of the previous block.
For example:
class Block {
constructor(blockId, dateTimeStamp, transactionData, previousTransactionHash) {
this.blockId = blockId;
this.dateTimeStamp = dateTimeStamp;
this.transactionData = transactionData;
this.previousTransactionHash = previousTransactionHash;
this.currentTransactionHash = this.calculateBlockDigest();
}
It consists of the data (which includes blockId, dateTimeStamp, transactionData, previousTransactionHash, nonce), the hash of the data (currentTransactionHash) and the hash of the previous transaction data.
Genesis block: A genesis block is the first block to be created at the beginning of the blockchain. For example:
`new Block(0, new Date().getTime().valueOf(), ‘First Block’, ‘0’);`
Each *block stores the following information:
-Index
-Timestamp
-Hash
-Previous hash
-Data
-Nonce.
class Block {
constructor (index, previousHash, timestamp, data, hash, nonce) {
this.index = index;
this.previousHash = previousHash;
this.timestamp = timestamp;
this.data = data;
this.hash = hash;
this.nonce = nonce;
}
module.exports = Block;
Index:
The index is the position of the block in the chain. The genesis block has an index of 0. The next block will have an index of 1.
Timestamp:
A record of when the block was created. The timestamp helps to keep the blockchain in order.
Hash:
A hash looks like a bunch of random numbers and letters.
It is a alphanumeric value that uniquely identifies data, or the “digital fingerprint” of data.
Properties of a hash:
-Hash has a fixed length.
-Same data always maps to same hash.
-Different data always maps to a different hash (within practical limitations).
-Is easy to compute.
-Is infeasible to convert hash to data.
-> A small change in data leads to a large change in hash.
Valid hash:
A valid hash for a blockchain is a hash that meets a certain requirement. For this blockchain, having three zeros at the beginning of the hash is the requirement for a valid hash.
The number of leading zeros required is the difficulty.
Block hash calculation:
A hashing function takes data as input, and returns a unique hash.
f ( data ) = hash
Since the hash is a “digital fingerprint” of the entire block, the data is the combination of index, timestamp, previous hash, block data, and nonce.
f ( index + previous hash + timestamp + data + nonce ) = hash
Replace the values for our genesis block, we get:(E)
f ( 0 + “0” + 1508270000000 + “Welcome to Blockchain Demo 2.0!” + 604 ) = 000dc75a315c77a1f9c98fb6247d03dd18ac52632d7dc6a9920261d8109b37cf
Previous hash:
The previous hash is the hash of the previous block.
The genesis block’s previous hash is “0” because there is no previous block.
Data:
Each block can store data against it.
In cryptocurrencies such as Bitcoin, the data would include money transactions.
Nonce:
The nonce is the number used to find a valid hash.
To find a valid hash, we need to find a nonce value that will produce a valid hash when used with the rest of the information from that block.
Mining Process:
The process of determining this nonce is called mining.
We start with a nonce of 0 and keep increment it by 1 until we find a valid hash.
As difficulty increases, the number of possible valid hashes decreases. With fewer possible valid hashes, it takes more processing power to find a valid hash.
Example to mine data:
mine(data) {
const newBlock = this.generateNextBlock(data);
try {
this.addBlock(newBlock);
} catch (err) {
throw err;
};
}
Where generateNextBlock(data) generates a block with required things like index, nonce, hash etc.. of the next block and addBlock(newBlock) add generated block to the chain.
Let’s see some info about data mutation:
Data mutation:
Since data is an input variable for the hash, changing the data will change the hash. The new hash will not have three leading zeros, and therefore becomes invalid.
Mutation effect:
Subsequent blocks will also be invalid.
A hash change will cause a mutation in the previous hash of subsequent blocks. Since previous hash is used to calculate the hash, subsequent hashes will also change. This will lead to a cascading invalidation of blocks.
Adding block to the blockchain:
In order to add blocks or transactions to the blockchain, we have to create a new block with a set of transactions, and add it to the blockchain as explained in the code example below:
addBlock(newBlock) {
if (this.isValidNewBlock(newBlock, this.latestBlock)) {
this.blockchain.push(newBlock);
} else {
throw “Error: Invalid block”;
}
}
Adding valid blocks:
When adding a new block to the blockchain, the new block needs to meet these requirements.
-> Block index one greater than latest block index.
-> Block previous hash equal to latest block hash.
-> Block hash meets difficulty requirement.
-> Block hash is correctly calculated.
Other peers on the network will be adding blocks to the blockchain, so new blocks need to be validated.
Stay tuned for next article…