def run_scenario(self): mint_gas = GAS_LIMIT_FOR_TOKEN_CONTRACT_CALL * 2 fund_tx, node_starter, node_addresses, node_count = self._initialize_nodes( ) ud_token_tx, udc_ctr, should_deposit_ud_token = self._initialize_udc( gas_limit=mint_gas, node_count=node_count) mint_tx = self._initialize_scenario_token( node_addresses=node_addresses, udc_ctr=udc_ctr, should_deposit_ud_token=should_deposit_ud_token, gas_limit=mint_gas, ) wait_for_txs(self.client, fund_tx | ud_token_tx | mint_tx) if node_starter is not None: log.debug("Waiting for nodes to finish starting") node_starter.get(block=True) first_node = self.get_node_baseurl(0) registered_tokens = set( self.session.get( API_URL_TOKENS.format(protocol=self.protocol, target_host=first_node)).json()) if self.token.checksum_address not in registered_tokens: for _ in range(5): code, msg = self.register_token(self.token.checksum_address, first_node) if 199 < code < 300: break gevent.sleep(1) else: log.error("Couldn't register token with network", code=code, message=msg) raise TokenRegistrationError(msg) last_node = self.node_controller[-1].base_url self.token_network_address = self.wait_for_token_network_discovery( last_node) log.info("Received token network address", token_network_address=self.token_network_address) # Start root task root_task_greenlet = gevent.spawn(self.root_task) greenlets = {root_task_greenlet} greenlets.add(self.node_controller.start_node_monitor()) try: gevent.joinall(greenlets, raise_error=True) except BaseException: if not root_task_greenlet.dead: # Make sure we kill the tasks if a node dies root_task_greenlet.kill() raise
def run_scenario(self): from scenario_player.tasks.base import get_task_class_for_type log.info("Fetching node addresses") unreachable_nodes = [node for node, addr in self.node_to_address.items() if not addr] if not self.node_to_address or unreachable_nodes: raise NodesUnreachableError(f"Raiden nodes unreachable: {','.join(unreachable_nodes)}") token_ctr = get_or_deploy_token(self.client, self.scenario) token_address = self.token_address = to_checksum_address(token_ctr.contract_address) first_node = first(self.raiden_nodes) token_settings = self.scenario.get('token') or {} token_balance_min = token_settings.get( 'balance_min', DEFAULT_TOKEN_BALANCE_MIN, ) mint_tx = [] for node, address in self.node_to_address.items(): balance = token_ctr.contract.functions.balanceOf(address).call() if balance < token_balance_min: mint_amount = token_balance_min - balance log.debug("Minting tokens for", address=address, node=node, amount=mint_amount) mint_tx.append(token_ctr.transact('mintFor', mint_amount, address)) elif balance > token_balance_min: log.warning("Node is overfunded", address=address, node=node, balance=balance) wait_for_txs(self.client, mint_tx) registered_tokens = set( self.session.get( API_URL_TOKENS.format(protocol=self.protocol, target_host=first_node), ).json(), ) if token_address not in registered_tokens: code, msg = self.register_token(token_address, first_node) if not 199 < code < 300: log.error("Couldn't register token with network", code=code, message=msg) raise TokenRegistrationError(msg) scenario_config = self.scenario.get('scenario') if not scenario_config: raise ScenarioError("Invalid scenario definition. Missing 'scenario' key.") try: (root_task_type, root_task_config), = scenario_config.items() except ValueError: # will be thrown if it's not a 1-element dict raise ScenarioError( "Invalid scenario definition. " "Exactly one root task is required below the 'scenario' key.", ) from None task_class = get_task_class_for_type(root_task_type) self.root_task = task_class(runner=self, config=root_task_config) self.root_task()
def run_scenario(self): fund_tx = [] node_starter: gevent.Greenlet = None if self.is_managed: self.node_controller.initialize_nodes() node_addresses = self.node_controller.addresses node_balances = { address: self.client.balance(address) for address in node_addresses } low_balances = { address: balance for address, balance in node_balances.items() if balance < NODE_ACCOUNT_BALANCE_MIN } if low_balances: log.info('Funding nodes', nodes=low_balances.keys()) fund_tx = [ self.client.send_transaction( to=address, startgas=21000, value=NODE_ACCOUNT_BALANCE_FUND - balance, ) for address, balance in low_balances.items() ] node_starter = self.node_controller.start(wait=False) else: log.info("Fetching node addresses") unreachable_nodes = [ node for node, addr in self.node_to_address.items() if not addr ] if not self.node_to_address or unreachable_nodes: raise NodesUnreachableError( f"Raiden nodes unreachable: {','.join(unreachable_nodes)}", ) token_ctr = get_or_deploy_token(self) token_address = self.token_address = to_checksum_address( token_ctr.contract_address) first_node = self.get_node_baseurl(0) token_settings = self.scenario.get('token') or {} token_balance_min = token_settings.get( 'balance_min', DEFAULT_TOKEN_BALANCE_MIN, ) token_balance_fund = token_settings.get( 'balance_fund', DEFAULT_TOKEN_BALANCE_FUND, ) mint_tx = [] if self.is_managed: addresses = self.node_controller.addresses else: addresses = self.node_to_address.values() for address in addresses: balance = token_ctr.contract.functions.balanceOf(address).call() if balance < token_balance_min: mint_amount = token_balance_fund - balance startgas = GAS_LIMIT_FOR_TOKEN_CONTRACT_CALL log.debug("Minting tokens for", address=address, amount=mint_amount) mint_tx.append( token_ctr.transact('mintFor', startgas, mint_amount, address)) elif balance > token_balance_min: log.warning("Node is overfunded", address=address, balance=balance) wait_for_txs(self.client, mint_tx + fund_tx) if node_starter is not None: log.debug('Waiting for nodes to finish starting') node_starter.get(block=True) registered_tokens = set( self.session.get( API_URL_TOKENS.format(protocol=self.protocol, target_host=first_node), ).json(), ) if token_address not in registered_tokens: code, msg = self.register_token(token_address, first_node) if not 199 < code < 300: log.error("Couldn't register token with network", code=code, message=msg) raise TokenRegistrationError(msg) # Start root task root_task_greenlet = gevent.spawn(self.root_task) greenlets = [root_task_greenlet] if self.is_managed: greenlets.append(self.node_controller.start_node_monitor()) try: gevent.joinall(greenlets, raise_error=True) except BaseException: if not root_task_greenlet.dead: # Make sure we kill the tasks if a node dies root_task_greenlet.kill() raise
def run_scenario(self): mint_gas = GAS_LIMIT_FOR_TOKEN_CONTRACT_CALL * 2 fund_tx, node_starter, node_addresses, node_count = self._initialize_nodes( ) ud_token_tx, udc_ctr, should_deposit_ud_token = self._initialize_udc( gas_limit=mint_gas, node_count=node_count) mint_tx = self._initialize_scenario_token( node_addresses=node_addresses, udc_ctr=udc_ctr, should_deposit_ud_token=should_deposit_ud_token, gas_limit=mint_gas, ) wait_for_txs(self.client, fund_tx | ud_token_tx | mint_tx) if node_starter is not None: log.debug("Waiting for nodes to finish starting") node_starter.get(block=True) first_node = self.get_node_baseurl(0) registered_tokens = set( self.session.get( API_URL_TOKENS.format(protocol=self.protocol, target_host=first_node)).json()) if self.token_address not in registered_tokens: for _ in range(5): code, msg = self.register_token(self.token_address, first_node) if 199 < code < 300: break gevent.sleep(1) else: log.error("Couldn't register token with network", code=code, message=msg) raise TokenRegistrationError(msg) # The nodes need some time to find the token, see # https://github.com/raiden-network/raiden/issues/3544 # FIXME: Add proper check via API log.info("Waiting till new network is found by nodes") while self.token_network_address is None: self.token_network_address = self.session.get( API_URL_TOKEN_NETWORK_ADDRESS.format( protocol=self.protocol, target_host=first_node, token_address=self.token_address, )).json() gevent.sleep(1) log.info("Received token network address", token_network_address=self.token_network_address) # Start root task root_task_greenlet = gevent.spawn(self.root_task) greenlets = {root_task_greenlet} if self.is_managed: greenlets.add(self.node_controller.start_node_monitor()) try: gevent.joinall(greenlets, raise_error=True) except BaseException: if not root_task_greenlet.dead: # Make sure we kill the tasks if a node dies root_task_greenlet.kill() raise
def run_scenario(self): if self.is_managed: self.node_controller.initialize_nodes() node_addresses = self.node_controller.addresses node_balances = { address: self.client.balance(address) for address in node_addresses } low_balances = { address: balance for address, balance in node_balances.items() if balance < NODE_ACCOUNT_BALANCE_MIN } fund_tx = [] if low_balances: log.info('Funding nodes', nodes=low_balances.keys()) fund_tx = [ self.client.send_transaction(address, NODE_ACCOUNT_BALANCE_FUND - balance) for address, balance in low_balances.items() ] self.node_controller.start() if fund_tx: wait_for_txs(self.client, fund_tx) else: log.info("Fetching node addresses") unreachable_nodes = [node for node, addr in self.node_to_address.items() if not addr] if not self.node_to_address or unreachable_nodes: raise NodesUnreachableError( f"Raiden nodes unreachable: {','.join(unreachable_nodes)}", ) token_ctr = get_or_deploy_token(self) token_address = self.token_address = to_checksum_address(token_ctr.contract_address) first_node = self.get_node_baseurl(0) token_settings = self.scenario.get('token') or {} token_balance_min = token_settings.get( 'balance_min', DEFAULT_TOKEN_BALANCE_MIN, ) mint_tx = [] if self.is_managed: addresses = self.node_controller.addresses else: addresses = self.node_to_address.values() for address in addresses: balance = token_ctr.contract.functions.balanceOf(address).call() if balance < token_balance_min: mint_amount = token_balance_min - balance log.debug("Minting tokens for", address=address, amount=mint_amount) mint_tx.append(token_ctr.transact('mintFor', mint_amount, address)) elif balance > token_balance_min: log.warning("Node is overfunded", address=address, balance=balance) wait_for_txs(self.client, mint_tx) registered_tokens = set( self.session.get( API_URL_TOKENS.format(protocol=self.protocol, target_host=first_node), ).json(), ) if token_address not in registered_tokens: code, msg = self.register_token(token_address, first_node) if not 199 < code < 300: log.error("Couldn't register token with network", code=code, message=msg) raise TokenRegistrationError(msg) self.root_task()