標籤:基於 python tor 處理 aaa 方便 python3 dig SHA256
源自 用不到 50 行的 Python 代碼構建最小的區塊鏈 (英文原文:Let’s Build the Tiniest Blockchain ) ,但是文中的代碼是基於python2的,python3環境下需要略做修改,修改後的代碼如下:(建議初次接觸區塊鏈的同學,先閱讀本文最後的參考文章)
import hashlib as hasherimport datetime as dateclass Block: def __init__(self, index, timestamp, data, previous_hash): self._index = index self._timestamp = timestamp self._data = data self._previous_hash = previous_hash self._hash = self.hash_block() @property def hash(self): return self._hash @property def index(self): return self._index @property def data(self): return self._data def hash_block(self): sha = hasher.sha256() temp = str(self._index) + str(self._timestamp) + str(self._data) + str(self._previous_hash) sha.update(temp.encode("utf8")) return sha.hexdigest()def create_genesis_block(): return Block(0, date.datetime.now(), "種子區塊", "0")def next_block(last_block): this_index = last_block.index + 1 this_timestamp = date.datetime.now() this_data = "我是新區塊 " + str(this_index) this_hash = last_block.hash return Block(this_index, this_timestamp, this_data, this_hash)block_chain = [create_genesis_block()]previous_block = block_chain[0]num_of_blocks_to_add = 20for i in range(0, num_of_blocks_to_add): block_to_add = next_block(previous_block) block_chain.append(block_to_add) previous_block = block_to_add print("Block #{} 已經加入區塊鏈!".format(block_to_add.index)) print("Hash: {}".format(block_to_add.hash)) print("Data: {}\n".format(block_to_add.data))
運行結果:
Block #1 已經加入區塊鏈!Hash: 403239f4c6fc4d83fb31c07d8c0b98d922e46fe6bcd8e3b05a70b46d5357a3caData: 我是新區塊 1Block #2 已經加入區塊鏈!Hash: 4e4e233cf54479b221d9a78e9558e4d923fc554366556a783e88798aa58dcdecData: 我是新區塊 2Block #3 已經加入區塊鏈!Hash: 35eef8b63e4e264f34fc557e637a22b6ae804ab75219291ccbae8f0c240e7ae8Data: 我是新區塊 3Block #4 已經加入區塊鏈!Hash: 61836d0c9c2b6c394db9ca77fbe2ad19daf63929f0c14e681ce3a3e9e87006e4Data: 我是新區塊 4Block #5 已經加入區塊鏈!Hash: c4ed97efd2fb4d97232c048119780e08b7afd99746a4734bd3c91253ffed5ee8Data: 我是新區塊 5Block #6 已經加入區塊鏈!Hash: b0f02cc59d9f3f9ec29080938765efaf5d93393632a2968d3026fbf2a02e7e33Data: 我是新區塊 6Block #7 已經加入區塊鏈!Hash: 0d285a0f9e902f0fcbaaa657e7ea39d098cd614517a7a66567acb511e7aa2eb3Data: 我是新區塊 7Block #8 已經加入區塊鏈!Hash: f461ac428043f328309da7cac33803206cea9912f0d4e8d8cf2786d21e5ff403Data: 我是新區塊 8Block #9 已經加入區塊鏈!Hash: 136a6c3f4f10b35515a07d4d4849c13ceae8cbbddb512583ab10d32b73d90d08Data: 我是新區塊 9Block #10 已經加入區塊鏈!Hash: 3da8f83f07745e1a955f5f55f75a8708453afb24b5d4686d7dc7658399563a82Data: 我是新區塊 10Block #11 已經加入區塊鏈!Hash: c1320b1d9e73843c718a8d708cabe7546ecd7b196afa322bba6836f01d0a078dData: 我是新區塊 11Block #12 已經加入區塊鏈!Hash: 075265e7b007ac2c5c3c1b88bdb0b8553c6330c515f2f584dd355a3ed64e179aData: 我是新區塊 12Block #13 已經加入區塊鏈!Hash: 9d212c6a850e3bbd9fbb99e00f576a4c4896483e8110b80793dc0a711867a7d4Data: 我是新區塊 13Block #14 已經加入區塊鏈!Hash: 97978b45405867ccf724e7f8498f6c69cc4dbf24adb41e95f35ca650cd79d987Data: 我是新區塊 14Block #15 已經加入區塊鏈!Hash: 7c3e7ad8a9d8042de861bfbff6d3c2154b1ec23442ea0ee20a7b00cdf25a1164Data: 我是新區塊 15Block #16 已經加入區塊鏈!Hash: bc4fbcd63e038674f18c83bc5460bdcc538c8b0f834019bf358eb6534f322e1aData: 我是新區塊 16Block #17 已經加入區塊鏈!Hash: 36688e8a3bd29f940b4528f460a92d0dc410fc14b6e2edfd834238c18c71dafaData: 我是新區塊 17Block #18 已經加入區塊鏈!Hash: 050a08c4137e05a0b90f842aea5534e5c8a80333e384eeeda5d5913849c741cdData: 我是新區塊 18Block #19 已經加入區塊鏈!Hash: c47c5707875d7dcf125b1a448b5cbe8fbe125614e48d87e14b7d2c853e1bc3faData: 我是新區塊 19Block #20 已經加入區塊鏈!Hash: 4d7bef7ed5d4afff9b3eaf33219be49f33f32476ad21f1ab6c2d70635078461aData: 我是新區塊 20
當然這隻是最基本的“資料結構”層級的hello world,方便大家理解區塊鏈的資料結構而已,真正區塊鏈中的挖礦速度控制(10分鐘1個塊)、分叉處理(最先達到6個塊的分叉勝出) 這些都沒有考慮進去。
參考文章:
中本聰的論文(中文翻譯)
阮一峰:區塊鏈入門教程
最基本的區塊鏈hello world(python3實現)