近期的区块链重回热点,如果你想深入了解区块链,那就来看一下本文,手把手教你构建一个自己的区块链。

作者:Captain编译

近期的区块链重回热点,如果你想深入了解区块链,那就来看一下本文,手把手教你构建一个自己的区块链。

弄懂区块链的最快方法-亲自构建一个

看到这篇文章,说明您也是对加密货币的兴起感兴趣,想知道区块链是如何工作的和其背后运行的技术原理。

但是想要搞懂区块链并不容易。我在众多的视频中苦苦钻研,跟随着漏洞百出的教程,经历着因区块链相关案例太少而产生的挫败感。

我喜欢从行动中学习。它迫使我从代码层面处理问题,从而解决问题。如果您和我一样做,那么在本指南的最后,您将拥有一个运行正常的区块链,并对它们的工作原理有深入的了解。

上手准备

请记住,区块链是一个不可变的、连续的记录链,称为块。它们可以包含事务、文件或您喜欢的任何数据。但是重要的是,它们通过使用哈希而被链接在一起。

如果您不确定什么是哈希值,请参考这里。

教程面向的人群?

可以轻松地阅读和编写一些基本的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结尾:

  1. 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. 计算工作量证明
  2. 奖励矿商(我们)通过增加一个交易给予我们1枚硬币
  3. 将新的区块上链 
    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:共识

我们目前已经拥有一个基本的区块链,可以接受交易并允许我们挖掘新的区块。但是区块链的重点在于它们应该去中心化。而且,如果它们是去中心,我们如何确保它们都反映相同的链?这叫做共识问题,如果我们要在网络中拥有多个节点,就必须实现共识算法。

注册新节点

在实现共识算法之前,我们需要一种让节点知道网络上相邻节点的方法。我们网络上的每个节点都应保留网络上其他节点的注册表。

因此,我们将需要更多的端点:

  1. /nodes/register 接受URL形式的新节点列表。
  2. /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

阅读目录(置顶)(长期更新计算机领域知识)

阅读目录(置顶)(长期更新计算机领域知识)

阅读目录(置顶)(长期科技领域知识)

歌谣带你看java面试题

第五十八期:从0到1 手把手教你建一个区块链相关推荐

  1. 第五十八期:AI艺术日渐繁荣,未来何去何从?

    本文的配图都是AI艺术领域领导者.德国艺术家马里奥·克林格曼(Mario Klingemann)利用人工智能创作的作品. 利用人工智能创作而成的画作近年来越来越受瞩目,有的作品甚至能在知名拍卖行拍得高 ...

  2. 鲁大师电动车智能化测评报告第十八期:加量降价,九号完成产品破圈?

    鲁大师第十八期智能化电动车测评排行榜数据来源于鲁大师智慧实验室,测评的车型均为市面上主流品牌的主流车型.截止目前,鲁大师智能化电动车测评的车型高达90余种,且还在不断增加和丰富中. 1. 测评依据 鲁 ...

  3. OpenCV学习笔记(五十六)——InputArray和OutputArray的那些事core OpenCV学习笔记(五十七)——在同一窗口显示多幅图片 OpenCV学习笔记(五十八)——读《Mast

    OpenCV学习笔记(五十六)--InputArray和OutputArray的那些事core 看过OpenCV源代码的朋友,肯定都知道很多函数的接口都是InputArray或者OutputArray ...

  4. [Python从零到壹] 五十八.图像增强及运算篇之图像锐化Sobel、Laplacian算子实现边缘检测

    欢迎大家来到"Python从零到壹",在这里我将分享约200篇Python系列文章,带大家一起去学习和玩耍,看看Python这个有趣的世界.所有文章都将结合案例.代码和作者的经验讲 ...

  5. CSDN 编程竞赛二十八期题解

    竞赛总览 CSDN 编程竞赛二十八期:比赛详情 (csdn.net) 本期竞赛的题目都很简单,但是非常考验读题和编码速度.这一次没有遇到bug,竞赛体验较好. 竞赛题解 题目1.小Q的鲜榨柠檬汁 团建 ...

  6. FME模板兴趣班第十八期(面遮挡处理) 任务小结

    班长欣欣 一.题目说明 对面进行遮挡处理,处理要求如下: 多个面之间存在压盖,要求压盖区域用Level高的遮挡Level低的,并将压盖区域从Level低的面中擦除.如下图所示: 面1.面2.面3 的L ...

  7. COS访谈第十八期:陈天奇

    COS访谈第十八期:陈天奇 [COS编辑部按] 受访者:陈天奇      采访者:何通   编辑:王小宁 简介:陈天奇,华盛顿大学计算机系博士生,研究方向为大规模机器学习.他曾获得KDD CUP 20 ...

  8. 第十八期基金定投-全民抗疫

    5请阅读本计划之前,仔细阅读以下内容: 1. 本计划仅供参考,市场有风险,投资需谨慎 2. 此计划的预计年限是2-3年,因此投资的钱是稳定而不用的钱 3. 请打算按此计划购买的朋友,再三考虑,是否熬得 ...

  9. CSDN 编程竞赛十八期题解

    竞赛总览 CSDN 编程竞赛十八期:比赛详情 (csdn.net) 感觉这次竞赛的题目有点偏数学了,尤其是第三道题,输入+输出就完事了,答案是一个数学公式. 竞赛题解 题目1.单链表排序 单链表的节点 ...

最新文章

  1. 51单片机教程哪个好?
  2. Emacs Org-mode学习笔记
  3. 【Python】内置方法pop
  4. mysql之子查询作业
  5. 学习强制删除正在运行的文件
  6. 蓝桥备赛第二周 前缀和
  7. C/C++笔试题目大全
  8. 13.multi-term-vector请求
  9. 数组元素的修改会影响到转换过来的结合_数组长度属性背后有什么魔力?
  10. 在 VirtualBox 中 CentOS 网络设置
  11. bootstrap panel 布局
  12. 怎样用javascript给控件赋值,使在服务器端得到此控件的值?或怎样将前台的(或js)中的值传递到后台
  13. 工信部回应“网传4G降速”:从未要求运营商降速
  14. 找出所有子集的异或总和再求和
  15. c语言字符输出128,如何将128位整数转换为C中的十进制ASCII字符串?
  16. noip模拟赛 猜数字
  17. GDB 调试 ---转 比较全的东东
  18. PDF转码html有乱码,PDF转换成为Word内容出现乱码怎么办
  19. java面试之简述一下 Java 垃圾回收机制?
  20. vue 自定义打印CLodop

热门文章

  1. 远程桌面不能连接,提示awgina.dll取代错误的解决办法
  2. linux下解压 cpio.gz格式文件
  3. 太阳系八大行星碰撞的视频_火星的身世:从太阳系的起源说起
  4. html显示hdf5文件,python读取hdf5文件
  5. surfaceview结束后怎么处理_污泥压滤机处理后的污泥怎么处置
  6. python二维数组操作_Python二维数组应用与操作
  7. php手机端多图预览上传,JS实现多图预览上传的实例代码
  8. 在HTML中怎么去掉超链接的下划线?
  9. make -j32 21 | tee show.log
  10. [react] React的render中可以写{if else}这样的判断吗?