Esempio n. 1
0
def populate_token_network_random(
    token_network_model: TokenNetwork,
    private_keys: List[str],
) -> None:
    # seed for pseudo-randomness from config constant, that changes from time to time
    random.seed(NUMBER_OF_CHANNELS)

    for channel_id_int in range(NUMBER_OF_CHANNELS):
        channel_id = ChannelIdentifier(channel_id_int)

        private_key1, private_key2 = random.sample(private_keys, 2)
        address1 = Address(private_key_to_address(private_key1))
        address2 = Address(private_key_to_address(private_key2))
        token_network_model.handle_channel_opened_event(
            channel_id,
            address1,
            address2,
        )

        # deposit to channels
        deposit1, deposit2 = random.sample(range(1000), 2)
        address1, address2 = token_network_model.channel_id_to_addresses[
            channel_id]
        token_network_model.handle_channel_new_deposit_event(
            channel_id,
            address1,
            deposit1,
        )
        token_network_model.handle_channel_new_deposit_event(
            channel_id,
            address2,
            deposit2,
        )
Esempio n. 2
0
def get_monitor_request_for_same_channel(
    get_random_address,
    get_random_privkey,
    token_network,
    state_db_sqlite,
):
    keys = [get_random_privkey() for i in range(3)]
    token_network_address = token_network.address

    channel_id = 1
    balance_hash_data = '0'
    state_db_sqlite.store_new_channel(
        channel_id,
        token_network_address,
        private_key_to_address(keys[0]),
        private_key_to_address(keys[1]),
    )

    def f(
        user=None,
        reward_amount=0,
        bad_key_for_bp=False,
        bad_key_for_non_closing=False,
    ):
        if user == 0:
            privkey = keys[0]
            privkey_non_closing = keys[1]
        else:
            privkey = keys[1]
            privkey_non_closing = keys[0]
        balance_proof = BalanceProof(
            channel_id,
            token_network_address,
            balance_hash=encode_hex(sha3(balance_hash_data.encode())),
        )
        balance_proof.signature = encode_hex(
            eth_sign(
                privkey if not bad_key_for_bp else keys[2],
                balance_proof.serialize_bin(),
            ))
        non_closing_signature = encode_hex(
            eth_sign(
                privkey_non_closing
                if not bad_key_for_non_closing else keys[2],
                balance_proof.serialize_bin(
                    msg_type=MessageTypeId.BALANCE_PROOF_UPDATE) +
                decode_hex(balance_proof.signature),
            ))

        monitor_request = MonitorRequest(
            balance_proof,
            non_closing_signature,
            reward_amount=reward_amount,
            monitor_address=get_random_address(),
        )
        monitor_request.reward_proof_signature = encode_hex(
            eth_sign(privkey, monitor_request.serialize_reward_proof()), )
        return monitor_request

    return f
Esempio n. 3
0
def create_signed_monitor_request(
    nonce: Nonce = Nonce(5),
    reward_amount: TokenAmount = DEFAULT_REWARD_AMOUNT,
    closing_privkey: str = DEFAULT_PRIVATE_KEY1,
    nonclosing_privkey: str = DEFAULT_PRIVATE_KEY2,
) -> MonitorRequest:
    bp = HashedBalanceProof(
        channel_identifier=DEFAULT_CHANNEL_IDENTIFIER,
        token_network_address=DEFAULT_TOKEN_NETWORK_ADDRESS,
        chain_id=ChainID(1),
        balance_hash="",
        nonce=nonce,
        additional_hash="",
        priv_key=closing_privkey,
    )
    monitor_request = bp.get_monitor_request(privkey=nonclosing_privkey,
                                             reward_amount=reward_amount,
                                             msc_address=TEST_MSC_ADDRESS)

    # Some signature correctness checks
    assert monitor_request.signer == private_key_to_address(closing_privkey)
    assert monitor_request.non_closing_signer == private_key_to_address(
        nonclosing_privkey)
    assert monitor_request.reward_proof_signer == private_key_to_address(
        nonclosing_privkey)

    return monitor_request
Esempio n. 4
0
 def __init__(self,
              private_key: str,
              state_db: StateDB = None,
              transport: Transport = None,
              blockchain: BlockchainMonitor = None) -> None:
     super().__init__()
     assert isinstance(private_key, str)
     assert isinstance(transport, Transport)
     assert isinstance(blockchain, BlockchainMonitor)
     assert isinstance(state_db, StateDB)
     self.private_key = private_key
     self.transport = transport
     self.blockchain = blockchain
     self.state_db = state_db
     self.stop_event = gevent.event.Event()
     assert is_checksum_address(private_key_to_address(self.private_key))
     self.transport.add_message_callback(
         lambda message: self.on_message_event(message))
     self.transport.privkey = lambda: self.private_key
     if state_db.is_initialized() is False:
         network_id = 6
         contract_address = '0xD5BE9a680AbbF01aB2d422035A64DB27ab01C624'
         receiver = private_key_to_address(private_key)
         state_db.setup_db(network_id, contract_address, receiver)
     self.task_list: List[gevent.Greenlet] = []
