2022-10-27 05:43:20 +00:00
|
|
|
import base64
|
|
|
|
import json
|
2022-09-20 15:03:52 +00:00
|
|
|
import logging
|
2020-11-18 15:15:57 +00:00
|
|
|
import re
|
2022-06-13 20:33:09 +00:00
|
|
|
import time
|
2022-10-27 05:43:20 +00:00
|
|
|
from typing import Optional
|
2020-11-18 15:15:57 +00:00
|
|
|
|
2022-09-20 15:03:52 +00:00
|
|
|
import allure
|
2023-01-09 12:46:03 +00:00
|
|
|
from frostfs_testlib.cli import NeoGo
|
|
|
|
from frostfs_testlib.shell import Shell
|
2023-02-19 23:58:07 +00:00
|
|
|
from frostfs_testlib.utils import converting_utils, datetime_utils, wallet_utils
|
2023-02-10 08:29:31 +00:00
|
|
|
from neo3.wallet import utils as neo3_utils
|
2023-02-14 08:09:37 +00:00
|
|
|
from neo3.wallet import wallet as neo3_wallet
|
2020-11-18 15:15:57 +00:00
|
|
|
|
2023-02-27 16:54:27 +00:00
|
|
|
from pytest_tests.helpers.cluster import MainChain, MorphChain
|
|
|
|
from pytest_tests.resources.common import (
|
|
|
|
FROSTFS_CONTRACT,
|
|
|
|
GAS_HASH,
|
|
|
|
MAINNET_BLOCK_TIME,
|
|
|
|
NEOGO_EXECUTABLE,
|
|
|
|
)
|
|
|
|
|
2022-09-20 15:03:52 +00:00
|
|
|
logger = logging.getLogger("NeoLogger")
|
2020-11-18 15:15:57 +00:00
|
|
|
|
2022-09-20 15:03:52 +00:00
|
|
|
EMPTY_PASSWORD = ""
|
|
|
|
TX_PERSIST_TIMEOUT = 15 # seconds
|
|
|
|
ASSET_POWER_MAINCHAIN = 10**8
|
|
|
|
ASSET_POWER_SIDECHAIN = 10**12
|
2021-08-25 17:08:54 +00:00
|
|
|
|
2022-10-27 05:43:20 +00:00
|
|
|
|
2022-12-05 22:31:45 +00:00
|
|
|
def get_nns_contract_hash(morph_chain: MorphChain) -> str:
|
|
|
|
return morph_chain.rpc_client.get_contract_state(1)["hash"]
|
2022-10-27 05:43:20 +00:00
|
|
|
|
|
|
|
|
2022-12-05 22:31:45 +00:00
|
|
|
def get_contract_hash(morph_chain: MorphChain, resolve_name: str, shell: Shell) -> str:
|
|
|
|
nns_contract_hash = get_nns_contract_hash(morph_chain)
|
2022-10-27 05:43:20 +00:00
|
|
|
neogo = NeoGo(shell=shell, neo_go_exec_path=NEOGO_EXECUTABLE)
|
|
|
|
out = neogo.contract.testinvokefunction(
|
|
|
|
scripthash=nns_contract_hash,
|
|
|
|
method="resolve",
|
|
|
|
arguments=f"string:{resolve_name} int:16",
|
2022-12-05 22:31:45 +00:00
|
|
|
rpc_endpoint=morph_chain.get_endpoint(),
|
2022-10-27 05:43:20 +00:00
|
|
|
)
|
|
|
|
stack_data = json.loads(out.stdout.replace("\n", ""))["stack"][0]["value"]
|
|
|
|
return bytes.decode(base64.b64decode(stack_data[0]["value"]))
|
2020-11-18 15:15:57 +00:00
|
|
|
|
|
|
|
|
2022-09-20 15:03:52 +00:00
|
|
|
@allure.step("Withdraw Mainnet Gas")
|
2022-12-05 22:31:45 +00:00
|
|
|
def withdraw_mainnet_gas(shell: Shell, main_chain: MainChain, wlt: str, amount: int):
|
2023-02-19 23:58:07 +00:00
|
|
|
address = wallet_utils.get_last_address_from_wallet(wlt, EMPTY_PASSWORD)
|
2023-02-10 08:29:31 +00:00
|
|
|
scripthash = neo3_utils.address_to_script_hash(address)
|
2022-10-27 05:43:20 +00:00
|
|
|
|
|
|
|
neogo = NeoGo(shell=shell, neo_go_exec_path=NEOGO_EXECUTABLE)
|
|
|
|
out = neogo.contract.invokefunction(
|
|
|
|
wallet=wlt,
|
|
|
|
address=address,
|
2022-12-05 22:31:45 +00:00
|
|
|
rpc_endpoint=main_chain.get_endpoint(),
|
2023-01-09 12:46:03 +00:00
|
|
|
scripthash=FROSTFS_CONTRACT,
|
2022-10-27 05:43:20 +00:00
|
|
|
method="withdraw",
|
|
|
|
arguments=f"{scripthash} int:{amount}",
|
|
|
|
multisig_hash=f"{scripthash}:Global",
|
|
|
|
wallet_password="",
|
2021-01-17 11:55:10 +00:00
|
|
|
)
|
2020-11-18 15:15:57 +00:00
|
|
|
|
2022-10-27 05:43:20 +00:00
|
|
|
m = re.match(r"^Sent invocation transaction (\w{64})$", out.stdout)
|
2020-11-29 00:46:53 +00:00
|
|
|
if m is None:
|
2020-11-30 10:43:19 +00:00
|
|
|
raise Exception("Can not get Tx.")
|
2020-11-29 00:46:53 +00:00
|
|
|
tx = m.group(1)
|
2022-06-13 20:33:09 +00:00
|
|
|
if not transaction_accepted(tx):
|
2022-06-28 22:20:27 +00:00
|
|
|
raise AssertionError(f"TX {tx} hasn't been processed")
|
2020-11-18 15:15:57 +00:00
|
|
|
|
|
|
|
|
2022-12-05 22:31:45 +00:00
|
|
|
def transaction_accepted(main_chain: MainChain, tx_id: str):
|
2020-11-18 15:15:57 +00:00
|
|
|
"""
|
2022-06-13 20:33:09 +00:00
|
|
|
This function returns True in case of accepted TX.
|
|
|
|
Args:
|
|
|
|
tx_id(str): transaction ID
|
|
|
|
Returns:
|
|
|
|
(bool)
|
2020-11-18 15:15:57 +00:00
|
|
|
"""
|
|
|
|
|
2021-08-25 17:08:54 +00:00
|
|
|
try:
|
2022-06-13 20:33:09 +00:00
|
|
|
for _ in range(0, TX_PERSIST_TIMEOUT):
|
|
|
|
time.sleep(1)
|
2022-12-05 22:31:45 +00:00
|
|
|
resp = main_chain.rpc_client.get_transaction_height(tx_id)
|
2022-06-13 20:33:09 +00:00
|
|
|
if resp is not None:
|
|
|
|
logger.info(f"TX is accepted in block: {resp}")
|
|
|
|
return True
|
2022-06-24 10:36:02 +00:00
|
|
|
except Exception as out:
|
|
|
|
logger.info(f"request failed with error: {out}")
|
|
|
|
raise out
|
2022-06-13 20:33:09 +00:00
|
|
|
return False
|
2020-11-18 15:15:57 +00:00
|
|
|
|
|
|
|
|
2023-01-09 12:46:03 +00:00
|
|
|
@allure.step("Get FrostFS Balance")
|
2022-12-05 22:31:45 +00:00
|
|
|
def get_balance(shell: Shell, morph_chain: MorphChain, wallet_path: str, wallet_password: str = ""):
|
2020-11-18 15:15:57 +00:00
|
|
|
"""
|
2023-01-09 12:46:03 +00:00
|
|
|
This function returns FrostFS balance for given wallet.
|
2020-11-18 15:15:57 +00:00
|
|
|
"""
|
2022-10-27 05:43:20 +00:00
|
|
|
with open(wallet_path) as wallet_file:
|
|
|
|
wallet = neo3_wallet.Wallet.from_json(json.load(wallet_file), password=wallet_password)
|
|
|
|
acc = wallet.accounts[-1]
|
2022-09-20 15:03:52 +00:00
|
|
|
payload = [{"type": "Hash160", "value": str(acc.script_hash)}]
|
2021-08-25 17:08:54 +00:00
|
|
|
try:
|
2022-12-05 22:31:45 +00:00
|
|
|
resp = morph_chain.rpc_client.invoke_function(
|
2023-01-09 12:46:03 +00:00
|
|
|
get_contract_hash(morph_chain, "balance.frostfs", shell=shell), "balanceOf", payload
|
2022-07-04 19:49:14 +00:00
|
|
|
)
|
2022-06-13 20:33:09 +00:00
|
|
|
logger.info(f"Got response \n{resp}")
|
2022-09-20 15:03:52 +00:00
|
|
|
value = int(resp["stack"][0]["value"])
|
2022-07-04 19:49:14 +00:00
|
|
|
return value / ASSET_POWER_SIDECHAIN
|
2022-06-24 10:36:02 +00:00
|
|
|
except Exception as out:
|
|
|
|
logger.error(f"failed to get wallet balance: {out}")
|
|
|
|
raise out
|
2020-11-18 15:15:57 +00:00
|
|
|
|
|
|
|
|
2022-10-27 05:43:20 +00:00
|
|
|
@allure.title("Transfer Gas")
|
|
|
|
def transfer_gas(
|
|
|
|
shell: Shell,
|
2022-09-20 15:03:52 +00:00
|
|
|
amount: int,
|
2022-12-05 22:31:45 +00:00
|
|
|
main_chain: MainChain,
|
|
|
|
wallet_from_path: Optional[str] = None,
|
|
|
|
wallet_from_password: Optional[str] = None,
|
|
|
|
address_from: Optional[str] = None,
|
2022-10-27 05:43:20 +00:00
|
|
|
address_to: Optional[str] = None,
|
|
|
|
wallet_to_path: Optional[str] = None,
|
|
|
|
wallet_to_password: Optional[str] = None,
|
2022-09-20 15:03:52 +00:00
|
|
|
):
|
2022-07-01 17:30:07 +00:00
|
|
|
"""
|
2022-06-13 20:33:09 +00:00
|
|
|
This function transfer GAS in main chain from mainnet wallet to
|
|
|
|
the provided wallet. If the wallet contains more than one address,
|
|
|
|
the assets will be transferred to the last one.
|
|
|
|
Args:
|
2022-10-27 05:43:20 +00:00
|
|
|
shell: Shell instance.
|
2022-11-01 09:30:05 +00:00
|
|
|
wallet_from_password: Password of the wallet; it is required to decode the wallet
|
|
|
|
and extract its addresses.
|
|
|
|
wallet_from_path: Path to chain node wallet.
|
|
|
|
address_from: The address of the wallet to transfer assets from.
|
|
|
|
wallet_to_path: The path to the wallet to transfer assets to.
|
|
|
|
wallet_to_password: The password to the wallet to transfer assets to.
|
|
|
|
address_to: The address of the wallet to transfer assets to.
|
|
|
|
amount: Amount of gas to transfer.
|
2022-07-01 17:30:07 +00:00
|
|
|
"""
|
2022-12-05 22:31:45 +00:00
|
|
|
wallet_from_path = wallet_from_path or main_chain.get_wallet_path()
|
|
|
|
wallet_from_password = (
|
|
|
|
wallet_from_password
|
|
|
|
if wallet_from_password is not None
|
|
|
|
else main_chain.get_wallet_password()
|
|
|
|
)
|
2023-02-19 23:58:07 +00:00
|
|
|
address_from = address_from or wallet_utils.get_last_address_from_wallet(
|
2022-12-05 22:31:45 +00:00
|
|
|
wallet_from_path, wallet_from_password
|
|
|
|
)
|
2023-02-19 23:58:07 +00:00
|
|
|
address_to = address_to or wallet_utils.get_last_address_from_wallet(
|
|
|
|
wallet_to_path, wallet_to_password
|
|
|
|
)
|
2022-10-27 05:43:20 +00:00
|
|
|
|
|
|
|
neogo = NeoGo(shell, neo_go_exec_path=NEOGO_EXECUTABLE)
|
|
|
|
out = neogo.nep17.transfer(
|
2022-12-05 22:31:45 +00:00
|
|
|
rpc_endpoint=main_chain.get_endpoint(),
|
2022-10-27 05:43:20 +00:00
|
|
|
wallet=wallet_from_path,
|
|
|
|
wallet_password=wallet_from_password,
|
|
|
|
amount=amount,
|
|
|
|
from_address=address_from,
|
|
|
|
to_address=address_to,
|
|
|
|
token="GAS",
|
|
|
|
force=True,
|
2022-09-20 15:03:52 +00:00
|
|
|
)
|
2022-10-27 05:43:20 +00:00
|
|
|
txid = out.stdout.strip().split("\n")[-1]
|
|
|
|
if len(txid) != 64:
|
|
|
|
raise Exception("Got no TXID after run the command")
|
2022-12-05 22:31:45 +00:00
|
|
|
if not transaction_accepted(main_chain, txid):
|
2022-06-28 22:20:27 +00:00
|
|
|
raise AssertionError(f"TX {txid} hasn't been processed")
|
2023-02-19 23:58:07 +00:00
|
|
|
time.sleep(datetime_utils.parse_time(MAINNET_BLOCK_TIME))
|
2022-06-13 20:33:09 +00:00
|
|
|
|
|
|
|
|
2023-01-09 12:46:03 +00:00
|
|
|
@allure.step("FrostFS Deposit")
|
2022-12-05 22:31:45 +00:00
|
|
|
def deposit_gas(
|
|
|
|
shell: Shell,
|
|
|
|
main_chain: MainChain,
|
|
|
|
amount: int,
|
|
|
|
wallet_from_path: str,
|
|
|
|
wallet_from_password: str,
|
|
|
|
):
|
2022-06-13 20:33:09 +00:00
|
|
|
"""
|
2023-01-09 12:46:03 +00:00
|
|
|
Transferring GAS from given wallet to FrostFS contract address.
|
2022-06-13 20:33:09 +00:00
|
|
|
"""
|
2023-01-09 12:46:03 +00:00
|
|
|
# get FrostFS contract address
|
2023-02-19 23:58:07 +00:00
|
|
|
deposit_addr = converting_utils.contract_hash_to_address(FROSTFS_CONTRACT)
|
2023-01-09 12:46:03 +00:00
|
|
|
logger.info(f"FrostFS contract address: {deposit_addr}")
|
2023-02-19 23:58:07 +00:00
|
|
|
address_from = wallet_utils.get_last_address_from_wallet(
|
2022-10-27 05:43:20 +00:00
|
|
|
wallet_path=wallet_from_path, wallet_password=wallet_from_password
|
|
|
|
)
|
|
|
|
transfer_gas(
|
|
|
|
shell=shell,
|
2022-12-05 22:31:45 +00:00
|
|
|
main_chain=main_chain,
|
2022-10-27 05:43:20 +00:00
|
|
|
amount=amount,
|
|
|
|
wallet_from_path=wallet_from_path,
|
|
|
|
wallet_from_password=wallet_from_password,
|
|
|
|
address_to=deposit_addr,
|
|
|
|
address_from=address_from,
|
2022-09-20 15:03:52 +00:00
|
|
|
)
|
2022-07-04 19:49:14 +00:00
|
|
|
|
|
|
|
|
2022-09-20 15:03:52 +00:00
|
|
|
@allure.step("Get Mainnet Balance")
|
2022-12-05 22:31:45 +00:00
|
|
|
def get_mainnet_balance(main_chain: MainChain, address: str):
|
|
|
|
resp = main_chain.rpc_client.get_nep17_balances(address=address)
|
2022-07-04 19:49:14 +00:00
|
|
|
logger.info(f"Got getnep17balances response: {resp}")
|
2022-09-20 15:03:52 +00:00
|
|
|
for balance in resp["balance"]:
|
|
|
|
if balance["assethash"] == GAS_HASH:
|
|
|
|
return float(balance["amount"]) / ASSET_POWER_MAINCHAIN
|
2022-07-04 19:49:14 +00:00
|
|
|
return float(0)
|
|
|
|
|
|
|
|
|
2022-09-20 15:03:52 +00:00
|
|
|
@allure.step("Get Sidechain Balance")
|
2022-12-05 22:31:45 +00:00
|
|
|
def get_sidechain_balance(morph_chain: MorphChain, address: str):
|
|
|
|
resp = morph_chain.rpc_client.get_nep17_balances(address=address)
|
2022-07-04 19:49:14 +00:00
|
|
|
logger.info(f"Got getnep17balances response: {resp}")
|
2022-09-20 15:03:52 +00:00
|
|
|
for balance in resp["balance"]:
|
|
|
|
if balance["assethash"] == GAS_HASH:
|
|
|
|
return float(balance["amount"]) / ASSET_POWER_SIDECHAIN
|
2022-07-04 19:49:14 +00:00
|
|
|
return float(0)
|