本文整理汇总了Python中wallet.Wallet.verify_transaction方法的典型用法代码示例。如果您正苦于以下问题:Python Wallet.verify_transaction方法的具体用法?Python Wallet.verify_transaction怎么用?Python Wallet.verify_transaction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类wallet.Wallet
的用法示例。
在下文中一共展示了Wallet.verify_transaction方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: verify_transaction
# 需要导入模块: from wallet import Wallet [as 别名]
# 或者: from wallet.Wallet import verify_transaction [as 别名]
def verify_transaction(transaction, get_balance, check_funds=True):
"""Verify a transaction by checking whether the sender has sufficient coins.
Arguments:
:transaction: The transaction that should be verified.
"""
if check_funds:
sender_balance = get_balance(transaction.sender)
return sender_balance >= transaction.amount and Wallet.verify_transaction(transaction)
else:
return Wallet.verify_transaction(transaction)
示例2: verify_transaction
# 需要导入模块: from wallet import Wallet [as 别名]
# 或者: from wallet.Wallet import verify_transaction [as 别名]
def verify_transaction(transaction, get_balance, check_funds=True):
"""Verify sender has sufficient balance to allow transaction to be processed"""
if check_funds:
sender_balance = get_balance(transaction.sender)
if sender_balance < transaction.amount:
print('Transaction amount {} exceeds balance {}'.format(transaction.amount,
sender_balance))
print('Insufficient balance!')
return False
if not Wallet.verify_transaction(transaction):
print('Bad signature => %s' % transaction.signature)
print('Signature verification failed!')
return False
return True
示例3: mine_block
# 需要导入模块: from wallet import Wallet [as 别名]
# 或者: from wallet.Wallet import verify_transaction [as 别名]
def mine_block(self):
"""Create a new block and add open transactions to it."""
# Fetch the currently last block of the blockchain
print(self.public_key)
if self.public_key == None:
return None
last_block = self.__chain[-1]
# Hash the last block (=> to be able to compare it to the stored hash value)
hashed_block = hash_block(last_block)
proof = self.proof_of_work()
# Miners should be rewarded, so let's create a reward transaction
# reward_transaction = {
# "sender": "MINNER",
# "recipient": owner,
# "amount": MINING_REWARD
# }
reward_transaction = Transaction(
'MINING', self.public_key, '', MINING_REWARD)
# Copy transaction instead of manipulating the original open_transactions list
# This ensures that if for some reason the mining should fail, we don't have the reward transaction stored in the open transactions
copied_transactions = self.__open_transactions[:]
for tx in copied_transactions:
if not Wallet.verify_transaction(tx):
return False
copied_transactions.append(reward_transaction)
block = Block(len(self.__chain), hashed_block,
copied_transactions, proof)
self.__chain.append(block)
self.__open_transactions = []
self.save_data()
for node in self.__peer_nodes:
url = 'http://{}/broadcast-block'.format(node)
coverted_block = block.__dict__.copy()
coverted_block['transactions'] = [
tx.__dict__ for tx in coverted_block['transactions']]
try:
response = requests.post(url, json={'block': coverted_block})
if response.status_code == 400 and response.status_code == 500:
print('Block declined, needs resolving')
if response.status_code == 409:
self.resolve_conflicts = True
except requests.exceptions.ConnectionError:
continue
return block
示例4: mine_block
# 需要导入模块: from wallet import Wallet [as 别名]
# 或者: from wallet.Wallet import verify_transaction [as 别名]
def mine_block(self):
"""Mine the outstanding, pending transactions and commit them to a new block.
Add the mining reward as a new pending transaction.
"""
if self.__public_key is None:
return None
last_block = self.__chain[-1]
hashed_block = hash_block(last_block)
proof = self.proof_of_work()
reward_transaction = Transaction('MINING', self.__public_key, '', MINING_REWARD)
copied_transactions = self.__open_transactions[:]
for tx in copied_transactions:
if not Wallet.verify_transaction(tx):
return None
copied_transactions.append(reward_transaction)
block = Block(len(self.__chain), hashed_block, copied_transactions, proof)
self.__chain.append(block)
self.__open_transactions = []
self.save_data()
for node in self.__peer_nodes:
url = 'http://{}/broadcast-block'.format(node)
converted_block = block.__dict__.copy()
converted_block['transactions'] = [tx.__dict__ for tx in block.transactions]
try:
response = requests.post(url, json={
'block': converted_block
})
if response.status_code == 400 or response.status_code == 500:
print('Block declined, needs resolving')
if response.status_code == 409:
self.resolve_conflicts = True
except requests.exceptions.ConnectionError:
continue
return block