コード例 #1
0
 def message(self, pubnub, message_object):
     print('Message channel: %s | Message object: %s' %
           (message_object.channel, message_object.message))
     #check if a block was received through the BLOCK channel and then add it to the chaina and then perform replace chain
     if message_object.channel == 'BLOCK':
         block = Block.from_json(message_object.message)
         potential_chain = self.blockchain.chain[:]
         #add received block to the chain
         potential_chain.append(block)
         #perform replace_chain operation
         try:
             self.blockchain.replace_chain(potential_chain)
             #After everytime a block is mined, we need to clear the transaction pool.
             self.transaction_pool.clear_transaction(self.blockchain)
             print("Chain replacement was successful")
         except Exception as e:
             print("Chain replacement was not successful: %s" % (e))
     elif message_object.channel == 'TRANSACTIONS':
         transaction = Transactions.from_json(message_object.message)
         self.transaction_pool.set_transaction(transaction)
コード例 #2
0
    def is_chain_transaction_valid(chain):
        """
		Method to verify if each of the transactions in the chain is valid.
		Transaction is valid if:
		1. Each transation appears only once in the blockchain.
		2. There is only one reward transaction.
		3. The transaction is valid.
		"""
        transaction_ids = set()
        for index, block in enumerate(chain):
            if index == 0:
                continue
            has_reward_transaction = False
            for transaction_json in block.data:
                transaction = Transactions.from_json(transaction_json)
                if transaction.input == MINING_REWARD_INPUT:
                    if has_reward_transaction is True:
                        raise Exception(
                            "Transaction with id %s has multiple rewards" %
                            (transaction.id))
                    has_reward_transaction = True
                else:
                    #calculate historical balance using a historical balance
                    historical_chain = chain[:index]
                    historical_blockchain = Blockchain()
                    historical_blockchain.chain = historical_chain
                    #get the balance amount of the address
                    balance = Wallet.calculate_balance(
                        historical_blockchain, transaction.input['address'])
                    if balance != transaction.input['amount']:
                        raise Exception(
                            "Transaction id %s did not have the correct balance"
                            % (transaction.id))
                if transaction.id in transaction_ids:
                    raise Exception("Transaction with id %s is not unique" %
                                    (transaction.id))
                transaction_ids.add(transaction.id)
                Transactions.verify_transaction(transaction)