Skip to content

Commit

Permalink
Merge pull request #270 from cheqd/DEV-720
Browse files Browse the repository at this point in the history
Get rid of circle dependencies from vdrtools
  • Loading branch information
Andrew Nikitin committed Feb 25, 2022
1 parent 54ec64f commit 5e01f29
Show file tree
Hide file tree
Showing 6 changed files with 299 additions and 420 deletions.
38 changes: 5 additions & 33 deletions .github/workflows/node-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ jobs:
journalctl -u service-name.service --no-pager
run-cosmos-tests-using-cli:
name: Run cosmos tests using CLI
name: Run cosmos and identity tests using CLI
runs-on: ubuntu-20.04
needs: [build-node-image, build-binary]
steps:
Expand Down Expand Up @@ -267,47 +267,19 @@ jobs:
working-directory: ./tests/e2e
run: |
pip3 install -r requirements.txt >> /dev/null
pip3 install --force-reinstall 'git+https://gitlab.com/evernym/verity/[email protected]#egg=vdr-tools&subdirectory=wrappers/python' -U
cp -R ${NODE_CONFIGS_BASE}/client/.cheqdnode /home/runner/
sudo chmod -R 777 /home/runner
- name: Run tests
- name: Run cosmos-related tests
run: |
export OP0_ADDRESS=$(cheqd-noded keys list --keyring-backend "test" | sed -nr 's/.*address: (.*?).*/\1/p' | sed -n 1p | sed 's/\r//g')
export OP1_ADDRESS=$(cheqd-noded keys list --keyring-backend "test" | sed -nr 's/.*address: (.*?).*/\1/p' | sed -n 2p | sed 's/\r//g')
pytest -v -rP tests/e2e/test_cosmos.py
run-identity-tests-using-vdr:
name: Run identity tests using VDR
runs-on: ubuntu-20.04
needs: build-testnet-image
steps:
- uses: actions/checkout@v2

- name: Download testnet image
uses: actions/download-artifact@v2
with:
name: cheqd-testnet-image.tar

- name: Load testnet image
run: docker load -i cheqd-testnet-image.tar

- name: Run test net container
run: |
docker run -d --rm -p "26657:26657" cheqd-testnet
bash tests/networks/tools/wait-for-chain.sh
- name: Set up test environment
working-directory: ./tests/e2e
- name: Run identity-related tests
run: |
sudo cp libindy.so /usr/lib/libindy.so
pip3 install -r requirements.txt >> /dev/null
pip3 install --force-reinstall 'git+https://gitlab.com/evernym/verity/[email protected]#egg=vdr-tools&subdirectory=wrappers/python' -U
- name: Run tests
run: |
export OP0_ADDRESS=""
export OP1_ADDRESS=""
export OP0_ADDRESS=$(cheqd-noded keys list --keyring-backend "test" | sed -nr 's/.*address: (.*?).*/\1/p' | sed -n 1p | sed 's/\r//g')
export OP1_ADDRESS=$(cheqd-noded keys list --keyring-backend "test" | sed -nr 's/.*address: (.*?).*/\1/p' | sed -n 2p | sed 's/\r//g')
pytest -v -rP tests/e2e/test_identity.py
run-identity-tests-using-cli:
Expand Down
File renamed without changes.
180 changes: 69 additions & 111 deletions tests/e2e/helpers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import sys
import os
import pexpect
Expand All @@ -7,13 +8,9 @@
import json
import time

from vdrtools import wallet
from vdrtools import cheqd_keys, cheqd_pool, cheqd_ledger
from vdrtools.error import CommonInvalidStructure

IMPLICIT_TIMEOUT = 30
ENCODING = "utf-8"
READ_BUFFER = 6000
READ_BUFFER = 60000

TEST_NET_NETWORK = "cheqd-testnet-2"
LOCAL_NET_NETWORK = "cheqd"
Expand All @@ -25,13 +22,14 @@
TEST_NET_DESTINATION_HTTP = f"{TEST_NET_NODE_HTTP} --chain-id 'cheqd-testnet-2'"
LOCAL_NET_DESTINATION = f"{LOCAL_NET_NODE_TCP} --chain-id 'cheqd'"
LOCAL_NET_DESTINATION_HTTP = f"{LOCAL_NET_NODE_HTTP} --chain-id 'cheqd'"
GAS_AMOUNT = 90000 # 70000 throws `out of gas` sometimes
GAS_PRICE = 25
TEST_NET_FEES = "--fees 5000000ncheq"
TEST_NET_GAS_X_GAS_PRICES = "--gas 90000 --gas-prices 25ncheq"
YES_FLAG = "-y"
KEYRING_BACKEND_TEST = "--keyring-backend test"
DENOM = "ncheq"
GAS_AMOUNT = 90000 # 70000 throws `out of gas` sometimes
GAS_PRICE = 25

TEST_NET_GAS_X_GAS_PRICES_INT = GAS_AMOUNT * GAS_PRICE
MAX_GAS_MAGIC_NUMBER = 1.3