Esempio n. 5
0
def populate_token_networks_random(
    token_networks: List[TokenNetwork],
    private_keys: List[str],
) -> None:
    # seed for pseudo-randomness from config constant, that changes from time to time
    random.seed(NUMBER_OF_CHANNELS)

    for token_network in token_networks:
        for channel_id_int in range(NUMBER_OF_CHANNELS):
            channel_id = ChannelIdentifier(channel_id_int)

            private_key1, private_key2 = random.sample(private_keys, 2)
            address1 = Address(private_key_to_address(private_key1))
            address2 = Address(private_key_to_address(private_key2))
            fee1 = random.randint(100, 10000)
            fee2 = random.randint(100, 10000)
            token_network.handle_channel_opened_event(channel_id, address1,
                                                      address2)

            # deposit to channels
            deposit1, deposit2 = random.sample(range(1000), 2)
            address1, address2 = token_network.channel_id_to_addresses[
                channel_id]
            token_network.handle_channel_new_deposit_event(
                channel_id, address1, deposit1)
            token_network.handle_channel_new_deposit_event(
                channel_id, address2, deposit2)
            # cuts negative values of probability distribution, fix with > 0 distribution

            token_network.update_fee(channel_id, address1, channel_id + 1,
                                     fee1)

            token_network.update_fee(channel_id, address2, channel_id + 1,
                                     fee2)
Esempio n. 6
0
def populate_token_network_random(token_network_model: TokenNetwork,
                                  private_keys: List[str]) -> None:
    # seed for pseudo-randomness from config constant, that changes from time to time
    random.seed(NUMBER_OF_CHANNELS)

    for channel_id_int in range(NUMBER_OF_CHANNELS):
        channel_id = ChannelID(channel_id_int)

        private_key1, private_key2 = random.sample(private_keys, 2)
        address1 = Address(private_key_to_address(private_key1))
        address2 = Address(private_key_to_address(private_key2))
        settle_timeout = 15
        token_network_model.handle_channel_opened_event(
            channel_id, address1, address2, settle_timeout)

        # deposit to channels
        deposit1 = TokenAmount(random.randint(0, 1000))
        deposit2 = TokenAmount(random.randint(0, 1000))
        address1, address2 = token_network_model.channel_id_to_addresses[
            channel_id]
        token_network_model.handle_channel_new_deposit_event(
            channel_id, address1, deposit1)
        token_network_model.handle_channel_new_deposit_event(
            channel_id, address2, deposit2)
        token_network_model.handle_channel_balance_update_message(
            UpdatePFS(
                canonical_identifier=CanonicalIdentifier(
                    chain_identifier=ChainID(1),
                    channel_identifier=channel_id,
                    token_network_address=TokenNetworkAddressBytes(
                        decode_hex(token_network_model.address)),
                ),
                updating_participant=decode_hex(address1),
                other_participant=decode_hex(address2),
                updating_nonce=Nonce(1),
                other_nonce=Nonce(1),
                updating_capacity=deposit1,
                other_capacity=deposit2,
                reveal_timeout=2,
                mediation_fee=FeeAmount(0),
            ))
        token_network_model.handle_channel_balance_update_message(
            UpdatePFS(
                canonical_identifier=CanonicalIdentifier(
                    chain_identifier=ChainID(1),
                    channel_identifier=channel_id,
                    token_network_address=TokenNetworkAddressBytes(
                        decode_hex(token_network_model.address)),
                ),
                updating_participant=decode_hex(address2),
                other_participant=decode_hex(address1),
                updating_nonce=Nonce(2),
                other_nonce=Nonce(1),
                updating_capacity=deposit2,
                other_capacity=deposit1,
                reveal_timeout=2,
                mediation_fee=FeeAmount(0),
            ))
Esempio n. 7
0
def test_pfs_rejects_capacity_update_with_wrong_nonces(
    pathfinding_service_web3_mock, ):
    pathfinding_service_web3_mock.chain_id = ChainID(1)

    token_network = TokenNetwork(
        token_network_address=DEFAULT_TOKEN_NETWORK_ADDRESS)

    pathfinding_service_web3_mock.token_networks[
        token_network.address] = token_network

    token_network.handle_channel_opened_event(
        channel_identifier=ChannelID(0),
        participant1=private_key_to_address(PRIVAT_KEY_EXAMPLE_1),
        participant2=private_key_to_address(PRIVAT_KEY_EXAMPLE_2),
        settle_timeout=15,
    )

    token_network.handle_channel_new_deposit_event(
        channel_identifier=ChannelID(0),
        receiver=private_key_to_address(PRIVAT_KEY_EXAMPLE_1),
        total_deposit=100,
    )

    token_network.handle_channel_new_deposit_event(
        channel_identifier=ChannelID(0),
        receiver=private_key_to_address(PRIVAT_KEY_EXAMPLE_2),
        total_deposit=100,
    )

    # Check that the new channel has id == 0
    assert token_network.channel_id_to_addresses[ChannelID(0)] == (
        private_key_to_address(PRIVAT_KEY_EXAMPLE_1),
        private_key_to_address(PRIVAT_KEY_EXAMPLE_2),
    )

    message = get_updatepfs_message(
        updating_participant=private_key_to_address(PRIVAT_KEY_EXAMPLE_1),
        other_participant=private_key_to_address(PRIVAT_KEY_EXAMPLE_2),
        privkey_signer=PRIVAT_KEY_EXAMPLE_1,
    )

    # Check first capacity update succeeded
    pathfinding_service_web3_mock.on_pfs_update(message)
    view_to_partner, view_from_partner = token_network.get_channel_views_for_partner(
        channel_identifier=ChannelID(0),
        updating_participant=private_key_to_address(PRIVAT_KEY_EXAMPLE_1),
        other_participant=private_key_to_address(PRIVAT_KEY_EXAMPLE_2),
    )
    assert view_to_partner.capacity == 90
    assert view_to_partner.update_nonce == 1
    assert view_from_partner.capacity == 110
    assert view_from_partner.update_nonce == 0

    # Send the same Capacity Update again - leads to an exception
    with pytest.raises(InvalidCapacityUpdate) as exinfo:
        pathfinding_service_web3_mock.on_pfs_update(message)
    assert "Capacity Update already received" in str(exinfo.value)
