区块链是什么?作为一个 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 )
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 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 种可能。
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
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 机制的区块链程序:
~$ 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
则是获得这个符合条件的哈希时所采用的随机幸运数字。
如果你希望马上开始学习以太坊区块链应用开发,可以访问汇智网的教程: