Ejemplo n.º 1
0
def test_validate_valid_nonce_increase(each_identity, delegate, accounts):
    to = accounts[2]
    value = 1000

    meta_transaction1 = each_identity.filled_and_signed_meta_transaction(
        MetaTransaction(to=to, value=value, nonce=1))
    meta_transaction2 = each_identity.filled_and_signed_meta_transaction(
        MetaTransaction(to=to, value=value, nonce=2))

    delegate.send_signed_meta_transaction(meta_transaction1)

    assert delegate.validate_meta_transaction(meta_transaction2)
Ejemplo n.º 2
0
def test_next_nonce(each_identity, delegate, accounts):
    to = accounts[2]
    value = 1000

    meta_transaction1 = each_identity.filled_and_signed_meta_transaction(
        MetaTransaction(to=to, value=value, nonce=1))
    meta_transaction2 = each_identity.filled_and_signed_meta_transaction(
        MetaTransaction(to=to, value=value, nonce=2))

    delegate.send_signed_meta_transaction(meta_transaction1)
    delegate.send_signed_meta_transaction(meta_transaction2)

    assert delegate.get_next_nonce(each_identity.address) == 3
Ejemplo n.º 3
0
def test_delegated_transaction_invalid_nonce(identity, delegate, accounts):
    to = accounts[2]
    value = 1000

    meta_transaction1 = identity.filled_and_signed_meta_transaction(
        MetaTransaction(to=to, value=value, nonce=1))
    meta_transaction2 = identity.filled_and_signed_meta_transaction(
        MetaTransaction(to=to, value=value, nonce=1))

    delegate.send_signed_meta_transaction(meta_transaction1)

    with pytest.raises(InvalidMetaTransactionException):
        delegate.send_signed_meta_transaction(meta_transaction2)
Ejemplo n.º 4
0
def test_delegated_transaction_nonce_gap_fails(each_identity, delegate, web3,
                                               accounts):
    to = accounts[2]
    value = 1000

    meta_transaction1 = each_identity.filled_and_signed_meta_transaction(
        MetaTransaction(to=to, value=value, nonce=1))
    meta_transaction2 = each_identity.filled_and_signed_meta_transaction(
        MetaTransaction(to=to, value=value, nonce=3))

    delegate.send_signed_meta_transaction(meta_transaction1)

    with pytest.raises(TransactionFailed):
        delegate.send_signed_meta_transaction(meta_transaction2)
Ejemplo n.º 5
0
def test_gas_pricing(delegate, delegate_config, gas_price_config, gas_price):
    config = dict(**delegate_config)
    config.update(gas_price_config)

    delegate.config = config

    assert delegate._calculate_gas_price(MetaTransaction()) == gas_price
Ejemplo n.º 6
0
def test_delegated_transaction_wrong_from(each_identity_contract,
                                          delegate_address, accounts,
                                          owner_key, chain_id):
    from_ = accounts[3]
    to = accounts[2]
    value = 1000

    meta_transaction = MetaTransaction(from_=from_,
                                       to=to,
                                       value=value,
                                       nonce=0,
                                       chain_id=chain_id).signed(owner_key)

    with pytest.raises(TransactionFailed):
        each_identity_contract.functions.executeTransaction(
            meta_transaction.to,
            meta_transaction.value,
            meta_transaction.data,
            meta_transaction.base_fee,
            meta_transaction.gas_price,
            meta_transaction.gas_limit,
            meta_transaction.fee_recipient,
            meta_transaction.currency_network_of_fees,
            meta_transaction.nonce,
            meta_transaction.time_limit,
            meta_transaction.operation_type.value,
            meta_transaction.signature,
        ).transact({"from": delegate_address})
