Пример #1
0
def test_get_handle_transaction_cosmwasm():
    """Test the get deploy transaction method."""
    cc2 = CosmosCrypto()
    cosmos_api = CosmosApi(**COSMOS_TESTNET_CONFIG)
    handle_msg = "handle_msg"
    sender_address = cc2.address
    contract_address = "contract_address"
    tx_fee = 1
    amount = 10
    handle_transaction = cosmos_api.get_handle_transaction(
        sender_address, contract_address, handle_msg, amount, tx_fee)

    assert type(handle_transaction) == dict and len(handle_transaction) == 6
    assert "account_number" in handle_transaction
    assert "chain_id" in handle_transaction
    assert "fee" in handle_transaction and handle_transaction["fee"] == {
        "amount": [{
            "denom": "atestfet",
            "amount": "{}".format(tx_fee)
        }],
        "gas": "80000",
    }
    assert "memo" in handle_transaction
    assert "msgs" in handle_transaction and len(
        handle_transaction["msgs"]) == 1
    msg = handle_transaction["msgs"][0]
    assert "type" in msg and msg["type"] == "wasm/execute"
    assert ("value" in msg and msg["value"]["sender"] == sender_address
            and msg["value"]["contract"] == contract_address
            and msg["value"]["msg"] == handle_msg
            and msg["value"]["sent_funds"] == [{
                "denom": "atestfet",
                "amount": str(amount)
            }])
    assert "sequence" in handle_transaction
Пример #2
0
def test_transfer():
    """Test transfer of wealth."""
    def try_transact(cc1, cc2, amount) -> str:
        attempts = 0
        while attempts < 3:
            fee = 1000
            tx_digest = cosmos_api.transfer(cc1, cc2.address, amount, fee)
            assert tx_digest is not None, "Failed to submit transfer!"
            not_settled = True
            elapsed_time = 0
            while not_settled and elapsed_time < 20:
                elapsed_time += 2
                time.sleep(2)
                is_settled = cosmos_api.is_transaction_settled(tx_digest)
                not_settled = not is_settled
            is_settled = cosmos_api.is_transaction_settled(tx_digest)
            if is_settled:
                attempts = 3
            else:
                attempts += 1
        assert is_settled, "Failed to complete tx on 3 attempts!"
        return tx_digest

    cosmos_api = CosmosApi(**COSMOS_TESTNET_CONFIG)
    cc1 = CosmosCrypto(private_key_path=COSMOS_PRIVATE_KEY_PATH)
    cc2 = CosmosCrypto()
    amount = 10000
    tx_digest = try_transact(cc1, cc2, amount)
    # TODO remove requirement for "" tx nonce stub
    is_valid = cosmos_api.is_transaction_valid(tx_digest, cc2.address,
                                               cc1.address, "", amount)
    assert is_valid, "Failed to settle tx correctly!"
Пример #3
0
def test_get_deploy_transaction_cosmwasm():
    """Test the get deploy transaction method."""
    cc2 = CosmosCrypto()
    cosmos_api = CosmosApi(**COSMOS_TESTNET_CONFIG)

    contract_interface = {"wasm_byte_code": b""}
    deployer_address = cc2.address
    deploy_transaction = cosmos_api.get_deploy_transaction(
        contract_interface, deployer_address)

    assert type(deploy_transaction) == dict and len(deploy_transaction) == 6
    assert "account_number" in deploy_transaction
    assert "chain_id" in deploy_transaction
    assert "fee" in deploy_transaction and deploy_transaction["fee"] == {
        "amount": [{
            "amount": "0",
            "denom": "atestfet"
        }],
        "gas": "80000",
    }
    assert "memo" in deploy_transaction
    assert "msgs" in deploy_transaction and len(
        deploy_transaction["msgs"]) == 1
    msg = deploy_transaction["msgs"][0]
    assert "type" in msg and msg["type"] == "wasm/store-code"
    assert ("value" in msg and msg["value"]["sender"] == deployer_address
            and msg["value"]["wasm_byte_code"]
            == contract_interface["wasm_byte_code"])
    assert "sequence" in deploy_transaction
