分类目录

链接

2018年 3月
 1234
567891011
12131415161718
19202122232425
262728293031  

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > 区块链 > 正文
区块链实现简易比特币C#.NET版
区块链 暂无评论 阅读(627)

 

  1. using System.Collections;  
  2. using System;  
  3. using static System.Console;  
  4.   
  5.   
  6. //简易区块链  
  7. namespace test  
  8. {     
  9.     //区块  
  10.     public class Block  
  11.     {  
  12.         //基本属性:上一个区块的Hash值,交易信息,Nonce变量,当前区块的Hash  
  13.         private int previousBlockHashCode;  
  14.         private string transactionInfo;  
  15.         private string nonce;  
  16.         private int blockHashCode;  
  17.   
  18.         //构造函数  
  19.         public Block()  
  20.         {  
  21.             transactionInfo = "the Creation";  
  22.             previousBlockHashCode = 0;  
  23.             blockHashCode = 0;  
  24.         }  
  25.   
  26.         public Block(string tranInfo,int previousBlockHash)  
  27.         {  
  28.             transactionInfo = tranInfo;  
  29.             previousBlockHashCode = previousBlockHash;  
  30.         }  
  31.   
  32.         //重载GetHashCode函数  
  33.         public override int GetHashCode()  
  34.         {  
  35.             nonce = DateTime.Now.ToFileTimeUtc().ToString() + transactionInfo + previousBlockHashCode;  
  36.             return nonce.GetHashCode();  
  37.         }  
  38.   
  39.         public int BlockHashCode  
  40.         {  
  41.             get { return blockHashCode; }  
  42.             set { blockHashCode = value; }  
  43.         }  
  44.         public string TransactionInfo  
  45.         {  
  46.             get { return transactionInfo; }  
  47.         }  
  48.         public int PreviousBlockHashCode  
  49.         {  
  50.             get { return previousBlockHashCode; }  
  51.         }  
  52.         public string Nonce  
  53.         {  
  54.             get { return nonce; }  
  55.         }  
  56.     }  
  57.   
  58.     //区块链  
  59.     public class Chain: DictionaryBase  
  60.     {  
  61.         //重载添加方法  
  62.         public void Add(Block newBlock)  
  63.         {  
  64.             Dictionary.Add(newBlock.BlockHashCode, newBlock);  
  65.         }  
  66.           
  67.         public Chain()  
  68.         {  
  69.             WriteLine("Create a BlockChain.");  
  70.             this.Add(new Block());  
  71.         }  
  72.   
  73.         //文字索引  
  74.         public Block this[int hash]  
  75.         {  
  76.             get { return (Block)Dictionary[hash]; }  
  77.         }  
  78.   
  79.         //通过Hash遍历输出区块链  
  80.         public void GetChainInfo(Block nowBlock)  
  81.         {  
  82.             WriteLine("\n\nPut out the BlockChain: ");  
  83.             Block p = nowBlock;  
  84.             do  
  85.             {  
  86.                 WriteLine(p.TransactionInfo);  
  87.                 p = this[p.PreviousBlockHashCode];  
  88.             } while (p.BlockHashCode != 0);  
  89.             WriteLine(p.TransactionInfo);  
  90.         }  
  91.     }  
  92.   
  93.     //广播当前Hash  
  94.     public class Broadcast  
  95.     {  
  96.         public static int hashNow;  
  97.     }  
  98.   
  99.     //挖矿  
  100.     public class Mining  
  101.     {  
  102.         //获得Hash值  
  103.         public static void GetHash(Block b)  
  104.         {  
  105.             WriteLine("Wait...");  
  106.             int hash;  
  107.             while (true)  
  108.             {  
  109.                 hash = b.GetHashCode();  
  110.                 int remainder = hash % 50;//难度设置  
  111.                 if (remainder == 22)  
  112.                 {  
  113.                     WriteLine("Mining succeed.");  
  114.                     break;  
  115.                 }   
  116.             }  
  117.             Broadcast.hashNow = hash;  
  118.             b.BlockHashCode = hash;  
  119.         }   
  120.           
  121.         //挖矿程序  
  122.         public static void Mine(Chain chain,Block block)  
  123.         {  
  124.             WriteLine("Start to get HashCode.");  
  125.             GetHash(block);  
  126.             chain.Add(block);  
  127.         }  
  128.     }  
  129.   
  130.     class Program  
  131.     {  
  132.         static void Main(string[] args)  
  133.         {  
  134.             Block testBlock = new Block("transaction no.1", 0);  
  135.             Chain testChain = new Chain();  
  136.             Mining.Mine(testChain, testBlock);  
  137.             testChain.GetChainInfo(testBlock);  
  138.         }  
  139.     }  
  140. }  

 

界面不高兴写了

 

自带GetHashCode函数:

(1)值类型:返回结构中第一个值的hash

(2)引用类型:返回引用的引用源的hash

============ 欢迎各位老板打赏~ ===========

【上篇】
【下篇】

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:区块链实现简易比特币C#.NET版 | Bruce's Blog

发表评论

留言无头像?