Ejemplo n.º 7
0
def test_meta_transaction_create_contract_fails(each_identity, delegate,
                                                non_payable_contract_inticode,
                                                web3, operation_type):
    """Test that the status in the event is False when deployment fails"""
    # To make the deployment fail we deploy a contract whose constructor is not payable
    # and transfer it eth during deployment
    deployed_contract_balance = 123
    meta_transaction = MetaTransaction(
        operation_type=operation_type,
        data=non_payable_contract_inticode,
        value=deployed_contract_balance,
    )
    meta_transaction = each_identity.filled_and_signed_meta_transaction(
        meta_transaction)
    tx_id = delegate.send_signed_meta_transaction(meta_transaction)

    execution_event = each_identity.contract.events.TransactionExecution.createFilter(
        fromBlock=0).get_all_entries()[0]["args"]

    # assert that the create failed
    assert get_transaction_status(web3, tx_id)
    assert execution_event["hash"] == meta_transaction.hash
    assert execution_event["status"] is False

    deploy_events = each_identity.contract.events.ContractDeployment.createFilter(
        fromBlock=0).get_all_entries()
    assert len(deploy_events) == 0
Ejemplo n.º 8
0
def test_get_not_found_meta_transaction_status(each_identity, delegate):

    meta_transaction = each_identity.filled_and_signed_meta_transaction(
        MetaTransaction(to=each_identity.address))

    meta_tx_status = delegate.get_meta_transaction_status(
        each_identity.address, meta_transaction.hash)
    assert meta_tx_status == MetaTransactionStatus.NOT_FOUND
Ejemplo n.º 9
0
def test_validate_valid_transfer(each_identity, delegate, accounts):
    to = accounts[2]
    value = 1000

    meta_transaction = each_identity.filled_and_signed_meta_transaction(
        MetaTransaction(to=to, value=value))

    assert delegate.validate_meta_transaction(meta_transaction)
Ejemplo n.º 10
0
def test_delegated_transaction_nonce_increase(each_identity, delegate, web3,
                                              accounts):
    to = accounts[2]
    value = 1000

    balance_before = web3.eth.getBalance(to)

    meta_transaction1 = each_identity.filled_and_signed_meta_transaction(
        MetaTransaction(to=to, value=value, nonce=1))
    meta_transaction2 = each_identity.filled_and_signed_meta_transaction(
        MetaTransaction(to=to, value=value, nonce=2))

    delegate.send_signed_meta_transaction(meta_transaction1)
    delegate.send_signed_meta_transaction(meta_transaction2)

    balance_after = web3.eth.getBalance(to)

    assert balance_after - balance_before == value + value
Ejemplo n.º 11
0
def test_estimate_gas(each_identity, delegate, accounts):
    to = accounts[2]
    value = 1000

    meta_transaction = each_identity.filled_and_signed_meta_transaction(
        MetaTransaction(to=to, value=value))

    assert isinstance(
        delegate.estimate_gas_signed_meta_transaction(meta_transaction), int)
Ejemplo n.º 12
0
def test_get_successful_meta_transaction_status(each_identity, delegate):

    meta_transaction = each_identity.filled_and_signed_meta_transaction(
        MetaTransaction(to=each_identity.address))

    delegate.send_signed_meta_transaction(meta_transaction)

    meta_tx_status = delegate.get_meta_transaction_status(
        each_identity.address, meta_transaction.hash)
    assert meta_tx_status == MetaTransactionStatus.SUCCESS
Ejemplo n.º 13
0
def test_delegated_transaction_invalid_signature(identity, delegate, accounts,
                                                 account_keys):
    to = accounts[2]
    value = 1000

    meta_transaction = MetaTransaction(from_=identity.address,
                                       to=to,
                                       value=value,
                                       nonce=0).signed(account_keys[3])

    with pytest.raises(InvalidMetaTransactionException):
        delegate.send_signed_meta_transaction(meta_transaction)
Ejemplo n.º 14
0
def test_delegated_transaction_same_tx_fails(each_identity, delegate, accounts,
                                             web3):
    to = accounts[2]
    value = 1000

    meta_transaction = MetaTransaction(to=to, value=value)
    meta_transaction = each_identity.filled_and_signed_meta_transaction(
        meta_transaction)
    delegate.send_signed_meta_transaction(meta_transaction)

    with pytest.raises(TransactionFailed):
        delegate.send_signed_meta_transaction(meta_transaction)
Ejemplo n.º 15
0
def test_meta_transaction_gas_limit(each_identity, delegate, web3, accounts):
    to = accounts[2]
    value = 1000
    gas_limit = 456

    meta_transaction = MetaTransaction(to=to, value=value, gas_limit=gas_limit)

    meta_transaction = each_identity.filled_and_signed_meta_transaction(
        meta_transaction)

    with pytest.raises(TransactionFailed):
        delegate.send_signed_meta_transaction(meta_transaction)
