Skip to content

Commit

Permalink
fixes after review 2
Browse files Browse the repository at this point in the history
  • Loading branch information
gabi-vuls committed Apr 16, 2024
1 parent 7816e19 commit 13f75d3
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 113 deletions.
2 changes: 1 addition & 1 deletion testing-suite/staking-v4/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Testing suite using mvx python sdk and chain-simulator for staking-v4 feature

## Overview:
- All tests are written based on scenarios from the testing plan that can be found [here](https://docs.google.com/spreadsheets/d/154IAPppvYBScPHYOVk696o7Izi4Ode9b7Lwk2iFt_p4/edit#gid=2043239020).
- All tests are written based on scenarios from the internal testing plan.

## How to run:

Expand Down
21 changes: 9 additions & 12 deletions testing-suite/staking-v4/chain_commander.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
import time



def setEgldToAddress(egld_ammount, erd_address):
def send_egld_to_address(egld_amount, erd_address):
details = {
'address': f'{erd_address}',
'balance': f'{egld_ammount}'
'balance': f'{egld_amount}'
}

details_list = [details]
Expand All @@ -22,29 +21,29 @@ def setEgldToAddress(egld_ammount, erd_address):
return response.text


def addBlocks(nr_of_blocks):
def add_blocks(nr_of_blocks):
req = requests.post(f"{DEFAULT_PROXY}/simulator/generate-blocks/{nr_of_blocks}")
return req.text


def addBlocksUntilEpochReached(epoch_to_be_reached: int):
def add_blocks_until_epoch_reached(epoch_to_be_reached: int):
req = requests.post(f"{DEFAULT_PROXY}/simulator/generate-blocks-until-epoch-reached/{str(epoch_to_be_reached)}")
return req.text


def addBlocksUntilTxSucceeded(tx_hash) -> str:
def add_blocks_until_tx_fully_executed(tx_hash) -> str:
print("Checking: ", tx_hash)
counter = 0

while counter < MAX_NUM_OF_BLOCKS_UNTIL_TX_SHOULD_BE_EXECUTED:
addBlocks(1)
add_blocks(1)

time.sleep(WAIT_UNTIL_API_REQUEST_IN_SEC)
if getStatusOfTx(tx_hash) == "pending":
if get_status_of_tx(tx_hash) == "pending":
counter += 1
else:
print("Tx fully executed after", counter, " blocks.")
return getStatusOfTx(tx_hash)
return get_status_of_tx(tx_hash)


def is_chain_online() -> bool:
Expand All @@ -69,8 +68,7 @@ def force_reset_validator_statistics():
return req.text


def addKey(private_keys: list) -> str:

def add_key(private_keys: list) -> str:
post_body = {
"privateKeysBase64": private_keys
}
Expand All @@ -79,4 +77,3 @@ def addKey(private_keys: list) -> str:
req = requests.post(f"{DEFAULT_PROXY}/simulator/add-keys", data=json_structure)

return req.text

4 changes: 2 additions & 2 deletions testing-suite/staking-v4/core/validatorKey.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def public_address(self) -> str:
# is using vm-query with "getBlsKeysStatus" function
def get_status(self, owner_address: str) -> str:
owner_address = Address.from_bech32(owner_address).to_hex()
key_status_pair = getBLSKeysStatus([owner_address])
key_status_pair = get_bls_key_status([owner_address])
if key_status_pair is None:
return "no bls keys on this owner"
for key, status in key_status_pair.items():
Expand Down Expand Up @@ -56,7 +56,7 @@ def get_state(self) -> str:

# using getOwner vm-query
def belongs_to(self, address: str) -> bool:
owner = getOwner([self.public_address()])
owner = get_owner([self.public_address()])
if owner == address:
return True
else:
Expand Down
4 changes: 2 additions & 2 deletions testing-suite/staking-v4/core/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ def get_balance(self) -> int:
return balance


def set_balance(self, egld_ammount):
def set_balance(self, egld_amount):
details = {
'address': f'{self.public_address()}',
'balance': f'{egld_ammount}'
'balance': f'{egld_amount}'
}

details_list = [details]
Expand Down
12 changes: 6 additions & 6 deletions testing-suite/staking-v4/delegation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from core.validatorKey import *


def createNewDelegationContract(owner: Wallet, AMOUNT="1250000000000000000000", SERVICE_FEE="00",
DELEGATION_CAP="00") -> str:
def create_new_delegation_contract(owner: Wallet, AMOUNT="1250000000000000000000", SERVICE_FEE="00",
DELEGATION_CAP="00") -> str:
# compute tx
tx = Transaction(sender=owner.get_address().to_bech32(),
receiver=SYSTEM_DELEGATION_MANAGER_CONTRACT,
Expand All @@ -37,7 +37,7 @@ def createNewDelegationContract(owner: Wallet, AMOUNT="1250000000000000000000",
return tx_hash


def whitelistForMerge(old_owner: Wallet, new_owner: Wallet, delegation_sc_address: str) -> str:
def whitelist_for_merge(old_owner: Wallet, new_owner: Wallet, delegation_sc_address: str) -> str:
delegation_sc_address = Address.from_bech32(delegation_sc_address)

# compute tx
Expand All @@ -62,7 +62,7 @@ def whitelistForMerge(old_owner: Wallet, new_owner: Wallet, delegation_sc_addres
return tx_hash


def mergeValidatorToDelegationWithWhitelist(new_owner: Wallet, delegation_sc_address: str):
def merge_validator_to_delegation_with_whitelist(new_owner: Wallet, delegation_sc_address: str):
delegation_sc_address_as_hex = Address.from_bech32(delegation_sc_address).to_hex()

# compute tx
Expand All @@ -87,7 +87,7 @@ def mergeValidatorToDelegationWithWhitelist(new_owner: Wallet, delegation_sc_add
return tx_hash


def addNodes(owner: Wallet, delegation_sc_address: str, validatorKeys: list[ValidatorKey]) -> str:
def add_nodes(owner: Wallet, delegation_sc_address: str, validatorKeys: list[ValidatorKey]) -> str:
# load needed data for stake transactions signatures
stake_signature_and_public_key = ''
for key in validatorKeys:
Expand Down Expand Up @@ -121,7 +121,7 @@ def addNodes(owner: Wallet, delegation_sc_address: str, validatorKeys: list[Vali
return tx_hash


def stakeNodes(owner: Wallet, delegation_sc_address: str, validatorKeys: list[ValidatorKey]):
def stake_nodes(owner: Wallet, delegation_sc_address: str, validatorKeys: list[ValidatorKey]):
pub_key_string = ''
for key in validatorKeys:
pub_key_string += f"@{key.public_address()}"
Expand Down
38 changes: 12 additions & 26 deletions testing-suite/staking-v4/get_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,7 @@
from constants import *


def getPublicAddressFromPem(pem: Path) -> str:
f = open(pem)
lines = f.readlines()
for line in lines:
if "BEGIN" in line:
line = line.split(" ")
address = line[-1].replace("-----", "")
if "\n" in address:
address = address.replace("\n", "")
break

return address


def getStatusOfTx(tx_hash: str) -> str:
def get_status_of_tx(tx_hash: str) -> str:
response = requests.get(f"{DEFAULT_PROXY}/transaction/{tx_hash}/process-status")
response.raise_for_status()
parsed = response.json()
Expand All @@ -36,7 +22,7 @@ def getStatusOfTx(tx_hash: str) -> str:
return status


def getDelegationContractAddressFromTx(tx_hash):
def get_delegation_contract_address_from_tx(tx_hash):

response = requests.get(f"{DEFAULT_PROXY}/transaction/{tx_hash}?withResults=True")
response.raise_for_status()
Expand All @@ -50,13 +36,13 @@ def getDelegationContractAddressFromTx(tx_hash):
topics = first_set_of_events.get("topics")
delegation_contract_address = topics[1]

delegation_contract_address = base64ToHex(delegation_contract_address)
delegation_contract_address = base64_to_hex(delegation_contract_address)
delegation_contract_address = Address.from_hex(delegation_contract_address, "erd").to_bech32()

return delegation_contract_address


def getBLSKeysStatus(owner_public_key_in_hex: list[str]):
def get_bls_key_status(owner_public_key_in_hex: list[str]):
key_status_pair = {}

post_body = {
Expand All @@ -79,15 +65,15 @@ def getBLSKeysStatus(owner_public_key_in_hex: list[str]):

# convert all elements from list to hex and add to final dict:
for i in range(0, len(key_status_temp_list), 2):
bls_decoded = base64ToHex(key_status_temp_list[i])
status_decoded = base64ToString(key_status_temp_list[i + 1])
bls_decoded = base64_to_hex(key_status_temp_list[i])
status_decoded = base64_to_string(key_status_temp_list[i + 1])

key_status_pair[bls_decoded] = status_decoded

return key_status_pair


def getOwner(public_validator_key: list[str]) -> str:
def get_owner(public_validator_key: list[str]) -> str:

post_body = {
"scAddress": STAKING_CONTRACT,
Expand All @@ -109,15 +95,15 @@ def getOwner(public_validator_key: list[str]) -> str:
address_list = tx_response_data.get("returnData")
address = address_list[0]

address = base64ToHex(address)
address = base64_to_hex(address)
address = Address.from_hex(address, "erd").to_bech32()

return address


def checkIfErrorIsPresentInTx(error, tx_hash) -> bool:
def check_if_error_is_present_in_tx(error, tx_hash) -> bool:
flag = False
error_bytes = stringToBase64(error)
error_bytes = string_to_base64(error)

req = requests.get(f"{DEFAULT_PROXY}/transaction/{tx_hash}?withResults=True")
response = req.text
Expand All @@ -131,7 +117,7 @@ def checkIfErrorIsPresentInTx(error, tx_hash) -> bool:
return flag


def getTotalStaked(owner: str):
def get_total_staked(owner: str):
address_in_hex = Address.from_bech32(owner).to_hex()
post_body = {
"scAddress": VALIDATOR_CONTRACT,
Expand All @@ -149,5 +135,5 @@ def getTotalStaked(owner: str):
total_staked_list = tx_response_data.get("returnData")
total_staked = total_staked_list[0]

total_staked = base64ToString(total_staked)
total_staked = base64_to_string(total_staked)
return total_staked
14 changes: 7 additions & 7 deletions testing-suite/staking-v4/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@
import string


def decimalToHex(value: int):
def decimal_to_hex(value: int):
hex_value = f'{value:x}'
if len(hex_value) % 2 > 0:
hex_value = "0" + hex_value
return hex_value


def base64ToHex(b):
def base64_to_hex(b):
return base64.b64decode(b).hex()


def stringToBase64(s):
def string_to_base64(s):
return base64.b64encode(s.encode('utf-8'))


def base64ToString(b):
def base64_to_string(b):
return base64.b64decode(b).decode('utf-8')


def replaceRandomDataWithAnotherRandomData(input_string: str) -> str:
def generateRandomLetter() -> str:
def replace_random_data_with_another_random_data(input_string: str) -> str:
def generate_random_letter() -> str:
return random.choice(string.ascii_letters)

letter_to_be_replaced = random.choice(input_string)
letter_to_replace_with = generateRandomLetter()
letter_to_replace_with = generate_random_letter()

new_string = input_string.replace(letter_to_be_replaced, letter_to_replace_with)
return new_string
16 changes: 8 additions & 8 deletions testing-suite/staking-v4/scenarios/_48.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,23 @@ def scenario(epoch: int):

if is_chain_online():
# === PRE-CONDITIONS ==============================================================
AMMOUNT_TO_MINT = "6000" + "000000000000000000"
AMOUNT_TO_MINT = "6000" + "000000000000000000"

_A = Wallet(Path("./wallets/walletKey_1.pem"))

# check if minting is successful
assert "success" in _A.set_balance(AMMOUNT_TO_MINT)
assert "success" in _A.set_balance(AMOUNT_TO_MINT)

# add some blocks
response = addBlocks(5)
response = add_blocks(5)
assert "success" in response
time.sleep(0.5)

# check balance
assert _A.get_balance() == AMMOUNT_TO_MINT
assert _A.get_balance() == AMOUNT_TO_MINT

# move to epoch
assert "success" in addBlocksUntilEpochReached(epoch)
assert "success" in add_blocks_until_epoch_reached(epoch)

# === STEP 1 ==============================================================
# 1) Stake with A 2 nodes
Expand All @@ -68,15 +68,15 @@ def scenario(epoch: int):
tx_hash = stake(_A, A_Keys)

# move few blocks and check tx
assert addBlocksUntilTxSucceeded(tx_hash) == "success"
assert add_blocks_until_tx_fully_executed(tx_hash) == "success"

# === STEP 2 ==============================================================
# 2) check balance of A to be - (5000+gas fees)
assert int(_A.get_balance()) < int(AMMOUNT_TO_MINT) - 5000
assert int(_A.get_balance()) < int(AMOUNT_TO_MINT) - 5000

# === STEP 3 ==============================================================
# 3) check total stake of A
total_staked = getTotalStaked(_A.public_address())
total_staked = get_total_staked(_A.public_address())
assert total_staked == "5000" + "000000000000000000"

# === STEP 4 ==============================================================
Expand Down
Loading

0 comments on commit 13f75d3

Please sign in to comment.