Esempio n. 8
0
def populate_token_network_random(
    token_network_model: TokenNetwork,
    private_keys: List[str],
) -> None:
    # seed for pseudo-randomness from config constant, that changes from time to time
    random.seed(NUMBER_OF_CHANNELS)

    for channel_id_int in range(NUMBER_OF_CHANNELS):
        channel_id = ChannelIdentifier(channel_id_int)

        private_key1, private_key2 = random.sample(private_keys, 2)
        address1 = Address(private_key_to_address(private_key1))
        address2 = Address(private_key_to_address(private_key2))
        settle_timeout = 15
        token_network_model.handle_channel_opened_event(
            channel_id,
            address1,
            address2,
            settle_timeout,
        )

        # deposit to channels
        deposit1, deposit2 = random.sample(range(1000), 2)
        address1, address2 = token_network_model.channel_id_to_addresses[
            channel_id]
        token_network_model.handle_channel_new_deposit_event(
            channel_id,
            address1,
            deposit1,
        )
        token_network_model.handle_channel_new_deposit_event(
            channel_id,
            address2,
            deposit2,
        )
        token_network_model.handle_channel_balance_update_message(
            channel_identifier=channel_id,
            updating_participant=address1,
            other_participant=address2,
            updating_nonce=1,
            other_nonce=1,
            updating_capacity=deposit1,
            other_capacity=deposit2,
            reveal_timeout=2,
        )
        token_network_model.handle_channel_balance_update_message(
            channel_identifier=channel_id,
            updating_participant=address2,
            other_participant=address1,
            updating_nonce=2,
            other_nonce=1,
            updating_capacity=deposit1,
            other_capacity=deposit2,
            reveal_timeout=2,
        )
Esempio n. 9
0
def withdraw(
    private_key: str,
    web3: Web3,
    contracts: Dict[str, Contract],
    start_block: BlockNumber,
    to: Optional[Address],
) -> None:
    log.info("Using RPC endpoint", rpc_url=get_web3_provider_info(web3))
    service_registry_contract = contracts[CONTRACT_SERVICE_REGISTRY]
    caller_address = private_key_to_address(private_key)

    # Find deposit contract address
    caller_address = private_key_to_address(private_key)
    deposit_contract_address = find_withdrawable_deposit(
        web3=web3,
        service_address=caller_address,
        service_registry_contract=service_registry_contract,
        start_block=start_block,
    )
    deposit_contract = web3.eth.contract(
        abi=CONTRACT_MANAGER.get_contract_abi(CONTRACT_DEPOSIT),
        address=deposit_contract_address)

    # Check usage of correct key
    withdrawer = deposit_contract.functions.withdrawer().call()
    if to_canonical_address(withdrawer) != caller_address:
        log.error(
            "You must use the key used to deposit when withdrawing",
            expected=withdrawer,
            actual=to_checksum_address(caller_address),
        )
        sys.exit(1)

    # Can we withdraw already?
    release_at = deposit_contract.functions.release_at().call()
    deprecated = service_registry_contract.functions.deprecated().call()
    if web3.eth.getBlock(
            "latest")["timestamp"] < release_at and not deprecated:
        log.error(
            "Too early to withdraw",
            released_at_utc=datetime.utcfromtimestamp(release_at).isoformat(),
        )
        sys.exit(1)

    receiver = to or private_key_to_address(private_key)
    checked_transact(
        web3=web3,
        sender_address=caller_address,
        function_call=deposit_contract.functions.withdraw(receiver),
        task_name="withdraw",
        wait_confirmation_interval=False,
    )
Esempio n. 10
0
def test_pfs_rejects_capacity_update_with_wrong_chain_id(
    pathfinding_service_web3_mock, ):

    message = get_updatepfs_message(
        chain_identifier=ChainID(121212),
        updating_participant=private_key_to_address(PRIVAT_KEY_EXAMPLE_1),
        other_participant=private_key_to_address(PRIVAT_KEY_EXAMPLE_2),
        privkey_signer=PRIVAT_KEY_EXAMPLE_1,
    )

    with pytest.raises(InvalidCapacityUpdate) as exinfo:
        pathfinding_service_web3_mock.on_pfs_update(message)
    assert "unknown chain identifier" in str(exinfo.value)
