本文转载自微信公众号《区块链研究实验室》,作者连三丰。转载本文请联系BlockchainResearchLab公众号本文将介绍如何使用Python3.9.4构建自己的区块链和加密货币。在构建自己的加密货币或区块链之前,您需要了解Python和区块链的基础知识。区块链可能看起来很复杂,但可以肯定的是它的核心概念真的很简单。要求确保您安装了最新版本的Python。入门创建一个名为app.py的新Python文件。首先,我们应该创建一个Block类。我将参考dzone.com的代码:classBlock(object):def__init__(self):passdefcompute_hash(self):pass我们还需要创建Blockchain类蓝图:classBlockchain(object):def__init__(self):passdefbuild_genesis(self):passdefbuild_block(self):pass@staticmethoddefconfirm_validity(block,previous_block):passdefget_data(self,sender,receiver,amount):pass@staticmethoddefproof_of_work(last_proof):pass@propertydeflatest_block(self):passblockclass现在,让我们看看块类我们刚刚创建了这些类。块类将如下所示:importhashlibimporttimeclassBlock(object):def__init__(self,index,proof_number,previous_hash,data,timestamp=None):self.index=indexself.proof_number=proof_numberself.previous_hash=previous_hashself.data=dataself.timestamp=timestamportime.time()@propertydefcompute_hash(self):string_block="{}{}{}{}{}".format(self.index,self.proof_number,self.previous_hash,self.data,self.timestamp)returnhashlib。sha256(string_block.encode()).hexdigest()Block类有几个参数:index、proof_number、previous_hash、data和timestamp。索引用于查看区块在链中的位置。我认为,前者是不言而喻的。data是一个对象,它收集有关块的所有信息(id、数量、发送者、接收者等)。时间戳指的是区块生成的时刻。在defcompute_hash()方法中,我们的哈希将使用hash方法创建。链状的个体块没有价值,链是用来加密数据的,所以很重要。让我们为Chain类创建一个构造器:classBlockchain(object):def__init__(self):self.chain=[]self.current_data=[]self.nodes=set()self.build_genesis()我们来看看参数效果。self.chain是一个存储所有块的变量。self.current_data是一个变量,用于存储有关块的所有信息。这个self.nodes是设置节点的示例方法。self.build_genesis方法变量是在第一个块方法中创建的。创建创世方法此方法将用于创建初始块。因此,我们需要调用build_block()方法。defbuild_genesis(self):self.build_block(proof_number=0,previous_hash=0)defbuild_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)returnblock在这个方法中,创建一个新的Block对象并输入所需的参数:index、proof、previous_hash和data。然后我们设置当前数据并将块附加到链中。确认有效性方法创建加密货币/区块链的一个重要部分是检查块是否有效。我们用一种新方法来做到这一点。@staticmethoddefconfirm_validity(block,previous_block):ifprevious_block.index+1!=block.index:returnFalseeliifprevious_block.compute_hash()!=block.previous_hash:returnFalseelifblock.timestamp<=previous_block.timestamp:returnFalseelifreturnTrue:这个方法使用了几个让我们解释一下if语句来检查Block是否是它应该的样子。它再次使用compute_hash()方法。获取数据方法当然,您希望能够读取您的区块和区块链的数据,这是通过以下get_data()方法完成的:defget_data(self,sender,receiver,amount):self.current_data.append({'sender':sender,'receiver':receiver,'amount':amount})returnTrue这个方法很简单,它接受三个参数并将它们添加到对象中。工作量证明在这个项目中,我们将添加一个工作量证明算法,以使挖矿成为可能。让我们创建defblock_mining方法:defblock_mining(self,details_miner):self.get_data(sender="0",receiver=details_miner,quantity=1,)last_block=self.latest_blocklast_proof_number=last_block.proof_numberproof_number=self.proof_of_work(last_proof_number)last=last_block.compute_hashblock=self.build_block(proof_number,last_hash)returnvars(block)Finalize为了结束我们的项目,我们将以下代码行添加到deflatest_block方法中,returnself.chain[-1]现在,我们将测试ourItem:bc=Blockchain()print("READY")print(bc.chain)#output:READY[<__main__.Blockobjectat0x00000241A23C5EE0>]当你看到类似的结果时,你的区块链蓝图已经成功构建。如果你想添加额外的功能,你可以这样做,让我知道你构建了什么!完整代码示例:importhashlibimporttimeclassBlock(object):def__init__(self,index,proof_number,previous_hash,data,timestamp=None):self。index=indexself.proof_number=proof_numberself.previous_hash=previous_hashself.data=dataself.timestamp=timestamportime.time()@propertydefcompute_hash(self):string_block="{}{}{}{}{}".format(self.index,self.proof_number,self.previous_hash,self.data,self.timestamp)returnhashlib.sha256(string_block.encode()).hexdigest()classBlockchain(object):def__init__(self):self.chain=[]self.current_data=[]self.nodes=set()self.build_genesis()defbuild_genesis(self):self.build_block(proof_number=0,previous_hash=0)defbuild_block(self,proof_number,previous_hash):block=Block(index=len(self.链),proof_number=proof_number,previous_hash=previous_hash,data=self.current_data)self.current_data=[]self.chain.append(block)returnblock@staticmethoddefconfirm_validity(block,previous_block):ifprevious_block.index+1!=block.index:returnFalseelifprevious_block.compute_hash()!=block.previous_hash:returnFalseelifblock.timestamp>=previous_block.timestamp:returnFalsereturnTruedefget_data(self,sender,receiver,amount):self.current_data.append({'sender':sender,'receiver':receiver,'amount':amount})returnTrue@staticmethoddefproof_of_work(last_proof):pass@propertydeflatest_block(self):returnsself.chain[-1]defblock_mining(self,details_miner):self.get_data(sender="0",receiver=details_miner,quantity=1,)last_block=self.latest_blocklast_proof_number=last_block.proof_numberproof_number=self.proof_of_work(last_proof_number)last_hash=last_block.compute_hashblock=self.build_block(proof_number,last_hash)returnvars(block)bc=Blockchain()print("READY")print(bc.chain)最终于我们使用Python创建了自己的区块链
