예제 #1
0
def monitoring_service_mock() -> Generator[MonitoringService, None, None]:
    web3_mock = Web3Mock()

    mock_udc = Mock(address=bytes([8] * 20))
    mock_udc.functions.effectiveBalance.return_value.call.return_value = 10000
    mock_udc.functions.token.return_value.call.return_value = to_checksum_address(
        bytes([7] * 20))
    ms = MonitoringService(
        web3=web3_mock,
        private_key=PrivateKey(
            decode_hex(
                "3a1076bf45ab87712ad64ccb3b10217737f7faacbf2872e88fdd9a537d8fe266"
            )),
        db_filename=":memory:",
        contracts={
            CONTRACT_TOKEN_NETWORK_REGISTRY: Mock(address=bytes([9] * 20)),
            CONTRACT_USER_DEPOSIT: mock_udc,
            CONTRACT_MONITORING_SERVICE: Mock(address=bytes([1] * 20)),
            CONTRACT_SERVICE_REGISTRY: Mock(address=bytes([2] * 20)),
        },
        sync_start_block=BlockNumber(0),
        required_confirmations=BlockTimeout(0),
        poll_interval=0,
    )

    yield ms
예제 #2
0
def monitoring_service(  # pylint: disable=too-many-arguments
    ms_address,
    web3: Web3,
    monitoring_service_contract,
    user_deposit_contract,
    token_network_registry_contract,
    ms_database: Database,
    get_private_key,
    service_registry,
):
    ms = MonitoringService(
        web3=web3,
        private_key=get_private_key(ms_address),
        contracts={
            CONTRACT_TOKEN_NETWORK_REGISTRY: token_network_registry_contract,
            CONTRACT_MONITORING_SERVICE: monitoring_service_contract,
            CONTRACT_USER_DEPOSIT: user_deposit_contract,
            CONTRACT_SERVICE_REGISTRY: service_registry,
        },
        sync_start_block=BlockNumber(0),
        required_confirmations=BlockTimeout(0),  # for faster tests
        poll_interval=0.01,  # for faster tests
        db_filename=":memory:",
    )
    # We need a shared db between MS and RC so the MS can use MR saved by the RC
    ms.context.database = ms_database
    ms.database = ms_database
    return ms
예제 #3
0
def main(  # pylint: disable=too-many-arguments
    private_key: str,
    state_db: str,
    web3: Web3,
    contracts: Dict[str, Contract],
    start_block: BlockNumber,
    confirmations: BlockNumber,
    min_reward: int,
    debug_shell: bool,
) -> int:
    """ The Monitoring service for the Raiden Network. """
    log.info("Starting Raiden Monitoring Service")

    ms = MonitoringService(
        web3=web3,
        private_key=private_key,
        contracts=contracts,
        sync_start_block=start_block,
        required_confirmations=confirmations,
        db_filename=state_db,
        min_reward=min_reward,
    )

    if debug_shell:
        import IPython

        IPython.embed()
        return 0

    ms.start()

    return 0
예제 #4
0
 def new_ms(filename):
     ms = MonitoringService(
         web3=Web3Mock(),
         private_key=server_private_key,
         contracts=contracts,
         db_filename=os.path.join(tmpdir, filename),
     )
     msc = Mock()
     ms.context.monitoring_service_contract = msc
     ms.monitor_mock = msc.functions.monitor.return_value.transact  # type: ignore
     ms.monitor_mock.return_value = bytes(0)  # type: ignore
     return ms
예제 #5
0
 def new_ms(filename):
     ms = MonitoringService(
         web3=Web3Mock(),
         private_key=server_private_key,
         contracts=contracts,
         db_filename=os.path.join(tmpdir, filename),
         poll_interval=0,
         required_confirmations=BlockTimeout(0),
         sync_start_block=BlockNumber(0),
     )
     msc = Mock()
     ms.context.monitoring_service_contract = msc
     ms.monitor_mock = msc.functions.monitor.return_value.transact
     ms.monitor_mock.return_value = bytes(0)
     return ms
