def from_json(blockchain_json):
        """
		Method to deserialize a Blockchain in the form of a dict into an actual Blockchain object.
		"""
        blockchain = Blockchain()
        blockchain.chain = list(
            map(lambda block: Block.from_json(block), blockchain_json))
Exemplo n.º 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)
            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')

        elif message_object.channel == CHANNELS['ENERGY']:
            print("recived etran")
            etran = Etran.from_json(message_object.message)
            self.account.etranPool.append(etran)
        
        elif message_object.channel == CHANNELS['ETRANACK']:
            print("recived etran ack")
            etran = Etran.from_json(message_object.message)
            self.account.etranPoolAck.append(etran)
Exemplo n.º 3
0
 def from_json(chain_json):
     '''
     De-serializes a list of serialized blocks into a Blockchain instance.
     '''
     blockchain = Blockchain()
     blockchain.chain = list(map(lambda b: Block.from_json(b), chain_json))
     return blockchain
Exemplo n.º 4
0
 def from_json(chain_json):
   """
   Deserialise a list of blocks into a blockchain instance.
   """
   blockchain = Blockchain()
   blockchain.chain = list(map(lambda block_json: Block.from_json(block_json), chain_json))
   return blockchain
 def from_json(chain_json):
     """
     Deserialize a list of serialized blocks into a blockchain instance
     The result will contain the chain list of block instance
     """
     blockchain = Blockchain()
     blockchain.chain = list(map(lambda block_json: Block.from_json(block_json), chain_json))
     return blockchain
Exemplo n.º 6
0
    def from_json(chain_json):
        # Deserialize a list of serialized blocks into a Blockchain instance.
        # The result will contain a chain list of Block instance.

        blockchain = Blockchain()
        blockchain.chain = list(
            map(lambda block_json: Block.from_json(block_json), chain_json))
        return blockchain
Exemplo n.º 7
0
 def from_json(chain_json):
     """
     Deserialize a list of serialized blocks into a blockchain isntance.
     The result will contain a chain list of Block instances
     """
     blockchain = Blockchain()
     blockchain.chain = map(lambda block_json: Block.from_json(block_json),
                            chain_json)
Exemplo n.º 8
0
    def from_json(chain_json):
        """
        Deserialize a list of serialized blocks into a Blockchain instance.
        The result will contain a chain list of Block instance
        """

        blockchain = Blockchain()
        blockchain.chain = [Block.from_json(block_json) for block_json in chain_json]
        return blockchain
Exemplo n.º 9
0
 def from_json(blockchain_json):
     """
     Deserialize chain json into Blockchain instence
     """
     blockchain = Blockchain()
     blockchain.chain = list(
         map(lambda block_json: Block.from_json(block_json),
             blockchain_json))
     return blockchain
Exemplo n.º 10
0
 def from_json(json_chain):
     """
     Deserialize a blockchain from json input
     :param json_chain: json input
     :return: blockchain instance
     """
     blockchain = Blockchain()
     blockchain.chain = list(map(lambda x: Block.from_json(x), json_chain))
     return blockchain
Exemplo n.º 11
0
 def from_json(chain_json):
     """
     reserialize list of serialized blocks to Blockchain object.
     """
     blockchain = BlockChain()
     blockchain.chain = [
         Block.from_json(block_json) for block_json in chain_json
     ]
     return blockchain
Exemplo n.º 12
0
    def from_json(chain_json):
        """
        Deserialise a list of serialised block into a Blockchain instance.
        The result will have list of Block instances
        """

        blockchain = Blockchain()
        blockchain.chain = list(
            map(lambda block_json: Block.from_json(block_json), chain_json))
        return blockchain
Exemplo n.º 13
0
    def from_json(chain_json):
        """
        Deserialize a list of json representation into a blockchain instance
        Result will have chain list of Block instance
        """
        blockchain = Blockchain()
        blockchain.chain = list(
            map(lambda block_json: Block.from_json(block_json), chain_json))

        return blockchain
