【51CTO.com快译】近期的区块链重回热点,如果你想深入了解区块链,那就来看一下本文,手把手教你构建一个自己的区块链。
弄懂区块链的最快方法-亲自构建一个
看到这篇文章,说明您也是对加密货币的兴起感兴趣,想知道区块链是如何工作的和其背后运行的技术原理。
但是想要搞懂区块链并不容易。我在众多的视频中苦苦钻研,跟随着漏洞百出的教程,经历着因区块链相关案例太少而产生的挫败感。
我喜欢从行动中学习。它迫使我从代码层面处理问题,从而解决问题。如果您和我一样做,那么在本指南的最后,您将拥有一个运行正常的区块链,并对它们的工作原理有深入的了解。
上手准备
请记住,区块链是一个不可变的、连续的记录链,称为块。它们可以包含事务、文件或您喜欢的任何数据。但是重要的是,它们通过使用哈希而被链接在一起。
如果您不确定什么是哈希值,请参考这里。
教程面向的人群?
可以轻松地阅读和编写一些基本的Python,并且对HTTP请求的工作方式有所了解,因为本文将通过HTTP与区块链进行交流。
需要准备什么?
确保已安装 Python 3.6 +(以及pip)。您还需要安装Flask和很棒的Requests库:
- pip install Flask==0.12.2 requests==2.18.4
您还需要HTTP客户端,例如Postman或cURL。
源代码可在此处获得。
步骤1:构建一个区块链
打开你最喜欢的文本编辑器或IDE,我个人喜欢 PyCharm。创建一个名为blockchain.py的新文件。我们将只使用一个文件,但是如果您有困惑了,可以随时参考源代码。
展示区块链
我们将创建一个Blockchain class,它的构造函数会创建一个初始的空列表(用于存储我们的区块链),另一个用于存储事务。这是脚本:
- class Blockchain(object):
- def __init__(self):
- self.chain = []
- self.current_transactions = []
- def new_block(self):
- # Creates a new Block and adds it to the chain
- pass
- def new_transaction(self):
- # Adds a new transaction to the list of transactions
- pass
- @staticmethod
- def hash(block):
- # Hashes a Block
- pass
- @property
- def last_block(self):
- # Returns the last Block in the chain
- pass
Blockchain class 的demo
我们的Blockchain class负责管理链。它将存储事物,并具有一些将新块添加到链中的辅助方法。让我们来尝试一些新的方法吧。
Block像什么?
每个Block都有以下的内容:
- 一个索引,
- 一个时间戳(Unix时间),
- 一个交易列表,
- 一个证明(稍后会有更多说明)
- 前一个区块的哈希值。
单个区块示例:
- block = {
- 'index': 1,
- 'timestamp': 1506057125.900785,
- 'transactions': [
- {
- 'sender': "8527147fe1f5426f9dd545de4b27ee00",
- 'recipient': "a77f5cdfa2934df3954a5c7c7da5df1f",
- 'amount': 5,
- }
- ],
- 'proof': 324984774000,
- 'previous_hash': "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
- }
在这一点上,链的概念应该很明显--每个新区块本身都包含前一个区块的哈希值。这是至关重要的,因为这给予了区块链的不可篡改性:如果攻击者破坏了链中较早的区块,则后续所有的区块都将包含不正确的哈希值。
不知道你能理解嘛,请多花些时间去理解它—这是区块链的核心思想。
添加事物到区块中
我们需要一种将事物添加到区块的方法。我们的new_transaction()方法有效,而且很简单:
- class Blockchain(object):
- ...
- def new_transaction(self, sender, recipient, amount):
- """
- Creates a new transaction to go into the next mined Block
- :param sender: Address of the Sender
- :param recipient: Address of the Recipient
- :param amount: Amount
- :return: The index of the Block that will hold this transaction
- """
- self.current_transactions.append({
- 'sender': sender,
- 'recipient': recipient,
- 'amount': amount,
- })
- return self.last_block['index'] + 1
在new_transaction()添加一个新的交易到列表中,它将返回到交易将被添加进去、即将被开采的区块的索引。这对之后提交交易的用户是很有用处的。
创建新区块
当我们Blockchain被实例化,我们需要用一个genesis块来播种它——一个没有前处理的块。还需要向我们的创世区块添加一个“证明”,这是挖掘(或工作证明)的结果。稍后我们将详细讨论挖矿。
除了在构造函数中创建genesis块,我们还将充实new_block()、new_transaction()和hash()的方法:
- import hashlib
- import json
- from time import time
- class Blockchain(object):
- def __init__(self):
- self.current_transactions = []
- self.chain = []
- # Create the genesis block
- self.new_block(previous_hash=1, proof=100)
- def new_block(self, proof, previous_hash=None):
- """
- Create a new Block in the Blockchain
- :param proof: The proof given by the Proof of Work algorithm
- :param previous_hash: (Optional) Hash of previous Block
- :return: New Block
- """
- block = {
- 'index': len(self.chain) + 1,
- 'timestamp': time(),
- 'transactions': self.current_transactions,
- 'proof': proof,
- 'previous_hash': previous_hash or self.hash(self.chain[-1]),
- }
- # Reset the current list of transactions
- self.current_transactions = []
- self.chain.append(block)
- return block
- def new_transaction(self, sender, recipient, amount):
- """
- Creates a new transaction to go into the next mined Block
- :param sender: Address of the Sender
- :param recipient: Address of the Recipient
- :param amount: Amount
- :return: The index of the Block that will hold this transaction
- """
- self.current_transactions.append({
- 'sender': sender,
- 'recipient': recipient,
- 'amount': amount,
- })
- return self.last_block['index'] + 1
- @property
- def last_block(self):
- return self.chain[-1]
- @staticmethod
- def hash(block):
- """
- Creates a SHA-256 hash of a Block
- :param block: Block
- :return:
- """
- # We must make sure that the Dictionary is Ordered, or we'll have inconsistent hashes
- block_string = json.dumps(block, sort_keys=True).encode()
- return hashlib.sha256(block_string).hexdigest()
上面的内容应该很简单—我添加了一些注释和文档字符串来帮助保持清楚明了。对区块链的表示几乎完成了。但此时,您一定想知道如何创建、锻造或挖掘新的区块。
了解工作量证明
工作算法(PoW)是在区块链上创建或挖掘新区块的方式。PoW的目标是发现可以解决问题的数字。从数字上来说,很难找到该数字,但是很容易被网络上的任何人进行验证。这是工作证明的核心思想。
我们将看一个非常简单的示例来帮助理解。
让我们决定某个整数X乘以另一个Y的哈希必须以0结尾。因此,对于这个简化的示例,让我们修复。在Python中实现:xy0hash(x * y) = ac23dc...0x = 5
- 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}')
解是y = 21。因为,产生的哈希值以0结尾:
- hash(5 * 21)= 1253e9373e ... 5e3600155e860
在比特币中,工作证明算法称为Hashcash。而且与我们上面的示例并没有太大不同。这是矿工为了创建一个新的块而竞相求解的算法。通常,难度由字符串中搜索的字符数决定。然后,通过在交易中获得硬币,矿工将借此获得奖励。
网络能够轻松验证他们的解决方案。
实施基本的工作证明
让我们为区块链实现类似的算法。我们的规则将类似于上面的示例:
找出一个数字 p ,当该数字与上一个块的解决方案进行哈希运算时,将产生一个带有4个前导4个0的哈希。
- import hashlib
- import json
- from time import time
- from uuid import uuid4
- class Blockchain(object):
- ...
- def proof_of_work(self, last_proof):
- """
- Simple Proof of Work Algorithm:
- - Find a number p' such that hash(pp') contains leading 4 zeroes, where p is the previous p'
- - p is the previous proof, and p' is the new proof
- :param last_proof:
- :return:
- """
- proof = 0
- while self.valid_proof(last_proof, proof) is False:
- proof += 1
- return proof
- @staticmethod
- def valid_proof(last_proof, proof):
- """
- Validates the Proof: Does hash(last_proof, proof) contain 4 leading zeroes?
- :param last_proof: Previous Proof
- :param proof: Current Proof
- :return: True if correct, False if not.
- """
- guess = f'{last_proof}{proof}'.encode()
- guess_hash = hashlib.sha256(guess).hexdigest()
- return guess_hash[:4] == "0000"
要调整算法的难度,我们可以修改前导零的数量。但是4就足够了。您会发现,添加单个前导零会极大地缩短寻找解决方案所需的时间。
我们的类快要完成了,我们已经准备好开始使用HTTP请求与其进行交互。
步骤2:我们的区块链作为API
我们将使用Python Flask框架。这是一个微框架,可轻松将端点映射到Python函数。这使我们可以使用HTTP请求通过web与区块链进行通信。
我们将创建三种方法:
- /transactions/new 创建一个新的交易块。
- /mine 告诉我们的服务器挖掘一个新块。
- /chain 返回完整的区块链。
设置Flask
我们的“服务器”将在我们的区块链网络中形成单个节点。让我们创建一个demo:
- import hashlib
- import json
- from textwrap import dedent
- from time import time
- from uuid import uuid4
- from flask import Flask
- class Blockchain(object):
- ...
- # Instantiate our Node
- app = Flask(__name__)
- # Generate a globally unique address for this node
- node_identifier = str(uuid4()).replace('-', '')
- # Instantiate the Blockchain
- blockchain = Blockchain()
- @app.route('/mine', methods=['GET'])
- def mine():
- return "We'll mine a new Block"
- @app.route('/transactions/new', methods=['POST'])
- def new_transaction():
- return "We'll add a new transaction"
- @app.route('/chain', methods=['GET'])
- def full_chain():
- response = {
- 'chain': blockchain.chain,
- 'length': len(blockchain.chain),
- }
- return jsonify(response), 200
- if __name__ == '__main__':
- app.run(host='0.0.0.0', port=5000)
简要说明:
- 第15行:实例化我们的节点;Flask更多信息。
- 第18行:为节点创建一个随机名称。
- 第21行:实例化Blockchain类。
- 第24–26行:创建/mine端点,这是一个GET请求。
- 第28–30行:创建/transactions/new端点,这是一个POST请求,因为我们将向它发送数据。
- 第32–38行:创建/chain端点,该端点返回完整的区块链。
- 40-41行:在端口5000上运行服务器。
交易端点
这就是交易请求的样子。这是用户发送到服务器的内容:
- {
- "sender": "my address",
- "recipient": "someone else's address",
- "amount": 5
- }
由于我们已经有了用于将事务添加到块中的类方法,因此其余操作很简单。让我们编写添加事务的函数:
- import hashlib
- import json
- from textwrap import dedent
- from time import time
- from uuid import uuid4
- from flask import Flask, jsonify, request
- ...
- @app.route('/transactions/new', methods=['POST'])
- def new_transaction():
- values = request.get_json()
- # Check that the required fields are in the POST'ed data
- required = ['sender', 'recipient', 'amount']
- if not all(k in values for k in required):
- return 'Missing values', 400
- # Create a new Transaction
- index = blockchain.new_transaction(values['sender'], values['recipient'], values['amount'])
- response = {'message': f'Transaction will be added to Block {index}'}
- return jsonify(response), 201
创建交易的方法
挖矿端点
我们的挖矿终点是奇迹发生的地方,这很容易上手。
它必须做三件事:
- 计算工作量证明
- 奖励矿商(我们)通过增加一个交易给予我们1枚硬币
- 将新的区块上链
- import hashlib
- import json
- from time import time
- from uuid import uuid4
- from flask import Flask, jsonify, request
- ...
- @app.route('/mine', methods=['GET'])
- def mine():
- # We run the proof of work algorithm to get the next proof...
- last_block = blockchain.last_block
- last_proof = last_block['proof']
- proof = blockchain.proof_of_work(last_proof)
- # We must receive a reward for finding the proof.
- # The sender is "0" to signify that this node has mined a new coin.
- blockchain.new_transaction(
- sender="0",
- recipient=node_identifier,
- amount=1,
- )
- # Forge the new Block by adding it to the chain
- previous_hash = blockchain.hash(last_block)
- block = blockchain.new_block(proof, previous_hash)
- response = {
- 'message': "New Block Forged",
- 'index': block['index'],
- 'transactions': block['transactions'],
- 'proof': block['proof'],
- 'previous_hash': block['previous_hash'],
- }
- return jsonify(response), 200
注意,已开采区块的接收者是我们节点的地址。而且,我们在这里所做的大部分工作只是与Blockchain类上的方法进行交互。至此,我们已经完成,可以开始与区块链进行交互了。
步骤3:与区块链交互
您可以使用普通的旧cURL或Postman通过网络与我们的API进行交互。
启动服务器:
- $ python blockchain.py
- * Running on http://127.0.0.1:5000/ (按CTRL + C退出)
让我们尝试通过向http://localhost:5000/mine:发出GET请求来挖掘一个块:
使用邮递员发出GET请求让我们创建一个新的事务,通过发送POST请求到http://localhost:5000/transactions/new,其主体包含我们的事务结构:
使用邮递员发出POST请求
如果您不使用Postman,可以使用cURL发出等效请求:http://localhost:5000/chain:
- {
- "chain": [
- {
- "index": 1,
- "previous_hash": 1,
- "proof": 100,
- "timestamp": 1506280650.770839,
- "transactions": []
- },
- {
- "index": 2,
- "previous_hash": "c099bc...bfb7",
- "proof": 35293,
- "timestamp": 1506280664.717925,
- "transactions": [
- {
- "amount": 1,
- "recipient": "8bbcb347e0634905b0cac7955bae152b",
- "sender": "0"
- }
- ]
- },
- {
- "index": 3,
- "previous_hash": "eff91a...10f2",
- "proof": 35089,
- "timestamp": 1506280666.1086972,
- "transactions": [
- {
- "amount": 1,
- "recipient": "8bbcb347e0634905b0cac7955bae152b",
- "sender": "0"
- }
- ]
- }
- ],
- "length": 3
- }
步骤4:共识
我们目前已经拥有一个基本的区块链,可以接受交易并允许我们挖掘新的区块。但是区块链的重点在于它们应该去中心化。而且,如果它们是去中心,我们如何确保它们都反映相同的链?这叫做共识问题,如果我们要在网络中拥有多个节点,就必须实现共识算法。
注册新节点
在实现共识算法之前,我们需要一种让节点知道网络上相邻节点的方法。我们网络上的每个节点都应保留网络上其他节点的注册表。
因此,我们将需要更多的端点:
- /nodes/register 接受URL形式的新节点列表。
- /nodes/resolve 实现我们的共识算法,该算法可以解决所有冲突-确保节点具有正确的链。
我们需要修改区块链的构造函数,并提供一种注册节点的方法:
- ...
- from urllib.parse import urlparse
- ...
- class Blockchain(object):
- def __init__(self):
- ...
- self.nodes = set()
- ...
- def register_node(self, address):
- """
- Add a new node to the list of nodes
- :param address: Address of node. Eg. 'http://192.168.0.5:5000'
- :return: None
- """
- parsed_url = urlparse(address)
- self.nodes.add(parsed_url.netloc)
一种将相邻节点添加到网络的方法
请注意,我们使用了a set()来保存节点列表。这是一种廉价方法,它确保添加新节点是幂等,这意味着无论我们添加特定节点多少次,它都将只出现一次。
实施共识算法
如上所述,当一个节点与另一节点具有不同的链时会发生冲突。为了解决这个问题,我们规定最长的有效链是具有最高权威的。换句话说,网络上最长的链是事实链。使用此算法,我们可以在网络中的节点之间达成共识。
- ...
- import requests
- class Blockchain(object)
- ...
- def valid_chain(self, chain):
- """
- Determine if a given blockchain is valid
- :param chain: A blockchain
- :return: True if valid, False if not
- """
- last_block = chain[0]
- current_index = 1
- while current_index < len(chain):
- block = chain[current_index]
- print(f'{last_block}')
- print(f'{block}')
- print("\n-----------\n")
- # Check that the hash of the block is correct
- if block['previous_hash'] != self.hash(last_block):
- return False
- # Check that the Proof of Work is correct
- if not self.valid_proof(last_block['proof'], block['proof']):
- return False
- last_block = block
- current_index += 1
- return True
- def resolve_conflicts(self):
- """
- This is our Consensus Algorithm, it resolves conflicts
- by replacing our chain with the longest one in the network.
- :return: True if our chain was replaced, False if not
- """
- neighbours = self.nodes
- new_chain = None
- # We're only looking for chains longer than ours
- max_length = len(self.chain)
- # Grab and verify the chains from all the nodes in our network
- for node in neighbours:
- response = requests.get(f'http://{node}/chain')
- if response.status_code == 200:
- length = response.json()['length']
- chain = response.json()['chain']
- # Check if the length is longer and the chain is valid
- if length > max_length and self.valid_chain(chain):
- max_length = length
- new_chain = chain
- # Replace our chain if we discovered a new, valid chain longer than ours
- if new_chain:
- self.chain = new_chain
- return True
- return False
第一种方法valid_chain()负责通过检查每个块并验证哈希和检验链是否有效。
resolve_conflicts()是一种方法,它会检查我们所有的相邻节点,下载它们的链并使用上述方法验证它们。如果找到有效链,其长度大于我们的长度,我们将替换它。
让我们将两个端点注册到我们的API,一个端点用于添加相邻节点,另一个端点用于解决冲突:
- @app.route('/nodes/register', methods=['POST'])
- def register_nodes():
- values = request.get_json()
- nodes = values.get('nodes')
- if nodes is None:
- return "Error: Please supply a valid list of nodes", 400
- for node in nodes:
- blockchain.register_node(node)
- response = {
- 'message': 'New nodes have been added',
- 'total_nodes': list(blockchain.nodes),
- }
- return jsonify(response), 201
- @app.route('/nodes/resolve', methods=['GET'])
- def consensus():
- replaced = blockchain.resolve_conflicts()
- if replaced:
- response = {
- 'message': 'Our chain was replaced',
- 'new_chain': blockchain.chain
- }
- else:
- response = {
- 'message': 'Our chain is authoritative',
- 'chain': blockchain.chain
- }
- return jsonify(response), 200
此时,您可以根据需要使用其他计算机,并在网络上启动不同的节点。或使用同一台计算机上的不同端口启动进程。我在机器上的另一个端口上旋转了另一个节点,并将其注册到当前节点。因此,我有两个节点:http://localhost:5000和http://localhost:5001。
注册新节点
然后,我在节点2上挖到了一些新区块,以确保链更长。之后,对GET /nodes/resolve在节点1上进行了调用,在该节点上,该链被共识算法替换:
工作中的共识算法
目前为止已经接近成功;可以去找一些朋友一起帮助测试您的区块链。
原文标题:Learn Blockchains by Building One,作者:Daniel van Flymen
【51CTO译稿,合作站点转载请注明原文译者和出处为51CTO.com】
本文转载自网络,原文链接:http://www.51cto.com/