Exemple #1
0
def make_session(auth: str, settings: SettingsConfig, node_config: NodesConfig) -> Session:
    num_connections = node_config.count * 10

    session = Session()
    if auth:
        session.auth = cast(Tuple[str, str], tuple(auth.split(":")))
    session.mount(
        "http", TimeOutHTTPAdapter(timeout=settings.timeout, pool_connections=num_connections)
    )
    session.mount(
        "https",
        TimeOutHTTPAdapter(timeout=settings.timeout, pool_connections=num_connections),
    )

    return session
Exemple #2
0
    def __init__(
        self,
        account: Account,
        rpc_url: str,
        auth: str,
        scenario_file: Path,
    ):
        self.task_count = 0
        self.running_task_count = 0
        self.root_task = None
        self.auth = auth

        self.scenario = yaml.load(scenario_file)

        nodes = self.scenario['nodes']
        if 'range' in nodes:
            range_config = nodes['range']
            template = range_config['template']
            self.raiden_nodes = [
                template.format(i)
                for i in range(range_config['first'], range_config['last'] + 1)
            ]
        else:
            self.raiden_nodes = nodes['list']
        self.node_commands = nodes.get('commands', {})

        settings = self.scenario.get('settings')
        if settings is None:
            settings = {}
        self.timeout = settings.get('timeout', TIMEOUT)
        self.protocol = settings.get('protocol', 'http')
        self.notification_email = settings.get('notify')

        self.client = JSONRPCClient(
            Web3(HTTPProvider(rpc_url)),
            privkey=account.privkey,
            gas_price_strategy=get_gas_price_strategy(settings.get('gas_price')),
        )

        self.session = Session()
        # WTF? https://github.com/requests/requests/issues/2605
        if auth:
            self.session.auth = tuple(auth.split(":"))
        self.session.mount('http', TimeOutHTTPAdapter(timeout=self.timeout))
        self.session.mount('https', TimeOutHTTPAdapter(timeout=self.timeout))

        self._node_to_address = None
        self.token_address = None
Exemple #3
0
    def __init__(
        self,
        account: Account,
        chain_urls: Dict[str, List[str]],
        auth: str,
        data_path: Path,
        scenario_file: Path,
    ):
        from scenario_player.tasks.base import get_task_class_for_type
        from scenario_player.node_support import RaidenReleaseKeeper, NodeController

        self.task_count = 0
        self.running_task_count = 0
        self.auth = auth
        self.release_keeper = RaidenReleaseKeeper(
            data_path.joinpath('raiden_releases'))
        self.task_cache = {}
        self.task_storage = defaultdict(dict)

        self.scenario_name = os.path.basename(
            scenario_file.name).partition('.')[0]
        self.scenario = yaml.load(scenario_file)
        self.scenario_version = self.scenario.get('version', 1)
        if self.scenario_version not in SUPPORTED_SCENARIO_VERSIONS:
            raise ScenarioError(
                f'Unexpected scenario version {self.scenario_version}')

        self.data_path = data_path.joinpath('scenarios', self.scenario_name)
        self.data_path.mkdir(exist_ok=True, parents=True)
        log.debug('Data path', path=self.data_path)

        self.run_number = 0
        run_number_file = self.data_path.joinpath('run_number.txt')
        if run_number_file.exists():
            self.run_number = int(run_number_file.read_text()) + 1
        run_number_file.write_text(str(self.run_number))
        log.info('Run number', run_number=self.run_number)

        nodes = self.scenario['nodes']

        node_mode = NodeMode.EXTERNAL.name
        if self.is_v2:
            node_mode = nodes.get('mode', '').upper()
            if not node_mode:
                raise ScenarioError(
                    'Version 2 scenarios require a "mode" in the "nodes" section.'
                )
        try:
            self.node_mode = NodeMode[node_mode]
        except KeyError:
            known_modes = ', '.join(mode.name.lower() for mode in NodeMode)
            raise ScenarioError(
                f'Unknown node mode "{node_mode}". Expected one of {known_modes}',
            ) from None

        if self.is_managed:
            self.node_controller = NodeController(
                self,
                nodes.get('raiden_version', 'LATEST'),
                nodes['count'],
                nodes.get('default_options', {}),
                nodes.get('node_options', {}),
            )
        else:
            if 'range' in nodes:
                range_config = nodes['range']
                template = range_config['template']
                self.raiden_nodes = [
                    template.format(i) for i in range(range_config['first'],
                                                      range_config['last'] + 1)
                ]
            else:
                self.raiden_nodes = nodes['list']
            self.node_commands = nodes.get('commands', {})

        settings = self.scenario.get('settings')
        if settings is None:
            settings = {}
        self.timeout = settings.get('timeout', TIMEOUT)
        if self.is_managed:
            self.protocol = 'http'
            if 'protocol' in settings:
                log.warning(
                    'The "protocol" setting is not supported in "managed" node mode.'
                )
        else:
            self.protocol = settings.get('protocol', 'http')
        self.notification_email = settings.get('notify')
        self.chain_name = settings.get('chain', 'any')

        if self.chain_name == 'any':
            self.chain_name = random.choice(list(chain_urls.keys()))
        elif self.chain_name not in chain_urls:
            raise ScenarioError(
                f'The scenario requested chain "{self.chain_name}" for which no RPC-URL is known.',
            )
        log.info('Using chain', chain=self.chain_name)
        self.eth_rpc_urls = chain_urls[self.chain_name]

        self.client = JSONRPCClient(
            Web3(HTTPProvider(chain_urls[self.chain_name][0])),
            privkey=account.privkey,
            gas_price_strategy=get_gas_price_strategy(
                settings.get('gas_price', 'fast')),
        )

        self.chain_id = self.client.web3.net.version

        balance = self.client.balance(account.address)
        if balance < OWN_ACCOUNT_BALANCE_MIN:
            raise ScenarioError(
                f'Insufficient balance ({balance / 10 ** 18} Eth) '
                f'in account {to_checksum_address(account.address)} on chain "{self.chain_name}"',
            )

        self.session = Session()
        # WTF? https://github.com/requests/requests/issues/2605
        if auth:
            self.session.auth = tuple(auth.split(":"))
        self.session.mount('http', TimeOutHTTPAdapter(timeout=self.timeout))
        self.session.mount('https', TimeOutHTTPAdapter(timeout=self.timeout))

        self._node_to_address = None
        self.token_address = None

        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)
