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 _broadcast_status(self):
     blockchain_length = self._get_blockchain_status_value()
     status_message_bytes = text_to_bytes('{}:{}'.format(
         blockchain_length, self.block_server_port))
     s = socket(AF_INET, SOCK_DGRAM)
     s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
     s.sendto(status_message_bytes,
              (BROADCAST_ADDRESS, self.broadcast_port))
     s.close()
     logging.debug('{} sent {}.'.format(SERVICE_NAME, blockchain_length))
Esempio n. 3
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. 4
0
    def send_transaction(transaction):
        encoded_transaction_text = transaction_encode(transaction)
        encoded_transaction_bytes = text_to_bytes(encoded_transaction_text)

        Network().send_transaction(encoded_transaction_bytes)
Esempio n. 5
0
def hash_string_to_hex(text):
    return hash_to_hex(text_to_bytes(text))
Esempio n. 6
0
def hash_string(text):
    return hash(text_to_bytes(text))
Esempio n. 7
0
 def validate_transaction(transaction):
     signature = transaction.signature
     public_key = text_to_bytes(transaction.public_key)
     transaction_details = text_to_bytes(transaction.get_details_for_signature())
     return Crypto.validate_signature(transaction_details, public_key, signature)
Esempio n. 8
0
 def __prepare_data_for_signing(self, data):
     if isinstance(data, str):
         data = text_to_bytes(data)
     return hash(data)