Esempio n. 1
0
 def _get_new_blocks(self, host, port, last_block_id):
     last_block_id_bytes = text_to_bytes(last_block_id)
     new_blocks_bytes = Network().send_block_request_and_wait(last_block_id_bytes, host, port)
     if len(new_blocks_bytes):
         new_blocks_json = bytes_to_text(new_blocks_bytes)
         return block_list_decode(new_blocks_json)
     else:
         # Unknown block requested, so probably the chain has forked
         return []
Esempio n. 2
0
    def _on_block_request(self, connection, request_bytes):
        block_id = bytes_to_text(request_bytes)
        logging.info('{} request for blocks following {}'.format(
            SERVICE_NAME, block_id))

        new_blocks = BlockchainLoader().process(
            lambda blockchain: blockchain.get_blocks_following(block_id))

        if new_blocks is not None:
            new_blocks_json = block_list_encode(new_blocks)
            new_blocks_bytes = text_to_bytes(new_blocks_json)
            connection.send(new_blocks_bytes)
            logging.info('{} sent {} new blocks'.format(
                SERVICE_NAME, len(new_blocks)))
        else:
            logging.info('{} unknown block requested: {}'.format(
                SERVICE_NAME, block_id))

        connection.close()
Esempio n. 3
0
    def run(self):
        self.socket = socket(AF_INET, SOCK_DGRAM)
        self.socket.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1)
        self.socket.bind(('', self.listener_port))
        logging.info('{} listening for status updates on port {}...'.format(SERVICE_NAME, self.listener_port))

        while not self.shutdown_event.is_set():
            try:
                bytes, addr = self.socket.recvfrom(BUFFER_SIZE)
                status_value, block_server_port = bytes_to_text(bytes).split(':')
                host = addr[0]
                logging.debug('{} received new status update of {} from {}:{}'.format(SERVICE_NAME, status_value, host, block_server_port))
                self.on_update(int(status_value), host, int(block_server_port))

            except OSError:
                logging.debug('{} error: {}'.format(SERVICE_NAME, sys.exc_info()))
                pass # probably close() was called

            except Exception:
                logging.error('{} error: {}'.format(SERVICE_NAME, sys.exc_info()))

        logging.info('{} shut down'.format(SERVICE_NAME))
Esempio n. 4
0
    def run(self):
        self.socket = socket(AF_INET, SOCK_DGRAM)
        self.socket.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1)
        self.socket.bind(('', self.listener_port))
        logging.info('{} listening for new transactions on port {}...'.format(SERVICE_NAME, self.listener_port))

        while not self.shutdown_event.is_set():
            try:
                bytes, addr = self.socket.recvfrom(BUFFER_SIZE)
                transaction_text = bytes_to_text(bytes)
                transaction = transaction_decode(transaction_text)

                logging.info('{} received new transaction for amount {} from {}'.format(SERVICE_NAME, transaction.amount, addr[0]))
                self.on_new_transaction(transaction)

            except OSError:
                logging.debug('{} error: {}'.format(SERVICE_NAME, sys.exc_info()))
                pass # probably close() was called

            except Exception:
                logging.error('{} error: {}'.format(SERVICE_NAME, sys.exc_info()))

        logging.info('{} shut down'.format(SERVICE_NAME))
Esempio n. 5
0
 def get_public_key(self):
     return bytes_to_text(self.key.publickey().exportKey(
         self.key_format.upper()))