frostfs-testcases/robot/resources/lib/payment_neogo.py

155 lines
4.4 KiB
Python
Raw Normal View History

2020-11-18 15:15:57 +00:00
#!/usr/bin/python3
import subprocess
import pexpect
import re
import uuid
import logging
import requests
import json
2020-11-30 10:33:05 +00:00
import os
import tarfile
import sys
sys.path.insert(0,'../neofs-keywords')
import converters
import wallet
2020-11-18 15:15:57 +00:00
2020-11-30 10:33:05 +00:00
from robot.api.deco import keyword
from robot.api import logger
import robot.errors
2020-11-18 15:15:57 +00:00
from robot.libraries.BuiltIn import BuiltIn
from common import *
2020-11-18 15:15:57 +00:00
ROBOT_AUTO_KEYWORDS = False
2020-11-18 15:15:57 +00:00
# path to neofs-cli executable
NEOFS_CLI_EXEC = os.getenv('NEOFS_CLI_EXEC', 'neofs-cli')
NEOGO_CLI_EXEC = os.getenv('NEOGO_CLI_EXEC', 'neo-go')
2020-11-18 15:15:57 +00:00
2020-11-30 10:33:05 +00:00
@keyword('Withdraw Mainnet Gas')
2020-11-18 15:15:57 +00:00
def withdraw_mainnet_gas(wallet: str, address: str, scripthash: str, amount: int):
cmd = (
f"{NEOGO_CLI_EXEC} contract invokefunction -w {wallet} -a {address} "
f"-r {NEO_MAINNET_ENDPOINT} {NEOFS_CONTRACT} withdraw {scripthash} "
f"int:{amount} -- {scripthash}:Global"
)
2020-11-18 15:15:57 +00:00
logger.info(f"Executing command: {cmd}")
2020-11-28 03:41:35 +00:00
out = _run_sh_with_passwd('', cmd)
2020-11-18 15:15:57 +00:00
logger.info(f"Command completed with output: {out}")
2020-11-29 00:46:53 +00:00
m = re.match(r'^Sent invocation transaction (\w{64})$', out)
if m is None:
raise Exception("Can not get Tx.")
2020-11-29 00:46:53 +00:00
tx = m.group(1)
return tx
2020-11-18 15:15:57 +00:00
2020-11-30 10:33:05 +00:00
@keyword('NeoFS Deposit')
def neofs_deposit(wallet_file: str, address: str, scripthash: str, amount: int, wallet_pass:str=''):
2020-11-18 15:15:57 +00:00
# 1) Get NeoFS contract address.
deposit_addr = converters.contract_hash_to_address(NEOFS_CONTRACT)
logger.info(f"deposit_addr: {deposit_addr}")
2020-11-18 15:15:57 +00:00
# 2) Transfer GAS to the NeoFS contract address.
out = wallet.new_nep17_transfer(address, deposit_addr, amount, 'GAS', wallet_file, '', NEO_MAINNET_ENDPOINT)
if len(out) != 64:
2020-11-18 15:15:57 +00:00
raise Exception("Can not get Tx.")
return out
2020-11-18 15:15:57 +00:00
@keyword('Transaction accepted in block')
def transaction_accepted_in_block(tx_id):
"""
This function return True in case of accepted TX.
Parameters:
:param tx_id: transaction is
:rtype: block number or Exception
"""
logger.info("Transaction id: %s" % tx_id)
headers = {'Content-type': 'application/json'}
data = { "jsonrpc": "2.0", "id": 5, "method": "gettransactionheight", "params": [ tx_id ] }
response = requests.post(NEO_MAINNET_ENDPOINT, json=data, headers=headers, verify=False)
2020-11-18 15:15:57 +00:00
if not response.ok:
raise Exception(f"""Failed:
request: {data},
response: {response.text},
status code: {response.status_code} {response.reason}""")
2020-11-18 15:15:57 +00:00
if (response.text == 0):
2020-11-18 15:15:57 +00:00
raise Exception( "Transaction is not found in the blocks." )
logger.info("Transaction has been found in the block %s." % response.text )
return response.text
2020-11-18 15:15:57 +00:00
@keyword('Get Transaction')
def get_transaction(tx_id: str):
"""
This function return information about TX.
Parameters:
:param tx_id: transaction id
"""
headers = {'Content-type': 'application/json'}
data = { "jsonrpc": "2.0", "id": 5, "method": "getapplicationlog", "params": [ tx_id ] }
response = requests.post(NEO_MAINNET_ENDPOINT, json=data, headers=headers, verify=False)
if not response.ok:
raise Exception(f"""Failed:
request: {data},
response: {response.text},
status code: {response.status_code} {response.reason}""")
else:
logger.info(response.text)
2020-11-18 15:15:57 +00:00
@keyword('Get NeoFS Balance')
2020-11-18 15:15:57 +00:00
def get_balance(privkey: str):
"""
This function returns NeoFS balance for selected public key.
:param public_key: neo public key
"""
balance = _get_balance_request(privkey)
return float(balance)
2020-11-18 15:15:57 +00:00
def _get_balance_request(privkey: str):
'''
Internal method.
'''
Cmd = (
f'{NEOFS_CLI_EXEC} --key {privkey} --rpc-endpoint {NEOFS_ENDPOINT}'
f' accounting balance'
)
2021-03-29 10:18:24 +00:00
logger.info(f"Cmd: {Cmd}")
2020-11-18 15:15:57 +00:00
complProc = subprocess.run(Cmd, check=True, universal_newlines=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=150, shell=True)
output = complProc.stdout
2021-03-29 10:18:24 +00:00
logger.info(f"Output: {output}")
2020-11-30 10:33:05 +00:00
2021-03-29 10:18:24 +00:00
if output is None:
BuiltIn().fatal_error(f'Can not parse balance: "{output}"')
2020-11-30 10:33:05 +00:00
2021-03-29 10:18:24 +00:00
logger.info(f"Balance for '{privkey}' is '{output}'" )
2020-11-18 15:15:57 +00:00
2021-03-29 10:18:24 +00:00
return output
2020-11-18 15:15:57 +00:00
2020-11-28 03:41:35 +00:00
def _run_sh_with_passwd(passwd, cmd):
p = pexpect.spawn(cmd)
p.expect(".*")
p.sendline(passwd + '\r')
2020-11-28 03:41:35 +00:00
p.wait()
# throw a string with password prompt
# take a string with tx hash
tx_hash = p.read().splitlines()[-1]
return tx_hash.decode()