Exemple #1
0
def test_settle_timeout_inrange(
    token_network,
    get_accounts,
    web3,
):
    """ The TokenNetwork constructor must enforce that settle timeout is in
    the valid range.

    Also asserts that the constants.py and the netting channel contract values
    are synched.
    """
    (A, B) = get_accounts(2)

    small_settle_timeout = TEST_SETTLE_TIMEOUT_MIN - 1
    large_settle_timeout = TEST_SETTLE_TIMEOUT_MAX + 1

    with pytest.raises(TransactionFailed):
        token_network.functions.openChannel(A, B,
                                            small_settle_timeout).transact()

    with pytest.raises(TransactionFailed):
        token_network.functions.openChannel(A, B,
                                            large_settle_timeout).transact()

    token_network.functions.openChannel(A, B,
                                        TEST_SETTLE_TIMEOUT_MIN).transact()
    (_, settle_block_number,
     state) = token_network.functions.getChannelInfo(A, B).call()

    assert settle_block_number == TEST_SETTLE_TIMEOUT_MIN

    token_network.functions.closeChannel(
        B,
        fake_bytes(32),
        0,
        fake_bytes(32),
        fake_bytes(64),
    ).transact({'from': A})
    web3.testing.mine(TEST_SETTLE_TIMEOUT_MIN)
    token_network.functions.settleChannel(
        A,
        0,
        0,
        fake_bytes(32),
        B,
        0,
        0,
        fake_bytes(32),
    ).transact({'from': A})
    token_network.functions.openChannel(A, B,
                                        TEST_SETTLE_TIMEOUT_MAX).transact()
    (_, settle_block_number,
     state) = token_network.functions.getChannelInfo(A, B).call()

    assert settle_block_number == TEST_SETTLE_TIMEOUT_MAX
def test_recoverAddressFromBalanceProofUpdate(
        token_network_test,
        create_balance_proof,
        create_balance_proof_update_signature,
        get_accounts,
):

    (A, B) = get_accounts(2)

    channel_identifier = fake_bytes(32)
    balance_proof = create_balance_proof(
        channel_identifier,
        A,
        other_token_network=token_network_test,
    )
    balance_proof_update_signature = create_balance_proof_update_signature(
        B,
        channel_identifier,
        *balance_proof,
        other_token_network=token_network_test,
    )

    balance_proof_update_signature_wrong_token_network = create_balance_proof_update_signature(
        B,
        channel_identifier,
        *balance_proof,
    )

    balance_proof_signed_B = create_balance_proof(
        channel_identifier,
        B,
        other_token_network=token_network_test,
    )
    balance_proof_update_signature_wrong_signer = create_balance_proof_update_signature(
        B,
        channel_identifier,
        *balance_proof_signed_B,
    )

    assert B == token_network_test.functions.recoverAddressFromBalanceProofUpdateMessagePublic(
        channel_identifier, *balance_proof, balance_proof_update_signature).call()

    assert B != token_network_test.functions.recoverAddressFromBalanceProofUpdateMessagePublic(
        channel_identifier,
        *balance_proof,
        balance_proof_update_signature_wrong_token_network,
    ).call()

    assert B != token_network_test.functions.recoverAddressFromBalanceProofUpdateMessagePublic(
        channel_identifier,
        *balance_proof,
        balance_proof_update_signature_wrong_signer,
    ).call()
def test_verifyWithdrawSignatures(
        token_network_test,
        create_withdraw_signatures,
        get_accounts,
):

    (A, B) = get_accounts(2)
    fake_signature = fake_bytes(64)
    channel_identifier = fake_hex(32)
    (signature_A, signature_B) = create_withdraw_signatures(
        [A, B],
        channel_identifier,
        A,
        1,
        token_network_test.address,
    )
    token_network_test.functions.verifyWithdrawSignaturesPublic(
        channel_identifier,
        A,
        B,
        1,
        signature_A,
        signature_B,
    ).call()

    with pytest.raises(TransactionFailed):
        token_network_test.functions.verifyWithdrawSignaturesPublic(
            channel_identifier,
            A,
            B,
            3,
            signature_B,
            signature_A,
        ).call()
    with pytest.raises(TransactionFailed):
        token_network_test.functions.verifyWithdrawSignaturesPublic(
            channel_identifier,
            A,
            B,
            3,
            signature_A,
            fake_signature,
        ).call()
    with pytest.raises(TransactionFailed):
        token_network_test.functions.verifyWithdrawSignaturesPublic(
            channel_identifier,
            A,
            B,
            3,
            fake_signature,
            signature_B,
        ).call()