def test_on_channel_settle(
    web3,
    generate_raiden_clients,
    get_random_privkey,
    monitoring_service_contract,
    send_funds,
):
    c1, c2 = generate_raiden_clients(2)
    ms_privkey = get_random_privkey()
    ms_address = private_key_to_address(ms_privkey)
    send_funds(ms_address)
    c1.open_channel(c2.address)
    balance_proof = c1.get_balance_proof(c2.address, transferred_amount=1)
    monitor_request = c2.get_monitor_request(
        c1.address,
        balance_proof,
        1,
        ms_address,
    )

    task = OnChannelSettle(
        monitor_request,
        PrivateContract(monitoring_service_contract),
        ms_privkey,
    )

    assert task._run() is True
    def __init__(
        self,
        web3: Web3,
        private_key: str,
        gas_limit: int,
        gas_price: int = 1,
        wait: int = 10,
        contracts_version: Optional[str] = None,
    ):
        self.web3 = web3
        self.private_key = private_key
        self.wait = wait
        self.owner = private_key_to_address(private_key)
        self.transaction = {'from': self.owner, 'gas_limit': gas_limit}
        if gas_price != 0:
            self.transaction['gasPrice'] = gas_price * denoms.gwei

        self.contracts_version = contracts_version
        self.precompiled_path = contracts_precompiled_path(
            self.contracts_version)
        self.contract_manager = ContractManager(self.precompiled_path)

        # Check that the precompiled data is correct
        self.contract_manager = ContractManager(contracts_source_path())
        self.contract_manager.checksum_contracts()
        self.contract_manager.verify_precompiled_checksums(
            self.precompiled_path)
Esempio n. 13
0
def main(
    ctx,
    rpc_provider,
    private_key,
    wait,
    gas_price,
    gas_limit,
):

    logging.basicConfig(level=logging.DEBUG)
    logging.getLogger('web3').setLevel(logging.INFO)
    logging.getLogger('urllib3').setLevel(logging.INFO)

    web3 = Web3(HTTPProvider(rpc_provider, request_kwargs={'timeout': 60}))
    web3.middleware_stack.inject(geth_poa_middleware, layer=0)
    print('Web3 provider is', web3.providers[0])

    private_key = get_private_key(private_key)
    assert private_key is not None
    owner = private_key_to_address(private_key)
    assert web3.eth.getBalance(owner) > 0, 'Account with insuficient funds.'
    deployer = ContractDeployer(
        web3,
        private_key,
        gas_limit,
        gas_price,
        wait,
    )
    ctx.obj = {}
    ctx.obj['deployer'] = deployer
    ctx.obj['deployed_contracts'] = {}
    ctx.obj['token_type'] = 'CustomToken'
    ctx.obj['wait'] = wait
def info(
    private_key: str,
    web3: Web3,
    contracts: Dict[str, Contract],
    start_block: BlockNumber,
) -> None:
    log.info("Using RPC endpoint", rpc_url=get_web3_provider_info(web3))
    service_registry_contract = contracts[CONTRACT_SERVICE_REGISTRY]
    deposit_token_address = service_registry_contract.functions.token().call()
    deposit_token_contract = web3.eth.contract(
        address=deposit_token_address,
        abi=CONTRACT_MANAGER.get_contract_abi(CONTRACT_CUSTOM_TOKEN),
    )
    caller_address = private_key_to_address(private_key)
    fmt_amount = get_token_formatter(deposit_token_contract)

    deposits = find_deposits(
        web3=web3,
        service_address=caller_address,
        service_registry_contract=service_registry_contract,
        start_block=start_block,
    )
    if not deposits:
        print("No deposits were made from this account.")
        return

    print("Deposits:")
    for dep in deposits:
        print(f" * block {dep['block_number']}", end=", ")
        print(f"amount: {fmt_amount(dep['amount'])}", end=", ")
        if dep["withdrawn"]:
            print("WITHDRAWN")
        else:
            print("increased validity till " + dep["valid_till"])
Esempio n. 15
0
def monitoring_service(
    server_private_key,
    blockchain,
    state_db_sqlite,
    web3,
    monitoring_service_contract,
    token_network_registry_contract,
    send_funds,
    contracts_manager: ContractManager,
):
    # send some eth & tokens to MS
    send_funds(private_key_to_address(server_private_key))
    register_service(
        web3=web3,
        contract_manager=contracts_manager,
        msc_contract_address=monitoring_service_contract.address,
        private_key=server_private_key,
    )

    ms = MonitoringService(
        web3=web3,
        contract_manager=contracts_manager,
        private_key=server_private_key,
        state_db=state_db_sqlite,
        registry_address=token_network_registry_contract.address,
        monitor_contract_address=monitoring_service_contract.address,
        required_confirmations=1,  # for faster tests
        poll_interval=0,  # for faster tests
    )
    ms.start()
    yield ms
    ms.stop()
Esempio n. 16
0
def monitoring_service(
        server_private_key,
        blockchain,
        dummy_transport,
        state_db_mock,
        web3,
        monitoring_service_contract,
        send_funds
):
    # send some eth & tokens to MS
    send_funds(private_key_to_address(server_private_key))
    register_service(
        web3,
        monitoring_service_contract.address,
        server_private_key
    )

    ms = MonitoringService(
        server_private_key,
        transport=dummy_transport,
        blockchain=blockchain,
        state_db=state_db_mock,
        monitor_contract_address=monitoring_service_contract.address
    )
    yield ms
    ms.stop()
