如何使用Python创建加密货币

开发 后端
在构建自己的加密货币或区块链之前,您需要了解Python和区块链的基础知识。区块链或许看起来很复杂,但可以肯定的是其核心概念确实非常简单。

[[399524]]

本文转载自微信公众号「区块链研究实验室」,作者链三丰 。转载本文请联系区块链研究实验室公众号

本文将向大家介绍如何使用Python 3.9.4建立属于自己的区块链和加密货币。在构建自己的加密货币或区块链之前,您需要了解Python和区块链的基础知识。区块链或许看起来很复杂,但可以肯定的是其核心概念确实非常简单。

要求

确保您已安装最新版本的Python。

入门

创建一个新的Python文件,文件名app.py。首先,我们应该创建一个Block类。

我将参考dzone.com的代码:

class Block(object): 
    def __init__(self): 
        pass 
 
    def compute_hash(self): 
        pass 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

我们还需要创建Blockchain类蓝图:

class Blockchain(object): 
    def __init__(self): 
        pass 
 
    def build_genesis(self): 
        pass 
 
    def build_block(self): 
        pass 
 
    @staticmethod 
    def confirm_validity(block, previous_block): 
        pass 
 
    def get_data(self, sender, receiver, amount): 
        pass 
 
    @staticmethod 
    def proof_of_work(last_proof): 
        pass 
 
    @property 
    def latest_block(self): 
        pass 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.

积木类

现在,让我们看一下我们刚刚创建的这些类。块类将如下所示:

import hashlib 
import time 
 
