区块链是什么?做为一个Ruby开发者,理解区块链的最好办法,就是 亲自动手实现一个。只须要50行Ruby代码你就能完全理解区块链技术的核心原理!git
若是你但愿立刻开始学习以太坊区块链应用开发,能够访问汇智网提供的出色的在线互动教程:github
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 ) end private def calc_hash sha = Digest::SHA256.new sha.update( @timestamp.to_s + @previous_hash + @data ) sha.hexdigest end end # class Block ##### ## let's get started ## build a blockchain a block at a time b0 = 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
~$ ruby blockchain.rb
将会输出相似下面的结果:oop
[#<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种可能。ui
blockchain_with_proof_of_work.rb:3d
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 end end def calc_hash_with_nonce( nonce=0 ) sha = Digest::SHA256.new sha.update( nonce.to_s + @timestamp.to_s + @previous_hash + @data ) sha.hexdigest end
如今咱们运行这个增长了POW机制的区块链程序:code
~$ 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
则是得到这个符合条件 的哈希时所采用的随机幸运数字。