def message(self, pubnub, message_obj): print( f"\n -- Channel: {message_obj.channel} | Message: {message_obj.message}" ) if (message_obj.channel == CHANNELS["BLOCK"]): prospective_block = Block.deserialize_from_json( message_obj.message) prospective_chain = self.blockchain.chain[:] prospective_chain.append(prospective_block) try: self.blockchain.surrogate_chain(prospective_chain) # purge recorded Tx from Pool self.transaction_pool.purge(self.blockchain) print(f"\n -- Chain surrogation successful.") except Exception as err: print(f"\n -- Chain surrogation failed. See: {err}") elif (message_obj.channel == CHANNELS["TRANSACTION"]): transaction = Transaction.deserialize_from_json( message_obj.message) self.transaction_pool.set_transaction(transaction) print( "\n -- New transaction successfully broadcast, added to the transaction pool." )
def deserialize_from_json(serialized_blockchain_obj): """ De-serialize a given list of serialized blocks into a Blockchain instance. """ blockchain = Blockchain() blockchain.chain = list( map( lambda serialized_block_obj: Block.deserialize_from_json( serialized_block_obj), serialized_blockchain_obj)) return blockchain