Esempio n. 17
0
def main(
    private_key: str,
    state_db: str,
    web3: Web3,
    contracts: Dict[str, Contract],
    start_block: BlockNumber,
    rdn_per_eth: float,
    expires_within: BlockNumber,
) -> None:
    pfs_address = private_key_to_address(private_key)
    chain_id = ChainID(web3.eth.chainId)
    database = PFSDatabase(filename=state_db,
                           chain_id=chain_id,
                           pfs_address=pfs_address,
                           sync_start_block=start_block)

    claim_cost_rdn = calc_claim_cost_rdn(web3, rdn_per_eth)
    ious = list(
        get_claimable_ious(
            database,
            expires_after=web3.eth.blockNumber,
            expires_before=BlockNumber(web3.eth.blockNumber + expires_within),
            claim_cost_rdn=claim_cost_rdn,
        ))
    print(f"Found {len(ious)} claimable IOUs")
    _, failures = claim_ious(ious, claim_cost_rdn,
                             contracts[CONTRACT_ONE_TO_N], web3, database)
    if failures:
        sys.exit(1)
Esempio n. 18
0
    def __init__(  # pylint: disable=too-many-arguments
        self,
        web3: Web3,
        contracts: Dict[str, Contract],
        private_key: PrivateKey,
        db_filename: str,
        sync_start_block: BlockNumber,
        required_confirmations: BlockTimeout,
        poll_interval: float,
        matrix_servers: Optional[List[str]] = None,
    ):
        super().__init__()

        self.web3 = web3
        self.registry_address = contracts[
            CONTRACT_TOKEN_NETWORK_REGISTRY].address
        self.user_deposit_contract = contracts[CONTRACT_USER_DEPOSIT]
        self.service_token_address = self.user_deposit_contract.functions.token(
        ).call()
        self.chain_id = ChainID(web3.eth.chainId)
        self.address = private_key_to_address(private_key)
        self.required_confirmations = required_confirmations
        self._poll_interval = poll_interval
        self._is_running = gevent.event.Event()

        log.info("PFS payment address", address=self.address)

        self.database = PFSDatabase(
            filename=db_filename,
            pfs_address=self.address,
            sync_start_block=sync_start_block,
            token_network_registry_address=to_canonical_address(
                self.registry_address),
            chain_id=self.chain_id,
            user_deposit_contract_address=to_canonical_address(
                self.user_deposit_contract.address),
            allow_create=True,
        )

        self.blockchain_state = BlockchainState(
            latest_committed_block=self.database.get_latest_committed_block(),
            token_network_registry_address=to_canonical_address(
                self.registry_address),
            chain_id=self.chain_id,
        )

        self.matrix_listener = MatrixListener(
            private_key=private_key,
            chain_id=self.chain_id,
            device_id=DeviceIDs.PFS,
            message_received_callback=self.handle_message,
            servers=matrix_servers,
        )

        self.token_networks = self._load_token_networks()
        self.updated = gevent.event.Event(
        )  # set whenever blocks are processed
        self.startup_finished = gevent.event.AsyncResult()

        self._init_metrics()
Esempio n. 19
0
def main(
    private_key: str,
    state_db: str,
    web3: Web3,
    contracts: Dict[str, Contract],
    start_block: BlockNumber,
    rdn_per_eth: float,
    expires_within: BlockNumber,
) -> None:
    pfs_address = private_key_to_address(private_key)
    chain_id = ChainID(int(web3.net.version))
    database = PFSDatabase(filename=state_db,
                           chain_id=chain_id,
                           pfs_address=pfs_address)

    one_to_n_contract = contracts[CONTRACT_ONE_TO_N]

    claim_cost_eth = 90897
    claim_cost_rdn = TokenAmount(int(claim_cost_eth / rdn_per_eth))
    ious = list(
        get_claimable_ious(
            database,
            expires_before=web3.eth.blockNumber + expires_within,
            claim_cost_rdn=claim_cost_rdn,
        ))
    print(f"Found {len(ious)} claimable IOUs")
    _, failures = claim_ious(ious, claim_cost_rdn, one_to_n_contract, web3,
                             database)
    if failures:
        sys.exit(1)
Esempio n. 20
0
def build_request_monitoring():
    non_closing_privkey = PrivateKey(get_random_privkey())
    non_closing_address = private_key_to_address(non_closing_privkey)

    def f(
        chain_id: ChainID = TEST_CHAIN_ID,
        amount: TokenAmount = TokenAmount(50),
        nonce: Nonce = Nonce(1),
        channel_id: ChannelID = ChannelID(1),
    ) -> RequestMonitoring:
        balance_proof = HashedBalanceProof(
            channel_identifier=channel_id,
            token_network_address=TokenNetworkAddress(b"1" * 20),
            chain_id=chain_id,
            nonce=nonce,
            additional_hash="",
            balance_hash=encode_hex(bytes([amount])),
            priv_key=PrivateKey(get_random_privkey()),
        )
        request_monitoring = balance_proof.get_request_monitoring(
            privkey=non_closing_privkey,
            reward_amount=TokenAmount(55),
            monitoring_service_contract_address=TEST_MSC_ADDRESS,
        )

        # usually not a property of RequestMonitoring, but added for convenience in these tests
        request_monitoring.non_closing_signer = to_checksum_address(non_closing_address)
        return request_monitoring

    return f