Exemple #4
0
    def __init__(
        self,
        account: Account,
        chain_urls: Dict[str, List[str]],
        auth: str,
        data_path: Path,
        scenario_file: Path,
        task_state_callback: Optional[
            Callable[["ScenarioRunner", "Task", "TaskState"], None]
        ] = None,
    ):
        from scenario_player.node_support import RaidenReleaseKeeper, NodeController

        self.auth = auth

        self.release_keeper = RaidenReleaseKeeper(data_path.joinpath("raiden_releases"))

        self.task_count = 0
        self.running_task_count = 0
        self.task_cache = {}
        self.task_state_callback = task_state_callback
        # Storage for arbitrary data tasks might need to persist
        self.task_storage = defaultdict(dict)

        scenario_name = scenario_file.stem
        self.data_path = data_path.joinpath("scenarios", scenario_name)
        self.data_path.mkdir(exist_ok=True, parents=True)
        self.yaml = ScenarioYAML(scenario_file, self.data_path)
        log.debug("Data path", path=self.data_path)

        # Determining the run number requires :attr:`.data_path`
        self.run_number = self.determine_run_number()

        self.node_controller = NodeController(self, self.yaml.nodes)

        self.protocol = "http"

        self.gas_limit = GAS_LIMIT_FOR_TOKEN_CONTRACT_CALL * 2

        self.chain_name, chain_urls = self.select_chain(chain_urls)
        self.eth_rpc_urls = chain_urls
        self.client = JSONRPCClient(
            Web3(HTTPProvider(chain_urls[0])),
            privkey=account.privkey,
            gas_price_strategy=self.yaml.settings.gas_price_strategy,
        )
        self.chain_id = int(self.client.web3.net.version)

        self.contract_manager = ContractManager(contracts_precompiled_path())

        balance = self.client.balance(account.address)
        if balance < OWN_ACCOUNT_BALANCE_MIN:
            raise ScenarioError(
                f"Insufficient balance ({balance / 10 ** 18} Eth) "
                f'in account {to_checksum_address(account.address)} on chain "{self.chain_name}"'
            )

        self.session = Session()
        if auth:
            self.session.auth = tuple(auth.split(":"))
        self.session.mount("http", TimeOutHTTPAdapter(timeout=self.yaml.settings.timeout))
        self.session.mount("https", TimeOutHTTPAdapter(timeout=self.yaml.settings.timeout))

        self.service_session = ServiceInterface(self.yaml.spaas)
        # Request an RPC Client instance ID from the RPC service and assign it to the runner.
        assign_rpc_instance_id(self, chain_urls[0], account.privkey, self.yaml.settings.gas_price)

        self.token = Token(self, data_path)
        self.udc = None

        self.token_network_address = None

        task_config = self.yaml.scenario.root_config
        task_class = self.yaml.scenario.root_class
        self.root_task = task_class(runner=self, config=task_config)
