예제 #1
0
def make_signed_transfer_from_counter(counter):
    lock = Lock(
        amount=next(counter),
        expiration=next(counter),
        secrethash=factories.make_secret_hash(next(counter)),
    )

    signed_transfer = factories.create(
        factories.LockedTransferSignedStateProperties(
            amount=next(counter),
            initiator=factories.make_initiator_address(),
            target=factories.make_target_address(),
            expiration=next(counter),
            secret=factories.make_secret(next(counter)),
            payment_identifier=next(counter),
            token=factories.make_token_address(),
            nonce=next(counter),
            transferred_amount=next(counter),
            locked_amount=next(counter),
            locksroot=sha3(lock.as_bytes),
            canonical_identifier=factories.make_canonical_identifier(
                token_network_address=factories.make_address(),
                channel_identifier=next(counter)),
            recipient=factories.make_address(),
            sender=factories.HOP1,
            pkey=factories.HOP1_KEY,
        ))

    return signed_transfer
예제 #2
0
def make_from_route_from_counter(counter):
    from_channel = factories.create(
        factories.NettingChannelStateProperties(
            canonical_identifier=factories.make_canonical_identifier(),
            token_address=factories.make_token_address(),
            partner_state=factories.NettingChannelEndStateProperties(
                balance=next(counter), address=factories.HOP1),
        ))
    from_hop = factories.make_hop_from_channel(from_channel)

    expiration = BlockExpiration(factories.UNIT_REVEAL_TIMEOUT + 1)

    from_transfer = factories.make_signed_transfer_for(
        from_channel,
        factories.LockedTransferSignedStateProperties(
            transferred_amount=TokenAmount(0),
            canonical_identifier=factories.make_canonical_identifier(
                token_network_address=from_channel.token_network_address),
            amount=TokenAmount(1),
            expiration=expiration,
            secret=sha3(factories.make_secret(next(counter))),
            initiator=factories.make_initiator_address(),
            target=factories.make_target_address(),
            payment_identifier=next(counter),
            sender=factories.HOP1,
            pkey=factories.HOP1_KEY,
        ),
    )
    return from_hop, from_transfer
예제 #3
0
def test_prepare_mediation_fee_config_flat_fee(cli_flat_fee, expected_channel_flat_fee):
    token_address = factories.make_token_address()
    fee_config = prepare_mediation_fee_config(
        cli_token_to_flat_fee=((token_address, cli_flat_fee),),
        cli_token_to_proportional_fee=((token_address, ProportionalFeeAmount(0)),),
        cli_token_to_proportional_imbalance_fee=((token_address, ProportionalFeeAmount(0)),),
    )

    assert fee_config.get_flat_fee(token_address) == expected_channel_flat_fee
예제 #4
0
def test_prepare_mediation_fee_config_prop_fee(cli_prop_fee):
    token_address = factories.make_token_address()
    fee_config = prepare_mediation_fee_config(
        cli_token_to_flat_fee=(),
        cli_token_to_proportional_fee=((token_address, ProportionalFeeAmount(cli_prop_fee)),),
        cli_token_to_proportional_imbalance_fee=((token_address, ProportionalFeeAmount(0)),),
    )

    cli_prop_fee_ratio = cli_prop_fee / 1e6
    channel_prop_fee_ratio = fee_config.get_proportional_fee(token_address) / 1e6

    assert isclose(
        1 + cli_prop_fee_ratio,
        1 + channel_prop_fee_ratio + channel_prop_fee_ratio * (1 + cli_prop_fee_ratio),
        rel_tol=1e-6,
    )
예제 #5
0
def test_get_event_with_balance_proof():
    """ All events which contain a balance proof must be found by when
    querying the database.
    """
    serializer = JSONSerializer()
    storage = SerializedSQLiteStorage(":memory:", serializer)
    counter = itertools.count(1)
    partner_address = factories.make_address()

    balance_proof = make_balance_proof_from_counter(counter)
    lock_expired = SendLockExpired(
        recipient=partner_address,
        message_identifier=MessageID(next(counter)),
        balance_proof=balance_proof,
        secrethash=factories.make_secret_hash(next(counter)),
        canonical_identifier=balance_proof.canonical_identifier,
    )
    locked_transfer = SendLockedTransfer(
        recipient=partner_address,
        message_identifier=MessageID(next(counter)),
        transfer=make_transfer_from_counter(counter),
        canonical_identifier=factories.make_canonical_identifier(),
    )
    send_balance_proof = SendBalanceProof(
        recipient=partner_address,
        message_identifier=MessageID(next(counter)),
        payment_identifier=factories.make_payment_id(),
        token_address=factories.make_token_address(),
        secret=factories.make_secret(next(counter)),
        balance_proof=make_balance_proof_from_counter(counter),
        canonical_identifier=factories.make_canonical_identifier(),
    )

    refund_transfer = SendRefundTransfer(
        recipient=partner_address,
        message_identifier=MessageID(next(counter)),
        transfer=make_transfer_from_counter(counter),
        canonical_identifier=factories.make_canonical_identifier(),
    )

    events_balanceproofs = [
        (lock_expired, lock_expired.balance_proof),
        (locked_transfer, locked_transfer.balance_proof),
        (send_balance_proof, send_balance_proof.balance_proof),
        (refund_transfer, refund_transfer.transfer.balance_proof),
    ]

    state_change = Block(BlockNumber(1), BlockGasLimit(1),
                         factories.make_block_hash())
    for event, _ in events_balanceproofs:
        state_change_identifiers = storage.write_state_changes([state_change])
        storage.write_events(events=[(state_change_identifiers[0], event)])

    for event, balance_proof in events_balanceproofs:
        event_record = get_event_with_balance_proof_by_balance_hash(
            storage=storage,
            canonical_identifier=balance_proof.canonical_identifier,
            balance_hash=balance_proof.balance_hash,
            recipient=partner_address,
        )
        assert event_record
        assert event_record.data == event

        event_record = get_event_with_balance_proof_by_locksroot(
            storage=storage,
            canonical_identifier=balance_proof.canonical_identifier,
            recipient=event.recipient,
            locksroot=balance_proof.locksroot,
        )
        assert event_record
        assert event_record.data == event

        # Checking that balance proof attribute can be accessed for all events.
        # Issue https://github.com/raiden-network/raiden/issues/3179
        assert event_record.data.balance_proof == event.balance_proof

    storage.close()
