日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python簡(jiǎn)單區(qū)塊鏈模擬詳解

系統(tǒng) 1888 0

最近學(xué)習(xí)了一點(diǎn)python,那就試著做一做簡(jiǎn)單的編程練習(xí)。

首先是這個(gè)編程的指導(dǎo)圖,如下:

python簡(jiǎn)單區(qū)塊鏈模擬詳解_第1張圖片

對(duì)的,類似一個(gè)簡(jiǎn)單區(qū)塊鏈的模擬。

代碼如下:

            
class DaDaBlockCoin:

 #index 索引,timestamp 時(shí)間戳,data 交易記錄,self_hash交易hash,last_hash,上個(gè)hash
 def __init__(self,idex,timestamp,data,last_hash):
  self.idex = idex
  self.timestamp = timestamp
  self.data = data
  self.last_hash = last_hash
  self.self_hash=self.hash_DaDaBlockCoin()


 def hash_DaDaBlockCoin(self):
  sha = hashlib.md5()#加密算法,這里可以選擇sha256,sha512,為了打印方便,所以選了md5
  #對(duì)數(shù)據(jù)整體加密
  datastr = str(self.idex)+str(self.timestamp)+str(self.data)+str(self.last_hash)
  sha.update(datastr.encode("utf-8"))
  return sha.hexdigest()

def create_first_DaDaBlock(): # 創(chuàng)世區(qū)塊

 return DaDaBlockCoin(0, datetime.datetime.now(), "love dadacoin", "0")

# last_block,上一個(gè)區(qū)塊
def create_money_DadaBlock(last_block): # 其它塊
 this_idex = last_block.idex + 1 # 索引加1
 this_timestamp = datetime.datetime.now()
 this_data = "love dada" + str(this_idex) # 模擬交易數(shù)據(jù)
 this_hash = last_block.self_hash # 取得上一塊的hash
 return DaDaBlockCoin(this_idex, this_timestamp, this_data, this_hash)

DaDaBlockCoins = [create_first_DaDaBlock()] # 區(qū)塊鏈列表,只有一個(gè)創(chuàng)世區(qū)塊
nums = 10
head_block = DaDaBlockCoins[0]
print(head_block.idex, head_block.timestamp, head_block.self_hash, head_block.last_hash)
for i in range(nums):
 dadaBlock_add = create_money_DadaBlock(head_block) # 創(chuàng)建一個(gè)區(qū)塊鏈的節(jié)點(diǎn)
 DaDaBlockCoins.append(dadaBlock_add)
 head_block = dadaBlock_add
 print(dadaBlock_add.idex, dadaBlock_add.timestamp, dadaBlock_add.self_hash, dadaBlock_add.last_hash)
          

打印結(jié)果如下:

python簡(jiǎn)單區(qū)塊鏈模擬詳解_第2張圖片

與開頭的指導(dǎo)思路完美契合,雖然只是很簡(jiǎn)單的模擬。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 西林县| 麻江县| 九龙城区| 利川市| 太康县| 渝北区| 泗洪县| 鄂托克旗| 碌曲县| 囊谦县| 南涧| 那曲县| 沙洋县| 曲阜市| 赤壁市| 秀山| 寻甸| 建平县| 来宾市| 石林| 吕梁市| 江安县| 景宁| 项城市| 米泉市| 万全县| 铅山县| 昂仁县| 黄陵县| 双辽市| 公主岭市| 定安县| 荥阳市| 柳州市| 淮阳县| 浦江县| 井陉县| 北辰区| 金昌市| 德州市| 申扎县|