精品欧美一区二区三区在线观看 _久久久久国色av免费观看性色_国产精品久久在线观看_亚洲第一综合网站_91精品又粗又猛又爽_小泽玛利亚一区二区免费_91亚洲精品国偷拍自产在线观看 _久久精品视频在线播放_美女精品久久久_欧美日韩国产成人在线

如何從0構(gòu)建區(qū)塊鏈之三

區(qū)塊鏈
由于我們正在構(gòu)建一個(gè)分類帳DEMO,因此讓我們遠(yuǎn)離將來將涉及的復(fù)雜術(shù)語(yǔ)和機(jī)制。我將使用注釋符號(hào)(#)來解釋每一行代碼,記住#之后的所有內(nèi)容都是注釋。

[[388228]]

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

Go:區(qū)塊鏈研究實(shí)驗(yàn)室 | 如何從0構(gòu)建區(qū)塊鏈(一)

Javascript:區(qū)塊鏈研究實(shí)驗(yàn)室 | 如何從0構(gòu)建區(qū)塊鏈(二)

現(xiàn)在讓我們使用Python來構(gòu)建另一個(gè)分類帳DEMO,這是增長(zhǎng)最快且最受歡迎的編程語(yǔ)言之一。

回顧一下,一個(gè)區(qū)塊鏈?zhǔn)且粋€(gè)區(qū)塊鏈,每個(gè)區(qū)塊包含圖1中列出的一些信息。由于我們正在構(gòu)建一個(gè)分類帳DEMO,因此讓我們遠(yuǎn)離將來將涉及的復(fù)雜術(shù)語(yǔ)和機(jī)制。我將使用注釋符號(hào)(#)來解釋每一行代碼,記住#之后的所有內(nèi)容都是注釋。

我們開始吧!

讓我們先導(dǎo)入兩個(gè)重要的庫(kù):

  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  

這兩個(gè)庫(kù)用于對(duì)生成的每個(gè)塊進(jìn)行哈希處理和加時(shí)間戳。

創(chuàng)建一個(gè)名為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 

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

  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 

部署區(qū)塊鏈時(shí),它只有一個(gè)區(qū)塊,即有史以來的第一個(gè)區(qū)塊,第一個(gè)區(qū)塊稱為創(chuàng)世區(qū)塊,以下所有區(qū)塊將被添加到第一個(gè)區(qū)塊之上,因此讓我們創(chuàng)建一個(gè)靜態(tài)方法,該方法將返回起源塊。

  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 

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

  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 

制作區(qū)塊并創(chuàng)建新的區(qū)塊方法,現(xiàn)在我們需要初始化區(qū)塊鏈以接收所有傳入的區(qū)塊。

  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  

鏈上只有創(chuàng)世塊,讓我們向分類賬中添加更多塊并進(jìn)行打印。

  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 

結(jié)果如下:

編號(hào)為1的區(qū)塊具有創(chuàng)世區(qū)塊的哈希值,該哈希值未在我們的區(qū)塊鏈中顯示,由我們決定是否顯示創(chuàng)世區(qū)塊,讓我向您展示如何打印其內(nèi)容。在之前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) 

這是最終結(jié)果:

現(xiàn)在,創(chuàng)始?jí)K在分類帳中變得可見。

恭喜你!您剛剛使用Python創(chuàng)建了另一個(gè)區(qū)塊鏈DEMO。

保持關(guān)注下一個(gè)高級(jí)概念??。

整個(gè)代碼:

  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  

 

責(zé)任編輯:武曉燕 來源: 區(qū)塊鏈研究實(shí)驗(yàn)室
相關(guān)推薦

2021-03-16 21:39:47

區(qū)塊鏈DEMOGo

2021-03-12 19:17:38

區(qū)塊鏈GoPython

2018-05-23 15:20:08

區(qū)塊鏈數(shù)字貨幣比特幣

2019-11-19 09:13:08

區(qū)塊鏈金融去中心化

2021-04-16 20:43:18

Go區(qū)塊鏈編程

2021-12-22 23:28:04

區(qū)塊鏈人工智能技術(shù)

2021-02-26 10:28:24

區(qū)塊鏈數(shù)字貨幣金融

2018-03-19 19:30:19

2021-09-23 22:40:10

區(qū)塊鏈比特幣技術(shù)

2018-03-27 09:52:30

區(qū)塊鏈數(shù)字貨幣比特幣

2021-05-10 15:09:47

區(qū)塊鏈互聯(lián)網(wǎng)金融

2018-01-23 11:09:04