def test_token_network_registry(
    deploy_client: JSONRPCClient,
    contract_manager: ContractManager,
    token_network_registry_address: TokenNetworkRegistryAddress,
    token_contract_name: str,
) -> None:
    proxy_manager = ProxyManager(
        rpc_client=deploy_client,
        contract_manager=contract_manager,
        metadata=ProxyManagerMetadata(
            token_network_registry_deployed_at=GENESIS_BLOCK_NUMBER,
            filters_start_at=GENESIS_BLOCK_NUMBER,
        ),
    )

    confirmed_block_identifier = deploy_client.get_confirmed_blockhash()

    token_network_registry_proxy = proxy_manager.token_network_registry(
        token_network_registry_address,
        block_identifier=confirmed_block_identifier)

    assert (token_network_registry_proxy.settlement_timeout_min(
        BLOCK_ID_LATEST) == TEST_SETTLE_TIMEOUT_MIN)
    assert (token_network_registry_proxy.settlement_timeout_max(
        BLOCK_ID_LATEST) == TEST_SETTLE_TIMEOUT_MAX)
    assert (token_network_registry_proxy.get_token_network_created(
        block_identifier=BLOCK_ID_LATEST) == 0)

    bad_token_address = make_token_address()

    # Registering a non-existing token network should fail
    with pytest.raises(AddressWithoutCode):
        token_network_registry_proxy.add_token(
            token_address=bad_token_address,
            channel_participant_deposit_limit=TokenAmount(UINT256_MAX),
            token_network_deposit_limit=TokenAmount(UINT256_MAX),
            given_block_identifier=confirmed_block_identifier,
        )

    test_token = deploy_token(
        deploy_client=deploy_client,
        contract_manager=contract_manager,
        initial_amount=TokenAmount(1000),
        decimals=0,
        token_name="TKN",
        token_symbol="TKN",
        token_contract_name=token_contract_name,
    )
    test_token_address = TokenAddress(to_canonical_address(test_token.address))

    # Check the proper exception is raised if the token does not comply to the
    # ERC20 interface. In this case the token does not have the totalSupply()
    # function implemented #3697 which is validated in the smart contract.
    with patch.object(Token, "total_supply", return_value=None):
        with pytest.raises(InvalidToken):
            token_network_registry_proxy.add_token(
                token_address=test_token_address,
                channel_participant_deposit_limit=TokenAmount(UINT256_MAX),
                token_network_deposit_limit=TokenAmount(UINT256_MAX),
                given_block_identifier=deploy_client.get_confirmed_blockhash(),
            )

    # Register a valid token
    preblockhash = deploy_client.get_confirmed_blockhash()
    token_network_address = token_network_registry_proxy.add_token(
        token_address=test_token_address,
        channel_participant_deposit_limit=TokenAmount(UINT256_MAX),
        token_network_deposit_limit=TokenAmount(UINT256_MAX),
        given_block_identifier=preblockhash,
    )
    assert token_network_address is not None
    assert (token_network_registry_proxy.get_token_network_created(
        block_identifier=BLOCK_ID_LATEST) == 1)

    # Re-registering the same token should fail with a recoverable error
    # because it is a race condition.
    with pytest.raises(RaidenRecoverableError):
        token_network_registry_proxy.add_token(
            token_address=test_token_address,
            channel_participant_deposit_limit=TokenAmount(UINT256_MAX),
            token_network_deposit_limit=TokenAmount(UINT256_MAX),
            given_block_identifier=preblockhash,
        )

    logs = token_network_registry_proxy.filter_token_added_events()
    assert is_same_address(logs[0]["args"]["token_address"],
                           test_token.address)
    assert is_same_address(logs[0]["args"]["token_network_address"],
                           token_network_address)
    assert (token_network_registry_proxy.get_token_network(
        bad_token_address, BLOCK_ID_LATEST) is None)

    result_address = token_network_registry_proxy.get_token_network(
        test_token_address, BLOCK_ID_LATEST)

    assert result_address
    assert to_normalized_address(result_address) == to_normalized_address(
        token_network_address)

    with pytest.raises(ValueError):
        assert token_network_registry_proxy.get_token_network(
            None,
            BLOCK_ID_LATEST  # type: ignore
        )

    # These are not registered token addresses
    assert (token_network_registry_proxy.get_token_network(
        bad_token_address, BLOCK_ID_LATEST) is None)
    assert (token_network_registry_proxy.get_token_network(
        test_token_address, BLOCK_ID_LATEST) is not None)
    address = token_network_registry_proxy.get_token_network(
        TokenAddress(token_network_address), BLOCK_ID_LATEST)
    assert address is None