Ejemplo n.º 16
0
def test_validate_wrong_signature(each_identity, delegate, accounts,
                                  account_keys, chain_id):
    to = accounts[2]
    value = 1000

    meta_transaction = MetaTransaction(from_=each_identity.address,
                                       to=to,
                                       value=value,
                                       nonce=0,
                                       chain_id=chain_id).signed(
                                           account_keys[3])

    assert not delegate.validate_meta_transaction(meta_transaction)
Ejemplo n.º 17
0
def test_validate_from_no_code(delegate, accounts, owner_key, chain_id):
    from_ = accounts[3]
    to = accounts[2]
    value = 1000

    meta_transaction = MetaTransaction(from_=from_,
                                       to=to,
                                       value=value,
                                       nonce=0,
                                       chain_id=chain_id).signed(owner_key)

    with pytest.raises(UnexpectedIdentityContractException):
        delegate.validate_meta_transaction(meta_transaction)
Ejemplo n.º 18
0
def test_delegated_transaction_invalid_identity_contract(
        delegate, accounts, account_keys):
    from_ = accounts[1]
    to = accounts[2]
    value = 1000

    meta_transaction = MetaTransaction(from_=from_,
                                       to=to,
                                       value=value,
                                       nonce=0).signed(account_keys[3])

    with pytest.raises(InvalidIdentityContractException):
        delegate.send_signed_meta_transaction(meta_transaction)
Ejemplo n.º 19
0
def test_validate_from_wrong_contract(delegate, accounts, owner_key,
                                      currency_network_contract, chain_id):
    from_ = currency_network_contract.address
    to = accounts[2]
    value = 1000

    meta_transaction = MetaTransaction(from_=from_,
                                       to=to,
                                       value=value,
                                       nonce=0,
                                       chain_id=chain_id).signed(owner_key)

    with pytest.raises(UnexpectedIdentityContractException):
        delegate.validate_meta_transaction(meta_transaction)
Ejemplo n.º 20
0
def test_meta_transaction_no_limit_valid(each_identity, delegate, web3,
                                         accounts):
    to = accounts[2]
    value = 1000
    time_limit = 0

    meta_transaction = MetaTransaction(to=to,
                                       value=value,
                                       time_limit=time_limit)

    meta_transaction = each_identity.filled_and_signed_meta_transaction(
        meta_transaction)
    tx_id = delegate.send_signed_meta_transaction(meta_transaction)

    assert get_transaction_status(web3, tx_id)
Ejemplo n.º 21
0
def test_delegated_transaction_wrong_signature(each_identity, delegate,
                                               accounts, account_keys, web3,
                                               chain_id):
    to = accounts[2]
    value = 1000

    meta_transaction = MetaTransaction(from_=each_identity.address,
                                       to=to,
                                       value=value,
                                       nonce=0,
                                       chain_id=chain_id).signed(
                                           account_keys[3])

    with pytest.raises(TransactionFailed):
        delegate.send_signed_meta_transaction(meta_transaction)
Ejemplo n.º 22
0
def test_meta_transaction_time_limit_invalid(each_identity, delegate, web3,
                                             accounts):
    to = accounts[2]
    value = 1000
    time_limit = web3.eth.getBlock("latest").timestamp - 1

    meta_transaction = MetaTransaction(to=to,
                                       value=value,
                                       time_limit=time_limit)

    meta_transaction = each_identity.filled_and_signed_meta_transaction(
        meta_transaction)

    with pytest.raises(TransactionFailed):
        delegate.send_signed_meta_transaction(meta_transaction)
