class Testcases(unittest.TestCase): def setUp(self): fixture_data() self.chain = Blockchain(mode="head") def test_is_irv(self): self.assertFalse(self.chain.is_irreversible_mode()) def test_info(self): info = self.chain.info() for i in [ "time", "dynamic_flags", "head_block_id", "head_block_number", "last_budget_time", ]: self.assertIn(i, info) def test_parameters(self): info = self.chain.chainParameters() for i in [ "worker_budget_per_day", "maintenance_interval", ]: self.assertIn(i, info) def test_network(self): info = self.chain.get_network() for i in [ "chain_id", "core_symbol", "prefix", ]: self.assertIn(i, info) def test_props(self): info = self.chain.get_chain_properties() for i in [ "id", "chain_id", "immutable_parameters", ]: self.assertIn(i, info) def test_block_num(self): num = self.chain.get_current_block_num() self.assertTrue(num > 100) def test_block(self): block = self.chain.get_current_block() self.assertIsInstance(block, Block) self.chain.block_time(1) self.chain.block_timestamp(1) def test_list_accounts(self): for account in self.chain.get_all_accounts(): self.assertIsInstance(account, str) # Break already break
def get_chain_properties(api_key: hug.types.text, request, hug_timer=5): """Bitshares current chain properties!""" if (check_api_token(api_key) == True): # Check the api key # API KEY VALID google_analytics(request, 'get_chain_properties') chain = Blockchain() chain_properties = chain.get_chain_properties() return {'chain_properties': chain_properties, 'valid_key': True, 'took': float(hug_timer)} else: # API KEY INVALID! return {'valid_key': False, 'took': float(hug_timer)}
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}
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 Testcases(unittest.TestCase): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.bts = BitShares( "wss://node.testnet.bitshares.eu", nobroadcast=True, ) self.bts.set_default_account("init0") set_shared_bitshares_instance(self.bts) self.chain = Blockchain(mode="head") def test_is_irv(self): self.assertFalse(self.chain.is_irreversible_mode()) def test_info(self): info = self.chain.info() for i in [ "time", "dynamic_flags", "head_block_id", "head_block_number", "last_budget_time" ]: self.assertIn(i, info) def test_parameters(self): info = self.chain.chainParameters() for i in [ "worker_budget_per_day", "maintenance_interval", ]: self.assertIn(i, info) def test_network(self): info = self.chain.get_network() for i in [ "chain_id", "core_symbol", "prefix", ]: self.assertIn(i, info) def test_props(self): info = self.chain.get_chain_properties() for i in [ "id", "chain_id", "immutable_parameters", ]: self.assertIn(i, info) def test_block_num(self): num = self.chain.get_current_block_num() self.assertTrue(num > 100) def test_block(self): block = self.chain.get_current_block() self.assertIsInstance(block, Block) self.chain.block_time(1) self.chain.block_timestamp(1) def test_list_accounts(self): for account in self.chain.get_all_accounts(): self.assertIsInstance(account, str) # Break already break