如何從0構(gòu)建區(qū)塊鏈之三
在前2集中,我們使用Go和Javascript構(gòu)建了兩個(gè)基本DEMO,傳送門(mén):
Go:區(qū)塊鏈研究實(shí)驗(yàn)室 | 如何從0構(gòu)建區(qū)塊鏈(一)
Javascript:區(qū)塊鏈研究實(shí)驗(yàn)室 | 如何從0構(gòu)建區(qū)塊鏈(二)
現(xiàn)在讓我們使用Python來(lái)構(gòu)建另一個(gè)分類(lèi)帳DEMO,這是增長(zhǎng)最快且最受歡迎的編程語(yǔ)言之一。
回顧一下,一個(gè)區(qū)塊鏈?zhǔn)且粋€(gè)區(qū)塊鏈,每個(gè)區(qū)塊包含圖1中列出的一些信息。由于我們正在構(gòu)建一個(gè)分類(lèi)帳DEMO,因此讓我們遠(yuǎn)離將來(lái)將涉及的復(fù)雜術(shù)語(yǔ)和機(jī)制。我將使用注釋符號(hào)(#)來(lái)解釋每一行代碼,記住#之后的所有內(nèi)容都是注釋。
我們開(kāi)始吧!
讓我們先導(dǎo)入兩個(gè)重要的庫(kù):
- # Start
 - import datetime as d # import the datetime library for our block timestamp and rename it as d for simplicity while typing
 - import hashlib as h # import the library for hashing our block data and rename it as h for simplicity while typing
 
這兩個(gè)庫(kù)用于對(duì)生成的每個(gè)塊進(jìn)行哈希處理和加時(shí)間戳。
創(chuàng)建一個(gè)名為Block的類(lèi):
- class Block: # create a class called Block
 - def __init__(self,index,timestamp,data ,prevhash): # declare an initial method that defines a block, a block contains the following information
 - self.index = index # a block contains an ID
 - self.timestamp =timestamp # a block contains a timestamp
 - self.data = data # a block contains some transactions
 - self.prevhash =prevhash # a block contains a hash of the previous block
 - self.hash =self.hashblock() # a block contains a hash, the hash is obtained by hashing all the data contained in the block
 
此類(lèi)具有一個(gè)包含所有塊信息的初始方法,但是沒(méi)有任何方法返回塊哈希,因此讓我們繼續(xù)在Block類(lèi)下創(chuàng)建它。
- def hashblock (self): # define a method for data encryption, this method will retain a hash of the block
 - block_encryption=h.sha256() # We need a sha256 function to hash the content of the block, so let's declare it here
 - block_encryption.update(str(self.index)+str(self.timestamp)+str(self.data)+str(self.prevhash)) # to encrypt the data in the block, We need just to sum everything and apply the hash function on it
 - return block_encryption.hexdigest() # let's return that hash result
 
部署區(qū)塊鏈時(shí),它只有一個(gè)區(qū)塊,即有史以來(lái)的第一個(gè)區(qū)塊,第一個(gè)區(qū)塊稱(chēng)為創(chuàng)世區(qū)塊,以下所有區(qū)塊將被添加到第一個(gè)區(qū)塊之上,因此讓我們創(chuàng)建一個(gè)靜態(tài)方法,該方法將返回起源塊。
- @staticmethod # declaring a static method for the genesis block
 - def genesisblock(): # this method is for generating the first block named genesis block
 - return Block(0,d.datetime.now(),"genesis block transaction"," ") # return the genesis block
 
每個(gè)塊之后是下一個(gè)塊,下一個(gè)塊是鏈上最近添加的塊,我們必須聲明另一個(gè)靜態(tài)方法來(lái)返回每個(gè)新塊,讓我們創(chuàng)建它。
- @staticmethod# let's declare another static method to get the next block
 - def newblock(lastblock): # get the next block, the block that comes after the previous block (prevblock+1)
 - index = lastblock.index+1 # the id of this block will be equals to the previous block + 1, which is logic
 - timestamp = d.datetime.now() # The timestamp of the next block
 - hashblock = lastblock.hash # the hash of this block
 - data = "Transaction " +str(index) # The data or transactions containing in that block
 - return Block(index,timestamp,data,hashblock)# return the entire block
 
制作區(qū)塊并創(chuàng)建新的區(qū)塊方法,現(xiàn)在我們需要初始化區(qū)塊鏈以接收所有傳入的區(qū)塊。
- blockchain = [Block.genesisblock()] # now it's time to initialize our blockchain with a genesis block in it
 - prevblock = blockchain[0] # the previous block is the genesis block itself since there is no block that comes before it at the indice 0
 
鏈上只有創(chuàng)世塊,讓我們向分類(lèi)賬中添加更多塊并進(jìn)行打印。
- for i in range (0,5): # the loop starts from here, we will print 5 blocks, this number can be increased if needed
 - addblock = Block.newblock(prevblock) # the block to be added to our chain
 - blockchain.append(addblock) # we add that block to our chain of blocks
 - prevblock =addblock #now the previous block becomes the last block so we can add another one if needed
 - print"Block ID #{} ".format(addblock.index) # show the block id
 - print"Timestamp:{}".format(addblock.timestamp)# show the block timestamp
 - print"Hash of the block:{}".format(addblock.hash)# show the hash of the added block
 - print"Previous Block Hash:{}".format(addblock.prevhash)# show the previous block hash
 - print"data:{}\n".format(addblock.data)# show the transactions or data contained in that block
 - # end
 
結(jié)果如下:
編號(hào)為1的區(qū)塊具有創(chuàng)世區(qū)塊的哈希值,該哈希值未在我們的區(qū)塊鏈中顯示,由我們決定是否顯示創(chuàng)世區(qū)塊,讓我向您展示如何打印其內(nèi)容。在之前for loop,添加以下行:
- # let's print the genesis block information
 - print"Block ID :{} ".format(prevblock.index)
 - print"Timestamp:{}".format(prevblock.timestamp)
 - print"Hash of the block:{}".format(prevblock.hash)
 - print"Previous Block Hash:{}".format(prevblock.prevhash)
 - print"data:{}\n".format(prevblock.data)
 
這是最終結(jié)果:
現(xiàn)在,創(chuàng)始?jí)K在分類(lèi)帳中變得可見(jiàn)。
恭喜你!您剛剛使用Python創(chuàng)建了另一個(gè)區(qū)塊鏈DEMO。
保持關(guān)注下一個(gè)高級(jí)概念??。
整個(gè)代碼:
- # Start
 - import datetime as d # import the datetime library for our block timestamp and rename it as d for simplicity while typing
 - import hashlib as h # import the library for hashing our block data and rename it as h for simplicity while typing
 - class Block: # create a Block class
 - def __init__(self,index,timestamp,data ,prevhash): # declare an initial method that defines a block, a block contains the following information
 - self.index = index # a block contains an ID
 - self.timestamp =timestamp # a block contains a timestamp
 - self.data = data # a block contains some transactions
 - self.prevhash =prevhash # a block contains a hash of the previous block
 - self.hash =self.hashblock() # a block contains a hash, the hash is obtained by hashing all the data contained in the block
 - def hashblock (self): # define a method for data encryption, this method will retain a hash of the block
 - block_encryption=h.sha256() # We need a sha256 function to hash the content of the block, so let's declare it here
 - block_encryption.update(str(self.index)+str(self.timestamp)+str(self.data)+str(self.prevhash)) # to encrypt the data in the block, We need just to sum everything and apply the hash function on it
 - return block_encryption.hexdigest() # let's return that hash result
 - @staticmethod # declaring a static method for the genesis block
 - def genesisblock(): # delcare a function for generating the first block named genesis
 - return Block(0,d.datetime.now(),"genesis block transaction"," ") # return the genesis block
 - @staticmethod# let's declare another static method to get the next block
 - def newblock(lastblock): # get the next block, the block that comes after the previous block (prevblock+1)
 - index = lastblock.index+1 # the id of this block will be equals to the previous block + 1, which is logic
 - timestamp = d.datetime.now() # The timestamp of the next block
 - hashblock = lastblock.hash # the hash of this block
 - data = "Transaction " +str(index) # The data or transactions containing in that block
 - return Block(index,timestamp,data,hashblock)# return the entire block
 - blockchain = [Block.genesisblock()] # now it's time to initialize our blockchain with a genesis block in it
 - prevblock = blockchain[0] # the previous block is the genesis block itself since there is no block that comes before it at the indice 0
 - # let's print the genesis block information
 - print"Block ID :{} ".format(prevblock.index)
 - print"Timestamp:{}".format(prevblock.timestamp)
 - print"Hash of the block:{}".format(prevblock.hash)
 - print"Previous Block Hash:{}".format(prevblock.prevhash)
 - print"data:{}\n".format(prevblock.data)
 - for i in range (0,5): # the loop starts from here, we will need only 5 blocks in our ledger for now, this number can be increased
 - addblock = Block.newblock(prevblock) # the block to be added to our chain
 - blockchain.append(addblock) # we add that block to our chain of blocks
 - prevblock =addblock #now the previous block becomes the last block so we can add another one if needed
 - print"Block ID #{} ".format(addblock.index) # show the block id
 - print"Timestamp:{}".format(addblock.timestamp)# show the block timestamp
 - print"Hash of the block:{}".format(addblock.hash)# show the hash of the added block
 - print"Previous Block Hash:{}".format(addblock.prevhash)# show the previous block hash
 - print"data:{}\n".format(addblock.data)# show the transactions or data contained in that block
 - # end
 


















 
 
 



 
 
 
 