Ejemplo n.º 23
0
def test_next_nonce(delegate, identity_contract, accounts, owner_key):

    source = identity_contract.address
    destination = accounts[3]

    meta_transaction = MetaTransaction(from_=source,
                                       to=destination,
                                       value=123,
                                       nonce=delegate.calc_next_nonce(source))
    signed_meta_transaction = meta_transaction.signed(owner_key)

    assert delegate.calc_next_nonce(source) == 1
    delegate.send_signed_meta_transaction(signed_meta_transaction)
    assert delegate.calc_next_nonce(source) == 2

    meta_transaction = MetaTransaction(from_=source,
                                       to=destination,
                                       value=123,
                                       nonce=delegate.calc_next_nonce(source))
    signed_meta_transaction = meta_transaction.signed(owner_key)

    assert delegate.calc_next_nonce(source) == 2
    delegate.send_signed_meta_transaction(signed_meta_transaction)
    assert delegate.calc_next_nonce(source) == 3
Ejemplo n.º 24
0
def test_delegated_transaction_transfer(web3, each_identity, delegate,
                                        accounts):
    to = accounts[2]
    value = 1000

    meta_transaction = MetaTransaction(to=to, value=value)

    balance_before = web3.eth.getBalance(to)
    meta_transaction = each_identity.filled_and_signed_meta_transaction(
        meta_transaction)
    tx_id = delegate.send_signed_meta_transaction(meta_transaction)

    balance_after = web3.eth.getBalance(to)

    assert get_transaction_status(web3, tx_id)
    assert balance_after - balance_before == value
Ejemplo n.º 25
0
def test_revoke_meta_transaction_nonce(each_identity, delegate, test_contract):
    """Test that we can revoke a meta-tx that uses nonce anti-replay mechanism via using the nonce"""
    to = test_contract.address
    argument = 10
    function_call = test_contract.functions.testFunction(argument)

    meta_transaction = each_identity.filled_and_signed_meta_transaction(
        MetaTransaction.from_function_call(function_call, to=to))

    revoke_meta_transaction = each_identity.filled_and_signed_meta_transaction(
        MetaTransaction(to=each_identity.address,
                        nonce=meta_transaction.nonce))
    delegate.send_signed_meta_transaction(revoke_meta_transaction)

    assert not delegate.validate_meta_transaction(meta_transaction)
    with pytest.raises(TransactionFailed):
        delegate.send_signed_meta_transaction(meta_transaction)
Ejemplo n.º 26
0
def test_meta_transaction_create_contract(
    each_identity,
    delegate,
    test_contract_initcode,
    test_contract_abi,
    web3,
    operation_type,
):
    deployed_contract_balance = 123
    meta_transaction = MetaTransaction(
        operation_type=operation_type,
        data=test_contract_initcode,
        value=deployed_contract_balance,
    )
    meta_transaction = each_identity.filled_and_signed_meta_transaction(
        meta_transaction)
    # It seems that web3 fails to estimate gas for deployment transactions
    tx_id = delegate.send_signed_meta_transaction(
        meta_transaction, transaction_options={"gas": 1_000_000})

    execution_event = each_identity.contract.events.TransactionExecution.createFilter(
        fromBlock=0).get_all_entries()[0]["args"]

    # assert that the create was successful
    assert get_transaction_status(web3, tx_id)
    assert execution_event["hash"] == meta_transaction.hash
    assert execution_event["status"] is True

    deploy_event = each_identity.contract.events.ContractDeployment.createFilter(
        fromBlock=0).get_all_entries()[0]["args"]
    deployed_contract = web3.eth.contract(deploy_event["deployed"],
                                          abi=test_contract_abi)

    # check that the contract was indeed deployed
    assert deployed_contract.functions.testPublicValue().call() == 123456
    assert web3.eth.getBalance(
        deployed_contract.address) == deployed_contract_balance

    if operation_type == MetaTransaction.OperationType.CREATE2:
        # check that create2 was used and we could pre-compute the deployed address
        create_2_address = build_create2_address(each_identity.address,
                                                 test_contract_initcode)
        assert deployed_contract.address == create_2_address