Exemplo n.º 14
0
    def from_json(chain_json):
        """
        Deserialize a list of serialized blocks into a Blockchain instance
        :param chain_json:
        :return:
        """
        blockchain = Blockchain()
        blockchain.chain = list(
            map(lambda block: Block.from_json(block), chain_json))

        return blockchain
    def from_json(chain_json):
        """
        Deserialize a list of serialzed blocks into a Blockchain instabce.
        The result will contain a chain of Block instance.
        """
        blockchain = Blockchain()
        # print('chain_json', chain_json)

        blockchain.chain = list(
            map(lambda block_json: Block.from_json(block_json), chain_json))

        return blockchain
 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)
         potential_chain = self.blockchain.chain[:]
         potential_chain.append(block)
         
         try:
             self.blockchain.replace_chain(potential_chain)
             print(f'\n-- Successfully replaced the local chain')
         except Exception as e:
             print(f'\n-- Did not replace chain: {e}')
Exemplo n.º 17
0
    def from_json(chain_json):
        """
        Deserialize a list of serialized blocks into a Blokchain instance.
        """
        blockchain = Blockchain()

        chain = []

        for block_json in chain_json:
            chain.append(Block.from_json(block_json))

        blockchain.chain = chain

        return blockchain
Exemplo n.º 18
0
    def message(self, pubsub, message):
        print(f"Channel: {message.channel}\nMessage: {message.message}\n\n")

        if message.channel == CHANNELS["BLOCK"]:
            new_block = Block.from_json(message.message)
            potential_chain = self.blockchain.chain[:]

            potential_chain.append(new_block)

            try:
                self.blockchain.replace_chain(potential_chain)
                print("The new block has been added successfully\n")

            except Exception as e:
                print(f"Failed to add new block !!!:\nError: {e}\n")
Exemplo n.º 19
0
 def message(self,pubnub,message_object):
     print(f'\n Incoming message_object:{message_object.channel} |Message:{message_object.message}')
     if message_object.channel==CHANNELS['BLOCK']:
         block=Block.from_json(message_object.message)
         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')
Exemplo n.º 20
0
def route_blockchain_replace():
    data = request.get_json()

    block = Block.from_json(data)
    new_blockchain = blockchain.chain[:]
    new_blockchain.append(block)

    try:
        blockchain.replace_chain(new_blockchain)
        message = 'Replaced blockchain'
    except Exception as e:
        message = f'Did not replace chain: {e}'

    response_data = {'message': message, 'code': 'SUCCESS'}

    return make_response(jsonify(response_data), 201)
Exemplo n.º 21
0
    def message(self, pubnub, message_object):
        print(
            f'\n-- Channel {message_object.channel} Message: {message_object.message}'
        )

        channel = message_object.channel
        message = message_object.message

        if channel == CHANNELS['BLOCK']:
            block = Block.from_json(message)
            potential_chain = self.blockchain.chain[:]
            potential_chain.append(block)
            try:
                self.blockchain.replace_chain(potential_chain)
                print('Blockchain is changed')
            except Exception as e:
                print(f'Blockchain is not changed: {e}')
Exemplo n.º 22
0
    def message(self, pubnub, msg_obj):
        print(
            f'\n-- Channel: {msg_obj.channel}\n-- Message: {msg_obj.message}')

        if msg_obj.channel == CHANNELS['BLOCK']:
            block = Block.from_json(msg_obj.message)
            potential_chain = self.blockchain.chain[:]
            potential_chain.append(block)
            try:
                self.blockchain.replace_chain(potential_chain)
                self.transaction_pool.clear_bc_transactions(self.blockchain)
                print(f'\n -- Successfully replaced the local chain.')
            except Exception as e:
                print(f'\n -- Could not replace the chain: {e}.')
        elif msg_obj.channel == CHANNELS['TRANSACTION']:
            trans = Transaction.from_json(msg_obj.message)
            self.transaction_pool.set_transaction(trans)
            print(f'\n -- Set the new transaction in the transaction pool')
