成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

如何從0構建區塊鏈之三

區塊鏈
由于我們正在構建一個分類帳DEMO,因此讓我們遠離將來將涉及的復雜術語和機制。我將使用注釋符號(#)來解釋每一行代碼,記住#之后的所有內容都是注釋。

[[388228]]

在前2集中,我們使用Go和Javascript構建了兩個基本DEMO,傳送門:

Go:區塊鏈研究實驗室 | 如何從0構建區塊鏈(一)

Javascript:區塊鏈研究實驗室 | 如何從0構建區塊鏈(二)

現在讓我們使用Python來構建另一個分類帳DEMO,這是增長最快且最受歡迎的編程語言之一。

回顧一下,一個區塊鏈是一個區塊鏈,每個區塊包含圖1中列出的一些信息。由于我們正在構建一個分類帳DEMO,因此讓我們遠離將來將涉及的復雜術語和機制。我將使用注釋符號(#)來解釋每一行代碼,記住#之后的所有內容都是注釋。

我們開始吧!

讓我們先導入兩個重要的庫:

  1. # Start 
  2. import datetime as d # import the datetime library for our block timestamp and rename it as d for simplicity while typing  
  3. import hashlib as h # import the library for hashing our block data and rename it as h for simplicity while typing  

這兩個庫用于對生成的每個塊進行哈希處理和加時間戳。

創建一個名為Block的類:

  1. class Block: # create a class called Block 
  2.     def __init__(self,index,timestamp,data ,prevhash): # declare an initial method that defines a block, a block contains the following information 
  3.         self.index = index # a block contains an ID 
  4.         self.timestamp =timestamp # a block contains a timestamp 
  5.         self.data = data # a block contains some transactions 
  6.         self.prevhash =prevhash # a block contains a hash of the previous block 
  7.         self.hash =self.hashblock() # a block contains a hash, the hash is obtained by hashing all the data contained in the block 

此類具有一個包含所有塊信息的初始方法,但是沒有任何方法返回塊哈希,因此讓我們繼續在Block類下創建它。

  1. def hashblock (self): # define a method for data encryption, this method will retain a hash of the block 
  2.         block_encryption=h.sha256() # We need a sha256 function to hash the content of the block, so let's declare it here 
  3.         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 
  4.         return block_encryption.hexdigest() # let's return that hash result 

部署區塊鏈時,它只有一個區塊,即有史以來的第一個區塊,第一個區塊稱為創世區塊,以下所有區塊將被添加到第一個區塊之上,因此讓我們創建一個靜態方法,該方法將返回起源塊。

  1. @staticmethod # declaring a static method for the genesis block 
  2.     def genesisblock(): # this method is for generating the first block named genesis block 
  3.         return Block(0,d.datetime.now(),"genesis block transaction"," ") # return the genesis block 

每個塊之后是下一個塊,下一個塊是鏈上最近添加的塊,我們必須聲明另一個靜態方法來返回每個新塊,讓我們創建它。

  1. @staticmethod# let's declare another static method to get the next block 
  2.     def newblock(lastblock): # get the next block, the block that comes after the previous block (prevblock+1) 
  3.         index = lastblock.index+1 # the id of this block will be equals to the previous block + 1, which is logic 
  4.         timestamp = d.datetime.now() # The timestamp of the next block 
  5.         hashblock = lastblock.hash # the hash of this block 
  6.         data = "Transaction " +str(index) # The data or transactions containing in that block 
  7.         return Block(index,timestamp,data,hashblock)# return the entire block 

制作區塊并創建新的區塊方法,現在我們需要初始化區塊鏈以接收所有傳入的區塊。

  1. blockchain = [Block.genesisblock()] # now it's time to initialize our blockchain with a genesis block in it 
  2. prevblock = blockchain[0] # the previous block is the genesis block itself since there is no block that comes before it at the indice 0  

鏈上只有創世塊,讓我們向分類賬中添加更多塊并進行打印。

  1. for i in range (0,5): # the loop starts from here, we will print 5 blocks, this number can be increased if needed 
  2.     addblock = Block.newblock(prevblock) #  the block to be added to our chain  
  3.     blockchain.append(addblock) # we add that block to our chain of blocks 
  4.     prevblock =addblock #now the previous block becomes the last block so we can add another one if needed 
  5.  
  6.     print"Block ID #{} ".format(addblock.index) # show the block id 
  7.     print"Timestamp:{}".format(addblock.timestamp)# show the block timestamp 
  8.     print"Hash of the block:{}".format(addblock.hash)# show the hash of the added block 
  9.     print"Previous Block Hash:{}".format(addblock.prevhash)# show the previous block hash 
  10.     print"data:{}\n".format(addblock.data)# show the transactions or data contained in that block 
  11.  
  12.  
  13.     # end 

結果如下:

編號為1的區塊具有創世區塊的哈希值,該哈希值未在我們的區塊鏈中顯示,由我們決定是否顯示創世區塊,讓我向您展示如何打印其內容。在之前for loop,添加以下行:

  1. # let's print the genesis block information 
  2. print"Block ID :{} ".format(prevblock.index)  
  3. print"Timestamp:{}".format(prevblock.timestamp
  4. print"Hash of the block:{}".format(prevblock.hash) 
  5. print"Previous Block Hash:{}".format(prevblock.prevhash) 
  6. print"data:{}\n".format(prevblock.data) 

這是最終結果:

現在,創始塊在分類帳中變得可見。

恭喜你!您剛剛使用Python創建了另一個區塊鏈DEMO。

保持關注下一個高級概念??。

整個代碼:

  1. # Start 
  2. import datetime as d # import the datetime library for our block timestamp and rename it as d for simplicity while typing  
  3. import hashlib as h # import the library for hashing our block data and rename it as h for simplicity while typing  
  4.  
  5.  
  6. class Block: # create a Block class 
  7.     def __init__(self,index,timestamp,data ,prevhash): # declare an initial method that defines a block, a block contains the following information 
  8.         self.index = index # a block contains an ID 
  9.         self.timestamp =timestamp # a block contains a timestamp 
  10.         self.data = data # a block contains some transactions 
  11.         self.prevhash =prevhash # a block contains a hash of the previous block 
  12.         self.hash =self.hashblock() # a block contains a hash, the hash is obtained by hashing all the data contained in the block 
  13.  
  14.     def hashblock (self): # define a method for data encryption, this method will retain a hash of the block 
  15.         block_encryption=h.sha256() # We need a sha256 function to hash the content of the block, so let's declare it here 
  16.         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 
  17.         return block_encryption.hexdigest() # let's return that hash result  
  18.      
  19.     @staticmethod # declaring a static method for the genesis block 
  20.     def genesisblock(): # delcare a function for generating the first block named genesis 
  21.         return Block(0,d.datetime.now(),"genesis block transaction"," ") # return the genesis block 
  22.      
  23.     @staticmethod# let's declare another static method to get the next block 
  24.     def newblock(lastblock): # get the next block, the block that comes after the previous block (prevblock+1) 
  25.         index = lastblock.index+1 # the id of this block will be equals to the previous block + 1, which is logic 
  26.         timestamp = d.datetime.now() # The timestamp of the next block 
  27.         hashblock = lastblock.hash # the hash of this block 
  28.         data = "Transaction " +str(index) # The data or transactions containing in that block 
  29.         return Block(index,timestamp,data,hashblock)# return the entire block 
  30.  
  31. blockchain = [Block.genesisblock()] # now it's time to initialize our blockchain with a genesis block in it 
  32. prevblock = blockchain[0] # the previous block is the genesis block itself since there is no block that comes before it at the indice 0  
  33.  
  34. # let's print the genesis block information 
  35. print"Block ID :{} ".format(prevblock.index)  
  36. print"Timestamp:{}".format(prevblock.timestamp
  37. print"Hash of the block:{}".format(prevblock.hash) 
  38. print"Previous Block Hash:{}".format(prevblock.prevhash) 
  39. print"data:{}\n".format(prevblock.data) 
  40.  
  41.  
  42. 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 
  43.     addblock = Block.newblock(prevblock) #  the block to be added to our chain  
  44.     blockchain.append(addblock) # we add that block to our chain of blocks 
  45.     prevblock =addblock #now the previous block becomes the last block so we can add another one if needed 
  46.  
  47.     print"Block ID #{} ".format(addblock.index) # show the block id 
  48.     print"Timestamp:{}".format(addblock.timestamp)# show the block timestamp 
  49.     print"Hash of the block:{}".format(addblock.hash)# show the hash of the added block 
  50.     print"Previous Block Hash:{}".format(addblock.prevhash)# show the previous block hash 
  51.     print"data:{}\n".format(addblock.data)# show the transactions or data contained in that block 
  52.  
  53.  
  54.     # end  

 

責任編輯:武曉燕 來源: 區塊鏈研究實驗室
相關推薦

2021-03-16 21:39:47

區塊鏈DEMOGo

2021-03-12 19:17:38

區塊鏈GoPython

2018-05-23 15:20:08

區塊鏈數字貨幣比特幣

2019-11-19 09:13:08

區塊鏈金融去中心化

2021-04-16 20:43:18

Go區塊鏈編程

2021-12-22 23:28:04

區塊鏈人工智能技術

2021-02-26 10:28:24

區塊鏈數字貨幣金融

2018-03-19 19:30:19

2021-09-23 22:40:10

區塊鏈比特幣技術

2018-03-27 09:52:30

區塊鏈數字貨幣比特幣

2021-05-10 15:09:47

區塊鏈互聯網金融

2018-01-23 11:09:04

區塊鏈技術重用

2022-10-18 08:00:00

2018-05-06 16:17:01

2020-08-18 10:58:05

區塊鏈比特幣區塊鏈戰略

2021-03-16 14:33:12

區塊鏈比特幣加密貨幣

2021-04-12 10:57:28

區塊鏈信任銀行

2022-04-18 14:50:00

區塊鏈安全交易

2021-04-11 11:31:05

區塊鏈記賬比特幣

2018-06-14 10:32:25

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 欧美日韩在线国产 | 亚洲 中文 欧美 日韩 在线观看 | 亚洲一区二区三区在线 | 中文字幕日韩在线观看 | 91在线观看免费视频 | 免费的日批视频 | 成人免费视频 | 久久久观看| 免费观看一级特黄欧美大片 | 在线免费黄色小视频 | 在线午夜| 欧美一级免费看 | 免费在线黄 | 亚洲福利在线观看 | 日本不卡一区 | 99亚洲综合| 91丨九色丨国产在线 | 久久久性色精品国产免费观看 | 日韩视频在线免费观看 | 色婷婷精品国产一区二区三区 | 91看片官网 | 欧美日韩三级 | 成人夜晚看av | 欧美日韩a | 日韩精品成人 | 久久久久久综合 | 精品国产一区二区三区久久 | 日本精品视频一区二区 | 在线免费观看成人 | 久久久久久久久精 | 精品久久久久久亚洲精品 | 国产精品网址 | 久久人体视频 | 午夜成人在线视频 | 亚洲欧美精品在线观看 | 少妇诱惑av | 中文二区 | 免费观看一级特黄欧美大片 | 欧美日产国产成人免费图片 | 欧美亚洲成人网 | 欧美成人一区二区三区 |