我正在尝试使用python创建自己的区块链。你知道吗
我拿了this source code,我正试图调整它以适应我的需要。 我的代码:
import datetime
import hashlib
class Block:
def __init__(
self,
previous_block_hash,
data,
timestamp,
sender,
):
self.previous_block_hash = previous_block_hash
self.data = data
self.timestamp = timestamp
self.hash = self.get_hash()
self.sender = self.get_sender()
@staticmethod
def create_genesis_block():
return Block('0', '0', datetime.datetime.now(), '')
def get_sender(self):
self.sender = input('Enter senders ID number: ')
return self.sender
def get_hash(self):
header_bin = str(self.previous_block_hash) + str(self.data) \
+ str(self.timestamp) + str(self.sender)
inner_hash = \
hashlib.sha256(header_bin.encode()).hexdigest().encode()
outer_hash = hashlib.sha256(inner_hash).hexdigest()
return outer_hash
blockchain = [Block.create_genesis_block()]
length_of_blockchain = len(blockchain)
print ('The genesis block has been created.')
print ('Hash: %s' % blockchain[0].hash)
blockchain.append(Block(blockchain[length_of_blockchain - 1].hash,
'Blockchain Number: '[length_of_blockchain - 1],
datetime.datetime.now(), Block.get_sender()))
我想向块中添加发送方和接收方。所以,我先从一个开始,那就是发送者。我不断得到这个错误here,我不知道如何修复它。一切都在本地测试,以确保一切检查,然后我将添加反应本机到它。希望有人能帮我
您正在尝试在初始化
self.sender
之前访问它。重新排序初始化可能对我们有帮助。你知道吗希望这有帮助
相关问题 更多 >
编程相关推荐