程序员们,只需三步,教你搭建一个区块链程序 | 经典好文 - Go语言中文社区

程序员们,只需三步,教你搭建一个区块链程序 | 经典好文


640?wx_fmt=jpeg

区块链,大家或许都不陌生,或多或少都对它有一些了解。不过,这些了解可能都是支离破碎的。当问及其中一些概念是如何实现的,你可能就「蒙圈」了。那想了解其中的实现细节怎么办呢?


这篇文章可以手把手的教你搭建一个区块链程序,让你从技术层面,详细了解区块链的实现细节。我相信你把这篇文章的代码跑一遍,你会有一种「哦!原来是这么实现的」想法。下面,我们就来试试吧。

作者 | Daniel van Flymen

译者 | Aholiab、科科



你会点开这篇文章,说明你跟我一样,眼看加密货币价格的暴涨,很想弄清楚区块链到底是什么?背后有哪些技术。


不过,要完全理解区块链不是件容易的事,起码对我而言。为了搞懂区块链,我看过大量的视频、研究过各种教程和为数不多的几个案例,整个过程虐心之极。


所以,我决定在实践中学习。在实践中学习的一大好处是它能逼着你去理解区块链最底层的原理,并且容易让人坚持下去。如果你也想试试这个方法的话,建议你好好读完这篇文章,跟着步骤一步步地去操作。这样,你不仅可以亲自开发出一个功能完备的区块链,同时也搞清楚了区块链的机制到底是什么?



准备工作


在开始之前,我们需要做些准备工作,搞清楚一些问题。


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

 

什么是散列?散列函数是一个输入值函数,从该输入创建一个确定输入值的输出值。更多解释可以点击下边这个链接:

https://learncryptography.com/hash-functions/what-are-hash-functions


这篇文章适合谁,首先Python程序员,你只要能轻松地读写一些基本的Python代码就可以了;第二是HTTP程序员,因为我们接下来讲到的区块链,是构建在HTTP上面的,这需要你起码要了解HTTP请求的工作原理。


我需要做什么?首先要确保安装了Python 3.6以上的环境和Flask,此外还需要安装一个碉堡的Requests库。版本信息如下:


 pip install Flask==0.12.2 requests==2.18.4 


哦对了,你还需要一个HTTP客户端,比如 Postman 或者 cURL


在哪里下载完整代码,请猛戳:

https://github.com/dvf/blockchain


下面跟着我一步一步来操作吧。



第一步:创建区块链


打开你常用的编辑器,我个人比较喜欢PyCharm。创建一个新的文件,命名为 blockchain.py。整个项目,我们都只会用到这一个文件。有不清楚的地方,可以参考源代码。


表示一个区块链


我们将创建一个 Blockchain 类,它的构造函数里创建了一个初始为空的列表(用于存储我们的区块链),和一个存储交易的列表。下边是这个类的代码:


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参数的作用是管理区块链,也用于存储交易信息和添加区块的方式。


区块到底长什么样?


每一个区块包含一个索引、一个时间戳、一个交易列表、一个证明(之后更多)和前一个区块的哈希值。


以下是一个区块的例子:


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: <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


在 new_transaction() 添加交易信息到列表中后,它会返回下一个将被开采区块的索引号,交易信息将被打包到这个区块上。这对稍后提交交易的用户有用。


创建新区块


在区块链创建完成后,我们需要创建一个创世区块(也就是区块链上的第一个区块)。当然,创世区块也需要被证明,这需要通过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 = []

        # 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: <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]),
        }

        # 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: <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):
        """
        Creates a SHA-256 hash of a Block
        :param block: <dict> Block
        :return: <str>
        """


        # 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 结尾。可以简单表示为:hash(x * y) = ac23dc...0。所以,我们的目标是找到满足这个条件的一个 y 值。为了方便理解,我们暂定x=5。下面我们就用Python来做这样一个运算:


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


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


系统能够很容易验证他们的解决方案。


实现基本的工作量证明


下面在我们刚刚创建好的区块链上,来实现一个相似的工作量证明算法。规则与上边那个简单的例子相似:


找到一个数字 p ,它和前边一个区块的解决数字进行散列,生成前4位为 0 的哈希值。


下面是具体的Python代码实现:


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: <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):
        """
        Validates the Proof: Does hash(last_proof, proof) contain 4 leading zeroes?
        :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 请求与其交互。



第二步:创建 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):
    ...


# 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 端点,它是用来返回整个 Blockchain 类。

  • 第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. 通过打包交易奖励矿工一个币;

  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类中的方法进行交互。下面可以开始与我们的区块链交互啦。



第三步:实现与 Blockchain 类交互


版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/Blockchain_lemon/article/details/80837336
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-04-18 17:14:31
  • 阅读 ( 1237 )
  • 分类:区块链

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