def get_number_of_transactions_for_address(self, addr): validate.check_address_and_die(addr, THIS_FILE) urlbuilder = BlockchainInfoURLBuilder( self.config.BLOCKCHAIN_INFO_API_KEY) url = urlbuilder.get_number_of_transactions_for_address(addr) response = self.throttled_fetch_url(url) try: jsonObj = json.loads(response) n_tx = jsonObj['n_tx'] if not n_tx: #panic msg = ("Expected 'n_tx' from JSON response but found none: " "'%s'") % (n_tx, str(response)) logger.log_and_die(msg) try: int(n_tx) except ValueError: msg = ( "Expected integer value for 'n_tx' in JSON response but " "found: '%s'") % (str(response)) logger.log_and_die(msg) return n_tx except ValueError as e: #Something weird came back from the API despite HTTP 200 OK, panic msg = ("Expected a JSON response for address '%s', instead " "received '%s'") % (addr, str(response)) logger.log_and_die(msg)
def get_number_of_transactions_for_address(self, addr): validate.check_address_and_die(addr, THIS_FILE) urlbuilder = BlockchainInfoURLBuilder( self.config.BLOCKCHAIN_INFO_API_KEY) url = urlbuilder.get_number_of_transactions_for_address(addr) response = self.throttled_fetch_url(url) try: jsonObj = json.loads(response) n_tx = jsonObj['n_tx'] if not n_tx: #panic msg = ("Expected 'n_tx' from JSON response but found none: " "'%s'") % (n_tx, str(response)) logger.log_and_die(msg) try: int(n_tx) except ValueError: msg = ("Expected integer value for 'n_tx' in JSON response but " "found: '%s'") % (str(response)) logger.log_and_die(msg) return n_tx except ValueError as e: #Something weird came back from the API despite HTTP 200 OK, panic msg = ("Expected a JSON response for address '%s', instead " "received '%s'") % (addr, str(response)) logger.log_and_die(msg)
def _does_output_have_prior_tx_history(self, addr, current_tx_id, block_height, benchmarker=None): """#Helper function for `process_tx`.""" dprint("Address to be validated: %s" % addr) validate.check_address_and_die(addr, THIS_FILE) if self.block_reader.is_first_transaction_for_address( addr, current_tx_id, block_height, benchmarker): return False else: return True
def is_first_transaction_for_address(self, addr, tx_id, block_height, benchmarker=None): validate.check_address_and_die(addr, THIS_FILE) #First, check the local database cache for this address. If it's not # there, add it to the cache as an address that has been seen, and # then do API lookups to determine whether this tx is the address's # first. if self.database_connector.has_address_been_seen_cache_if_not( addr, block_height): if benchmarker is not None: benchmarker.increment_blockchain_info_queries_avoided_by_caching( ) benchmarker.increment_blockchain_info_queries_avoided_by_caching( ) return False n_tx = self.get_number_of_transactions_for_address(addr) offset = int(n_tx) - 1 urlbuilder = BlockchainInfoURLBuilder( self.config.BLOCKCHAIN_INFO_API_KEY) url = urlbuilder.get_tx_for_address_at_offset(addr, offset) response = self.throttled_fetch_url(url) try: jsonObj = json.loads(response) tx_list = jsonObj['txs'] if not tx_list: #address has no history return True if tx_list[0]: #Check if the first tx is the tx in question first_tx_id = tx_list[0]['hash'] if first_tx_id == tx_id: return True else: return False else: #Something wrong, this tx list seems to be neither empty nor # contains a first tx, panic! msg = ("Expected zero or one transactions for address '%s'" % addr) logger.log_and_die(msg) except ValueError as e: #Something weird came back from the API despite HTTP 200 OK, panic msg = ("Expected a JSON response for address '%s', instead " "received '%s'") % (addr, str(response)) logger.log_and_die(msg)
def is_first_transaction_for_address(self, addr, tx_id, block_height, benchmarker = None): validate.check_address_and_die(addr, THIS_FILE) #First, check the local database cache for this address. If it's not # there, add it to the cache as an address that has been seen, and # then do API lookups to determine whether this tx is the address's # first. if self.database_connector.has_address_been_seen_cache_if_not(addr, block_height): if benchmarker is not None: benchmarker.increment_blockchain_info_queries_avoided_by_caching() benchmarker.increment_blockchain_info_queries_avoided_by_caching() return False n_tx = self.get_number_of_transactions_for_address(addr) offset = int(n_tx) - 1 urlbuilder = BlockchainInfoURLBuilder( self.config.BLOCKCHAIN_INFO_API_KEY) url = urlbuilder.get_tx_for_address_at_offset(addr, offset) response = self.throttled_fetch_url(url) try: jsonObj = json.loads(response) tx_list = jsonObj['txs'] if not tx_list: #address has no history return True if tx_list[0]: #Check if the first tx is the tx in question first_tx_id = tx_list[0]['hash'] if first_tx_id == tx_id: return True else: return False else: #Something wrong, this tx list seems to be neither empty nor # contains a first tx, panic! msg = ("Expected zero or one transactions for address '%s'" % addr) logger.log_and_die(msg) except ValueError as e: #Something weird came back from the API despite HTTP 200 OK, panic msg = ("Expected a JSON response for address '%s', instead " "received '%s'") % (addr, str(response)) logger.log_and_die(msg)
def get_tx_for_address_at_offset(self, address, offset): validate.check_address_and_die(address, THIS_FILE) validate.check_int_and_die(offset, 'offset', THIS_FILE) return ("https://blockchain.info/address/%s?format=json&offset=%d%s" % (address, offset, self.api_key_str))