Expand All @@ -45,6 +43,10 @@
LOCAL_RECEIVER_ADDRESS = os.environ["OP1_ADDRESS"]

CODE_0 = "\"code\":0"
CODE_5 = "\"code\":5"
CODE_11 = "\"code\":11"
CODE_1203 = "\"code\":1203"
CODE_1100 = "\"code\":1100"
CODE_0_DIGIT = 0


Expand All @@ -53,9 +55,18 @@ def random_string(length):


def run(command_base, command, params, expected_output):
# ToDo: Make it more clear.
# Quick hack for getting passing timouted transactions
timeout_str = "Error(.*?)timed out waiting for tx to be included in a block"
cli = pexpect.spawn(f"{command_base} {command} {params}", encoding=ENCODING, timeout=IMPLICIT_TIMEOUT, maxread=READ_BUFFER)
cli.logfile = sys.stdout
cli.expect(expected_output)
try:
cli.expect(expected_output)
except pexpect.exceptions.EOF as err:
if re.search(timeout_str, cli.before):
get_balance(LOCAL_SENDER_ADDRESS, LOCAL_NET_DESTINATION)
return cli
raise err

return cli

Expand All @@ -72,13 +83,16 @@ def get_balance(address, network_destination):
return balance


async def get_balance_vdr(pool_alias, address):
request = await cheqd_ledger.bank.build_query_balance(address, DENOM)
res = await cheqd_pool.abci_query(pool_alias, request)
res = await cheqd_ledger.bank.parse_query_balance_resp(res)
sender_balance = json.loads(res)["balance"]["amount"]
def json_loads(s_to_load: str) -> dict:
s = copy.copy(s_to_load)
s = s.replace("\\", "")
s = s.replace("\"[", "[")
s = s.replace("]\"", "]")
return json.loads(s)


return sender_balance
def get_gas_extimation(s_to_search: str) -> int:
return int(re.findall(r"\d+", s_to_search)[0])


def send_with_note(note):
Expand All @@ -93,71 +107,6 @@ def send_with_note(note):
return tx_hash, note


async def send_tx_helper(pool_alias, wallet_handle, key_alias, public_key, sender_address, msg, memo):
account_number, sequence_number = await get_base_account_number_and_sequence(pool_alias, sender_address)
timeout_height = await get_timeout_height(pool_alias)
test_tx = await cheqd_ledger.auth.build_tx(
pool_alias, public_key, msg, account_number, sequence_number, GAS_AMOUNT, GAS_AMOUNT*GAS_PRICE, DENOM, sender_address, timeout_height, memo
)
request = await cheqd_ledger.tx.build_query_simulate(test_tx)
response = await cheqd_pool.abci_query(pool_alias, request)
response = await cheqd_ledger.tx.parse_query_simulate_resp(response)
gas_estimation = json.loads(response)["gas_info"]["gas_used"]
fees = int(gas_estimation*MAX_GAS_MAGIC_NUMBER*GAS_PRICE)
prod_tx = await cheqd_ledger.auth.build_tx(
pool_alias, public_key, msg, account_number, sequence_number, int(gas_estimation*MAX_GAS_MAGIC_NUMBER), fees, DENOM, SENDER_ADDRESS, timeout_height, memo
)
tx_signed = await cheqd_ledger.auth.sign_tx(wallet_handle, key_alias, prod_tx)
res = json.loads(await cheqd_pool.broadcast_tx_commit(pool_alias, tx_signed))
tx_hash = res["hash"]

return res, tx_hash, fees


async def send_tx_helper_alt(pool_alias, wallet_handle, key_alias, public_key, sender_address, msg, memo):
account_number, sequence_number = await get_base_account_number_and_sequence(pool_alias, sender_address)
timeout_height = await get_timeout_height(pool_alias)
tx = await cheqd_ledger.auth.build_tx(
pool_alias, public_key, msg, account_number, sequence_number, GAS_AMOUNT, GAS_AMOUNT*GAS_PRICE, DENOM, sender_address, timeout_height, memo
)
tx_signed = await cheqd_ledger.auth.sign_tx(wallet_handle, key_alias, tx)
res = json.loads(await cheqd_pool.broadcast_tx_commit(pool_alias, tx_signed))
tx_hash = res["hash"]

return res, tx_hash


async def get_tx_helper(pool_alias, tx_hash):
request = await cheqd_ledger.tx.build_query_get_tx_by_hash(tx_hash)
res = await cheqd_pool.abci_query(pool_alias, request)
res = json.loads(await cheqd_ledger.tx.parse_query_get_tx_by_hash_resp(res))

return res


async def create_did_helper(pool_alias, wallet_handle, key_alias, public_key, sender_address, fqdid, vk, memo):
req = await cheqd_ledger.cheqd.build_msg_create_did(fqdid, vk)
signed_req = await cheqd_ledger.cheqd.sign_msg_write_request(wallet_handle, fqdid, bytes(req))
res, _, _ = await send_tx_helper(pool_alias, wallet_handle, key_alias, public_key, sender_address, bytes(signed_req), memo)

