async def run_in_executor(self, query_name, func, kwargs): start = time.perf_counter() try: result = await asyncio.get_running_loop().run_in_executor( self.session_mgr.query_executor, func, kwargs) except asyncio.CancelledError: raise except reader.SQLiteInterruptedError as error: metrics = self.get_metrics_or_placeholder_for_api(query_name) metrics.query_interrupt(start, error.metrics) raise RPCError(JSONRPC.QUERY_TIMEOUT, 'sqlite query timed out') except reader.SQLiteOperationalError as error: metrics = self.get_metrics_or_placeholder_for_api(query_name) metrics.query_error(start, error.metrics) raise RPCError(JSONRPC.INTERNAL_ERROR, 'query failed to execute') except Exception: log.exception("dear devs, please handle this exception better") metrics = self.get_metrics_or_placeholder_for_api(query_name) metrics.query_error(start, {}) raise RPCError(JSONRPC.INTERNAL_ERROR, 'unknown server error') if self.env.track_metrics: metrics = self.get_metrics_or_placeholder_for_api(query_name) (result, metrics_data) = result metrics.query_response(start, metrics_data) return base64.b64encode(result).decode()
def assert_claim_id(self, value): '''Raise an RPCError if the value is not a valid claim id hash.''' try: if len(util.hex_to_bytes(value)) == 20: return except Exception: pass raise RPCError(1, f'{value} should be a claim id hash')
def assert_tx_hash(self, value): '''Raise an RPCError if the value is not a valid transaction hash.''' try: if len(util.hex_to_bytes(value)) == 32: return except Exception: pass raise RPCError(1, f'{value} should be a transaction hash')
async def run_in_executor(self, metrics: APICallMetrics, func, kwargs): start = time.perf_counter() try: result = await asyncio.get_running_loop().run_in_executor( self.session_mgr.query_executor, func, kwargs) except reader.SQLiteInterruptedError as error: metrics.query_interrupt(start, error.metrics) raise RPCError(JSONRPC.QUERY_TIMEOUT, 'sqlite query timed out') except reader.SQLiteOperationalError as error: metrics.query_error(start, error.metrics) raise RPCError(JSONRPC.INTERNAL_ERROR, 'query failed to execute') except: metrics.query_error(start, {}) raise RPCError(JSONRPC.INTERNAL_ERROR, 'unknown server error') if self.env.track_metrics: (result, metrics_data) = result metrics.query_response(start, metrics_data) return base64.b64encode(result).decode()
async def utxo_get_address(self, tx_hash, index): # fixme: lbryum # Used only for electrum client command-line requests. We no # longer index by address, so need to request the raw # transaction. So it works for any TXO not just UTXOs. self.assert_tx_hash(tx_hash) try: index = int(index) if index < 0: raise ValueError except ValueError: raise RPCError(1, "index has to be >= 0 and integer") raw_tx = await self.daemon_request('getrawtransaction', tx_hash) if not raw_tx: return None raw_tx = util.hex_to_bytes(raw_tx) tx = self.coin.DESERIALIZER(raw_tx).read_tx() if index >= len(tx.outputs): return None return self.coin.address_from_script(tx.outputs[index].pk_script)
async def wrapper(*args, **kwargs): try: return await decorated_function(*args, **kwargs) except DaemonError as daemon_error: raise RPCError(1, daemon_error.args[0])