Demystifying Blockchains

0
6565

A blockchain is a continuously growing list of records, called blocks, which are linked and secured using cryptography to ensure data security.

Data security is of paramount importance to corporations. Enterprises need to establish high levels of trust and offer guarantees on the security of the data being shared with them while interacting with other enterprises. The major concern of any enterprise about data security is data integrity. What many in the enterprise domain worry about is, “Is my data accurate?”

Data integrity ensures that the data is accurate, untampered with and consistent across the life cycle of any transaction. Enterprises share data like invoices, orders, etc. The integrity of this data is the pillar on which their businesses are built.

Blockchain

A blockchain is a distributed public ledger of transactions that no person or company owns or controls. Instead, every user can access the entire blockchain, and every transaction from any account to any other account, as it is recorded in a secure and verifiable form using algorithms of cryptography. In short, a blockchain ensures data integrity.

A blockchain provides data integrity due to its unique and significant features. Some of these are listed below.

Timeless validation for a transaction: Each transaction in a blockchain has a signature digest attached to it which depends on all the previous transactions, without the expiration date. Due to this, each transaction can be validated at any point in time by anyone without the risk of the data being altered or tampered with.

Highly scalable and portable: A blockchain is a decentralised ledger distributed across the globe, and it ensures very high availability and resilience against disaster.

Tamper-proof: A blockchain uses asymmetric or elliptic curve cryptography under the hood. Besides, each transaction gets added to the blockchain only after validation, and each transaction also depends on the previous transaction. Hence, a blockchain is nearly impossible to tamper with without anyone noticing.

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. The link to the GitHub repository can be found at https://github.com/abhiit89/Ang Coins. So do check the GitHub repo and go through the ‘readme’ as it contains the instructions on how to run the code locally.

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();

}

The definition of the block, inside a blockchain, is presented in the above example. 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’);

Adding a 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:

addNewTransactionBlockToTransactionChain(currentBlock) {

currentBlock.previousTransactionHash = this.returnLatestBlock().currentTransactionHash;

currentBlock.currentTransactionHash = currentBlock.calculateBlockDigest();

this.transactionChain.push(currentBlock);

}

In the above code example, we calculate the hash of the previous transaction and the hash of the current transaction before pushing the new block to the blockchain. We also validate the new block before adding it to the blockchain using the method described below.

Validating the blockchain

Each block needs to be validated before it gets added to the blockchain. The validation we used in our implementation is described below:

isBlockChainValid() {

for (let blockCount = 1; blockCount < this.transactionChain.length; blockCount++) {

const currentBlockInBlockChain = this.transactionChain[blockCount];

const previousBlockInBlockChain = this.transactionChain[blockCount - 1];

if (currentBlockInBlockChain.currentTransactionHash !== currentBlockInBlockChain.calculateBlockDigest()) {

return false;

}

if (currentBlockInBlockChain.previousTransactionHash !== previousBlockInBlockChain.currentTransactionHash) {

return false;

}

}

return true;

}

In this implementation, there are a lot of features missing as of now, like validation of the funds, the rollback feature in case the newly added block corrupts the blockchain, etc. If anyone is interested in tackling fund validation, the rollback or any other issue they find, please go to my GitHub repository, create an issue and the fix for it, and send me a pull request or just fork the repository and use it whichever way this code suits your requirements.

A point to be noted here is that in this implementation, there are numerous ways to tamper with the blockchain. One way is to tamper with the data alone. The implementation for that is done in the branch https://github.com/abhiit89/AngCoins/tree/tampering_data.

Another way is to not only change the data but also update the hash. Even then the current implementation can invalidate it. The code for it is available in the branch https://github.com/abhiit89/AngCoins/tree/tampering_data_with_updated_hash.

Proof of work

With the current implementation, it is still possible that someone can spam the blockchain by changing the data in one block and updating the hash in all the following blocks in the blockchain. In order to prevent that, the concept of the ‘proof of work’ suggests a difficulty or condition that each block that is generated has to meet before getting added to the blockchain. This difficulty prevents very frequent generation of the block, as the hashing algorithm used to generate the block is not under the control of the person creating the block. In this way, it becomes a game of hit and miss to try to generate the block that meets the required conditions.

For our implementation, we have set the difficult task that each block generated must have two ‘00’ in the beginning of the hash, in order to be added to the blockchain. For example, we can modify the function to add a new block to include the difficult task, given as below:

addNewTransactionBlockToTransactionChain(currentBlock) {

currentBlock.previousTransactionHash = this.returnLatestBlock().currentTransactionHash;

currentBlock.mineNewBlock(this.difficulty);

this.transactionChain.push(currentBlock);

}

This calls the mining function (which validates the difficult conditions):

mineNewBlock(difficulty) {

while(this.currentTransactionHash.substring(0, difficulty) !== Array(difficulty + 1).join(‘0’)) {

this.nonce++;

this.currentTransactionHash = this.calculateBlockDigest();

}

console.log(‘New Block Mined --> ‘ + this.currentTransactionHash);

}

The complete code for this implementation can be seen in the branch https://github.com/abhiit89/AngCoins/tree/block_chain_mining.

Blockchain providers

Blockchain technology, with its unprecedented way of managing trust and data and of executing procedures, can transform businesses. Here are some open source blockchain platforms.

HyperLedger: Hyperledger nurtures and endorses a wide array of businesses around blockchain technologies, including distributed ledgers, smart contracts, etc. Hyperledger encourages the re-use of common building blocks and enables the speedy invention of distributed ledger technology components.

Project link: https://hyperledger.org/projects

Project GitHub link: https://github.com/hyperledger

Openchain: Openchain is an open source distributed ledger technology. It is ideal for enterprises, and deals in issuing and managing digital assets in a robust, secure and scalable way.

Project link: https://www.openchain.org/

Ethereum project: This is a distributed framework that runs smart contracts—applications that run exactly as programmed in a secured virtual environment without downtime or the possibility of tampering, as this platform leverages the custom built blockchain.

Project link: https://www.ethereum.org/

LEAVE A REPLY

Please enter your comment!
Please enter your name here