Ejemplo n.º 27
0
def test_deploy_identity(
    currency_network,
    delegate,
    accounts,
    proxy_factory,
    owner_key,
    identity_implementation,
    signature_of_owner_on_implementation,
    web3,
):
    """
    Tests that the deployment of an identity contract by the relay server delegate works
    by using it to execute a meta-transaction
    """

    identity_contract_address = delegate.deploy_identity(
        proxy_factory.address,
        identity_implementation.address,
        signature_of_owner_on_implementation,
    )
    web3.eth.sendTransaction({
        "to": identity_contract_address,
        "from": accounts[0],
        "value": 1_000_000
    })

    destination = accounts[3]
    balance_before = web3.eth.getBalance(destination)

    meta_transaction = MetaTransaction(
        to=destination,
        value=1,
        currency_network_of_fees=currency_network.address,
        chain_id=web3.eth.chainId,
    )
    signed_meta_transaction = attr.evolve(meta_transaction,
                                          from_=identity_contract_address,
                                          nonce=0).signed(owner_key)
    delegate.send_signed_meta_transaction(signed_meta_transaction)

    balance_after = web3.eth.getBalance(destination)

    assert balance_after - balance_before == 1
Ejemplo n.º 28
0
def test_meta_transaction_signature_corresponds_to_clientlib_signature(
        each_identity, owner_key):
    # Tests that the signature obtained from the contracts and the clientlib implementation match,
    # If this test needs to be changed, the corresponding test in the clientlib should also be changed
    # See: clientlib/tests/unit/IdentityWallet.test.ts: 'should sign meta-transaction'
    from_ = "0xF2E246BB76DF876Cef8b38ae84130F4F55De395b"
    to = "0x51a240271AB8AB9f9a21C82d9a85396b704E164d"
    chain_id = 0
    value = 0
    data = "0x46432830000000000000000000000000000000000000000000000000000000000000000a"
    base_fee = 1
    gas_price = 123
    gas_limit = 456
    fee_recipient = "0xF2E246BB76DF876Cef8b38ae84130F4F55De395b"
    currency_network_of_fees = "0x51a240271AB8AB9f9a21C82d9a85396b704E164d"
    nonce = 1
    time_limit = 123456

    meta_transaction = MetaTransaction(
        from_=from_,
        chain_id=chain_id,
        to=to,
        value=value,
        data=data,
        base_fee=base_fee,
        gas_price=gas_price,
        gas_limit=gas_limit,
        fee_recipient=fee_recipient,
        currency_network_of_fees=currency_network_of_fees,
        nonce=nonce,
        time_limit=time_limit,
    )

    signature = each_identity.signed_meta_transaction(
        meta_transaction).signature

    assert (
        str(owner_key) ==
        "0x0000000000000000000000000000000000000000000000000000000000000001")
    assert (signature.hex(
    ) == "639db755a4e0642c2ec76485cf623c58b635c54f9ce375088fad40a128779d7a060"
            "ceb63129eb9681216a844a0577184b1d3266fc6ac00fbbe23e72b592c33c200")
Ejemplo n.º 29
0
def test_set_delegate_transaction_params(web3, each_identity, delegate,
                                         accounts):

    meta_transaction = each_identity.filled_and_signed_meta_transaction(
        MetaTransaction(to=each_identity.address))

    transaction_options = {
        "gasPrice": 10000,
        "gas": 1000000,
        "from": accounts[2]
    }

    assert transaction_options["from"] != delegate.delegate_address
    tx_hash = delegate.send_signed_meta_transaction(
        meta_transaction, transaction_options=transaction_options.copy())

    tx = web3.eth.getTransaction(tx_hash)

    assert tx["from"] == transaction_options["from"]
    assert tx["gas"] == transaction_options["gas"]
    assert tx["gasPrice"] == transaction_options["gasPrice"]
Ejemplo n.º 30
0
def test_delegate_meta_transaction(delegate, identity, web3, accounts,
                                   owner_key):
    """"
    Tests that a transaction is sent by the delegate upon receiving a meta-transaction.
    """

    meta_transaction = MetaTransaction(
        from_=identity.address,
        to=accounts[2],
        value=123,
        data=(1234).to_bytes(10, byteorder="big"),
        nonce=1,
        extra_data=(123456789).to_bytes(10, byteorder="big"),
    )

    signed_meta_transaction = meta_transaction.signed(owner_key)

    tx_hash = delegate.send_signed_meta_transaction(signed_meta_transaction)
    tx = web3.eth.getTransaction(tx_hash)

    assert tx["from"] == web3.eth.coinbase
    assert HexBytes(tx["to"]) == identity.address