def test_recoverAddressFromBalanceProof(
        token_network_test,
        create_balance_proof,
        get_accounts,
):
    (A, B) = get_accounts(2)

    channel_identifier = fake_bytes(32)
    balance_proof = create_balance_proof(
        channel_identifier,
        A,
        other_token_network=token_network_test,
    )

    balance_proof_wrong_token_network = create_balance_proof(
        channel_identifier,
        A,
    )

    balance_proof_other_signer = create_balance_proof(
        channel_identifier,
        A,
        signer=B,
        other_token_network=token_network_test,
    )

    assert A == token_network_test.functions.recoverAddressFromBalanceProofPublic(
        channel_identifier, *balance_proof).call()

    assert B == token_network_test.functions.recoverAddressFromBalanceProofPublic(
        channel_identifier,
        *balance_proof_other_signer,
    ).call()

    assert A != token_network_test.functions.recoverAddressFromBalanceProofPublic(
        channel_identifier, *balance_proof_wrong_token_network).call()
def test_recoverAddressFromCooperativeSettleSignature(
        token_network_test,
        create_cooperative_settle_signatures,
        get_accounts,
):
    (A, B) = get_accounts(2)
    channel_identifier = fake_bytes(32)
    fake_signature = fake_bytes(64)

    (signature_A, signature_B) = create_cooperative_settle_signatures(
        [A, B],
        channel_identifier,
        A,
        0,
        B,
        0,
        other_token_network=token_network_test,
    )
    assert A == token_network_test.functions.recoverAddressFromCooperativeSettleSignaturePublic(
        channel_identifier,
        A,
        0,
        B,
        0,
        signature_A,
    ).call()

    assert B == token_network_test.functions.recoverAddressFromCooperativeSettleSignaturePublic(
        channel_identifier,
        A,
        0,
        B,
        0,
        signature_B,
    ).call()

    assert B != token_network_test.functions.recoverAddressFromCooperativeSettleSignaturePublic(
        channel_identifier,
        A,
        0,
        B,
        0,
        signature_A,
    ).call()

    assert A != token_network_test.functions.recoverAddressFromCooperativeSettleSignaturePublic(
        channel_identifier,
        A,
        0,
        B,
        0,
        signature_B,
    ).call()

    with pytest.raises(TransactionFailed):
        token_network_test.functions.recoverAddressFromCooperativeSettleSignaturePublic(
            channel_identifier,
            A,
            0,
            B,
            0,
            fake_signature,
        ).call()
def test_recoverAddressFromWithdrawMessage(
        token_network_test,
        create_withdraw_signatures,
        create_channel_and_deposit,
        get_accounts,
):
    (A, B) = get_accounts(2)
    fake_signature = fake_bytes(64)
    deposit_A = 5
    deposit_B = 7
    withdraw_A = 3
    channel_identifier = create_channel_and_deposit(A, B, deposit_A, deposit_B)
    (signature_A, signature_B) = create_withdraw_signatures(
        [A, B],
        channel_identifier,
        A,
        withdraw_A,
        token_network_test.address,
    )

    recovered_address_A = token_network_test.functions.recoverAddressFromWithdrawMessagePublic(
        channel_identifier,
        A,
        withdraw_A,
        signature_A,
    ).call()
    assert recovered_address_A == A

    recovered_address_B = token_network_test.functions.recoverAddressFromWithdrawMessagePublic(
        channel_identifier,
        A,
        withdraw_A,
        signature_B,
    ).call()
    assert recovered_address_B == B

    with pytest.raises(TransactionFailed):
        token_network_test.functions.recoverAddressFromWithdrawMessagePublic(
            channel_identifier,
            A,
            withdraw_A,
            fake_signature,
        ).call()

    wrong_participant = token_network_test.functions.recoverAddressFromWithdrawMessagePublic(
        channel_identifier,
        B,
        withdraw_A,
        signature_A,
    ).call()
    assert recovered_address_A != wrong_participant

    wrong_withdraw_value = token_network_test.functions.recoverAddressFromWithdrawMessagePublic(
        channel_identifier,
        A,
        1,
        signature_A,
    ).call()

    assert recovered_address_A != wrong_withdraw_value

    wrong_signature = token_network_test.functions.recoverAddressFromWithdrawMessagePublic(
        channel_identifier,
        A,
        withdraw_A,
        signature_B,
    ).call()

    assert recovered_address_A != wrong_signature