예제 #6
0
 def new_ms(filename):
     ms = MonitoringService(
         web3=web3,
         contract_manager=contracts_manager,
         private_key=server_private_key,
         registry_address=token_network_registry_contract.address,
         monitor_contract_address=monitoring_service_contract.address,
         db_filename=os.path.join(tmpdir, filename),
         user_deposit_contract_address=user_deposit_contract.address,
     )
     ms.bcl = MockBlockchainListener(events)  # type: ignore
     msc = Mock()
     ms.context.monitoring_service_contract = msc
     ms.monitor_mock = msc.functions.monitor.return_value.transact  # type:ignore
     ms.monitor_mock.return_value = bytes(0)  # type:ignore
     return ms
예제 #7
0
 def new_ms(filename):
     ms = MonitoringService(
         web3=web3,
         private_key=server_private_key,
         contracts={
             CONTRACT_TOKEN_NETWORK_REGISTRY:
             token_network_registry_contract,
             CONTRACT_MONITORING_SERVICE: monitoring_service_contract,
             CONTRACT_USER_DEPOSIT: user_deposit_contract,
         },
         db_filename=os.path.join(tmpdir, filename),
     )
     ms.bcl = MockBlockchainListener(events)  # type: ignore
     msc = Mock()
     ms.context.monitoring_service_contract = msc
     ms.monitor_mock = msc.functions.monitor.return_value.transact  # type: ignore
     ms.monitor_mock.return_value = bytes(0)  # type: ignore
     return ms
예제 #8
0
def main(  # pylint: disable=too-many-arguments
    private_key: str,
    state_db: str,
    web3: Web3,
    contracts: Dict[str, Contract],
    start_block: BlockNumber,
    confirmations: BlockNumber,
    min_reward: int,
    debug_shell: bool,
) -> int:
    """ The Monitoring service for the Raiden Network. """
    log.info("Starting Raiden Monitoring Service")
    log.info("Web3 client", node_address=web3.providers[0].endpoint_uri)
    hex_addresses = {
        name: to_checksum_address(contract.address)
        for name, contract in contracts.items()
    }
    log.info("Contract information",
             addresses=hex_addresses,
             start_block=start_block)

    ms = MonitoringService(
        web3=web3,
        private_key=private_key,
        contracts=contracts,
        sync_start_block=start_block,
        required_confirmations=confirmations,
        db_filename=state_db,
        min_reward=min_reward,
    )

    if debug_shell:
        import IPython

        IPython.embed()
        return 0

    ms.start()

    return 0
예제 #9
0
def monitoring_service(
    server_private_key,
    web3,
    monitoring_service_contract,
    user_deposit_contract,
    token_network_registry_contract,
    send_funds,
    contracts_manager: ContractManager,
    service_registry,
    custom_token,
    ms_database,
):
    # register MS in ServiceRegistry
    ms_address = private_key_to_address(server_private_key)
    send_funds(ms_address)
    deposit = 10  # any amount is sufficient for regsitration, right now
    custom_token.functions.mint(deposit).transact({'from': ms_address})
    custom_token.functions.approve(
        service_registry.address,
        deposit,
    ).transact({'from': ms_address})
    service_registry.functions.deposit(deposit).transact({'from': ms_address})

    ms = MonitoringService(
        web3=web3,
        contract_manager=contracts_manager,
        private_key=server_private_key,
        registry_address=token_network_registry_contract.address,
        monitor_contract_address=monitoring_service_contract.address,
        user_deposit_contract_address=user_deposit_contract.address,
        required_confirmations=1,  # for faster tests
        poll_interval=0.01,  # for faster tests
        db_filename=':memory:',
    )
    # We need a shared db between MS and RC so the MS can use MR saved by the RC
    ms.context.db = ms_database
    return ms
예제 #10
0
def main(
    private_key: str,
    state_db: str,
    web3: Web3,
    contracts: Dict[str, Contract],
    start_block: BlockNumber,
    confirmations: BlockNumber,
    min_reward: int,
) -> int:
    """ The Monitoring service for the Raiden Network. """
    log.info("Starting Raiden Monitoring Service")

    ms = MonitoringService(
        web3=web3,
        private_key=private_key,
        contracts=contracts,
        sync_start_block=start_block,
        required_confirmations=confirmations,
        db_filename=state_db,
        min_reward=min_reward,
    )
    ms.start()

    return 0
