源自 用不到 50 行的 Python 代码构建最小的区块链 (英文原文:Let’s Build the Tiniest Blockchain ) ,但是文中的代码是基于python2的,python3环境下需要略做修改,修改后的代码如下:(建议初次接触区块链的同学,先阅读本文最后的参考文章)
import hashlib as hasher
import datetime as date class 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 = 20 for 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: 403239f4c6fc4d83fb31c07d8c0b98d922e46fe6bcd8e3b05a70b46d5357a3ca
Data: 我是新区块 1 Block #2 已经加入区块链!
Hash: 4e4e233cf54479b221d9a78e9558e4d923fc554366556a783e88798aa58dcdec
Data: 我是新区块 2 Block #3 已经加入区块链!
Hash: 35eef8b63e4e264f34fc557e637a22b6ae804ab75219291ccbae8f0c240e7ae8
Data: 我是新区块 3 Block #4 已经加入区块链!
Hash: 61836d0c9c2b6c394db9ca77fbe2ad19daf63929f0c14e681ce3a3e9e87006e4
Data: 我是新区块 4 Block #5 已经加入区块链!
Hash: c4ed97efd2fb4d97232c048119780e08b7afd99746a4734bd3c91253ffed5ee8
Data: 我是新区块 5 Block #6 已经加入区块链!
Hash: b0f02cc59d9f3f9ec29080938765efaf5d93393632a2968d3026fbf2a02e7e33
Data: 我是新区块 6 Block #7 已经加入区块链!
Hash: 0d285a0f9e902f0fcbaaa657e7ea39d098cd614517a7a66567acb511e7aa2eb3
Data: 我是新区块 7 Block #8 已经加入区块链!
Hash: f461ac428043f328309da7cac33803206cea9912f0d4e8d8cf2786d21e5ff403
Data: 我是新区块 8 Block #9 已经加入区块链!
Hash: 136a6c3f4f10b35515a07d4d4849c13ceae8cbbddb512583ab10d32b73d90d08
Data: 我是新区块 9 Block #10 已经加入区块链!
Hash: 3da8f83f07745e1a955f5f55f75a8708453afb24b5d4686d7dc7658399563a82
Data: 我是新区块 10 Block #11 已经加入区块链!
Hash: c1320b1d9e73843c718a8d708cabe7546ecd7b196afa322bba6836f01d0a078d
Data: 我是新区块 11 Block #12 已经加入区块链!
Hash: 075265e7b007ac2c5c3c1b88bdb0b8553c6330c515f2f584dd355a3ed64e179a
Data: 我是新区块 12 Block #13 已经加入区块链!
Hash: 9d212c6a850e3bbd9fbb99e00f576a4c4896483e8110b80793dc0a711867a7d4
Data: 我是新区块 13 Block #14 已经加入区块链!
Hash: 97978b45405867ccf724e7f8498f6c69cc4dbf24adb41e95f35ca650cd79d987
Data: 我是新区块 14 Block #15 已经加入区块链!
Hash: 7c3e7ad8a9d8042de861bfbff6d3c2154b1ec23442ea0ee20a7b00cdf25a1164
Data: 我是新区块 15 Block #16 已经加入区块链!
Hash: bc4fbcd63e038674f18c83bc5460bdcc538c8b0f834019bf358eb6534f322e1a
Data: 我是新区块 16 Block #17 已经加入区块链!
Hash: 36688e8a3bd29f940b4528f460a92d0dc410fc14b6e2edfd834238c18c71dafa
Data: 我是新区块 17 Block #18 已经加入区块链!
Hash: 050a08c4137e05a0b90f842aea5534e5c8a80333e384eeeda5d5913849c741cd
Data: 我是新区块 18 Block #19 已经加入区块链!
Hash: c47c5707875d7dcf125b1a448b5cbe8fbe125614e48d87e14b7d2c853e1bc3fa
Data: 我是新区块 19 Block #20 已经加入区块链!
Hash: 4d7bef7ed5d4afff9b3eaf33219be49f33f32476ad21f1ab6c2d70635078461a
Data: 我是新区块 20
当然这只是最基本的“数据结构”级别的hello world,方便大家理解区块链的数据结构而已,真正区块链中的挖矿速度控制(10分钟1个块)、分叉处理(最先达到6个块的分叉胜出) 这些都没有考虑进去。
参考文章: