def get_config(api_key: hug.types.text, request, hug_timer=5): """Bitshares current chain config!""" if (check_api_token(api_key) == True): # Check the api key # API KEY VALID google_analytics(request, 'get_config') chain = Blockchain() chain_config = chain.config() return {'chain_config': chain_config, 'valid_key': True, 'took': float(hug_timer)} else: # API KEY INVALID! return {'valid_key': False, 'took': float(hug_timer)}
class NodeCall(): """ Concurent call a method for node.""" def __init__(self, node: str): """ Connect to a specified node.""" self.bts = BitShares(node) def call_wrapper(self, call, method: str, kwargs: dict): # Copy method from call to responce. result: dict = {"method": method} if not method or not call: result['error']: str = "" "`{0}` is not implemented!".format(method) else: try: # call can return dict, list, str result['result'] = call(self, **kwargs) except (RPCError, UnhandledRPCError) as err: result['error']: str = str(err) return result def get_global_properties(self): """ Retrieve the current global_property_object.""" return self.bts.info() def get_block(self, block_num: int): """ Retrieve a full, signed block.""" result = Block(block_num, blockchain_instance=self.bts) return dict(result) def get_chain_properties(self): """ Retrieve the chain_property_object associated with the chain.""" self.chain = Blockchain(blockchain_instance=self.bts) return self.chain.get_chain_properties() def get_dynamic_global_properties(self): """ This call returns the *dynamic global properties*.""" self.chain = Blockchain(blockchain_instance=self.bts) return self.chain.info() def get_config(self): """ Retrieve compile-time constants.""" self.chain = Blockchain(blockchain_instance=self.bts) return self.chain.config() def get_accounts(self, account_ids: list) -> list: """ Get a list of accounts by ID. :param str account_ids: Identify of the account :param bitshares.bitshares.BitShares blockchain_instance: BitShares instance :returns: Account data list :rtype: list :raises bitshares.exceptions.AccountDoesNotExistsException: if account does not exist """ result = [] for account_id in account_ids: account = Account(account_id, blockchain_instance=self.bts) result.append(dict(account)) return result def get_chain_id(self): """ Get the chain ID.""" self.chain = Blockchain(blockchain_instance=self.bts) return self.chain.get_chain_properties() def get_transaction(self, block_num: int, trx_in_block: int): """ Fetch an individual processed transaction from a block.""" result = self.bts.rpc.get_transaction(block_num, trx_in_block) return dict(result)
class NodeCalls(object): """ Connect to a specified node and perform calls.""" def __init__(self, scenario: dict, roundup: dict): self.scenario: dict = scenario self.roundup = roundup def connect(self, node: str, **kwargs): """ Connect to a specified node.""" self.bts = BitShares(node, kwargs) log.info('Connected to node "{0}".'.format(self.bts.rpc.url)) if not getattr(self, 'chain', None): self.chain = Blockchain(blockchain_instance=self.bts) def run(self): try: self.connect(self.scenario.get("node")) except BaseException as err: log.critical('Fail to connect to node "{0} due to {1}".'.format( self.scenario.get("node"), err) ) log.error('Scenario run has stopped.') return if not self.scenario or not self.scenario.get("stages", []): log.warning("Empty stages!") for stage in self.scenario.get("stages", []): start_time = time.time() method: str = stage.get("method", '') call = getattr(self, method, lambda: None) # Copy method from call to responce. result: dict = {"method": method} if not method or not call: result['result']['message']: str = "" "`{0}` is not implemented!".format(method) log.error(json.dumps(result, indent=(2 * ' '))) continue else: kwargs: dict = stage.get("params", {}) try: result['result']: str = call(**kwargs) log.info(json.dumps(result, indent=(2 * ' '))) except (RPCError, UnhandledRPCError) as err: result['result']['message']: str = str(err) log.error(json.dumps(result, indent=(2 * ' '))) # Track time spent on calls, sum up to table self.roundup[method] = self.roundup.get(method, 0) + time.time() - start_time def get_global_properties(self): """ Retrieve the current global_property_object.""" return self.bts.info() def get_block(self, block_num: int): """ Retrieve a full, signed block.""" return Block(block_num, blockchain_instance=self.bts, lazy=False) def get_chain_properties(self): """ Retrieve the chain_property_object associated with the chain.""" return self.chain.get_chain_properties() def get_dynamic_global_properties(self): """ This call returns the *dynamic global properties*.""" return self.chain.info() def get_config(self): """ Retrieve compile-time constants.""" return self.chain.config() # def get_all_accounts(self, start='', stop='', steps=1e3, **kwargs): # """ Yields account names between start and stop. # # :param str start: Start at this account name # :param str stop: Stop at this account name # :param int steps: Obtain ``steps`` ret with a single call from RPC # """ # return json.dumps((account for account in self.chain.get_all_accounts( # start, stop, steps)), iterable_as_array=True) def get_accounts(self, account_ids: list) -> list: """ Get a list of accounts by ID. :param str account_ids: Identify of the account :param bitshares.bitshares.BitShares blockchain_instance: BitShares instance :returns: Account data list :rtype: list :raises bitshares.exceptions.AccountDoesNotExistsException: if account does not exist """ result = [] for account_id in account_ids: account = Account(account_id, blockchain_instance=self.bts) result.append(account) return result def get_chain_id(self): """ Get the chain ID.""" return {"chain_id": self.chain.get_chain_properties()["chain_id"]} @log_exceptions def get_transaction(self, block_num: int, trx_in_block: int): """ Fetch an individual processed transaction from a block.""" return self.bts.rpc.get_transaction(block_num, trx_in_block) def get_proposed_transactions(self, account: str): """ Obtain a list of pending proposals for an account. :param str account: Account name :param bitshares blockchain_instance: BitShares() instance to use when accesing a RPC """ proposals: list = Proposals(account, blockchain_instance=self.bts) return {"proposed_transactions": proposals}