예제 #11
0
def main(
    keystore_file: str,
    password: str,
    eth_rpc: str,
    registry_address: Address,
    monitor_contract_address: Address,
    user_deposit_contract_address: Address,
    start_block: int,
    confirmations: int,
    log_level: str,
    state_db: str,
    min_reward: int,
) -> None:
    setup_logging(log_level)

    with open(keystore_file, 'r') as keystore:
        try:
            private_key = Account.decrypt(
                keyfile_json=json.load(keystore),
                password=password,
            )
        except ValueError as error:
            log.critical(
                'Could not decode keyfile with given password. Please try again.',
                reason=str(error),
            )
            sys.exit(1)

    provider = HTTPProvider(eth_rpc)
    web3 = Web3(provider)
    contract_manager = ContractManager(contracts_precompiled_path())
    contract_infos = get_contract_addresses_and_start_block(
        chain_id=int(web3.net.version),
        contracts_version=None,
        token_network_registry_address=registry_address,
        monitor_contract_address=monitor_contract_address,
        user_deposit_contract_address=user_deposit_contract_address,
        start_block=start_block,
    )

    if contract_infos is None:
        log.critical(
            'Could not find correct contracts to use. Please check your configuration'
        )
        sys.exit(1)
    else:
        log.info(
            'Contract information',
            registry_address=contract_infos[CONTRACT_TOKEN_NETWORK_REGISTRY],
            monitor_contract_address=contract_infos[
                CONTRACT_MONITORING_SERVICE],
            user_deposit_contract_address=contract_infos[
                CONTRACT_USER_DEPOSIT],
            sync_start_block=contract_infos[START_BLOCK_ID],
        )

    ms = MonitoringService(
        web3=web3,
        contract_manager=contract_manager,
        private_key=private_key,
        registry_address=contract_infos[CONTRACT_TOKEN_NETWORK_REGISTRY],
        monitor_contract_address=contract_infos[CONTRACT_MONITORING_SERVICE],
        user_deposit_contract_address=contract_infos[CONTRACT_USER_DEPOSIT],
        sync_start_block=contract_infos[START_BLOCK_ID],
        required_confirmations=confirmations,
        db_filename=state_db,
        min_reward=min_reward,
    )
    ms.start()
예제 #12
0
def main(  # pylint: disable=too-many-arguments,too-many-locals
    private_key: PrivateKey,
    state_db: str,
    web3: Web3,
    contracts: Dict[str, Contract],
    start_block: BlockNumber,
    host: str,
    port: int,
    min_reward: int,
    confirmations: BlockTimeout,
    operator: str,
    info_message: str,
    debug_shell: bool,
    accept_disclaimer: bool,
) -> int:
    """The Monitoring service for the Raiden Network."""
    log.info("Starting Raiden Monitoring Service")
    click.secho(MS_DISCLAIMER, fg="yellow")
    if not accept_disclaimer:
        click.confirm(CONFIRMATION_OF_UNDERSTANDING, abort=True)

    if not confirmations:
        chain_id = ChainID(web3.eth.chain_id)
        confirmations = (BlockTimeout(0) if "arbitrum" in ID_TO_CHAINNAME.get(
            chain_id, "") else DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS)
        log.info("Setting number of confirmation blocks",
                 confirmations=confirmations)

    log.info("Using RPC endpoint", rpc_url=get_web3_provider_info(web3))
    hex_addresses = {
        name: to_checksum_address(contract.address)
        for name, contract in contracts.items()
    }
    log.info("Contract information",
             addresses=hex_addresses,
             start_block=start_block)

    task = None
    api = None
    try:
        service = MonitoringService(
            web3=web3,
            private_key=private_key,
            contracts=contracts,
            sync_start_block=start_block,
            required_confirmations=confirmations,
            poll_interval=DEFAULT_POLL_INTERVALL,
            db_filename=state_db,
            min_reward=min_reward,
        )

        if debug_shell:
            import IPython

            IPython.embed()
            return 0

        task = spawn_named("MonitoringService", service.start)

        log.debug("Starting API")
        api = MSApi(monitoring_service=service,
                    operator=operator,
                    info_message=info_message)
        api.run(host=host, port=port)

        task.get()
    finally:
        log.info("Stopping Monitoring Service...")
        if api:
            api.stop()
        if task:
            task.kill()
            task.get()

    return 0