Пример #4
0
def test_format_default():
    """Test if default CosmosSDK transaction is correctly formated."""
    account = CosmosCrypto()
    cc2 = CosmosCrypto()
    cosmos_api = CosmosApi(**COSMOS_TESTNET_CONFIG)

    amount = 10000

    transfer_transaction = cosmos_api.get_transfer_transaction(
        sender_address=account.address,
        destination_address=cc2.address,
        amount=amount,
        tx_fee=1000,
        tx_nonce="something",
    )

    signed_transaction = cc2.sign_transaction(transfer_transaction)

    assert "tx" in signed_transaction
    assert "signatures" in signed_transaction["tx"]
    assert len(signed_transaction["tx"]["signatures"]) == 1

    assert "pub_key" in signed_transaction["tx"]["signatures"][0]
    assert "value" in signed_transaction["tx"]["signatures"][0]["pub_key"]
    base64_pbk = signed_transaction["tx"]["signatures"][0]["pub_key"]["value"]

    assert "signature" in signed_transaction["tx"]["signatures"][0]
    signature = signed_transaction["tx"]["signatures"][0]["signature"]

    default_formated_transaction = cc2.format_default_transaction(
        transfer_transaction, signature, base64_pbk)

    # Compare default formatted transaction with signed transaction
    assert signed_transaction == default_formated_transaction
Пример #5
0
def test_get_balance():
    """Test the balance is zero for a new account."""
    cosmos_api = CosmosApi(**COSMOS_TESTNET_CONFIG)
    cc = CosmosCrypto()
    balance = cosmos_api.get_balance(cc.address)
    assert balance == 0, "New account has a positive balance."
    balance = get_wealth(cc.address)
    assert balance > 0, "Existing account has no balance."
Пример #6
0
def test_get_balance():
    """Test the balance is zero for a new account."""
    cosmos_api = CosmosApi(**COSMOS_TESTNET_CONFIG)
    cc = CosmosCrypto()
    balance = cosmos_api.get_balance(cc.address)
    assert balance == 0, "New account has a positive balance."
    cc = CosmosCrypto(private_key_path=COSMOS_PRIVATE_KEY_PATH)
    balance = cosmos_api.get_balance(cc.address)
    assert balance > 0, "Existing account has no balance."
Пример #7
0
def test_send_signed_transaction_wasm_transaction():
    """Test the send_signed_transaction method for a wasm transaction."""
    cosmos_api = CosmosApi(**COSMOS_TESTNET_CONFIG)
    tx_signed = {"value": {"msg": [{"type": "wasm/store-code"}]}}
    with mock.patch.object(cosmos_api,
                           "try_execute_wasm_transaction",
                           return_value="digest"):
        result = cosmos_api.send_signed_transaction(tx_signed=tx_signed)
    assert result == "digest"
Пример #8
0
def test_try_execute_wasm_transaction():
    """Test the execute wasm query method."""
    cosmos_api = CosmosApi(**COSMOS_TESTNET_CONFIG)
    process_mock = mock.Mock()
    output = "output".encode("ascii")
    attrs = {"communicate.return_value": (output, "error")}
    process_mock.configure_mock(**attrs)
    with mock.patch("subprocess.Popen", return_value=process_mock):
        result = cosmos_api.try_execute_wasm_transaction(
            tx_signed="signed_transaction")
    assert result == output.decode("ascii")
Пример #9
0
def get_wealth(address: str):
    """Get wealth for test."""
    cosmos_api = CosmosApi(**COSMOS_TESTNET_CONFIG)
    CosmosFaucetApi().get_wealth(address)
    balance = 0
    timeout = 0
    while timeout < 40 and balance == 0:
        time.sleep(1)
        timeout += 1
        _balance = cosmos_api.get_balance(address)
        balance = _balance if _balance is not None else 0
    return balance