Esempio n. 21
0
def test_monitor_request(get_random_bp, get_random_privkey, get_random_address):
    balance_proof = get_random_bp()
    client_privkey = get_random_privkey()
    client_address = private_key_to_address(client_privkey)
    balance_proof.signature = encode_hex(sign_data(client_privkey, balance_proof.serialize_bin()))
    monitor_request = MonitorRequest(
        balance_proof,
        non_closing_signature=balance_proof.signature,
        reward_sender_address=client_address,
        reward_proof_signature='',
        reward_amount=1,
        monitor_address=get_random_address()
    )

    serialized = monitor_request.serialize_data()
    monitor_request_verify = MonitorRequest.deserialize(serialized)
    balance_proof_verify = monitor_request_verify.balance_proof
    assert is_same_address(monitor_request_verify.monitor_address, monitor_request.monitor_address)
    assert is_same_address(
        monitor_request_verify.reward_sender_address,
        monitor_request.reward_sender_address
    )
    assert monitor_request_verify.non_closing_signature == monitor_request.non_closing_signature
    assert monitor_request_verify.reward_amount == monitor_request.reward_amount
    assert is_same_address(
        balance_proof_verify.token_network_address,
        balance_proof.token_network_address
    )
    assert balance_proof_verify.chain_id == balance_proof.chain_id
    assert balance_proof_verify.channel_identifier == balance_proof.channel_identifier
    assert balance_proof_verify.nonce == balance_proof.nonce
Esempio n. 22
0
def test_get_iou(api_sut: PFSApi, api_url: str, token_network_model: TokenNetwork, make_iou):
    privkey = PrivateKey(get_random_privkey())
    sender = private_key_to_address(privkey)
    url = api_url + f"/v1/{to_checksum_address(token_network_model.address)}/payment/iou"

    def make_params(timestamp: str):
        params = {
            "sender": to_checksum_address(sender),
            "receiver": to_checksum_address(api_sut.pathfinding_service.address),
            "timestamp": timestamp,
        }
        local_signer = LocalSigner(private_key=privkey)
        params["signature"] = encode_hex(
            local_signer.sign(
                to_canonical_address(params["sender"])
                + to_canonical_address(params["receiver"])
                + params["timestamp"].encode("utf8")
            )
        )
        return params

    # Request without IOU in database
    params = make_params(datetime.utcnow().isoformat())
    response = requests.get(url, params=params)
    assert response.status_code == 404, response.json()
    assert response.json() == {"last_iou": None}

    # Add IOU to database
    iou = make_iou(
        privkey, api_sut.pathfinding_service.address, one_to_n_address=api_sut.one_to_n_address
    )
    iou.claimed = False
    api_sut.pathfinding_service.database.upsert_iou(iou)

    # Is returned IOU the one save into the db?
    response = requests.get(url, params=params)
    assert response.status_code == 200, response.json()
    iou_dict = IOU.Schema(exclude=["claimed"]).dump(iou)
    assert response.json()["last_iou"] == iou_dict

    # Invalid signatures must fail
    params["signature"] = encode_hex((int(params["signature"], 16) + 1).to_bytes(65, "big"))
    response = requests.get(url, params=params)
    assert response.status_code == 400, response.json()
    assert response.json()["error_code"] == exceptions.InvalidSignature.error_code

    # Timestamp must no be too old to prevent replay attacks
    old_timestamp = datetime.utcnow() - timedelta(days=1)
    params = make_params(old_timestamp.isoformat())
    response = requests.get(url, params=params)
    assert response.status_code == 400, response.json()
    assert response.json()["error_code"] == exceptions.RequestOutdated.error_code

    # Timestamp with timezone info is invalid
    for timestamp in (datetime.now(tz=timezone.utc).isoformat(), "2019-11-07T12:52:25.079Z"):
        params = make_params(timestamp)
        response = requests.get(url, params=params)
        assert response.status_code == 400, response.json()
        assert response.json()["error_code"] == exceptions.InvalidRequest.error_code
Esempio n. 23
0
def test_pfs_rejects_capacity_update_with_impossible_updating_capacity(
    pathfinding_service_web3_mock, ):
    pathfinding_service_web3_mock.chain_id = ChainID(1)

    token_network = TokenNetwork(
        token_network_address=DEFAULT_TOKEN_NETWORK_ADDRESS)

    pathfinding_service_web3_mock.token_networks[
        token_network.address] = token_network

    token_network.handle_channel_opened_event(
        channel_identifier=ChannelID(0),
        participant1=private_key_to_address(PRIVAT_KEY_EXAMPLE_1),
        participant2=private_key_to_address(PRIVAT_KEY_EXAMPLE_2),
        settle_timeout=15,
    )

    token_network.handle_channel_new_deposit_event(
        channel_identifier=ChannelID(0),
        receiver=private_key_to_address(PRIVAT_KEY_EXAMPLE_1),
        total_deposit=100,
    )

    token_network.handle_channel_new_deposit_event(
        channel_identifier=ChannelID(0),
        receiver=private_key_to_address(PRIVAT_KEY_EXAMPLE_2),
        total_deposit=100,
    )

    # Check that the new channel has id == 0
    assert token_network.channel_id_to_addresses[ChannelID(0)] == (
        private_key_to_address(PRIVAT_KEY_EXAMPLE_1),
        private_key_to_address(PRIVAT_KEY_EXAMPLE_2),
    )

    with patch(
            "pathfinding_service.service.recover_signer_from_capacity_update",
            private_key_to_address(PRIVAT_KEY_EXAMPLE_1),
    ):
        message = get_updatepfs_message(
            updating_participant=private_key_to_address(PRIVAT_KEY_EXAMPLE_1),
            other_participant=private_key_to_address(PRIVAT_KEY_EXAMPLE_2),
            updating_capacity=TokenAmount(UINT256_MAX),
            privkey_signer=PRIVAT_KEY_EXAMPLE_1,
        )
        message.updating_capacity = TokenAmount(UINT256_MAX + 1)

        with pytest.raises(InvalidCapacityUpdate) as exinfo:
            pathfinding_service_web3_mock.on_pfs_update(message)
        assert "with impossible updating_capacity" in str(exinfo.value)