區(qū)塊鏈技術(shù)重用

2022-10-18 08:00:00

2022-03-29 09:18:55

區(qū)塊鏈

2019-01-24 15:50:06

區(qū)塊鏈數(shù)字貨幣比特幣

2018-06-14 10:32:25

2021-02-20 22:35:17

區(qū)塊鏈比特幣記賬

2021-04-11 11:31:05

區(qū)塊鏈記賬比特幣

2021-03-16 14:33:12

區(qū)塊鏈比特幣加密貨幣

2020-08-18 10:58:05

區(qū)塊鏈比特幣區(qū)塊鏈戰(zhàn)略
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

黄色精品网站| 欧美特黄色片| 91丨porny丨中文| 欧美亚洲伦理www| 好吊色视频一区二区三区| 牛牛电影国产一区二区| 中文字幕在线视频久| 91亚洲精品一区二区乱码| 97视频在线观看免费| 成人无码www在线看免费| 精品91久久| 中文字幕乱码日本亚洲一区二区| 国产精品香蕉在线观看| 国产盗摄一区二区三区在线| 奇米一区二区| 精品福利在线视频| 欧美一级在线亚洲天堂| 欧美做受xxxxxⅹ性视频| 88xx成人永久免费观看| 亚洲国产成人午夜在线一区| 91九色国产在线| 麻豆亚洲av成人无码久久精品| 桃色av一区二区| 日本一区二区动态图| 91色琪琪电影亚洲精品久久| 国产午夜福利片| 日韩av有码| 亚洲成色777777在线观看影院| 国产xxxxx在线观看| 麻豆视频在线| 丁香婷婷综合激情五月色| 日韩美女免费观看| 久久久久久久久久久97| 欧美aa视频| 亚洲另类中文字| 玛丽玛丽电影原版免费观看1977 | 992kp免费看片| bl在线肉h视频大尺度| 国产人伦精品一区二区| 久久国产精品首页| 丰满少妇在线观看资源站| 日韩精品第二页| 午夜av区久久| 久久久久亚洲av无码专区喷水| 深夜福利在线看| 久久永久免费| 久久久女人电视剧免费播放下载| 国产精品久久久久久久av| av在线精品| 中文字幕乱码久久午夜不卡| 国产不卡一区二区在线观看| 在线观看av大片| 欧美中文字幕一区二区| 欧美日韩不卡一区| 91免费视频网站在线观看| www在线视频| 国产精品进线69影院| 免费国产一区二区| 天天干天天干天天干| 国产成人综合在线观看| 成人激情免费在线| 亚洲在线视频播放| 久久久久欧美精品| 亚洲人成电影网站色xx| 国产白袜脚足j棉袜在线观看| 国产电影一区| 9191成人精品久久| the porn av| 一区二区视频免费完整版观看| 亚洲成人综合网站| 欧美视频免费看欧美视频| 2024最新电影在线免费观看| 亚洲欧洲三级电影| 性刺激综合网| 高清国产福利在线观看| 久久久久免费观看| 欧美日韩三区四区| 美女做暖暖视频免费在线观看全部网址91| 92精品国产成人观看免费| 精品欧美一区二区精品久久| 手机看片一区二区| 久久婷婷国产综合精品青草 | 在线视频亚洲欧美中文| 欧美一区二区三区电影| 91精产国品一二三产区别沈先生| 欧美黑粗硬大| 中国精品18videos性欧美| 久久se精品一区精品二区| 国产精品99久久久久久久久久久久| 日韩精品成人在线| 国产精品日韩久久久| 98精品国产自产在线观看| 亚洲天堂日韩av| 免播放器亚洲| 亚洲欧洲自拍偷拍| 成人黄色a级片| 日韩大片在线播放| 麻豆国产va免费精品高清在线| 国产午夜福利一区| 99国产精品一区二区| 欧美成人一二三| 久久精品免费在线| 亚洲专区一区二区三区| 国产精品久久久久久久久久小说| 亚洲一区二区影视| 成人一区二区三区视频在线观看 | 欧美极品少妇无套实战| 成人性生交大片免费看网站 | 亚洲欧洲日夜超级视频| 青青影院在线观看| 亚洲一区国产视频| 国产一区二区视频免费在线观看| 精品美女一区| 精品久久久久久综合日本欧美| 精品久久久久久中文字幕人妻最新| 精品国产一区二区三区四区| 555www色欧美视频| 扒开伸进免费视频| 精品99久久| 久久九九免费视频| 亚洲天堂日韩av| 精品一区中文字幕| 精品国产乱码久久久久久郑州公司| 神马午夜精品95| 国产精品久久久久久久第一福利| 97中文字幕在线| 欧美与亚洲与日本直播| 精品少妇一区二区三区视频免付费 | 国产三级精品三级在线| 9999久久久久| 一本色道久久88综合日韩精品| 久久久av一区| 久久久久国产免费| 一区二区三区四区在线看 | 日本中文字幕在线观看| 亚洲一区二区三区激情| 一区二区三区 日韩| 欧美精品中文| 蜜臀久久99精品久久久久久宅男 | 久久久亚洲影院| 无码人妻精品一区二区蜜桃色欲| 国产精品亚洲专一区二区三区 | 人妻少妇被粗大爽9797pw| 高清久久精品| 原创国产精品91| 国产a∨精品一区二区三区仙踪林| 老汉av免费一区二区三区| 精品免费视频123区| 青青青国内视频在线观看软件| 欧美日韩精品一区二区三区| 大又大又粗又硬又爽少妇毛片| 欧美久久影院| 国产成人一区二区三区电影| 午夜精品小视频| 亚洲欧美乱综合| 中文字幕第21页| 视频精品在线观看| 青青精品视频播放| 午夜视频www| 亚洲国产精品一区二区尤物区| 91丝袜超薄交口足| 91日韩欧美| 国产欧美日韩精品丝袜高跟鞋| 四虎永久在线精品免费网址| 亚洲自拍偷拍麻豆| 日本一本在线视频| 中国成人一区| 亚洲xxxxx| 中文字幕中文字幕在线十八区| 欧美日韩视频在线一区二区| 五月婷婷欧美激情| 免费观看久久久4p| 一区二区精品在线观看| 成人性片免费| 在线观看日韩专区| 在线免费看av的网站| 久久久久久久av麻豆果冻| 午夜精品久久久久久久男人的天堂| 一本色道无码道dvd在线观看| 国产乱人伦精品一区| 国内外成人免费激情在线视频| 欧美一级在线免费观看| 天天影视网天天综合色在线播放| 精品国产乱码久久久久夜深人妻| 亚洲视频日本| 国产一级精品aaaaa看| www.超碰在线| 亚洲精品视频网上网址在线观看| 91video| 国产日韩欧美在线一区| 岛国毛片在线播放| 欧美福利影院| 亚洲japanese制服美女| 麻豆av在线播放| 日韩成人xxxx| 中文字幕777| 欧美激情一区二区三区蜜桃视频| 黄色一级二级三级| 99久久99久久精品国产片桃花| 亚洲www在线| 欧美aaa免费| 亚洲精品一区二区网址| 国产91av在线播放| 亚洲激情图片qvod| 深夜视频在线观看| 久久夜色精品| 国产女人18毛片| 欧美爱爱网站| 国产精品一区二区性色av| 污视频网站在线免费| 精品在线观看国产| 在线观看国产成人| 亚洲成年人影院| 长河落日免费高清观看| 国产成人aaa| 一区二区三区视频在线观看免费| 亚洲欧洲中文字幕| 乱一区二区三区在线播放| 伊人久久大香伊蕉在人线观看热v 伊人久久大香线蕉综合影院首页 伊人久久大香 | 国自产精品手机在线观看视频| 午夜精品一二三区| 在线免费一区三区| 极品魔鬼身材女神啪啪精品| 国产99精品国产| 亚洲男人天堂色| 一区在线免费| 在线视频91| 中文在线综合| 国产在线999| 中文字幕影音在线| 欧美国产日产韩国视频| 国产在线视频你懂得| 欧美精品一区二区不卡| 亚洲系列在线观看| 欧美视频在线观看免费| 三上悠亚在线观看视频| 久久亚洲二区三区| 国产情侣久久久久aⅴ免费| 蜜臀av一区二区| 日韩精品视频久久| 亚洲第一黄网| 久久久久久av无码免费网站下载| 香蕉久久夜色精品国产更新时间| 亚洲free性xxxx护士白浆| 国产情侣一区二区三区| 国产a级全部精品| 擼擼色在线看观看免费| 欧美老肥婆性猛交视频| av天在线观看| 日韩精品欧美国产精品忘忧草| 99久久免费国产精精品| 欧美日韩在线播放一区| 亚洲欧美偷拍视频| 亚洲1区2区3区4区| 久久精品免费在线| 亚洲综合色成人| 五月婷婷综合激情网| 久久精品视频免费观看| 在线观看国产三级| 国产一区二区在线电影| 最新国产黄色网址| 奇米在线7777在线精品| 茄子视频成人免费观看| 国产欧美激情| 免费无码毛片一区二三区| 亚洲黄色在线| 成人在线免费播放视频| 久久精品国产一区二区三| 久久久久xxxx| 成人激情黄色小说| 女~淫辱の触手3d动漫| 欧美高清在线精品一区| √天堂中文官网8在线| 亚洲一区二区三区自拍| 一区二区三区视频免费看| 色婷婷精品大在线视频| 91麻豆国产在线| 日韩美一区二区三区| 亚洲av成人精品毛片| 中文字幕9999| 蜜臀av在线播放| 日韩美女写真福利在线观看| 日韩av黄色| 国产日韩一区二区| 欧美精品一区二区久久| 秋霞在线一区二区| 在线成人欧美| 亚洲精品乱码久久久久久按摩观| 日韩熟女精品一区二区三区| 日韩欧美中文字幕在线观看| 中文字幕在线观看精品| 欧美va亚洲va香蕉在线| 黄色小视频在线观看| 超薄丝袜一区二区| 人人草在线视频| 成人xvideos免费视频| 国产一区二区三区不卡av| 亚洲国产精品久久久久婷婷老年 | 成年人三级视频| 日韩视频三区| 亚洲a级黄色片| 91欧美一区二区| 少妇人妻丰满做爰xxx| 日韩欧美亚洲国产一区| 国产免费av观看| 亚洲欧洲偷拍精品| 少女频道在线观看免费播放电视剧| 欧美一区深夜视频| www.久久久.com| 欧美一进一出视频| 激情丁香综合| 奇米视频7777| 国产色综合一区| 国产精品第九页| 91精品一区二区三区在线观看| 嫩草研究院在线观看| 欧美激情极品视频| 国产美女亚洲精品7777| 日韩三级电影免费观看| 国产欧美二区| 中文字幕一区二区三区乱码不卡| 国产精品美女久久久久久久网站| 国产又爽又黄的视频| 日韩精品在线网站| 久草资源在线| 国产精品视频免费在线观看| 午夜先锋成人动漫在线| 欧美成人高潮一二区在线看| 国产乱码精品一区二区三区忘忧草 | 不卡的av电影| 欧美三级在线免费观看| 欧美日韩aaa| 午夜老司机在线观看| 国产不卡av在线| 久久91麻豆精品一区| 777777av| 怕怕欧美视频免费大全| 一区二区亚洲精品国产| 黑人精品视频| 99一区二区| 欧美日韩1080p| 国产伦精品一区二区三区妓女下载| 中文字幕在线不卡一区 | 午夜在线一区| 国产人妻人伦精品1国产丝袜| 午夜影院在线观看欧美| 国产成人无码www免费视频播放| 久久国产精品久久久| 麻豆国产一区| 国产又粗又大又爽的视频| 国产一区二区在线视频| 免费中文字幕在线| 欧美成人官网二区| 黄色美女视频在线观看| 99久热re在线精品996热视频| 一区二区日韩欧美| 午夜影院免费版| 亚洲综合清纯丝袜自拍| 黄色福利在线观看| 91大神在线播放精品| 少妇精品久久久一区二区| 激情内射人妻1区2区3区| 国产亚洲欧美一级| 在线观看毛片网站| 久久久久99精品久久久久| 涩爱av色老久久精品偷偷鲁| 69sex久久精品国产麻豆| 91在线视频免费观看| 欧美日韩a v| 日韩综合中文字幕| 日韩最新av| 日韩视频第二页| 欧美激情一区二区三区四区| 国产麻豆免费视频| 欧美激情一区二区三级高清视频| 美国成人xxx| 黄色片在线免费| 亚洲四区在线观看| 欧美 日韩 国产 成人 在线 91| 91av视频在线免费观看| 成人免费在线播放| 一二三区视频在线观看| 色综合久久久久综合99| 欧美一级二级三级区| 国产九色精品| 日本午夜一区二区| 青青草精品在线视频| 亚洲美女自拍视频| 粉嫩av国产一区二区三区| 午夜免费福利小电影| 中文子幕无线码一区tr| 性一交一乱一乱一视频| 国产成人拍精品视频午夜网站| 一区二区在线| 国产美女免费网站| 精品国产一区久久| 巨胸喷奶水www久久久免费动漫| 男人添女人下部视频免费| 久久精品夜色噜噜亚洲aⅴ| 999久久久久|