Пример #10
0
def test_sign_and_recover_message_public_key():
    """Test the signing and the recovery function for the eth_crypto."""
    account = CosmosCrypto(COSMOS_PRIVATE_KEY_PATH)
    sign_bytes = account.sign_message(message=b"hello")
    assert len(sign_bytes) > 0, "The len(signature) must not be 0"
    recovered_public_keys = CosmosApi.recover_public_keys_from_message(
        message=b"hello", signature=sign_bytes)
    assert len(
        recovered_public_keys) == 2, "Wrong number of public keys recovered."
    assert (CosmosApi.get_address_from_public_key(
        recovered_public_keys[0]) == account.address
            ), "Failed to recover the correct address."
Пример #11
0
def test_generate_nonce():
    """Test generate nonce."""
    nonce = CosmosApi.generate_tx_nonce(
        seller="some_seller_addr", client="some_buyer_addr"
    )
    assert len(nonce) > 0 and int(
        nonce, 16
    ), "The len(nonce) must not be 0 and must be hex"
Пример #12
0
def test_sign_and_recover_message():
    """Test the signing and the recovery of a message."""
    account = CosmosCrypto(COSMOS_PRIVATE_KEY_PATH)
    sign_bytes = account.sign_message(message=b"hello")
    assert len(sign_bytes) > 0, "The len(signature) must not be 0"
    recovered_addresses = CosmosApi.recover_message(message=b"hello",
                                                    signature=sign_bytes)
    assert (account.address
            in recovered_addresses), "Failed to recover the correct address."
Пример #13
0
def test_construct_sign_and_submit_transfer_transaction():
    """Test the construction, signing and submitting of a transfer transaction."""
    account = CosmosCrypto()
    balance = get_wealth(account.address)
    assert balance > 0, "Failed to fund account."
    cc2 = CosmosCrypto()
    cosmos_api = CosmosApi(**COSMOS_TESTNET_CONFIG)

    amount = 10000
    assert amount < balance, "Not enough funds."
    transfer_transaction = cosmos_api.get_transfer_transaction(
        sender_address=account.address,
        destination_address=cc2.address,
        amount=amount,
        tx_fee=1000,
        tx_nonce="something",
    )
    assert (isinstance(transfer_transaction, dict)
            and len(transfer_transaction)
            == 6), "Incorrect transfer_transaction constructed."

    signed_transaction = account.sign_transaction(transfer_transaction)
    assert (isinstance(signed_transaction, dict)
            and len(signed_transaction["tx"]) == 4
            and isinstance(signed_transaction["tx"]["signatures"],
                           list)), "Incorrect signed_transaction constructed."

    transaction_digest = cosmos_api.send_signed_transaction(signed_transaction)
    assert transaction_digest is not None, "Failed to submit transfer transaction!"

    not_settled = True
    elapsed_time = 0
    while not_settled and elapsed_time < 20:
        elapsed_time += 1
        time.sleep(2)
        transaction_receipt = cosmos_api.get_transaction_receipt(
            transaction_digest)
        if transaction_receipt is None:
            continue
        is_settled = cosmos_api.is_transaction_settled(transaction_receipt)
        not_settled = not is_settled
    assert transaction_receipt is not None, "Failed to retrieve transaction receipt."
    assert is_settled, "Failed to verify tx!"

    tx = cosmos_api.get_transaction(transaction_digest)
    is_valid = cosmos_api.is_transaction_valid(tx, cc2.address,
                                               account.address, "", amount)
    assert is_valid, "Failed to settle tx correctly!"
    assert tx == transaction_receipt, "Should be same!"