Esempio n. 24
0
def test_get_iou(api_sut: ServiceApi, api_url: str,
                 token_network_model: TokenNetwork, make_iou):
    privkey = get_random_privkey()
    sender = private_key_to_address(privkey)
    url = api_url + f"/{to_checksum_address(token_network_model.address)}/payment/iou"

    def make_params(timestamp: datetime):
        params = {
            "sender": to_checksum_address(sender),
            "receiver":
            to_checksum_address(api_sut.pathfinding_service.address),
            "timestamp": timestamp.isoformat(),
        }
        local_signer = LocalSigner(private_key=decode_hex(privkey))
        params["signature"] = encode_hex(
            local_signer.sign(
                pack_data(
                    (params["sender"], "address"),
                    (params["receiver"], "address"),
                    (params["timestamp"], "string"),
                )))
        return params

    # Request without IOU in database
    params = make_params(datetime.utcnow())
    response = requests.get(url, params=params)
    assert response.status_code == 404, response.json()
    assert response.json() == {"last_iou": None}

    # Add IOU to database
    iou = make_iou(privkey,
                   api_sut.pathfinding_service.address,
                   one_to_n_address=api_sut.one_to_n_address)
    iou.claimed = False
    api_sut.pathfinding_service.database.upsert_iou(iou)

    # Is returned IOU the one save into the db?
    response = requests.get(url, params=params)
    assert response.status_code == 200, response.json()
    iou_dict = IOU.Schema(exclude=["claimed"]).dump(iou)
    assert response.json()["last_iou"] == iou_dict

    # Invalid signatures must fail
    params["signature"] = encode_hex(
        (int(params["signature"], 16) + 1).to_bytes(65, "big"))
    response = requests.get(url, params=params)
    assert response.status_code == 400, response.json()
    assert response.json(
    )["error_code"] == exceptions.InvalidSignature.error_code

    # Timestamp must no be too old to prevent replay attacks
    params = make_params(datetime.utcnow() - timedelta(days=1))
    response = requests.get(url, params=params)
    assert response.status_code == 400, response.json()
    assert response.json(
    )["error_code"] == exceptions.RequestOutdated.error_code
Esempio n. 25
0
 def get():
     privkey = get_random_privkey()
     address = private_key_to_address(privkey)
     ethereum_tester.add_account(privkey)
     web3.eth.sendTransaction({
         'from': faucet_address,
         'to': address,
         'value': 1 * denoms.ether
     })
     return address
Esempio n. 26
0
 def __init__(self, privkey: str, token_network_contract: Contract,
              token_contract: Contract) -> None:
     self.privkey = privkey
     self.address = private_key_to_address(privkey)
     self.contract = token_network_contract
     self.token_contract = token_contract
     self.partner_to_channel_id: Dict[Address, ChannelIdentifier] = dict()
     self.token_network_abi = None
     self.client_registry: Dict[Address, 'MockRaidenNode'] = dict()
     self.web3 = self.contract.web3
Esempio n. 27
0
def test_get_iou(
    api_sut: ServiceApi, api_url: str, pathfinding_service_mock, token_network_model: TokenNetwork
):
    privkey = get_random_privkey()
    sender = private_key_to_address(privkey)
    url = api_url + f'/{token_network_model.address}/payment/iou'

    def make_params(timestamp: datetime):
        params = {
            'sender': sender,
            'receiver': api_sut.pathfinding_service.address,
            'timestamp': timestamp.isoformat(),
        }
        local_signer = LocalSigner(private_key=decode_hex(privkey))
        params['signature'] = encode_hex(
            local_signer.sign(
                pack_data(
                    ['address', 'address', 'string'],
                    [params['sender'], params['receiver'], params['timestamp']],
                )
            )
        )
        return params

    # Request without IOU in database
    params = make_params(datetime.utcnow())
    response = requests.get(url, params=params)
    assert response.status_code == 404, response.json()
    assert response.json() == {'last_iou': None}

    # Add IOU to database
    iou = make_iou(privkey, api_sut.pathfinding_service.address)
    iou.claimed = False
    api_sut.pathfinding_service.database.upsert_iou(iou)

    # Is returned IOU the one save into the db?
    response = requests.get(url, params=params)
    assert response.status_code == 200, response.json()
    iou_dict = IOU.Schema(exclude=['claimed']).dump(iou)[0]
    assert response.json()['last_iou'] == iou_dict

    # Invalid signatures must fail
    params['signature'] = encode_hex((int(params['signature'], 16) + 1).to_bytes(65, 'big'))
    response = requests.get(url, params=params)
    assert response.status_code == 400, response.json()
    assert response.json()['error_code'] == exceptions.InvalidSignature.error_code

    # Timestamp must no be too old to prevent replay attacks
    params = make_params(datetime.utcnow() - timedelta(days=1))
    response = requests.get(url, params=params)
    assert response.status_code == 400, response.json()
    assert response.json()['error_code'] == exceptions.RequestOutdated.error_code

    # kill all running greenlets
    gevent.killall([obj for obj in gc.get_objects() if isinstance(obj, gevent.Greenlet)])
