区块链是什么?作为一个Ruby开发者,理解区块链的最好办法,就是亲自动手实现一个。只需要50行Ruby代码你就能彻底理解区块链的核心原理!
区块链 = 区块组成的链表?
class Block attr_reader :timestamp attr_reader :data attr_reader :previous_hash attr_reader :hash def initialize(data, previous_hash) @timestamp = Time.now @data = data @previous_hash = previous_hash @hash = calc_hash end def self.first( data="Genesis" ) # create genesis (big bang! first) block ## note: uses all zero for previous_hash ("0") Block.new( data, "0000000000000000000000000000000000000000000000000000000000000000" ) end def self.next( previous, data="Transaction Data..." ) Block.new( data, previous.hash ) endprivate def calc_hash sha = Digest::SHA256.new sha.update( @timestamp.to_s + @previous_hash + @data ) sha.hexdigest endend # class Block####### let's get started## build a blockchain a block at a timeb0 = Block.first( "Genesis" ) b1 = Block.next( b0, "Transaction Data..." ) b2 = Block.next( b1, "Transaction Data......" ) b3 = Block.next( b2, "More Transaction Data..." ) blockchain = [b0, b1, b2, b3] pp blockchain
执行上面程序:
~$ ruby blockchain.rb
将会输出类似下面的结果:
[#<Block:0x1eed2a0 @timestamp = 1637-09-15 20:52:38, @data = "Genesis", @previous_hash = "0000000000000000000000000000000000000000000000000000000000000000", @hash = "edbd4e11e69bc399a9ccd8faaea44fb27410fe8e3023bb9462450a0a9c4caa1b">, #<Block:0x1eec9a0 @timestamp = 1637-09-15 21:02:38, @data = "Transaction Data...", @previous_hash = "edbd4e11e69bc399a9ccd8faaea44fb27410fe8e3023bb9462450a0a9c4caa1b", @hash = "eb8ecbf6d5870763ae246e37539d82e37052cb32f88bb8c59971f9978e437743">, #<Block:0x1eec838 @timestamp = 1637-09-15 21:12:38, @data = "Transaction Data......", @previous_hash = "eb8ecbf6d5870763ae246e37539d82e37052cb32f88bb8c59971f9978e437743", @hash = "be50017ee4bbcb33844b3dc2b7c4e476d46569b5df5762d14ceba9355f0a85f4">, #<Block:0x1eec6d0 @timestamp = 1637-09-15 21:22:38, @data = "More Transaction Data...", @previous_hash = "be50017ee4bbcb33844b3dc2b7c4e476d46569b5df5762d14ceba9355f0a85f4", @hash = "5ee2981606328abfe0c3b1171440f0df746c1e1f8b3b56c351727f7da7ae5d8d">]
你先等等,难道区块链就是链表吗?
当然不是。我们使用链表的目的是获得指向前一个块的引用:在区块链中,每个块都必须有一个标识符,而这个标识符还必须依赖于前一个块的标识符,这意味着如果你要替换区块链中的一个块,就必须重算后面所有块的标识符。在上面的实现中,你可以看到我们调用calc_hash
方法计算块的标识符时,需要传入前一个块的签名,就是这个意思。
那工作量证明算法呢?
现在让我们添加工作量证明算法的实现。在经典的区块链中,你必须通过计算得到00开头的哈希作为块的标识符,前缀的0越多,计算量就越大,也就越困难。出于简单考虑,让我们将难度设定为两个前缀0,也就是说,2^16 = 256种可能。
blockchain_with_proof_of_work.rb:
def compute_hash_with_proof_of_work( difficulty="00" ) nonce = 0 loop do hash = calc_hash_with_nonce( nonce ) if hash.start_with?( difficulty ) return [nonce,hash] ## bingo! proof of work if hash starts with leading zeros (00) else nonce += 1 ## keep trying (and trying and trying) end endenddef calc_hash_with_nonce( nonce=0 ) sha = Digest::SHA256.new sha.update( nonce.to_s + @timestamp.to_s + @previous_hash + @data ) sha.hexdigestend
现在我们运行这个增加了POW机制的区块链程序:
~$ ruby blockchain_with_proof_of_work.rb
输出结果如下:
[#<Block:0x1e204f0 @timestamp = 1637-09-20 20:13:38, @data = "Genesis", @previous_hash = "0000000000000000000000000000000000000000000000000000000000000000", @nonce = 242, @hash = "00b8e77e27378f9aa0afbcea3a2882bb62f6663771dee053364beb1887e18bcf">, #<Block:0x1e56e20 @timestamp = 1637-09-20 20:23:38, @data = "Transaction Data...", @previous_hash = "00b8e77e27378f9aa0afbcea3a2882bb62f6663771dee053364beb1887e18bcf", @nonce = 46, @hash = "00aae8d2e9387e13c71b33f8cd205d336ac250d2828011f5970062912985a9af">, #<Block:0x1e2bd58 @timestamp = 1637-09-20 20:33:38, @data = "Transaction Data......", @previous_hash = "00aae8d2e9387e13c71b33f8cd205d336ac250d2828011f5970062912985a9af", @nonce = 350, @hash = "00ea45e0f4683c3bec4364f349ee2b6816be0c9fd95cfd5ffcc6ed572c62f190">, #<Block:0x1fa8338 @timestamp = 1637-09-20 20:43:38, @data = "More Transaction Data...", @previous_hash = "00ea45e0f4683c3bec4364f349ee2b6816be0c9fd95cfd5ffcc6ed572c62f190", @nonce = 59, @hash = "00436f0fca677652963e904ce4c624606a255946b921132d5b1f70f7d86c4ab8">]
你看到和前一个版本的差别的吗?现在所有的hash
都是00开头的,nonce
则是获得这个符合条件
的哈希时所采用的随机幸运数字。
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