Example #1
0
    def deserialize(cls, pool: list):
        """
        Create a new transactions pool instance from the provided serialized pool.

        :param list pool: serialized pool of transactions.
        :return TransactionsPool: transactions pool created from provided stringified pool.
        """
        transactions = list(
            map(lambda transaction: Transaction.deserialize(transaction),
                pool))
        pool = {transaction.uuid: transaction for transaction in transactions}
        return cls(pool)
Example #2
0
    async def _message_handler(self, socket: Socket):
        """
        Handle the incoming socket client and process the message data.
        It exits normally when the connection is closed.

        :param Socket socket: incoming socket client.
        """
        async for message in socket:
            data = parse(message)
            channel = data.get('channel')
            if channel == CHANNELS.get('node'):
                uri = data.get('content')
                info_msg = f'Uri listed. {uri}.'
                logger.info(f'[P2PServer] Node received. {info_msg}')
                self.add_uris(uri)
                await self._connect_socket(self._send_chain, uri)
            elif channel == CHANNELS.get('sync'):
                uris = data.get('content')
                self.add_uris(uris)
                info_msg = f'Total uris: {self.nodes.uris.array}.'
                logger.info(
                    f'[P2PServer] Synchronization finished. {info_msg}')
            elif channel == CHANNELS.get('chain'):
                chain = data.get('content')
                logger.info(f'[P2PServer] Chain received. {chain}.')
                blockchain = Blockchain.deserialize(chain)
                self.blockchain.set_valid_chain(blockchain.chain)
                self.transactions_pool.clear_pool(self.blockchain)
            elif channel == CHANNELS.get('transact'):
                transaction_info = data.get('content')
                logger.info(
                    f'[P2PServer] Transaction received. {transaction_info}.')
                transaction = Transaction.deserialize(transaction_info)
                self.transactions_pool.add_transaction(transaction)
            else:
                error_msg = f'Unknown channel received: {channel}.'
                logger.error(f'[P2PServer] Channel error. {error_msg}')
Example #3
0
 def test_transaction_deserialize(self):
     serialized = self.transaction.serialize()
     self.assertIsInstance(Transaction.deserialize(serialized), Transaction)