-
大小: 11KB文件類型: .rar金幣: 2下載: 0 次發布日期: 2021-05-09
- 語言: Python
- 標簽: 區塊鏈??入門代碼??merkletree??
資源簡介
Python學習區塊鏈入門代碼。區塊中數據使用MerkleTree結構,包含挖礦模擬等內容。代碼易讀,適合初步入門學習。

代碼片段和文件信息
#?-*-?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??#?產生唯一標示
????????self.nonce?=?None??#?nonce值(難度值)
????????self.data?=?data??#?區塊內容
????????self.timestamp?=?str(time.time())??#?區塊標識時間戳
????????self.ver?=?“1.0“??#?區塊鏈版本號
????????self.random_num?=?random.randint(10002000)??#?區塊頭隨機數
????????self.previous_hash?=?previous_hash??#?父節點哈希值
????????if?isinstance(data?list):
????????????self.mt?=?merkletree(data)
????????????self.merkle_root?=??str(self.mt.merkle_root)?#?區塊MT樹TopHash值
????????else:
????????????self.mt?=?list()
????????????self.merkle_root?=?“None“??#?區塊MT樹TopHash值
????def?hash(self?nonce=None):
????????‘‘‘
????????????計算區塊的哈希值?拼接區塊
????????‘‘‘
????????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
????????#?循環直到生成一個有效的哈希值
????????while?True:
????????????the_hash?=?self.hash(nonce=cur_nonce)
????????????if?self.hash_is_valid(the_hash):??#?如果生成的哈希值有效
????????????????self.nonce?=?cur_nonce??#?保持當前nonce值
????????????????break??#?并退出
????????????else:
????????????????cur_nonce?+=?1??#?若當前哈希值無效,更新nonce值,進行加1操作
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????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最新破解方法
評論
共有 條評論