走进区块链(一):用Python实现第一个区块链小程序

源代码链接:https://github.com/dvf/blockchain

大家好!这是本人踩在巨人的肩膀上,实现的第一个区块链小程序。在实现代码的过程中,我遇到了很多问题,不过,幸好都解决了。在这里,我会尽可能的将操作步骤写得更详细。接下来,让我们一起走进区块链吧!

一、初识区块链

区块链是由不可变的、有顺序记录的区块组成。他们可以包含交易数据、文件数据或者其他你想要记录的数据。不过最重要的是,这些区块通过哈希表链接在一起。

提到哈希表,当然离不开散列函数哈希函数只是一个接收输入值的函数,并从该输入创建一个输入值以确定的输入值。对于任何x输入值,只要运行哈希函数,您将始终收到相同的y输出值。这样,每个输入都有一个确定的输出。

二、开发环境及工具

1、Python 3.6以上:下载安装教程请参考 https://www.cnblogs.com/Yanjy-OnlyOne/p/9764143.html

2、开发工具:本人喜欢用PyCharm,下载安装教程请参考如下链接

      https://jingyan.baidu.com/article/636f38bb9d8e27d6b84610ba.html

    (注意:请在PyCharm中安装好Flask以及Requests库)

3、HTTP客户端:比如 Postman,下载安装教程请参考https://www.cnblogs.com/xiaxiaoxu/p/8858437.html

三、开始创建区块链

在PyCharm中,创建一个新的文件,命名为 blockchain.py。整个项目,我们都只会用到这一个文件。

1、创建Blockchain类

首先,创建一个Blockchain类,在其构造函数中创建了两个空列表,一个用于储存区块链,一个用于储存交易。

在这里,Blockchain参数的作用是管理区块链,可以存储交易信息和添加区块。

​
class Blockchain(object):
    def __init__(self):
        self.chain = []
        self.current_transactions = []

    def new_block(self):
        # 创建一个新的区块,并将其加入到链中
        pass

    def new_transaction(self):
        # 向交易列表中添加一个新的交易
        pass

    @staticmethod
    def hash(block):
        # Hashes a Block 散列块
        pass

    @property
    def last_block(self):
        # 返回区块链中最新的区块
        pass

