Python逐步实现区块链

步骤一(2018/8/6)

这大概是,最简单的区块链demo

简述:

  • 在这个代码中实现最最最基础的区块链部分(相信大部分大佬甚至不会认为这是个区块链)
  • 代码其实在晚上传的代码。不过之前的那个代码有点老了(而且还有些错误),在对部分文字进行修改之后实现了下面的代码
  • 本代码会创建20个block。main函数部分,只是不断的更替previous指针指向,从而实现创建。
  • 有一个错误,我是在看了这篇文章”Unicode-objects must be encoded before hashing 错误解决办法” 之后得到的解答,非常感谢。

代码:

import hashlib as hasher
import datetime as date


def create_genesis_block():
    # Manually construct a block with
    # index zero and arbitrary previous hash
    return Block(0, date.datetime.now(), "Genesis Block", "0")


def next_block(last_block):
    this_index = last_block.index + 1
    this_timestamp = date.datetime.now()
    this_data = "Hey! I'm block " + str(this_index)
    this_hash = last_block.hash
    return Block(this_index, this_timestamp, this_data, this_hash)


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()

    def hash_block(self):
        sha = hasher.sha256()
        sha.update((str(self.index) +
                   str(self.timestamp) +
                   str(self.data) +
                   str(self.previous_hash)).encode('utf-8'))
        return sha.hexdigest()


blockchain = [create_genesis_block()]
previous_block = blockchain[0]

# How many blocks should we add to the chain
# after the genesis block
num_of_blocks_to_add = 20

# Add blocks to the chain
for i in range(0, num_of_blocks_to_add):
    block_to_add = next_block(previous_block)
    blockchain.append(block_to_add)
    previous_block = block_to_add
    # Tell everyone about it!
    print("Block #{} has been added to the blockchain!".format(block_to_add.index))
    print("Hash: {}".format(block_to_add.hash))

一种输出:

Block #1 has been added to the blockchain!
Hash: dc5489e6377dbd07c2c46b194615d9d7d723de481311ee627c72b46ee6c71325
Block #2 has been added to the blockchain!
Hash: 6a692d4cdd27fad320ef3a8d4b6f9283031e606d919d0a2bb7f0c1b98c12539e
Block #3 has been added to the blockchain!
Hash: 60ea76918b74a5182c9577f9a5e219f0b4d2e46150038b453e41483ff426ae4c
Block #4 has been added to the blockchain!
Hash: 7e2bda063cbc77ad971c68682cadef557b9fc1f8bc61dc22c3c6124651f7a18f
Block #5 has been added to the blockchain!
Hash: 2b51e538ebe8409749a92b23b55ac4f932ef307e2f1d392fc5518ec0f156d8bd
Block #6 has been added to the blockchain!
Hash: de28d3c59d39099594e64c46590596997ed8df208ea6363bcf19ac8a127bfcb7
Block #7 has been added to the blockchain!
Hash: 26613ab62d3d4da489e37c705923362c6923c7b46b29d17096a72efded42cee8
Block #8 has been added to the blockchain!
Hash: 351ee1b9fa397317d2a8b7a946dd9ba7852b9f61818e19a04ab6c37d5760b625
Block #9 has been added to the blockchain!
Hash: fd2e7fce08ab5c2458fc74e83c77a8b22f45da363f8d4cc66f19cf0b4813996d
Block #10 has been added to the blockchain!
Hash: 35e4862c0ee245b900a95205da3a425e8c60e70f86fd726ed1a7da969a75a007
Block #11 has been added to the blockchain!
Hash: 461bdcddc99ad2b6a71144681a5465b77ccabf400b3cdb7bbb5756b0030d835b
Block #12 has been added to the blockchain!
Hash: 6a8845baff3d0da34312b21de349a23a783d0495b40633328049c17441a3ab00
Block #13 has been added to the blockchain!
Hash: 46d7e317942c6179adcda50e9de48b2d5bf8fdcf020b8d99581b42cd2a789fad
Block #14 has been added to the blockchain!
Hash: 86aea109ab6f4a33856d2141e687cb0b3a950cf52abfa77bfe7264a785828cba
Block #15 has been added to the blockchain!
Hash: 0a80fdc5accb9ef3ed244488b4c50030c4e5d12c7e955596e35acfe3dff5eba1
Block #16 has been added to the blockchain!
Hash: 465e00b90fd6920808ce8740180f23207ad705b39a887fde63dc449597fe434a
Block #17 has been added to the blockchain!
Hash: 0e71ac5529771a92ddd39056c3c0fbc157c3fc48aeeb1e391ce53aefe14f1df1
Block #18 has been added to the blockchain!
Hash: 3c7ae5426f92341eb2dbee81f34fb27847a4233db60b4f828a4b3f73b6e76295
Block #19 has been added to the blockchain!
Hash: a7f545bb7a8b0048b852714bc92814117401f29231f63d16705168d500598764
Block #20 has been added to the blockchain!
Hash: ec45fd3a29cbde55dc4542fb940ef85d709321184b4c9e9603b20ac334471731

后期会逐渐完善~

猜你喜欢

转载自blog.csdn.net/a19990412/article/details/81474323