示例#1
0
 def from_json(chain_json):
     """
     Deserialize a list of serialized blocks into a Blockchain instance.
     The results will containg a chain list of Block instances.
     """
     blockchain = Blockchain()
     blockchain.chain = list(
         map(lambda block_json: Block.from_json(block_json), chain_json))
     return blockchain
示例#2
0
    def message(self, pubnub, message_object):
        print(
            f'\n-- Channel: {message_object.channel} | Message: {message_object.message}'
        )

        if message_object.channel == CHANNELS['BLOCK']:
            block = Block.from_json(message_object.message)
            # [:] -> all the elements in the array, in that case all blocks in chain
            potential_chain = self.blockchain.chain[:]
            potential_chain.append(block)

            try:
                self.blockchain.replace_chain(potential_chain)
                self.transaction_pool.clear_blockchain_transactions(
                    self.blockchain)
                print('\n -- Successfully replaced the local chain')
            except Exception as e:
                print(f'\n -- Did not replace chain: {e}')
        elif message_object.channel == CHANNELS['TRANSACTION']:
            transaction = Transaction.from_json(message_object.message)
            self.transaction_pool.set_transaction(transaction)
            print('\n -- Set the new transaction in the transaction pool')