想要理解区块链如何工做最好的方式就是制做一个啦。node
Learn Blockchains by Building One 加密货币与区块链(三):什么是信任python
ruby, blockchain, consensusgit
做为一个匪菜大军中的一员,不了解区块链是不能称为一个合格的匪菜的。空气币的火热,让更多的匪菜充满了渴望,彷佛咱们也须要了解一些在这个背后的基础。了解这个其实并非很简单,由于更多的匪菜喜欢看到的是绿色的涨幅,而不是背后的技术,你能够在得(不)道上面找到不少奇怪的Talk,可是你的确得不到。
我喜欢边作边学,看完下面的例子,我相信你能成为一颗不同的匪菜。github
区块链(Blockchain),顾名思义就是由块组成的链,每个块就是一些数据,而后经过一种手法把这个块链接起来,就是用一个哈希函数 H,把block B[i]的哈希值 H(B[i]) 包含在下一个 block B[i+1] 里。H 具备单向性,也就是知道 B 就很容易算出 H(B),可是反过来若是只知道 H(B) 的值很难构造出一个知足条件的 B。固然啦,这个其实就是一个链表,POC。这样作的结果就意味着若是其中任何一块被修改了。而由于 H(B0) 是 B1 的一部分,因此致使 H(B1) 也要跟着变。若是有人要修改记录在这个链上的数据,就须要修改后面全部的块。这个就叫作Merkle List。若是你用过Gayhub,那么你应该也知道,Git存储的方式就是基于Merkle List。web
在你开始以前,我和大家说这篇教程使用的是Ruby语言写的。这里用了一些很简单的库来帮助咱们能够作一个简单的Web Application,cuba
, faraday
。这里就很少作解释了。算法
在开始前,你能够在这里看到源代码传送门json
咱们在这里建立一个Blockchain的Blueprint安全
class Blockchain
end
复制代码
Emmmm, that was a joke.ruby
class Blockchain
class << self
def hash blk
end
end
def initialize
end
def new_block
end
def new_transacction
end
def last_block
end
end
复制代码
咱们的Blockchain是用来对链初始化,而后添加一些经常使用的操做的,new_block, new_transaction, hash等。bash
那么一个Block应该是什么样子的呢?
block = {
'index': 1,
'timestamp': 1506057125,
'transactions': [
{
'sender': "8527147fe1f5426f9dd545de4b27ee00",
'recipient': "a77f5cdfa2934df3954a5c7c7da5df1f",
'amount': 5,
}
],
'proof': 324984774000,
'previous_hash': "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
}
复制代码
接下来咱们须要建立新的块了,在咱们的Blockchain初始化的时候,咱们须要给他一个创世块,一个没有祖先的块,同时咱们也须要给创世块增长一个 proof
,这是挖矿的结果,我稍后再说啦。
咱们如今须要了解什么是 PoW (Proof of Work),顾名思义就是新的区块是如何产生或者如何被挖出来的,它存在的目的就是发现可以解决一个问题的数字,这个数字须要具有两个属性,难找和易验证。
咱们举一个简单的例子
from hashlib import sha256
x = 5
y = 0 # We don't know what y should be yet...
while sha256(f'{x*y}'.encode()).hexdigest()[-1] != "0":
y += 1
print(f'The solution is y = {y}')
复制代码
那么结果就是21,在比特币中,PoW的算法叫作Hashcash,和上面的例子是差很少的,矿工们算出结果以后是会被奖赏的,矿工会在一个交易中收到一个币。
PoW算法是很简单的,那么咱们如今的题目就是:
找到一个数字p,使得hash(pp')的结果包含是由4个0开头的。这里p表明以前的proof,p'是新的proof
...
def PoW(last_proof)
# proof of work algorithm (PoW)
proof = 0
while valid_proof?(last_proof, proof) == false
proof += 1
end
proof
end
...
private
def valid_proof?(last_proof, proof)
Digest::SHA256.hexdigest("#{last_proof}#{proof}")[0..3] == "0000"
end
复制代码
若是你须要修改算法的难度,那么你只须要修改以0开头的个数就能够了。
咱们这里用 cuba
作一个很小的 web 服务,它主要包含了三个功能
node_identifier = SecureRandom.hex(20)
blockchain = Blockchain.new
Cuba.define do
on get do
on 'mine' do
last_block = blockchain.last_block
last_proof = last_block[:proof]
proof = blockchain.PoW(last_proof)
blockchain.new_transaction("0", node_identifier.to_s, 1)
previous_hash = Blockchain.hash(last_block)
blk = blockchain.new_block(proof, previous_hash)
data = {
message: 'new block forged',
index: blk[:index],
transactions: blk[:transactions],
proof: blk[:proof],
previous_hash: blk[:previous_hash]
}
as_json {{ data: data }}
end
on 'chain' do
as_json do
{
data: {
chain: blockchain.chain,
length: blockchain.chain.count
}
}
end
end
end
on post do
on 'transactions/new' do
on param('sender'), param('recipient'), param('amount') do |sender, recipient, amount|
index = blockchain.new_transaction(sender,recipient, amount.to_f)
as_json {{ data: "transaction will be added to block #{index}"}}
end
on true do
as_json 400 do
{ error: 'missing params'}
end
end
end
end
end
复制代码
接下来就能够跑一跑咱们的简单的服务器啦
thin start -p 3000
复制代码
你可使用 Postman 或者 curl 来调用咱们的服务。
共识,这个很重要,在分布式系统中,你须要保证数据的一致性,因此你须要知道咱们须要经过一种什么样的算法来保证咱们始终指向一条链。
def valid_chain?(chain)
last_block = chain[0]
current_index = 1
while current_index < chain.size
block = chain[current_index]
puts "count 1"
# Check that the hash of the block is correct
if block[:previous_hash] != Blockchain.hash(last_block)
return false
end
# Check that the Proof of Work is correct
if !valid_proof?(last_block[:proof], block[:proof])
return false
end
last_block = block
current_index += 1
end
return true
end
def resolve_conflicts
new_chain = nil
# Only looking for chains longer than this one
max_length = @chain.count
aval = @nodes.delete @current_node
aval.each do |node|
conn = Faraday.new(url: "http://#{node}/chain")
res = conn.get do |conn_get|
conn_get.options.open_timeout = 15
conn_get.options.timeout = 15
end
if res.status == 200
content = JSON.parse(res.body, symbolize_names: true)
length = content[:data][:length]
chain = content[:data][:chain]
ap "node #{node} len #{length > max_length} valid_chain #{valid_chain?(chain)}"
if length > max_length && valid_chain?(chain)
max_length = length
new_chain = chain
end
end
end
if new_chain
puts "found new chain here"
@chain = new_chain
return true
end
return false
end
# in server.rb
on 'nodes/resolve' do
blockchain.current_node = "#{env["SERVER_NAME"]}:#{env["SERVER_PORT"]}"
resolved = blockchain.resolve_conflicts
if resolved
data = {
message: "our chain was replaced",
new_chain: blockchain.chain
}
else
data = {
message: "our chain was authorized",
new_chain: blockchain.chain
}
end
as_json {{ data: data }}
end
复制代码
valid_chain? 用来判断这个链是否正确的,能够看到 resolve_conflicts 里面主要用到的判断就是链长和是不是一条合格的链,若是更长而且合法,那么当前的链就替换为该链。不过这里值得注意的是,你须要把nodes中的本身的节点移除掉,这样实际上是能够提升速度,并能够解决一个问题,单线程的机器中,你不能call本身。(会有一个OpenTimeOUt的错误,老是这里你把本身自己的节点去掉就能够了。)
到这里,你就能够用另外一台机器或者使用不一样的端口来模拟多个节点,我这边 Rakefile 里面默认是经过单机不一样端口来模拟多个节点。
完整的代码请参考传送门
但愿这个东西可以给你一些启发。
固然啦,最后送上一句话。
若是你认为技术能解决安全问题,那么你既不懂安全也不懂技术。 :)
有不少关于区块链的文章都说「区块链解决的核心问题是信任问题」,可是我没有看到有人回答了关键的问题:到底什么是信任?什么是所谓「信任问题」,它存不存在?什么算是「解决了信任问题」?事实上若是在 Google 上搜一下这句话,会找到大量的复制粘贴和人云亦云。Brice Schneier 书里那句话改一改也是适用的,若是有人认为技术能解决信任问题,那么他恐怕既不懂信任也不懂技术。