class Block(object): 
    def __init__(self, index, proof_number, previous_hash, data, timestamp=None): 
        self.index = index 
        self.proof_number = proof_number 
        self.previous_hash = previous_hash 
        self.data = data 
        self.timestamp = timestamp or time.time() 
 
    @property     
    def compute_hash(self): 
        string_block = "{}{}{}{}{}".format(self.index, self.proof_number, self.previous_hash, self.data, self.timestamp
        return hashlib.sha256(string_block.encode()).hexdigest() 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

Block类采用几个参数:index,proof_number,previous_hash,data和timestamp。

  • 索引用于查看块在链中的哪个位置。
  • 我认为,前者是不言而喻的。
  • 数据是一个对象,它收集有关该区块的所有信息(id,金额,发送者,接收者等)。
  • 时间戳是指生成块的时刻。

在def compute_hash()方法中,我们的哈希将使用hash方法创建。

链条类

单独的块没有任何价值,并且使用链来加密数据,因此很重要。让我们为Chain类创建构造函数:

class Blockchain(object): 
    def __init__(self): 
        self.chain = [] 
        self.current_data = [] 
        self.nodes = set() 
        self.build_genesiss() 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

让我们看一下这些参数的作用。

  • 该self.chain是一个变量,它存储所有的块。
  • 该self.current_data是一个变量,它存储所有关于该块的信息。
  • 这self.nodes是用于设置节点的示例方法。
  • 所述self.build_genesis方法变量是创建第一块中的方法。

建立创世方法

此方法将用于创建初始块。因此,我们需要调用该build_block()方法。

def build_genesis(self): 
        self.build_block(proof_number=0, previous_hash=0) 
 
    def build_block(self, proof_number, previous_hash): 
        block = Block( 
            index=len(self.chain), 
            proof_number=proof_number, 
            previous_hash=previous_hash, 
            data=self.current_data 
        ) 
 
        self.current_data = [] 
        self.chain.append(block) 
 
        return block 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

在此方法中,创建一个新Block对象并输入所需的参数:索引,证明,previous_hash和数据。然后,我们设置当前数据并将该块附加到链中。

确认有效性方法

创建加密货币/区块链的重要部分是检查区块是否有效。我们用一种新方法来做到这一点。

@staticmethod 
    def confirm_validity(block, previous_block): 
        if previous_block.index + 1 != block.index
            return False 
 
        elif previous_block.compute_hash() != block.previous_hash: 
            return False 
 
        elif block.timestamp & lt; = previous_block.timestamp
            return False 
 
        return True 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

让我们解释一下:此方法使用几个if语句来检查Block是否是它应该的块。它compute_hash()再次使用该方法。

获取数据方法

当然,您希望能够读取您的块和区块链的数据,这是通过以下get_data()方法完成的:

def get_data(self, sender, receiver, amount): 
            self.current_data.append({ 
                'sender': sender, 
                'receiver': receiver, 
                'amount': amount 
            }) 
 
            return True 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

该方法非常简单,它采用三个参数并将它们添加到对象中。

工作证明

在这个项目中,我们将添加一个工作量证明算法以使挖掘成为可能。

让我们创建def block_mining方法:

def block_mining(self, details_miner): 
        self.get_data( 
            sender="0"
            receiver=details_miner, 
            quantity=1, 
        ) 
 
        last_block = self.latest_block 
        last_proof_number = last_block.proof_number 
        proof_number = self.proof_of_work(last_proof_number) 
        last_hash = last_block.compute_hash 
 
        block = self.build_block(proof_number, last_hash) 
 
        return vars(block) 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

定稿

为了结束我们的项目,我们将以下代码行添加到def latest_block方法中,

return self.chain[-1] 
  • 1.

现在,我们将测试我们的项目:

bc = Blockchain() 
print("READY"
print(bc.chain) 
#output
READY 
[<__main__.Block object at 0x00000241A23C5EE0>] 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

当您看到类似的结果时,您的区块链蓝图已成功构建。如果您想添加额外的功能,则可以这样做,请告诉我您所构建的内容!

完整代码示例:

import hashlib 
import time 
 
 
class Block(object): 
    def __init__(self, index, proof_number, previous_hash, data, timestamp=None): 
        self.index = index 
        self.proof_number = proof_number 
        self.previous_hash = previous_hash 
        self.data = data 
        self.timestamp = timestamp or time.time() 
 
    @property 
    def compute_hash(self): 
        string_block = "{}{}{}{}{}".format( 
            self.index, self.proof_number, self.previous_hash, self.data, self.timestamp
        return hashlib.sha256(string_block.encode()).hexdigest() 
 
 
class Blockchain(object): 
    def __init__(self): 
        self.chain = [] 
        self.current_data = [] 
        self.nodes = set() 
        self.build_genesis() 
 
    def build_genesis(self): 
        self.build_block(proof_number=0, previous_hash=0) 
 
    def build_block(self, proof_number, previous_hash): 
        block = Block( 
            index=len(self.chain), 
            proof_number=proof_number, 
            previous_hash=previous_hash, 
            data=self.current_data 
        ) 
 
        self.current_data = [] 
        self.chain.append(block) 
 
        return block 
 
    @staticmethod 
    def confirm_validity(block, previous_block): 
        if previous_block.index + 1 != block.index
            return False 
 
        elif previous_block.compute_hash() != block.previous_hash: 
            return False 
 
        elif block.timestamp >= previous_block.timestamp
            return False 
 
        return True 
 
    def get_data(self, sender, receiver, amount): 
        self.current_data.append({ 
            'sender': sender, 
            'receiver': receiver, 
            'amount': amount 
        }) 
 
        return True 
 
    @staticmethod 
    def proof_of_work(last_proof): 
        pass 
 
    @property 
    def latest_block(self): 
        return self.chain[-1] 
 
    def block_mining(self, details_miner): 
        self.get_data( 
            sender="0"
            receiver=details_miner, 
            quantity=1, 
        ) 
 
        last_block = self.latest_block 
        last_proof_number = last_block.proof_number 
        proof_number = self.proof_of_work(last_proof_number) 
        last_hash = last_block.compute_hash 
 
        block = self.build_block(proof_number, last_hash) 
 
        return vars(block) 
 
 
bc = Blockchain() 
print("READY"
print(bc.chain) 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.

终于我们使用Python创建了自己的区块链。

 

责任编辑:武晓燕 来源: 区块链研究实验室
相关推荐

2022-06-06 15:41:51

加密货币安全勒索软件

2021-04-12 16:31:09

加密货币资金金融

2021-04-12 11:23:43

加密货币技术比特币

2022-06-10 14:10:38

加密货币代币比特币

2021-03-23 12:15:32

加密货币比特币货币

2022-07-29 15:08:05

加密货币去中心化工具

2022-01-12 11:12:21

区块链加密货币技术

2022-02-18 00:02:31

加密货币爱情安全

2021-11-14 22:25:18

加密货币物联网区块链

2021-02-02 21:43:47

加密货币以太坊比特币

2022-05-04 23:29:42

加密货币代币比特币

2021-06-04 10:31:41

PythonUniswap加密货币

2021-06-09 14:26:50

加密货币比特币货币

2022-05-26 14:52:00

加密货币金融女性

2022-04-08 14:06:08

加密货币捐赠慈善

2021-02-26 20:32:40

加密货币比特币货币

2022-04-19 15:39:08

区块链加密货币交易

2021-04-12 10:29:56

加密货币货币比特币

2021-10-22 22:49:29

加密货币区块链货币

2021-04-08 22:38:15

加密货币安全数据
点赞
收藏

51CTO技术栈公众号