Пример #1
0
 def test_blockchain_deserialize(self):
     deserialized = Blockchain.deserialize(self.serialized)
     self.assertIsInstance(deserialized, Blockchain)
     self.assertTrue(hasattr(deserialized, 'chain'))
     for block_one, block_two in zip(deserialized.chain,
                                     self.blockchain.chain):
         self.assertTrue(block_one == block_two)
Пример #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}')