Esempio n. 28
0
    def __init__(  # pylint: disable=too-many-arguments
        self,
        web3: Web3,
        contracts: Dict[str, Contract],
        private_key: str,
        db_filename: str,
        sync_start_block: BlockNumber = BlockNumber(0),
        required_confirmations: int = 8,
        poll_interval: float = 10,
        matrix_servers: List[str] = None,
    ):
        super().__init__()

        self.web3 = web3
        self.registry_address = contracts[
            CONTRACT_TOKEN_NETWORK_REGISTRY].address
        self.user_deposit_contract = contracts[CONTRACT_USER_DEPOSIT]
        self.chain_id = ChainID(int(web3.net.version))
        self.address = private_key_to_address(private_key)
        self.required_confirmations = required_confirmations
        self._poll_interval = poll_interval
        self._is_running = gevent.event.Event()

        log.info("PFS payment address", address=self.address)

        self.blockchain_state = BlockchainState(
            latest_commited_block=BlockNumber(0),
            token_network_registry_address=self.registry_address,
            chain_id=self.chain_id,
        )

        self.database = PFSDatabase(
            filename=db_filename,
            pfs_address=self.address,
            sync_start_block=sync_start_block,
            token_network_registry_address=self.registry_address,
            chain_id=self.chain_id,
            user_deposit_contract_address=self.user_deposit_contract.address,
            allow_create=True,
        )

        self.matrix_listener = MatrixListener(
            private_key=private_key,
            chain_id=self.chain_id,
            service_room_suffix=PATH_FINDING_BROADCASTING_ROOM,
            message_received_callback=self.handle_message,
            address_reachability_changed_callback=self.
            handle_reachability_change,
            servers=matrix_servers,
        )

        self.address_to_reachability: Dict[Address,
                                           AddressReachability] = dict()
        self.token_networks = self._load_token_networks()
Esempio n. 29
0
def main(
    private_key: str,
    state_db: str,  # pylint: disable=unused-argument
    web3: Web3,
    contracts: Dict[str, Contract],
    start_block: BlockNumber,  # pylint: disable=unused-argument
    service_url: str,
) -> None:
    """
    Registers the address of a service deployment with the `ServiceRegistry`.

    The address that is registered is derived from the supplied private key.
    It also sets or updates the URL of the services deployment.
    """
    # Add middleware to sign transactions by default
    web3.middleware_stack.add(construct_sign_and_send_raw_middleware(private_key))

    service_address = private_key_to_address(private_key)
    log.info("Running service registration script", account_address=service_address)

    service_registry_contract = contracts[CONTRACT_SERVICE_REGISTRY]

    # check if already registered
    currently_registered = service_registry_contract.functions.hasValidRegistration(
        service_address
    ).call()
    current_url = service_registry_contract.functions.urls(service_address).call()
    log.info(
        "Current ServiceRegistry information for service address",
        service_address=service_address,
        currently_registered=currently_registered,
        current_url=current_url,
    )

    # Register if not yet done
    if not currently_registered:
        deposit_to_registry(
            web3=web3,
            service_registry_contract=service_registry_contract,
            user_deposit_contract=contracts[CONTRACT_USER_DEPOSIT],
            service_address=service_address,
        )

    update_service_url(
        web3=web3,
        service_registry_contract=service_registry_contract,
        service_address=service_address,
        service_url=service_url,
        current_url=current_url,
    )

    current_url = service_registry_contract.functions.urls(service_address).call()

    log.info("Updated infos", current_url=current_url)
Esempio n. 30
0
def test_pfs_rejects_capacity_update_with_wrong_token_network_address(
    pathfinding_service_web3_mock, ):
    pathfinding_service_web3_mock.chain_id = ChainID(1)

    token_network = TokenNetwork(
        token_network_address=DEFAULT_TOKEN_NETWORK_ADDRESS)

    pathfinding_service_web3_mock.token_networks[
        token_network.address] = token_network

    message = get_updatepfs_message(
        token_network_address=TokenNetworkAddressBytes(
            decode_hex("0x" + "1" * 40)),
        updating_participant=private_key_to_address(PRIVAT_KEY_EXAMPLE_1),
        other_participant=private_key_to_address(PRIVAT_KEY_EXAMPLE_2),
        privkey_signer=PRIVAT_KEY_EXAMPLE_1,
    )

    with pytest.raises(InvalidCapacityUpdate) as exinfo:
        pathfinding_service_web3_mock.on_pfs_update(message)
    assert "unknown token network" in str(exinfo.value)