Learn About Enterprise Blockchain

“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
 
Enterprise Blockchain
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#.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Security.Cryptography;  
  4. using System.Text;  
  5.   
  6. namespace CSharpBlockchain  
  7. {  
  8.     public class Block  
  9.     {  
  10.         public int Index { getset; }  
  11.         public DateTime TimeStamp { getset; }  
  12.         public string PreviousHash { getset; }  
  13.         public string Hash { getset; }  
  14.         public string Data { getset; }  
  15.   
  16.         public Block(DateTime timeStamp, string previousHash, string data)  
  17.         {  
  18.             Index = 0;  
  19.             TimeStamp = timeStamp;  
  20.             PreviousHash = previousHash;  
  21.             Data = data;  
  22.             Hash = GetHash();  
  23.         }  
  24.   
  25.         public string GetHash()  
  26.         {  
  27.             SHA256 sha256 = SHA256.Create();  
  28.   
  29.             byte[] inputBytes = Encoding.ASCII.GetBytes($"{TimeStamp}-{PreviousHash ?? ""}-{Data}");  
  30.             byte[] outputBytes = sha256.ComputeHash(inputBytes);  
  31.   
  32.             return Convert.ToBase64String(outputBytes);  
  33.         }  
  34.     }  
  35. }  
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.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4.   
  5. namespace CSharpBlockchain  
  6. {  
  7.     public class BlockChain  
  8.     {  
  9.         private  IList<Block>  blockChain ;  
  10.   
  11.           
  12.   
  13.         public BlockChain()  
  14.         {  
  15.             InitBlockChain();  
  16.             AddBlock();  
  17.         }  
  18.   
  19.         public void InitBlockChain()  
  20.         {  
  21.             blockChain = new List<Block>();  
  22.         }  
  23.   
  24.           
  25.         public IList<Block> GetBlockChain()  
  26.         {  
  27.              
  28.                return blockChain;  
  29.         }  
  30.         public Block GetNewBlock()  
  31.         {  
  32.             return new Block(DateTime.Now, null"{}");  
  33.         }  
  34.   
  35.         public void AddBlock()  
  36.         {  
  37.             blockChain.Add(GetNewBlock());  
  38.         }  
  39.           
  40.         public Block GetLastBlock()  
  41.         {  
  42.             return blockChain[blockChain.Count - 1];  
  43.         }  
  44.   
  45.         public void AddBlock(Block block)  
  46.         {  
  47.             Block latestBlock = GetLastBlock();  
  48.             block.Index = latestBlock.Index + 1;  
  49.             block.PreviousHash = latestBlock.Hash;  
  50.             block.Hash = block.GetHash();  
  51.             blockChain.Add(block);  
  52.         }  
  53.   
  54.         public bool CheckValidity()  
  55.         {  
  56.             for (int i = 1; i < blockChain.Count; i++)  
  57.             {  
  58.                 Block currentBlock = blockChain[i];  
  59.                 Block previousBlock = blockChain[i - 1];  
  60.   
  61.                 if (currentBlock.Hash != currentBlock.GetHash())  
  62.                 {  
  63.                     return false;  
  64.                 }  
  65.   
  66.                 if (currentBlock.PreviousHash != previousBlock.Hash)  
  67.                 {  
  68.                     return false;  
  69.                 }  
  70.             }  
  71.             return true;  
  72.         }  
  73.     }  
  74. }  
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#.
  1. using System;  
  2. using System.Collections.Generic;  
  3.   
  4. namespace CSharpBlockchain  
  5. {  
  6.     class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             BlockChain blockchain = new BlockChain();  
  11.             blockchain.AddBlock(new Block(DateTime.Now, null"{send-er:James,receiver:John,amount:100}"));  
  12.             blockchain.AddBlock(new Block(DateTime.Now, null"{send-er:John,receiver:James,amount:30}"));  
  13.             blockchain.AddBlock(new Block(DateTime.Now, null"{send-er:John,receiver:James,amount:40}"));  
  14.   
  15.        
  16.             Console.WriteLine($"Block  Chain Validity: {blockchain.CheckValidity()}");  
  17.   
  18.             Console.WriteLine($"Update amount to 2000");  
  19.             IList<Block> blocks = blockchain.GetBlockChain();  
  20.             blocks[1].Data = "{sender:James,receiver:John,amount:2000}";  
  21.   
  22.             Console.WriteLine($"Block  Chain Validity: {blockchain.CheckValidity()}");  
  23.   
  24.             Console.WriteLine($"Get hash");  
  25.             //IList<Block> blocks = blockchain.GetBlocks();  
  26.             blocks[1].Hash = blocks[1].GetHash();  
  27.   
  28.             Console.WriteLine($"Block  Chain Validity: {blockchain.CheckValidity()}");  
  29.   
  30.             Console.WriteLine($"Get the Latest BlockChain");  
  31.             blocks[2].PreviousHash = blocks[1].Hash;  
  32.             blocks[2].Hash = blocks[2].GetHash();  
  33.             blocks[3].PreviousHash = blocks[2].Hash;  
  34.             blocks[3].Hash = blocks[3].GetHash();  
  35.   
  36.             Console.WriteLine($"Block  Chain Validity: {blockchain.CheckValidity()}");  
  37.         }  
  38.     }  
  39. }  
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.


Similar Articles