The smallest blockchain is built from Python code with less than 50 lines (original: Let's build the tiniest Blockchain), but the code in the text is based on Python2, and the PYTHON3 environment needs to be modified slightly as follows: (built The first contact with the blockchain students, first read the last reference article of this article)
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 = Previo Us_hash Self._hash = Self.hash_block () @property def hash (self): return Self._hash @property def Index (self): return self._index @property def data: 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, D Ate.datetime.now (), "seed Block", "0") def next_block (last_block): This_index = last_block.index + 1 This_timestamp = date. DateTime.Now () This_data = "I am a new district block" + str (this_index) This_hash = Last_block.hash return block (This_index, this_t Imestamp, 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 #{} has been added to the blockchain!"). Format (Block_to_add.index)) print ("Hash: {}". Format (Block_to_add.hash)) print ("Data: {}\n". Format (block_to_ Add.data))
Run Result:
Block #1 has been added to the blockchain! Hash:403239f4c6fc4d83fb31c07d8c0b98d922e46fe6bcd8e3b05a70b46d5357a3cadata: I am new District block 1Block #2 have joined the blockchain! Hash:4e4e233cf54479b221d9a78e9558e4d923fc554366556a783e88798aa58dcdecdata: I am new District block 2Block #3 have joined the blockchain! Hash:35eef8b63e4e264f34fc557e637a22b6ae804ab75219291ccbae8f0c240e7ae8data: I am new District block 3Block #4 have joined the blockchain! Hash:61836d0c9c2b6c394db9ca77fbe2ad19daf63929f0c14e681ce3a3e9e87006e4data: I am new District block 4Block #5 have joined the blockchain! Hash:c4ed97efd2fb4d97232c048119780e08b7afd99746a4734bd3c91253ffed5ee8data: I am new District block 5Block #6 have joined the blockchain! Hash:b0f02cc59d9f3f9ec29080938765efaf5d93393632a2968d3026fbf2a02e7e33data: I am new District block 6Block #7 have joined the blockchain! Hash:0d285a0f9e902f0fcbaaa657e7ea39d098cd614517a7a66567acb511e7aa2eb3data: I am new District block 7Block #8 have joined the blockchain! Hash:f461ac428043f328309da7cac33803206cea9912f0d4e8d8cf2786d21e5ff403data: I am new District block 8Block #9 have joined the blockchain! Hash:136a6c3f4f10b35515a07d4d4849c13ceae8cbbddb512583ab10d32b73d90d08data: I am new District block 9Block #10 have joined the blockchain! Hash:3da8f83f07745e1a955f5f55f75a8708453afb24b5d4686d7dc7658399563a82data: I'm a new district block.10Block #11 have joined the blockchain! Hash:c1320b1d9e73843c718a8d708cabe7546ecd7b196afa322bba6836f01d0a078ddata: I am new District block 11Block #12 have joined the blockchain! Hash:075265e7b007ac2c5c3c1b88bdb0b8553c6330c515f2f584dd355a3ed64e179adata: I am new District block 12Block #13 have joined the blockchain! Hash:9d212c6a850e3bbd9fbb99e00f576a4c4896483e8110b80793dc0a711867a7d4data: I am new District block 13Block #14 have joined the blockchain! Hash:97978b45405867ccf724e7f8498f6c69cc4dbf24adb41e95f35ca650cd79d987data: I am new District block 14Block #15 have joined the blockchain! Hash:7c3e7ad8a9d8042de861bfbff6d3c2154b1ec23442ea0ee20a7b00cdf25a1164data: I am new District block 15Block #16 have joined the blockchain! Hash:bc4fbcd63e038674f18c83bc5460bdcc538c8b0f834019bf358eb6534f322e1adata: I am new District block 16Block #17 have joined the blockchain! Hash:36688e8a3bd29f940b4528f460a92d0dc410fc14b6e2edfd834238c18c71dafadata: I am new District block 17Block #18 have joined the blockchain! Hash:050a08c4137e05a0b90f842aea5534e5c8a80333e384eeeda5d5913849c741cddata: I am new District block 18Block #19 have joined the blockchain! Hash:c47c5707875d7dcf125b1a448b5cbe8fbe125614e48d87e14b7d2c853e1bc3fadata: I am new District block 19Block #20 have joined the blockchain! hash:4d7bef7ed5d4afff9b3eaf33219be49f33f32476ad21f1ab6c2d70635078461aData: I am a new district block 20
Of course this is just the most basic "data structure" level of Hello World, easy to understand the blockchain data structure, the real blockchain mining speed control (10 minutes 1 blocks), fork processing (the first to reach 6 blocks of the fork wins) These are not taken into account.
Reference article:
Satoshi's paper (Chinese translation)
Nanyi: Blockchain Starter Tutorial
The most basic blockchain Hello World (Python3 implementation)