一个基本的区块链框架源码,想用的拿去造币吧!
所在版块:心情闲聊 发贴时间:2018-06-13 12:46

用户信息
复制本帖HTML代码
高亮: 今天贴 X 昨天贴 X 前天贴 X 
public struct Block
{
/// <summary>
/// 区块位置
/// </summary>
public int Index { get; set; }
/// <summary>
/// 区块生成时间戳
/// </summary>
public string TimeStamp { get; set; }
/// <summary>
/// 心率数值
/// </summary>
public int BPM { get; set; }
/// <summary>
/// 区块 SHA-256 散列值
/// </summary>
public string Hash { get; set; }
/// <summary>
/// 前一个区块 SHA-256 散列值
/// </summary>
public string PrevHash { get; set; }
}

public static class BlockGenerator
{
public static List<Block> _blockChain = new List<Block>();
}


/// <summary>
/// 计算区块 HASH 值
/// </summary>
/// <param name="block">区块实例</param>
/// <returns>计算完成的区块散列值</returns>
public static string CalculateHash(Block block)
{
string calculationStr = $"{block.Index}{block.TimeStamp}{block.BPM}{block.PrevHash}";

SHA256 sha256Generator = SHA256.Create();
byte[] sha256HashBytes = sha256Generator.ComputeHash(Encoding.UTF8.GetBytes(calculationStr));

StringBuilder sha256StrBuilder = new StringBuilder();
foreach (byte @byte in sha256HashBytes)
{
sha256StrBuilder.Append(@byte.ToString("x2"));
}

return sha256StrBuilder.ToString();
}

/// <summary>
/// 生成新的区块
/// </summary>
/// <param name="oldBlock">旧的区块数据</param>
/// <param name="BPM">心率</param>
/// <returns>新的区块</returns>
public static Block GenerateBlock(Block oldBlock, int BPM)
{
Block newBlock = new Block()
{
Index = oldBlock.Index + 1,
TimeStamp = CalculateCurrentTimeUTC(),
BPM = BPM,
PrevHash = oldBlock.Hash
};

newBlock.Hash = CalculateHash(newBlock);
return newBlock;
}

/// <summary>
/// 计算当前时间的 UTC 表示格式
/// </summary>
/// <returns>UTC 时间字符串</returns>
public static string CalculateCurrentTimeUTC()
{
DateTime startTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
DateTime nowTime = DateTime.Now;

long unixTime = (long)Math.Round((nowTime - startTime).TotalMilliseconds, MidpointRounding.AwayFromZero);
return unixTime.ToString();
}


/// <summary>
/// 检验区块是否有效
/// </summary>
/// <param name="newBlock">新生成的区块数据</param>
/// <param name="oldBlock">旧的区块数据</param>
/// <returns>有效返回 TRUE,无效返回 FALSE</returns>
public static bool IsBlockValid(Block newBlock, Block oldBlock)
{
if (oldBlock.Index + 1 != newBlock.Index) return false;
if (oldBlock.Hash != newBlock.PrevHash) return false;
if (CalculateHash(newBlock) != newBlock.Hash) return false;

return true;
}

/// <summary>
/// 如果新的区块链比当前区块链更新,则切换当前区块链为最新区块链
/// </summary>
/// <param name="newBlockChain">新的区块链</param>
public static void SwitchChain(List<Block> newBlockChain)
{
if (newBlockChain.Count > _blockChain.Count)
{
_blockChain = newBlockChain;
}
}


现在整个区块链的基本操作已经完成,现在我们需要让他运转起来,我们来到 StartUp 当中,添加两个新的路由:
app.Map("/BlockChain", _ =>
{
_.Run(async context =>
{
if (context.Request.Method == "POST")
{
// 增加区块链
if (BlockGenerator._blockChain.Count == 0)
{
Block firstBlock = new Block()
{
Index = 0,
TimeStamp = BlockGenerator.CalculateCurrentTimeUTC(),
BPM = 0,
Hash = string.Empty,
PrevHash = string.Empty
};

BlockGenerator._blockChain.Add(firstBlock);

await context.Response.WriteAsync(JsonConvert.SerializeObject(firstBlock));
}
else
{
int.TryParse(context.Request.Form["BPM"][0], out int bpm);

Block oldBlock = BlockGenerator._blockChain.Last();
Block newBlock = BlockGenerator.GenerateBlock(oldBlock, bpm);

if (BlockGenerator.IsBlockValid(newBlock, oldBlock))
{
List<Block> newBlockChain = new List<Block>();
foreach (var block in BlockGenerator._blockChain)
{
newBlockChain.Add(block);
}

newBlockChain.Add(newBlock);
BlockGenerator.SwitchChain(newBlockChain);
}

await context.Response.WriteAsync(JsonConvert.SerializeObject(newBlock));
}
}
});
});

app.Map("/BlockChains", _ =>
{
_.Run(async context =>
{
await context.Response.WriteAsync(JsonConvert.SerializeObject(BlockGenerator._blockChain));
});
});
.
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!

 相关帖子 我要回复↙ ↗回到正文
少一点撕X,多一点互相学习讨论吧,我是来请教的 凡人   (469 bytes , 5988reads )
一个基本的区块链框架源码,想用的拿去造币吧! seanwen   (4695 bytes , 29reads )
区块链峰会意义不大 hdhxt   (54 bytes , 43reads )
帮助楼主理解下这类与会条件 x4   (51 bytes , 65reads )
同去同去 紧跟恬淡   (0 bytes , 23reads )
哈哈哈 我刚刚在微信收到几遍 凡人   (0 bytes , 26reads )
我怎没有啊 笑天   (35 bytes , 27reads )
同去 紧跟恬淡   (36 bytes , 37reads )
不厚道的层主 笑天   (12 bytes , 26reads )
这个可以有啊 笑天   (61 bytes , 24reads )
同去吧,华新联谊会? 凡人   (20 bytes , 21reads )
监管缺位的交易所可以做什么事情 鹏哥   (714 bytes , 126reads )
让你来帮我把关你又不干 凡人   (0 bytes , 33reads )
都是命。。你懂得 功夫熊猫   (0 bytes , 36reads )
过段时间,我拉你去看间公司吧 icky   (32 bytes , 36reads )
求带 凡人   (0 bytes , 35reads )
鹏哥   (6 bytes , 29reads )
所以 no8D8U   (328 bytes , 46reads )
我只能说个人见解啊 鹏哥   (1337 bytes , 32reads )
觉得很有道理 fatwallet   (48 bytes , 25reads )
认识很深刻的专业人士啊 笑天   (134 bytes , 35reads )
主要是因为商业模式的转变 Serenityblue   (48 bytes , 26reads )
必须赞这个, 小馋牛   (104 bytes , 35reads )
你造为啥没人气么 しろ   (348 bytes , 35reads )
不会 icky   (18 bytes , 27reads )
絮叨是真的 鹏哥   (142 bytes , 28reads )
关于公钥私钥 鹏哥   (1285 bytes , 109reads )
这不过是传统的加密方式罢了。 iND   (268 bytes , 35reads )
听说韩国的刚被hack,各种币在比赛跳水呢.... 小馋牛   (24 bytes , 44reads )
炒作 你懂的 功夫熊猫   (0 bytes , 23reads )
归根到底 japgolly   (730 bytes , 126reads )
隐私隐私隐私,重要的事情说三遍 我是老万   (32 bytes , 30reads )
我没做过保险经纪... 凡人   (0 bytes , 29reads )
都被忽悠了吧? iND   (384 bytes , 37reads )
顶一个,一针见血啊! yaoyao8   (94 bytes , 29reads )
去中心化不难理解 Serenityblue   (1490 bytes , 31reads )
一针见血 笑天   (272 bytes , 23reads )
手误。 iND   (156 bytes , 51reads )
同意 笑天   (94 bytes , 25reads )
对于自己不熟悉的领域 japgolly   (170 bytes , 48reads )
求科普,请吃饭/喝咖啡都可以 凡人   (0 bytes , 46reads )
我是奔着吃饭来的。。:) Serenityblue   (294 bytes , 35reads )
同求科普 格格巫婆   (42 bytes , 40reads )
我请你吧。作为交换, 到时你给科普一下 Serenityblue   (77 bytes , 44reads )
我没本事说这么大的书哒 格格巫婆   (12 bytes , 44reads )
拜四上午刚好去NUS见客 笑天   (10 bytes , 20reads )
周四中午有与欧洲的例会 Serenityblue   (72 bytes , 29reads )
白老师认错人了 笑天   (46 bytes , 23reads )
蓝老师居然回复我,激动! 凡人   (28 bytes , 35reads )
明天吧? 今天来不及了。中午一会有appointment Serenityblue   (36 bytes , 35reads )
别走啊, iND   (127 bytes , 42reads )
你期待我说什么? japgolly   (198 bytes , 42reads )
全球各大公司重招区块链“人才” ? iND   (882 bytes , 89reads )
精辟 鹏哥   (35 bytes , 30reads )
目前还没有哪个知名科技公司用区块链出来唬人吧。。。 GoogleEarth   (0 bytes , 20reads )
基本上大的科技公司都在推这个区块链 Serenityblue   (167 bytes , 24reads )
蓝老师你是不是经常穿越来穿越去&#128512; 月光下跳舞   (30 bytes , 27reads )
我对于区块链也是新手 Serenityblue   (439 bytes , 28reads )
随机应变性如神 笑天   (17 bytes , 27reads )
同意区块链可以帮助货币虚拟化的进程 笑天   (157 bytes , 24reads )
AI以前是R&D的, 现在才开始逐步大规模工业化 Serenityblue   (123 bytes , 34reads )
科学技术是生产力吗? 笑天   (189 bytes , 26reads )
你提到的老大是? Serenityblue   (6 bytes , 31reads )
这个和我们希望金老师讲一些 鹏哥   (138 bytes , 35reads )
层主可以在华新科普一下呗 鹏哥   (20 bytes , 41reads )
同求科普 no8D8U   (14 bytes , 49reads )
楼主,我把你和隔壁的铜牌相提并认! 王者农药   (91 bytes , 42reads )
完全不是同一个人啊 熊猫妹妹   (20 bytes , 33reads )
他是前辈,不敢并提呢 凡人   (88 bytes , 47reads )
其实我认为的相提并认是你们都是做房屋销售的高手, 王者农药   (260 bytes , 40reads )
咿? 熊猫妹妹   (81 bytes , 40reads )
好美丽的名字 笑天   (2 bytes , 24reads )
郑是人妖,那能和我们金老师能比 王者农药   (120 bytes , 44reads )
如果每次都是反指 笑天   (12 bytes , 28reads )
好奇 熊猫妹妹   (8 bytes , 23reads )
好奇,隔壁哪里,离我家远么? 笑天   (4 bytes , 24reads )
狮城论坛 凡人   (0 bytes , 51reads )
额,我没说什么,我也不认识她所以不评论 凡人   (68 bytes , 34reads )
楼主参加的是那个刚刚举办的 xiaoyadan   (32 bytes , 42reads )
是的,遇到好几个熟人 凡人   (24 bytes , 43reads )
赶紧搜了一下这个峰会 HelloThere   (48 bytes , 22reads )
也不一定啦 我只是说主办人 凡人   (38 bytes , 38reads )
去参加完那个会后感觉自己都高大上了 xiaoyadan   (100 bytes , 35reads )
我跟你感觉相反:太low 凡人   (583 bytes , 50reads )
xiaoyadan   (44 bytes , 29reads )
人多,傻也多,钱好像更多 笑天   (10 bytes , 29reads )
就一个字 japgolly   (188 bytes , 38reads )
区块链是很有前途的技术,炒币是看谁是韭菜的博傻游戏 小馋牛   (242 bytes , 59reads )
今天没人发行个纪念币什么的 kcowen   (6 bytes , 30reads )
美国有准备啊 笑天   (32 bytes , 24reads )
哈哈哈说不定真有( ̄▽ ̄) 小馋牛   (0 bytes , 30reads )
我只搜了一下XMX,震惊了 凡人   (208 bytes , 41reads )
没人讨论? iND   (18 bytes , 40reads )
好的 凡人   (0 bytes , 51reads )