50行Python代码构建小型区块链DogCoin(狗币)
本文介绍了如何使用python构建一个小型的区块链技术,使用Python2实现,代码不到50行。
这个简单的区块链。这将被称为DogCoin(狗币)。
(一)定义区块链的块结构。
像比特币一样,我们的DogCoin每个区块将包括作为区块的索引的哈希值,时间戳,数据以及前一个块的哈希值和新的哈希值。如下图:
index |
timestamp |
data |
previous_hash |
hash |
Python代码:
import hashlib as hasher
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))
return sha.hexdigest()
(二)创建创世区块。
如前所述,每个块都需要上一个块的信息。但是这就出现了一个问题:区块链中的第一个区块,也就是“创世区块”,如何创建?
按照模块化原则,我们将创建一个函数,只需返回一个创世区块,该区块的索引为0,它在“previous hash”参数中具有任意数据值和任意值。
如下图:
Index:0 |
timestamp |
Data:Genesis Block |
previous_hash |
hash |
Python代码:
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”)
(三) 创建后续区块。
我们需要一个新的函数来生成区块链中的后续区块。该函数将链中的前一个区块作为参数,创建要生成的区块的数据,并返回具有其相应数据的新块。新产生的区块会存储先前区块中的哈希值,区块链的完整性随着每个新的区块而增加。这个哈希链作为加密证明,有助于确保一旦新区块被添加到区块链中,它不能被替换或删除。
如下图:
Index:2 |
timestamp |
Data:Hey! I’m block 2 |
previous_hash |
hash |
Python代码:
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)
(四)创建(组装)我们的区块链DogCoin。
区块链其实就是一个将普通指针换成哈希指针的链表,所以每个区块不仅包含了上一个区块的地址,还包括了上一个区块的哈希值 。
列表的第一个元素是创世区块,然后需要添加后续区块。本例中,我们只添加了10个新的块,用for循环搞定。
如下图:
后续区块(4) |
后续区块(3) |
后续区块(2) |
创世区块(1) |
Python代码:
# Create the blockchain and add the genesis block
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 blockchain!”.format(block_to_add.index)
print “Hash: {}n”.format(block_to_add.hash)
(五)打印区块链DogCoin的每个块!
我们的区块链生效了!
完整Python.代码如下:(python 2.7)
import hashlib as hasher
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))
return sha.hexdigest()
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)
# Create the blockchain and add the genesis block
blockchain = [create_genesis_block()]
previous_block = blockchain[0]
#Print genesis block
print “DogCoin”
print “—————“
print “block index: {}”.format(blockchain[0].index)
print “timestamp: {}”.format(blockchain[0]. timestamp)
print “data: {}”.format(blockchain[0]. data)
print “previous_hash: {}”.format(blockchain[0]. previous_hash)
print “hash: {}n”.format(blockchain[0].hash)
print “—————“
# How many blocks should we add to the chain
# after the genesis block
num_of_blocks_to_add = 10
# 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 blockchain!”.format(block_to_add.index)
#print “Hash: {}n”.format(block_to_add.hash)
print “DogCoin”
print “—————“
print “block index: {}”.format(block_to_add.index)
print “timestamp: {}”.format(block_to_add. timestamp)
print “data: {}”.format(block_to_add. data)
print “previous_hash: {}”.format(block_to_add. previous_hash)
print “hash: {}n”.format(block_to_add.hash)
print “—————“
演示输出:
DogCoin
—————
block index: 0
timestamp: 2018-03-15 13:59:12.750000
data: Genesis Block
previous_hash: 0
hash: 978123c9edd1b41d5e35ce5126169cbd9f6f63efbc538b4d9ca63bb26da1a9b1
—————
DogCoin
—————
block index: 1
timestamp: 2018-03-15 13:59:12.828000
data: Hey! I’m block 1
previous_hash: 978123c9edd1b41d5e35ce5126169cbd9f6f63efbc538b4d9ca63bb26da1a9b1
hash: 0de84484d23722cde7987a9dbe4ed0af9e415679888328fc3b9c069827727cfa
—————
DogCoin
—————
block index: 2
timestamp: 2018-03-15 13:59:12.906000
data: Hey! I’m block 2
previous_hash: 0de84484d23722cde7987a9dbe4ed0af9e415679888328fc3b9c069827727cfa
hash: 7c0ce5d65626e4af2907ef6c8acfe05e9b8c39dcd5229583c0d4bc4f3d66a775
—————
DogCoin
—————
block index: 3
timestamp: 2018-03-15 13:59:12.984000
data: Hey! I’m block 3
previous_hash: 7c0ce5d65626e4af2907ef6c8acfe05e9b8c39dcd5229583c0d4bc4f3d66a775
hash: c1b7d949eea10af88659713b6145bd5069221d8fd49b337be8a8780684e0a16f
—————
DogCoin
—————
block index: 4
timestamp: 2018-03-15 13:59:13.078000
data: Hey! I’m block 4
previous_hash: c1b7d949eea10af88659713b6145bd5069221d8fd49b337be8a8780684e0a16f
hash: 4915c65e6dc42d18cdd5489f458181f746aefafd7bc8b43e8c47c68dd0c90998
—————
DogCoin
—————
block index: 5
timestamp: 2018-03-15 13:59:13.156000
data: Hey! I’m block 5
previous_hash: 4915c65e6dc42d18cdd5489f458181f746aefafd7bc8b43e8c47c68dd0c90998
hash: 95f61d76cabcf80e0ee3549ae88aa96f7b40e76f487ab1baa4ce0dee79fefbe8
—————
DogCoin
—————
block index: 6
timestamp: 2018-03-15 13:59:13.250000
data: Hey! I’m block 6
previous_hash: 95f61d76cabcf80e0ee3549ae88aa96f7b40e76f487ab1baa4ce0dee79fefbe8
hash: 3206120a85d02a7b2b3ab85188913db304e6929141b0e4956a5ab65e21e508fe
—————
DogCoin
—————
block index: 7
timestamp: 2018-03-15 13:59:13.343000
data: Hey! I’m block 7
previous_hash: 3206120a85d02a7b2b3ab85188913db304e6929141b0e4956a5ab65e21e508fe
hash: 54c4229a20dbe5e014b930eb31438c84bc6732240d023c8016dc17ad7b1b0287
—————
DogCoin
—————
block index: 8
timestamp: 2018-03-15 13:59:13.421000
data: Hey! I’m block 8
previous_hash: 54c4229a20dbe5e014b930eb31438c84bc6732240d023c8016dc17ad7b1b0287
hash: 27d4561bd21fd36a794926139de39a3ea32ced70ac19e644544884ea923d418f
—————
DogCoin
—————
block index: 9
timestamp: 2018-03-15 13:59:13.500000
data: Hey! I’m block 9
previous_hash: 27d4561bd21fd36a794926139de39a3ea32ced70ac19e644544884ea923d418f
hash: 143821446e2ccc4d7695242a591e0cd69f689a4b5f3c4c861c736deb8e5bee52
—————
DogCoin
—————
block index: 10
timestamp: 2018-03-15 13:59:13.578000
data: Hey! I’m block 10
previous_hash: 143821446e2ccc4d7695242a591e0cd69f689a4b5f3c4c861c736deb8e5bee52
hash: 1a66358fb32db223a2f998061e3bc90354b0ddb8d2c3a013cc86c63060c5c547
—————
声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:dandanxi6@qq.com