Introduction to blockchain part -2:
Welcome to Introduction part on blockchain.
NOTE: You will get complete code files at the end of intro to blockchain (mostly in next article).
Def: Blockchain:
Lets take the defination 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 cryptofgraphy hash of the previous block, a timestamp and transaction data (generally represented as a Merkel tree).
records - a record is a basic data structure. Records in a database are usually called "rows".
cryptography - is the practice and study of techiniques for secure communication in the presence of third parties called adversaries.
crypto hash - is a hash fucntion 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;
}
get genesis() {
new Block(
0,
"0",
1508270000000,
"Welcome to Blockchain Demo 2.0!",
"000dc75a315c77a1f9c98fb6247d03dd18ac52632d7dc6a9920261d8109b37cf",
604
);
}
}
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.
// class Blockchain {
// constructor() {
// this.blockchain = [Block.genesis()];
this.difficulty = 3;
// }
// get() { ... }
// get latestBlock() { ... }
isValidHashDifficulty(hash) {
for (var i = 0; i < hash.length; i++) {
if (hash[i] !== "0") {
break;
};
}
return i >= this.difficulty;
}
// };
// module.exports = Blockchain;
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
const crypto = require("crypto");
// class Blockchain {
// constructor() { ... }
// get() { ... }
// get latestBlock() { ... }
// isValidHashDifficulty(hash) { ... }
calculateHashForBlock(block) {
const { index, previousHash, timestamp, transactions, nonce } = block;
return this.calculateHash(
index,
previousHash,
timestamp,
transactions,
nonce
);
}
calculateHash(index, previousHash, timestamp, data, nonce) {
return crypto
.createHash("sha256") // SHA256 Hash Function
.update(index + previousHash + timestamp + data + nonce)
.digest("hex");
}
// };
// module.exports = Blockchain;
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.
``
// const crypto = require("crypto");
// class Blockchain {
// constructor() { ... }
// get() { ... }
// get latestBlock() { ... }
// isValidHashDifficulty(hash) { ... }
// calculateHashForBlock(block) { ... }
// calculateHash(...) { ... }
// mine(data) { ... }
generateNextBlock(data) {
const nextIndex = this.latestBlock.index + 1;
const previousHash = this.latestBlock.hash;
let timestamp = new Date().getTime();
let nonce = 0;
let nextHash = this.calculateHash(
nextIndex,
previousHash,
timestamp,
data,
nonce
);
while (!this.isValidHashDifficulty(nextHash)) {
nonce = nonce + 1;
timestamp = new Date().getTime();
nextHash = this.calculateHash(
nextIndex,
previousHash,
timestamp,
data,
nonce
);
}
const nextBlock = new Block(
nextIndex,
previousBlock.hash,
nextTimestamp,
data,
nextHash,
nonce
);
return nextBlock;
}
// };
// module.exports = Blockchain;
Stay tuned for next article...