“Over the next decade, there will be disruption as significant as the Internet was for publishing, where blockchain is going to disrupt dozens of industries, one being capital markets and Wall Street.”
- Patrick M. Byrne
https://cdn.pixabay.com/photo/2017/12/14/14/23/blockchain-3019120_960_720.png
A blockchain is a database that is centralized that requires two parties to make clear transfers. It reduces the need to have a central authority. Many of the organizations need a consolidated database. It is going to be a value proposition for any company that wants to be the technology's early adopter-the block chain.
Blockchain stores the transactions and the transactions need to be encrypted. Blockchain consists of blocks. Each block has a set of transactions. Let us see the implementation of the blockchain in C#.
- using System;
- using System.Collections.Generic;
- using System.Security.Cryptography;
- using System.Text;
-
- namespace CSharpBlockchain
- {
- public class Block
- {
- public int Index { get; set; }
- public DateTime TimeStamp { get; set; }
- public string PreviousHash { get; set; }
- public string Hash { get; set; }
- public string Data { get; set; }
-
- public Block(DateTime timeStamp, string previousHash, string data)
- {
- Index = 0;
- TimeStamp = timeStamp;
- PreviousHash = previousHash;
- Data = data;
- Hash = GetHash();
- }
-
- public string GetHash()
- {
- SHA256 sha256 = SHA256.Create();
-
- byte[] inputBytes = Encoding.ASCII.GetBytes($"{TimeStamp}-{PreviousHash ?? ""}-{Data}");
- byte[] outputBytes = sha256.ComputeHash(inputBytes);
-
- return Convert.ToBase64String(outputBytes);
- }
- }
- }
Proof of work is the method of attaching to a block chain a series of transactions. This process's duration is about 10 minutes. The attached block has immutability rights. These blocks are being extracted by the miners. The transaction is authenticated using the source's public key. Miners choose the transactions to add to the block. Miners profit from this opportunity.
Let us now look at the implementation of the BlockChain in C# below.
- using System;
- using System.Collections.Generic;
- using System.Text;
-
- namespace CSharpBlockchain
- {
- public class BlockChain
- {
- private IList<Block> blockChain ;
-
-
-
- public BlockChain()
- {
- InitBlockChain();
- AddBlock();
- }
-
- public void InitBlockChain()
- {
- blockChain = new List<Block>();
- }
-
-
- public IList<Block> GetBlockChain()
- {
-
- return blockChain;
- }
- public Block GetNewBlock()
- {
- return new Block(DateTime.Now, null, "{}");
- }
-
- public void AddBlock()
- {
- blockChain.Add(GetNewBlock());
- }
-
- public Block GetLastBlock()
- {
- return blockChain[blockChain.Count - 1];
- }
-
- public void AddBlock(Block block)
- {
- Block latestBlock = GetLastBlock();
- block.Index = latestBlock.Index + 1;
- block.PreviousHash = latestBlock.Hash;
- block.Hash = block.GetHash();
- blockChain.Add(block);
- }
-
- public bool CheckValidity()
- {
- for (int i = 1; i < blockChain.Count; i++)
- {
- Block currentBlock = blockChain[i];
- Block previousBlock = blockChain[i - 1];
-
- if (currentBlock.Hash != currentBlock.GetHash())
- {
- return false;
- }
-
- if (currentBlock.PreviousHash != previousBlock.Hash)
- {
- return false;
- }
- }
- return true;
- }
- }
- }
In a blockchain, blocks are lined to each other. The modification of a block affects the next chain of blocks. Nonce is the term related to integer variable used for hashing the transactions using the SHA 256 algorithm or other algorithms.
Now let us create a block chain with three blocks in C#.
- using System;
- using System.Collections.Generic;
-
- namespace CSharpBlockchain
- {
- class Program
- {
- static void Main(string[] args)
- {
- BlockChain blockchain = new BlockChain();
- blockchain.AddBlock(new Block(DateTime.Now, null, "{send-er:James,receiver:John,amount:100}"));
- blockchain.AddBlock(new Block(DateTime.Now, null, "{send-er:John,receiver:James,amount:30}"));
- blockchain.AddBlock(new Block(DateTime.Now, null, "{send-er:John,receiver:James,amount:40}"));
-
-
- Console.WriteLine($"Block Chain Validity: {blockchain.CheckValidity()}");
-
- Console.WriteLine($"Update amount to 2000");
- IList<Block> blocks = blockchain.GetBlockChain();
- blocks[1].Data = "{sender:James,receiver:John,amount:2000}";
-
- Console.WriteLine($"Block Chain Validity: {blockchain.CheckValidity()}");
-
- Console.WriteLine($"Get hash");
-
- blocks[1].Hash = blocks[1].GetHash();
-
- Console.WriteLine($"Block Chain Validity: {blockchain.CheckValidity()}");
-
- Console.WriteLine($"Get the Latest BlockChain");
- blocks[2].PreviousHash = blocks[1].Hash;
- blocks[2].Hash = blocks[2].GetHash();
- blocks[3].PreviousHash = blocks[2].Hash;
- blocks[3].Hash = blocks[3].GetHash();
-
- Console.WriteLine($"Block Chain Validity: {blockchain.CheckValidity()}");
- }
- }
- }
In this example, you can see three blocks being added and the validity of the block chain is printed out. The blocks are encrypted using hashing algorithms. The hash of the blocks are printed.
Enteprises are using block chain as a transaction ledger. The next generation database or reporting data store will be block chain. The financial reports and transaction mining will happen on the block chain for compliance and decision making. The source code for this article is found at github
.