Exemple #5
0
    def __init__(
        self,
        account: Account,
        chain_urls: Dict[str, List[str]],
        auth: str,
        data_path: Path,
        scenario_file: Path,
        task_state_callback: Optional[Callable[
            ["ScenarioRunner", Task, TaskState], None]] = None,
    ):
        from scenario_player.node_support import RaidenReleaseKeeper, NodeController

        self.task_count = 0
        self.running_task_count = 0
        self.auth = auth
        self.release_keeper = RaidenReleaseKeeper(
            data_path.joinpath("raiden_releases"))
        self.task_cache = {}
        self.task_state_callback = task_state_callback
        # Storage for arbitrary data tasks might need to persist
        self.task_storage = defaultdict(dict)

        self.scenario = Scenario(scenario_file)
        self.scenario_name = self.scenario.name

        self.data_path = data_path.joinpath("scenarios", self.scenario.name)
        self.data_path.mkdir(exist_ok=True, parents=True)
        log.debug("Data path", path=self.data_path)

        self.run_number = self.determine_run_number()

        self.node_mode = self.scenario.nodes.mode

        if self.is_managed:
            self.node_controller = NodeController(
                self,
                self.scenario.nodes.raiden_version,
                self.scenario.nodes.count,
                self.scenario.nodes.default_options,
                self.scenario.nodes.node_options,
            )
        else:
            self.raiden_nodes = self.scenario.nodes
            self.node_commands = self.scenario.nodes.commands

        self.timeout = self.scenario.timeout
        self.protocol = self.scenario.protocol

        self.notification_email = self.scenario.notification_email

        self.chain_name, chain_urls = self.select_chain(chain_urls)
        self.eth_rpc_urls = chain_urls

        self.client = JSONRPCClient(
            Web3(HTTPProvider(chain_urls[0])),
            privkey=account.privkey,
            gas_price_strategy=self.scenario.gas_price_strategy,
        )

        self.chain_id = int(self.client.web3.net.version)
        self.contract_manager = ContractManager(contracts_precompiled_path())

        balance = self.client.balance(account.address)
        if balance < OWN_ACCOUNT_BALANCE_MIN:
            raise ScenarioError(
                f"Insufficient balance ({balance / 10 ** 18} Eth) "
                f'in account {to_checksum_address(account.address)} on chain "{self.chain_name}"'
            )

        self.session = Session()
        if auth:
            self.session.auth = tuple(auth.split(":"))
        self.session.mount("http", TimeOutHTTPAdapter(timeout=self.timeout))
        self.session.mount("https", TimeOutHTTPAdapter(timeout=self.timeout))

        self._node_to_address = None
        self.token_address = None
        self.token_deployment_block = 0
        self.token_network_address = None

        task_config = self.scenario.task_config
        task_class = self.scenario.task_class
        self.root_task = task_class(runner=self, config=task_config)
Exemple #6
0
    def __init__(
        self,
        account: Account,
        auth: str,
        chain: str,
        data_path: Union[Path, str],
        scenario_file: Path,
        task_state_callback: Optional[
            Callable[["ScenarioRunner", "Task", "TaskState"], None]
        ] = None,
    ):
        from scenario_player.node_support import RaidenReleaseKeeper, NodeController

        self.auth = auth

        self.release_keeper = RaidenReleaseKeeper(data_path.joinpath("raiden_releases"))

        self.task_count = 0
        self.running_task_count = 0
        self.task_cache = {}
        self.task_state_callback = task_state_callback
        # Storage for arbitrary data tasks might need to persist
        self.task_storage = defaultdict(dict)

        self.definition = ScenarioDefinition(scenario_file, data_path)

        log.debug("Local seed", seed=self.local_seed)

        self.run_number = self.determine_run_number()

        self.node_controller = NodeController(self, self.definition.nodes)

        self.protocol = "http"
        if chain:
            name, endpoint = chain.split(":", maxsplit=1)
            # Set CLI overrides.
            self.definition.settings._cli_chain = name
            self.definition.settings._cli_rpc_address = endpoint

        self.client = JSONRPCClient(
            Web3(HTTPProvider(self.definition.settings.eth_client_rpc_address)),
            privkey=account.privkey,
            gas_price_strategy=self.definition.settings.gas_price_strategy,
        )

        self.definition.settings.chain_id = int(self.client.web3.net.version)
        self.contract_manager = ContractManager(contracts_precompiled_path())

        balance = self.client.balance(account.address)
        if balance < OWN_ACCOUNT_BALANCE_MIN:
            raise ScenarioError(
                f"Insufficient balance ({balance / 10 ** 18} Eth) "
                f"in account {to_checksum_address(account.address)} "
                f'on chain "{self.definition.settings.chain}"'
                f" - it needs additional {(OWN_ACCOUNT_BALANCE_MIN - balance) / 10 ** 18} Eth ("
                f"that is {OWN_ACCOUNT_BALANCE_MIN - balance} Wei)."
            )

        self.session = Session()
        if auth:
            self.session.auth = tuple(auth.split(":"))
        self.session.mount("http", TimeOutHTTPAdapter(timeout=self.definition.settings.timeout))
        self.session.mount("https", TimeOutHTTPAdapter(timeout=self.definition.settings.timeout))

        self.service_session = ServiceInterface(self.definition.spaas)
        # Assign an RPC Client instance ID on the RPC service, for this run.
        self.definition.spaas.rpc.assign_rpc_instance(
            self, account.privkey, self.definition.settings.gas_price
        )
        self.token = Token(self, data_path)
        self.udc = None

        self.token_network_address = None

        task_config = self.definition.scenario.root_config
        task_class = self.definition.scenario.root_class
        self.root_task = task_class(runner=self, config=task_config)