# block.py
import hashlib
import uuid
class Block(object):
def __init__(self, data=None, previous_hash=None):
self.identifier = uuid.uuid4().hex # 产生唯一标示
self.nonce = None # nonce值
self.data = data # 区块内容
self.previous_hash = previous_hash # 父节点哈希值
def hash(self, nonce=None):
'''
计算区块的哈希值
'''
message = hashlib.sha256()
message.update(self.identifier.encode('utf-8'))
message.update(str(nonce).encode('utf-8'))
message.update(str(self.data).encode('utf-8'))
message.update(str(self.previous_hash).encode('utf-8'))
return message.hexdigest()
def hash_is_valid(self, the_hash):
'''
校验区块哈希值有否有效
'''
return the_hash.startswith('0000')
def __repr__(self):
return 'Block<Hash: {}, Nonce: {}>'.format(self.hash(), self.nonce) # 创建一个内容为hello world的内容块
block = Block('Hello World')
block # block.py
import hashlib
import uuid
class Block(object):
def __init__(self, data=None, previous_hash=None):
self.identifier = uuid.uuid4().hex # 产生唯一标示
self.nonce = None # nonce值
self.data = data # 区块内容
self.previous_hash = previous_hash # 父节点哈希值
def hash(self, nonce=None):
'''
计算区块的哈希值
'''
message = hashlib.sha256()
message.update(self.identifier.encode('utf-8'))
message.update(str(nonce).encode('utf-8'))
message.update(str(self.data).encode('utf-8'))
message.update(str(self.previous_hash).encode('utf-8'))
return message.hexdigest()
def hash_is_valid(self, the_hash):
'''
校验区块哈希值有否有效
'''
return the_hash.startswith('0000')
def __repr__(self):
return 'Block<Hash: {}, Nonce: {}>'.format(self.hash(self.nonce), self.nonce)
'''
新增挖矿函数
'''
def mine(self):
# 初始化nonce为0
cur_nonce = self.nonce or 0
# 循环直到生成一个有效的哈希值
while True:
the_hash = self.hash(nonce=cur_nonce)
if self.hash_is_valid(the_hash): # 如果生成的哈希值有效
self.nonce = cur_nonce # 保持当前nonce值
break # 并退出
else:
cur_nonce += 1 # 若当前哈希值无效,更新nonce值,进行加1操作 block = Block('Hello World')
# 挖矿,循环直至找到合适的nonce
block.mine()
# 打印
block class BlockChain(object):
def __init__(self):
self.head = None # 指向最新的一个区块
self.blocks = {} # 包含所有区块的一个字典
'''
添加区块函数
'''
def add_block(self, new_block):
previous_hash = self.head.hash() if self.head else None
new_block.previous_hash = previous_hash
self.blocks[new_block.identifier] = {
'block': new_block,
'previous_hash': previous_hash,
'previous': self.head,
}
self.head = new_block
def __repr__(self):
num_existing_blocks = len(self.blocks)
return 'Blockchain<{} Blocks, Head: {}>'.format(
num_existing_blocks,
self.head.identifier if self.head else None
) # 初始化 chain = BlockChain() # 打印 chain
# 添加区块 chain.add_block(block) # 打印 chain
# 添加更多的区块 for i in range(6): new_block = Block(i) new_block.mine() chain.add_block(new_block) # 打印 chain
Blockchain<7 Blocks, Head: e7cb24ec7acd42a4aaebe7faee9e0713>
以上就是一个简单区块链,后面还会涉及到区块链的有效性。当区块链中一个区块被改变后,这个区块的哈希就会改变,从而影响到这块区块之后的区块,致使这个区块链不再有效。这些将在后续继续深入。
以上就是Python基础入门--区块链的详细内容,更多请关注Gxl网其它相关文章!
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did81795