Exemplo n.º 23
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)
            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(f"\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(f"\n -- Set the new transaction in the transaction pool")
    def message(self, pubnub, msg_obj):
        print(
            f'\n-- Incoming Channel: {msg_obj.channel} | message: {msg_obj.message}'
        )
        if msg_obj.channel == CHANNELS['BLOCK']:
            block = Block.from_json(msg_obj.message)
            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('successfullt replaced the chaihn')
            except Exception as e:
                print(f'\n -- Did not replace chain {e}')

        elif msg_obj.channel == CHANNELS['TRANSACTION']:
            transaction = Transaction.from_json(msg_obj.message)
            self.transaction_pool.set_transaction(transaction)
            print('\n-- Set the new transaction in the transaction pool')
Exemplo n.º 25
0
    def message(self, pubnub, message_object):
        print(
            f"Message: {message_object.message} on Channel: {message_object.channel}"
        )
        if message_object.channel == CHANNELS['BLOCK']:
            block = Block.from_json(message_object.message)
            updated_chain = self.blockchain.chain[:]
            updated_chain.append(block)
            try:
                self.blockchain.replace_chain(updated_chain)
                self.transaction_pool.clear_transactions_added_to_blockchain(
                    self.blockchain)
                print("Successfully Updated the chain!")
            except Exception as e:
                print(f"Chain update failed: {e}")
        elif message_object.channel == CHANNELS["TRANSACTION"]:
            transaction = Transaction.from_json(message_object.message)

            self.transaction_pool.add_transaction(transaction)
            print("\n -- Added new transaction to transaction pool")
Exemplo n.º 26
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)
Exemplo n.º 27
0
    def message(self, pubnub, message_obj):
        print(
            f'\n-- Channel: {message_obj.channel} || Message: {message_obj.message}'
        )

        if message_obj.channel == CHANNELS['BLOCK']:
            block = Block.from_json(message_obj.message)
            new_chain = self.blockchain.chain[:]
            new_chain.append(block)

            try:
                self.blockchain.replace_chain(new_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_obj.channel == CHANNELS['TRANSACTION']:
            transaction = Transaction.from_json(message_obj.message)
            self.transaction_pool.add_transaction(transaction)
            print('\n -- Added new transaction to the transaction pool!')
Exemplo n.º 28
0
 def message(self, pubnub, message_object):
     print(f'\n Incoming Message : {message_object}')
     if message_object.channel == CHANNELS['BLOCK']:
         block = Block.from_json(message_object.message)
         print('Block to mine ', block)
         potential_chain = self.blockchain.chain
         # potential_chain.append(block)
         print(potential_chain)
         try:
             self.blockchain.replace_chain(potential_chain)
             self.transaction_pool.clear_blockchain_transactions(
                 self.blockchain)
             print('Chain Replaced')
         except Exception as e:
             print(f'Did not replace" {e}')
     elif message_object.channel == CHANNELS['TRANSACTION']:
         transaction = Transaction.from_json(message_object.message)
         self.transaction_pool.set_transaction(transaction)
         print('New Transaction in Pool')
     elif message_object.channel == CHANNELS['ASSETS']:
         assetsTran = AssetsTransaction.from_json(message_object.message)
         self.assetPool.set_transaction(assetsTran)
         print('New Assets Transaction is added')
Exemplo n.º 29
0
 def from_json(json_chain):
     """
     deserialises the json data
     """
     return list(map(lambda block: Block.from_json(block), json_chain))
Exemplo n.º 30
0
 def from_json(chain_json):
     blockchain = Blockchain()
     blockchain.chain = list(
         map(lambda block_json: Block.from_json(block_json), chain_json))
     return blockchain