在区块链中,每一个区块包含一个索引、一个时间戳、一个交易列表、一个证明(之后更多)和前一个区块的哈希值。如下所示:

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):
        """
        生成新交易信息,这个交易信息将加入到下一个待挖的区块中

        :param sender: <str> Address of the Sender
        :param recipient: <str> Address of the Recipient
        :param amount: <int> Amount
        :return: <int> 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

在区块链创建完成后,我们需要创建一个创世区块(也就是区块链上的第一个区块)。当然,创世区块也需要被证明,这需要通过PoW的挖矿机制。除了在构造函数中创建创世区块,我们还需要用new_block()、new_transaction() 和 hash()参数对其进行完善。代码如下:

import hashlib
import json
from time import time


class Blockchain(object):
    def __init__(self):
        self.current_transactions = []
        self.chain = []

        # 创建创世区块
        self.new_block(previous_hash=1, proof=100)

    def new_block(self, proof, previous_hash=None):
        """
        生成新块
        :param proof: <int> The proof given by the Proof of Work algorithm
        :param previous_hash: (Optional) <str> Hash of previous Block
        :return: <dict> 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]),
        }

        # 重置当前交易列表
        self.current_transactions = []

        self.chain.append(block)
        return block

    def new_transaction(self, sender, recipient, amount):
        """
        生成新交易信息,此交易信息将加入到下一个待挖的区块中
        :param sender: <str> Address of the Sender
        :param recipient: <str> Address of the Recipient
        :param amount: <int> Amount
        :return: <int> 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):
        """
        生成块的 SHA-256 hash值
        :param block: <dict> Block
        :return: <str>
        """

        # 我们必须确保字典是有序的,否则我们会有不一致的哈希值
        block_string = json.dumps(block, sort_keys=True).encode()
        return hashlib.sha256(block_string).hexdigest()

接下来,我们来看看挖矿到底是如何进行的?在此之前,我们需要先理解工作量证明(PoW),因为新的区块需要工作量证明算法来构造。工作量证明的目的就是找出一个符合特定条件的数字,这个数字很难计算出来,但容易验证。这就是工作量证明的核心思想。

那么,为了方便大家理解,我们举个例子:

某个整数 x 乘以另外一个数 y ,所得结果的哈希值必须是以 0 结尾,即:hash(x * y) = ac23dc...0。所以,我们的目标是找到满足这个条件的一个 y 值。为了方便理解,我们暂定x=5。下面我们就用Python来做这样一个运算:

from hashlib import sha256

x = 5
y = 0
while sha256(f'{x * y}'.encode()).hexdigest()[-1] != "0":
    y += 1
print(f'The solution is y = {y}')

最终,计算结果是 y=21。因此,生成的以 0 结尾的哈希值是:

hash(5* 21) = 1253e9373e781b7500266caa55150e08e210bc8cd8cc70d89985e3600155e860

在比特币中,PoW算法被称为Hashcash,原理跟上面例子差不多。矿工们为了能创建一个新区块,铆足劲儿做着上面的数学题(只有胜出者才能添加区块)。一般而言,证明的难度取决于字符串中搜索的字符数量,先找到正确数字的矿工就能够在每笔交易中获得比特币作为奖励。

下面在我们刚刚创建好的区块链上,来实现一个相似的工作量证明算法。规则与上边那个简单的例子相似,找到一个数字 p ,它和前边一个区块的解决数字进行散列,生成前4位为 0 的哈希值。代码如下:

​
import hashlib
import json

from time import time
from uuid import uuid4


class Blockchain(object):
    ...

    def proof_of_work(self, last_proof):
        """
        简单的工作量证明:
         - 查找一个 p' 使得 hash(pp') 以4个0开头
         - p 是上一个块的证明,  p' 是当前的证明
        :param last_proof: <int>
        :return: <int>
        """

        proof = 0
        while self.valid_proof(last_proof, proof) is False:
            proof += 1

        return proof

    @staticmethod
    def valid_proof(last_proof, proof):
        """
        验证证明: 是否hash(last_proof, proof)以4个0开头?
        :param last_proof: <int> Previous Proof
        :param proof: <int> Current Proof
        :return: <bool> True if correct, False if not.
        """

        guess = f'{last_proof}{proof}'.encode()
        guess_hash = hashlib.sha256(guess).hexdigest()
        return guess_hash[:4] == "0000"

我们可以通过修改哈希值前 0 的数量,来调整算法的难度。一般来说,4位已经是足够了。每在哈希值前多加一个0,计算所花费的时间将呈指数倍增加。

到这儿,我们的类基本写好了。下面,我们准备通过 HTTP 请求与其交互。

2、创建API

我们打算使用 Python 的 Flask 框架,它是一个轻型框架,可以很容易实现端点到Python函数的映射。这样,我们就可以使用 HTTP 请求通过网页访问我们的区块链了。

我们用以下三个方法创建:

/transactions/new 为一个区块创建一个新的交易;

/mine 告诉我们的服务器开采一个新的区块;

/chain 返回完整的 Blockchain 类。

接下来,我们来搭建Flask框架。我们的服务器会在区块链网络中形成单个节点。下面来创建一些样板代码:

import hashlib
import json
from textwrap import dedent
from time import time
from uuid import uuid4

from flask import Flask


class Blockchain(object):
    ...


# 实例化我们的节点;加载 Flask 框架
app = Flask(__name__)

# 为我们的节点创建一个随机名称
node_identifier = str(uuid4()).replace('-', '')

# 实例化 Blockchain 类
blockchain = Blockchain()

# 创建 /mine 端点,这是一个GET请求
@app.route('/mine', methods=['GET'])
def mine():
    return "We'll mine a new Block"

# 创建 /transactions/new 端点,这是一个 POST 请求,我们将用它来发送数据
@app.route('/transactions/new', methods=['POST'])
def new_transaction():
    return "We'll add a new transaction"

# 创建 /chain 端点,它是用来返回整个 Blockchain 类
@app.route('/chain', methods=['GET'])
def full_chain():
    response = {
        'chain': blockchain.chain,
        'length': len(blockchain.chain),
    }
    return jsonify(response), 200

# 设置服务器运行端口为 5000
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=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()

    # 检查所需字段是否在过账数据中
    required = ['sender', 'recipient', 'amount']
    if not all(k in values for k in required):
        return 'Missing values', 400

    # 创建新交易
    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. 通过打包交易奖励矿工一个币;

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():
    # 我们运行工作证明算法来获得下一个证明
    last_block = blockchain.last_block
    last_proof = last_block['proof']
    proof = blockchain.proof_of_work(last_proof)

    # 由于找到了证据,我们会收到一份奖励
    # sender为“0”,表示此节点已挖掘了一个新货币
    blockchain.new_transaction(
        sender="0",
        recipient=node_identifier,
        amount=1,
    )

    # 将新块添加到链中打造新的区块
    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、实现与 Blockchain 类交互

接下来,我们用Postman 通过网络和刚才生成的 API 进行交互。

首先,启动服务器,也就是运行我们的blockchain.py文件。

然后,在Postman中,通过向以下地址发送请求,我们可以尝试一下挖矿。

下面我们通过向下面链接发送post请求,来创建一个新的交易:http://localhost:5000/transactions/new 。

请求中需要包含我们的交易结构。

完成上面步骤之后,需要重启下服务器。这时候,我挖出了2个区块,获得了3个币的奖励。这里,我们还可以像以下地址发送请求,来对整条链进行检查。

{
    "chain": [
        {
            "index": 1,
            "previous_hash": "1",
            "proof": 100,
            "timestamp": 1556419069.1459346,
            "transactions": []
        },
        {
            "index": 2,
            "previous_hash": "2ec1df0d062b439bce88c4c7134ae901e38ec89551778c1d7e3f89214bc58f50",
            "proof": 68862,
            "timestamp": 1556419347.283694,
            "transactions": [
                {
                    "amount": 1,
                    "recipient": "9f9769c7ebaa449289c4c53fcb2199ba",
                    "sender": "0"
                }
            ]
        },
        {
            "index": 3,
            "previous_hash": "b910a925273a6059fde1bb2b3e85f36b78baba18cdfa03d189fb139592c10e88",
            "proof": 109124,
            "timestamp": 1556419580.4322467,
            "transactions": [
                {
                    "amount": 5,
                    "recipient": "someone-other-address",
                    "sender": "d4ee26eee15148ee92c6cd394edd974e"
                },
                {
                    "amount": 1,
                    "recipient": "9f9769c7ebaa449289c4c53fcb2199ba",
                    "sender": "0"
                }
            ]
        }
    ],
    "length": 3
}

4、形成共识

在上面的步骤中,我们已经创建完成了一个简单的区块链,并且能够实现交易、挖矿等基本功能。 不过,区块链上的节点应该是分散的。 如果它们是分散的,我们究竟如何确保它们记录的都是同一条链? 这就叫共识问题。如果我们的网络中需要多个节点,我们必须实现共识算法。

首先,注册新节点。

在我们实现共识算法之前,需要解决一个问题:在同一个网络上,让其中一个节点知道它的相邻节点有哪些。每一个节点需要网络上的其他节点进行注册。因此,我们将需要更多的节点:

  1. /nodes/register 接受URL形式的新节点列表。

  2. /nodes/resolve 实现我们的共识算法,它可以解决任何争议,保证节点具有正确的链。

下面,我们需要修改Blockchain类的结构,以及找到注册节点实现的方法。

...
from urllib.parse import urlparse
...


class Blockchain(object):
    def __init__(self):
        ...
        self.nodes = set()
        ...

    def register_node(self, address):
        """
        向节点列表添加新节点
        :param address: <str> Address of node. Eg. 'http://192.168.0.5:5000'
        :return: None
        """

        parsed_url = urlparse(address)
        self.nodes.add(parsed_url.netloc)

注意,set()函数用来保存节点列表。 它是确保添加的新节点具有幂等性的方法,意思是无论我们使用这个方法添加特定节点多少次,它都只会出现一次。

先前提到的,当某个节点与另一个节点的记录不一致时,会「打架」。为了解决这个冲突,我们需要制定一个规则,即最长而有效的链是最有权威性的。换句话说,网络上最长的链就是事实。使用这个算法,我们就可以在我们的网络上达成共识。

...
import requests


class Blockchain(object)
    ...

    def valid_chain(self, chain):
        """
        确认指定的区块链是否有效
        :param chain: <list> A blockchain
        :return: <bool> 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")
            # 检查区块的哈希是否正确
            if block['previous_hash'] != self.hash(last_block):
                return False

            # 检查工作证明是否正确
            if not self.valid_proof(last_block['proof'], block['proof']):
                return False

            last_block = block
            current_index += 1

        return True

    # resolve_conflicts()是遍历我们所有相邻节点的方法,会下载它们的链,然后使用上述方法去验证.
    # 如果找到一个有效的链,其长度大于我们的链,就将我们的链条替换为该链.
    def resolve_conflicts(self):
        """
        这是我们的共识算法,它解决了冲突通过用网络中最长的链替换我们的链.
        :return: <bool> True if our chain was replaced, False if not
        """

        neighbours = self.nodes
        new_chain = None

        # 我们只找比我们的链更长的链
        max_length = len(self.chain)

        # 抓取并验证网络中所有节点的链
        for node in neighbours:
            response = requests.get(f'http://{node}/chain')

            if response.status_code == 200:
                length = response.json()['length']
                chain = response.json()['chain']

                # 检查链条长度是否更长,链条是否有效
                if length > max_length and self.valid_chain(chain):
                    max_length = length
                    new_chain = chain

        # 如果我们发现新的有效链条比我们的链条长,请更换链条
        if new_chain:
            self.chain = new_chain
            return True

        return False

