-
大小: 11KB文件類型: .rar金幣: 2下載: 0 次發(fā)布日期: 2021-05-09
- 語言: Python
- 標(biāo)簽: 區(qū)塊鏈??入門代碼??merkletree??
資源簡介
Python學(xué)習(xí)區(qū)塊鏈入門代碼。區(qū)塊中數(shù)據(jù)使用MerkleTree結(jié)構(gòu),包含挖礦模擬等內(nèi)容。代碼易讀,適合初步入門學(xué)習(xí)。

代碼片段和文件信息
#?-*-?coding:utf-8?-*-
import?hashlib
import?uuid
import?random
import?time
from?merkletree?import?*
class?Block(object):
????def?__init__(self?data=None?previous_hash=None):
????????self.identifier?=?uuid.uuid4().hex??#?產(chǎn)生唯一標(biāo)示
????????self.nonce?=?None??#?nonce值(難度值)
????????self.data?=?data??#?區(qū)塊內(nèi)容
????????self.timestamp?=?str(time.time())??#?區(qū)塊標(biāo)識時(shí)間戳
????????self.ver?=?“1.0“??#?區(qū)塊鏈版本號
????????self.random_num?=?random.randint(10002000)??#?區(qū)塊頭隨機(jī)數(shù)
????????self.previous_hash?=?previous_hash??#?父節(jié)點(diǎn)哈希值
????????if?isinstance(data?list):
????????????self.mt?=?merkletree(data)
????????????self.merkle_root?=??str(self.mt.merkle_root)?#?區(qū)塊MT樹TopHash值
????????else:
????????????self.mt?=?list()
????????????self.merkle_root?=?“None“??#?區(qū)塊MT樹TopHash值
????def?hash(self?nonce=None):
????????‘‘‘
????????????計(jì)算區(qū)塊的哈希值?拼接區(qū)塊
????????‘‘‘
????????message?=?hashlib.sha256()
????????message.update(str(self.previous_hash).encode(‘utf-8‘))
????????message.update(self.ver.encode(‘utf-8‘))
????????message.update(self.timestamp.encode(‘utf-8‘))
????????message.update(self.identifier.encode(‘utf-8‘))
????????message.update(str(nonce).encode(‘utf-8‘))
????????message.update(str(self.random_num).encode(‘utf-8‘))
????????message.update(str(self.merkle_root).encode(‘utf-8‘))
????????return?message.hexdigest()
????def?hash_is_valid(self?the_hash):
????????return?the_hash.startswith(‘0000‘)
????def?__str__(self):
????????return?‘Block‘.format(self.hash(self.nonce)?self.nonce)
????def?__repr__(self):
????????return?‘Block‘.format(self.hash(self.nonce)?self.nonce)
????def?mine(self):
????????#?初始化nonce為0
????????cur_nonce?=?self.nonce?or?0
????????#?循環(huán)直到生成一個(gè)有效的哈希值
????????while?True:
????????????the_hash?=?self.hash(nonce=cur_nonce)
????????????if?self.hash_is_valid(the_hash):??#?如果生成的哈希值有效
????????????????self.nonce?=?cur_nonce??#?保持當(dāng)前nonce值
????????????????break??#?并退出
????????????else:
????????????????cur_nonce?+=?1??#?若當(dāng)前哈希值無效,更新nonce值,進(jìn)行加1操作
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件???????2282??2018-03-16?01:39??blockchain_2_代碼\block.py
?????文件???????1086??2018-02-28?13:44??blockchain_2_代碼\blockchain.py
?????文件???????2707??2018-03-16?13:17??blockchain_2_代碼\main.py
?????文件????????472??2018-03-15?23:38??blockchain_2_代碼\.idea\blockchain_1.iml
?????文件????????276??2018-02-28?12:51??blockchain_2_代碼\.idea\modules.xm
?????文件??????23220??2018-03-16?18:58??blockchain_2_代碼\.idea\workspace.xm
?????文件????????226??2018-03-15?23:38??blockchain_2_代碼\.idea\misc.xm
?????文件???????1210??2018-03-16?00:36??blockchain_2_代碼\__pycache__\blockchain.cpython-36.pyc
?????文件???????1727??2018-03-16?01:34??blockchain_2_代碼\__pycache__\merkletree.cpython-36.pyc
?????文件???????2011??2018-03-16?01:39??blockchain_2_代碼\__pycache__\block.cpython-36.pyc
?????文件???????2081??2018-03-16?01:34??blockchain_2_代碼\merkletree.py
?????目錄??????????0??2018-02-28?12:52??blockchain_2_代碼\.idea\inspectionProfiles
?????目錄??????????0??2018-02-28?15:21??blockchain_2_代碼\.idea
?????目錄??????????0??2018-02-28?13:46??blockchain_2_代碼\__pycache__
?????目錄??????????0??2018-02-28?14:01??blockchain_2_代碼
-----------?---------??----------?-----??----
????????????????37298????????????????????15
- 上一篇:利用爬蟲獲取IP的地理位置
- 下一篇:GitStack_2.3.10最新破解方法
評論
共有 條評論