Пример #14
0
def test_get_init_transaction_cosmwasm():
    """Test the get deploy transaction method."""
    cc2 = CosmosCrypto()
    cosmos_api = CosmosApi(**COSMOS_TESTNET_CONFIG)
    init_msg = "init_msg"
    code_id = 1
    deployer_address = cc2.address
    tx_fee = 1
    amount = 10
    deploy_transaction = cosmos_api.get_init_transaction(
        deployer_address, code_id, init_msg, amount, tx_fee)

    assert type(deploy_transaction) == dict and len(deploy_transaction) == 6
    assert "account_number" in deploy_transaction
    assert "chain_id" in deploy_transaction
    assert "fee" in deploy_transaction and deploy_transaction["fee"] == {
        "amount": [{
            "denom": "atestfet",
            "amount": "{}".format(tx_fee)
        }],
        "gas": "80000",
    }
    assert "memo" in deploy_transaction
    assert "msgs" in deploy_transaction and len(
        deploy_transaction["msgs"]) == 1
    msg = deploy_transaction["msgs"][0]
    assert "type" in msg and msg["type"] == "wasm/instantiate"
    assert ("value" in msg and msg["value"]["sender"] == deployer_address
            and msg["value"]["code_id"] == str(code_id)
            and msg["value"]["label"] == ""
            and msg["value"]["init_msg"] == init_msg
            and msg["value"]["init_funds"] == [{
                "denom": "atestfet",
                "amount": str(amount)
            }])
    assert "sequence" in deploy_transaction
Пример #15
0
def _instantiate_api(identifier: str, config: Dict[str, Union[str, int]]) -> LedgerApi:
    """
    Instantiate a ledger api.

    :param identifier: the ledger identifier
    :param config: the config of the api
    :return: the ledger api
    """
    retry = 0
    is_connected = False
    while retry < MAX_CONNECTION_RETRY:
        if identifier not in SUPPORTED_LEDGER_APIS:
            raise ValueError(
                "Unsupported identifier {} in ledger apis.".format(identifier)
            )
        try:
            if identifier == FetchAIApi.identifier:
                api = FetchAIApi(**config)  # type: LedgerApi
            elif identifier == EthereumApi.identifier:
                api = EthereumApi(
                    cast(str, config["address"]), cast(str, config["gas_price"])
                )
            elif identifier == CosmosApi.identifier:
                api = CosmosApi(**config)
            is_connected = True
            break
        except Exception:  # pragma: no cover
            retry += 1
            logger.debug(
                "Connection attempt {} to {} ledger with provided config {} failed.".format(
                    retry, identifier, config
                )
            )
            time.sleep(0.5)
    if not is_connected:  # pragma: no cover
        logger.error(
            "Cannot connect to {} ledger with provided config {} after {} attemps. Giving up!".format(
                identifier, config, MAX_CONNECTION_RETRY
            )
        )
        sys.exit(1)
    return api
Пример #16
0
def test_cosmos_api_get_deploy_transaction(*args):
    """Test CosmosApi._get_deploy_transaction."""
    cosmos_api = CosmosApi()
    assert cosmos_api.get_deploy_transaction(*[Mock()] * 2) is None
Пример #17
0
def test_load_contract_interface():
    """Test the load_contract_interface method."""
    path = Path(ROOT_DIR, "tests", "data", "dummy_contract", "build",
                "some.wasm")
    result = CosmosApi.load_contract_interface(path)
    assert "wasm_byte_code" in result
Пример #18
0
def test_get_contract_instance():
    """Test the get contract instance method."""
    cosmos_api = CosmosApi(**COSMOS_TESTNET_CONFIG)
    assert cosmos_api.get_contract_instance("interface") is None
Пример #19
0
def test_api_none():
    """Test the "api" of the cryptoApi is none."""
    cosmos_api = CosmosApi(**COSMOS_TESTNET_CONFIG)
    assert cosmos_api.api is None, "The api property is not None."
Пример #20
0
def test_api_creation():
    """Test api instantiation."""
    assert CosmosApi(**COSMOS_TESTNET_CONFIG), "Failed to initialise the api"
Пример #21
0
def test_validate_address():
    """Test the is_valid_address functionality."""
    account = CosmosCrypto()
    assert CosmosApi.is_valid_address(account.address)
    assert not CosmosApi.is_valid_address(account.address + "wrong")
Пример #22
0
def test_cosmos_api_get_transfer_transaction(*args):
    """Test CosmosApi.get_transfer_transaction."""
    cosmos_api = CosmosApi()
    assert cosmos_api.get_transfer_transaction(*[Mock()] * 7) is None
Пример #23
0
def test_get_hash():
    """Test the get hash functionality."""
    expected_hash = "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
    hash_ = CosmosApi.get_hash(message=b"hello")
    assert expected_hash == hash_