return res


async def query_did_helper(pool_alias, fqdid):
req = await cheqd_ledger.cheqd.build_query_get_did(fqdid)
res = await cheqd_pool.abci_query(pool_alias, req)

return res


async def update_did_helper(pool_alias, wallet_handle, key_alias, public_key, sender_address, fqdid, new_vk, version_id, memo):
req = await cheqd_ledger.cheqd.build_msg_update_did(fqdid, new_vk, version_id)
signed_req = await cheqd_ledger.cheqd.sign_msg_write_request(wallet_handle, fqdid, bytes(req))
res, _, _ = await send_tx_helper(pool_alias, wallet_handle, key_alias, public_key, sender_address, bytes(signed_req), memo)

return res


def set_up_operator():
name = random_string(10)
cli = run("cheqd-noded keys", "add", f"{name} {KEYRING_BACKEND_TEST}", r"mnemonic: \"\"")
Expand All @@ -170,35 +119,44 @@ def set_up_operator():
return name, address, pubkey


async def wallet_helper(wallet_id=None, wallet_key="", wallet_key_derivation_method="ARGON2I_INT"):
if not wallet_id:
wallet_id = random_string(25)
wallet_config = json.dumps({"id": wallet_id})
wallet_credentials = json.dumps({"key": wallet_key, "key_derivation_method": wallet_key_derivation_method})
await wallet.create_wallet(wallet_config, wallet_credentials)
wallet_handle = await wallet.open_wallet(wallet_config, wallet_credentials)

return wallet_handle, wallet_config, wallet_credentials


async def get_base_account_number_and_sequence(pool_alias, account_id):
req = await cheqd_ledger.auth.build_query_account(account_id)
resp = await cheqd_pool.abci_query(pool_alias, req)
resp = await cheqd_ledger.auth.parse_query_account_resp(resp)
account = json.loads(resp)["account"]
base_account = account["value"]
account_number = base_account["account_number"]
account_sequence = base_account["sequence"]

return account_number, account_sequence


async def get_timeout_height(pool_alias):
TIMEOUT = 50
try:
info = await cheqd_pool.abci_info(pool_alias)
info = json.loads(info)
current_height = info["response"]["last_block_height"]
return int(current_height) + TIMEOUT
except CommonInvalidStructure:
return 150
def build_create_did_msg(did: str,
key_id: str,
ver_pub_multibase_58: str) -> str:
return f'{{ "id": "{did}", \
"verification_method": [{{ \
"id": "{key_id}", \
"type": "Ed25519VerificationKey2020", \
"controller": "{did}", \
"public_key_multibase": "{ver_pub_multibase_58}" \
}}], \
"authentication": [ \
"{key_id}" \
] \
}} \ '


def build_update_did_msg(did: str,
key_id: str,
ver_pub_multibase_58: str,
version_id: str) -> dict:
return json.loads(f'{{ "id": "{did}", \
"version_id": "{version_id}", \
"verification_method": [{{ \
"id": "{key_id}", \
"type": "Ed25519VerificationKey2020", \
"controller": "{did}", \
"public_key_multibase": "{ver_pub_multibase_58}" \
}}], \
"authentication": [ \
"{key_id}" \
] \
}}')


def generate_ed25519_key() -> dict:
cli = run(
"cheqd-noded debug",
"ed25519 random",
"",
"")
return json_loads(cli.read())
7 changes: 3 additions & 4 deletions tests/e2e/test_cosmos.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
import re
import time
import getpass
from hypothesis import settings, given, strategies, Phase, Verbosity
from string import digits, ascii_letters
from hypothesis import settings, given, strategies
from string import ascii_letters
from helpers import run, run_interaction, get_balance, send_with_note, set_up_operator, random_string, \
TEST_NET_NETWORK, TEST_NET_NODE_TCP, TEST_NET_NODE_HTTP, TEST_NET_DESTINATION, TEST_NET_DESTINATION_HTTP, \
LOCAL_NET_NETWORK, LOCAL_NET_NODE_TCP, LOCAL_NET_NODE_HTTP, LOCAL_NET_DESTINATION, LOCAL_NET_DESTINATION_HTTP, \
TEST_NET_FEES, TEST_NET_GAS_X_GAS_PRICES, YES_FLAG, IMPLICIT_TIMEOUT, \
LOCAL_SENDER_ADDRESS, LOCAL_RECEIVER_ADDRESS,CODE_0, TEST_NET_GAS_X_GAS_PRICES_INT, KEYRING_BACKEND_TEST
LOCAL_SENDER_ADDRESS, LOCAL_RECEIVER_ADDRESS, CODE_0, TEST_NET_GAS_X_GAS_PRICES_INT, KEYRING_BACKEND_TEST


@pytest.mark.parametrize(
Expand Down
Loading

0 comments on commit 5e01f29

Please sign in to comment.