下面我们在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。

之后,我重新创建了另一个文件blockchain2.py。此文件与blockchain.py不同的是,端口号改为5001。如下所示:

接下来,我们运行这两个文件。然后,我在第二个节点上挖出了一些新的区块,以确保第二个节点的链条比第一个节点的链条更长。 之后,我在第一个节点上调用 GET / nodes / resolve,使其中链通过共识算法被第二个节点的链条取代:

好了,你的第一个区块链程序就已经创建成功啦!

  • 4
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
node.js Windowv 上安装Node.js Windows 安装包(.msi) : 32 位安装包下载地址 : http://nodejs.org/dist/v0.10.26/node-v0.10.26-x86.msi 64 位安装包下载地址 : http://nodejs.org/dist/v0.10.26/x64/node-v0.10.26-x64.msi 安装步骤: 步骤 1 : 双击下载后的安装包 node-v0.10.26-x86.msi,如下所示: install-node-msi-version-on-windows-step1 步骤 2 : 点击以上的Run(运行),将出现如下界面: install-node-msi-version-on-windows-step2 步骤 3 : 勾选接受协议选项,点击 next(下一步) 按钮 : install-node-msi-version-on-windows-step3 步骤 4 : Node.js默认安装目录为 "C:\Program Files\nodejs\" , 你可以修改目录,并点击 next(下一步): install-node-msi-ve rsion-on-windows-step4 步骤 5 : 点击树形图标来选择你需要的安装模式 , 然后点击下一步 next(下一步) install-node-msi-version-on-windows-step5 步骤 6 :点击 Install(安装) 开始安装Node.js。你也可以点击 Back(返回)来修改先前的配置。 然后并点击 next(下一步): install-node-msi-version-on-windows-step6 安装过程: install-node-msi-version-on-windows-step7 点击 Finish(完成)按钮退出安装向导。 install-node-msi-version-on-windows-step8 检测PATH环境变量是否配置了Node.js,点击开始=》运行=》输入"cmd" => 输入命令"path",输出如下结果: PATH=C:\oraclexe\app\oracle\product\10.2.0\server\bin;C:\Windows\system32; C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\; c:\python32\python;C:\MinGW\bin;C:\Program Files\GTK2-Runtime\lib; C:\Program Files\MySQL\MySQL Server 5.5\bin;C:\Program Files\nodejs\; C:\Users\rg\AppData\Roaming\npm 我们可以看到环境变量中已经包含了C:\Program Files\nodejs\ 检查Node.js版本 node-version-test Windows 二进制文件 (.exe)安装 : 32 位安装包下载地址 : http://nodejs.org/dist/v0.10.26/node.exe 64 位安装包下载地址 : http://nodejs.org/dist/v0.10.26/x64/node.exe 安装步骤 步骤 1 : 双击下载的安装包 Node.exe ,将出现如下界面 : install-node-exe-on-windows-step1 点击 Run(运行)按钮将出现命令行窗口: install-node-exe-on-windows-step21 版本测试 进入 node.exe 所在的目录,如下所示: node-version 如果你获得以上输出结果,说明你已经成功安装了Node.js。 Linux上安装 Node.js Ubuntu 源码安装 以下部分我们将介绍在Ubuntu Linux下安装 Node.js 。 其他的Linux系统,如Centos等类似如下安装步骤。 在 Github 上获取 Node.js 源码: install-node-msi-version-on-linux-step1 install-node-msi-version-on-linux-step2 在完成下载后,将源码包名改为 'node'。 install-node-msi-version-on-linux-step3 修改目录权限: install-node-msi-version-on-linux-step4 使用 './configure' 创建编译文件。 install-node-msi-version-on-linux-step5 编译: make。 install-node-msi-version-on-linux-step6 完成安装: make install。 install-node-msi-version-on-linux-step7 最后我们输入'node --version' 命令来查看Node.js是否安装成功。 install-node-msi-version-on-linux-step8 Ubuntu apt-get命令安装 命令格式如下: sudo apt-get install nodejs sudo apt-get install npm centOS下安装nodejs 1、下载源码,你需要在http://nodejs.org/下载最新的Nodejs版本,本文以v0.10.24为例: cd /usr/local/src/ wget http://nodejs.org/dist/v0.10.24/node-v0.10.24.tar.gz 2、解压源码 tar zxvf node-v0.10.24.tar.gz 3、 编译安装 cd node-v0.10.24 ./configure --prefix=/usr/local/node/0.10.24 make make install 4、 配置NODE_HOME,进入profile编辑环境变量 vim /etc/profile 设置nodejs环境变量,在export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL 一行的上面添加如下内容: #set for nodejs export NODE_HOME=/usr/local/node/0.10.24 export PATH=$NODE_HOME/bin:$PATH :wq保存并退出,编译/etc/profile 使配置生效 source /etc/profile 验证是否安装配置成功 node -v 输出 v0.10.24 表示配置成功 npm模块安装路径 /usr/local/node/0.10.24/lib/node_modules/ 注:Nodejs 官网提供了编译好的Linux二进制包,你也可以下载下来直接应用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值