Skip to content

Commit

Permalink
Update tests for whitelist
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-neon committed Oct 10, 2024
1 parent 22c582c commit 6518da5
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 21 deletions.
238 changes: 221 additions & 17 deletions integration/tests/neon_evm/test_cancel_trx.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,246 @@
import json
import os
import time

import pytest
from solana.transaction import Transaction

from integration.tests.neon_evm.utils.transaction_checks import (
check_holder_account_tag,
decode_logs,
)
from utils.instructions import make_Cancel
from utils.layouts import FINALIZED_STORAGE_ACCOUNT_INFO_LAYOUT
from .utils.constants import TAG_FINALIZED_STATE

from .utils.constants import TAG_ACTIVE_STATE, TAG_FINALIZED_STATE
from .utils.contract import make_contract_call_trx
from .utils.storage import create_holder
from .utils.transaction_checks import check_holder_account_tag


# We need test here two types of transaction
class TestCancelTrx:

def test_cancel_trx(self, operator_keypair, rw_lock_contract, user_account, treasury_pool, evm_loader):
"""EVM can cancel transaction and finalize storage account"""
signed_tx = make_contract_call_trx(evm_loader, user_account, rw_lock_contract, "unchange_storage(uint8,uint8)", [1, 1])
def test_try_to_cancel_trx_before_timeout_expiration(
self, operator_keypair, rw_lock_contract, user_account, treasury_pool, evm_loader
):
"""EVM can't cancel transaction and finalize storage account"""
signed_tx = make_contract_call_trx(
evm_loader, user_account, rw_lock_contract, "unchange_storage(uint8,uint8)", [1, 1]
)

storage_account = create_holder(operator_keypair, evm_loader)
user_nonce_before_first_step = evm_loader.get_neon_nonce(user_account.eth_address)
operator_balance = evm_loader.get_operator_balance_pubkey(operator_keypair)

receipt = evm_loader.send_transaction_step_from_instruction(operator_keypair, operator_balance, treasury_pool, storage_account,
signed_tx,
[rw_lock_contract.solana_address,
rw_lock_contract.balance_account_address,
user_account.balance_account_address],
1, operator_keypair)
receipt = evm_loader.send_transaction_step_from_instruction(
operator_keypair,
operator_balance,
treasury_pool,
storage_account,
signed_tx,
[
rw_lock_contract.solana_address,
rw_lock_contract.balance_account_address,
user_account.balance_account_address,
],
1,
operator_keypair,
)

assert receipt.value.transaction.meta.err is None

user_nonce_after_first_step = evm_loader.get_neon_nonce(user_account.eth_address)
assert user_nonce_before_first_step + 1 == user_nonce_after_first_step
trx = Transaction()
trx.add(
make_Cancel(evm_loader.loader_id, storage_account, operator_keypair,operator_balance, signed_tx.hash,
[rw_lock_contract.solana_address,
rw_lock_contract.balance_account_address,
user_account.balance_account_address])
make_Cancel(
evm_loader.loader_id,
storage_account,
operator_keypair,
operator_balance,
signed_tx.hash,
[
rw_lock_contract.solana_address,
rw_lock_contract.balance_account_address,
user_account.balance_account_address,
],
)
)
evm_loader.send_tx(trx, operator_keypair)
check_holder_account_tag(storage_account, FINALIZED_STORAGE_ACCOUNT_INFO_LAYOUT, TAG_ACTIVE_STATE)

@pytest.mark.skipif(os.getenv("PROXY_IP", "") != "", reason="Proxy cancels transaction before this timeout.")
def test_try_to_cancel_trx_after_timeout_expiration(
self, operator_keypair, user_account, rw_lock_contract, treasury_pool, evm_loader, sol_client
):
"""EVM can cancel transaction and finalize storage account"""
signed_tx = make_contract_call_trx(
evm_loader, user_account, rw_lock_contract, "unchange_storage(uint8,uint8)", [1, 1]
)

storage_account = create_holder(operator_keypair, evm_loader)
operator_balance = evm_loader.get_operator_balance_pubkey(operator_keypair)

evm_loader.send_transaction_step_from_instruction(
operator_keypair,
operator_balance,
treasury_pool,
storage_account,
signed_tx,
[
rw_lock_contract.solana_address,
rw_lock_contract.balance_account_address,
user_account.balance_account_address,
],
1,
operator_keypair,
)
check_holder_account_tag(storage_account, FINALIZED_STORAGE_ACCOUNT_INFO_LAYOUT, TAG_ACTIVE_STATE)
initial_slot = json.loads(sol_client.get_slot().to_json())["result"]
initial_time = time.time() # used for exiting in emergency case

while True:
current_slot = json.loads(sol_client.get_slot().to_json())["result"]
print(f"Initial slot: {initial_slot}; current slot: {current_slot} -> Diff: {current_slot - initial_slot}")
if current_slot - initial_slot > 100:
break
if time.time() - initial_time > 150:
assert current_slot - initial_slot > 100, "Timeout expired, less than 100 slots in 150 seconds"
time.sleep(0.4)

trx = Transaction()
trx.add(
make_Cancel(
evm_loader.loader_id,
storage_account,
operator_keypair,
operator_balance,
signed_tx.hash,
[
rw_lock_contract.solana_address,
rw_lock_contract.balance_account_address,
user_account.balance_account_address,
],
)
)
receipt_cancel = evm_loader.send_tx(trx, operator_keypair)
print(f"\n{receipt_cancel}\n")
print(f"\n{decode_logs(receipt_cancel.value.transaction.meta.log_messages)}\n")
check_holder_account_tag(storage_account, FINALIZED_STORAGE_ACCOUNT_INFO_LAYOUT, TAG_FINALIZED_STATE)

@pytest.mark.skipif(os.getenv("PROXY_IP", "") != "", reason="Proxy cancels transaction before this timeout.")
def test_try_to_cancel_trx_after_timeout_expiration_with_continue(
self, operator_keypair, user_account, rw_lock_contract, treasury_pool, evm_loader, sol_client
):
"""EVM can cancel transaction and finalize storage account only after last activity"""
signed_tx = make_contract_call_trx(
evm_loader, user_account, rw_lock_contract, "unchange_storage(uint8,uint8)", [1, 1]
)

storage_account = create_holder(operator_keypair, evm_loader)
operator_balance = evm_loader.get_operator_balance_pubkey(operator_keypair)

evm_loader.send_transaction_step_from_instruction(
operator_keypair,
operator_balance,
treasury_pool,
storage_account,
signed_tx,
[
rw_lock_contract.solana_address,
rw_lock_contract.balance_account_address,
user_account.balance_account_address,
],
1,
operator_keypair,
)
check_holder_account_tag(storage_account, FINALIZED_STORAGE_ACCOUNT_INFO_LAYOUT, TAG_ACTIVE_STATE)
initial_slot = json.loads(sol_client.get_slot().to_json())["result"]
initial_time = time.time() # used for exiting in emergency case

while True:
current_slot = json.loads(sol_client.get_slot().to_json())["result"]
print(f"Initial slot: {initial_slot}; current slot: {current_slot} -> Diff: {current_slot - initial_slot}")
if current_slot - initial_slot > 50:
break
if time.time() - initial_time > 150:
assert current_slot - initial_slot > 100, "Timeout expired, less than 100 slots in 150 seconds"
time.sleep(0.4)

# Executing one more step to update lastUsedSlot
evm_loader.send_transaction_step_from_instruction(
operator_keypair,
operator_balance,
treasury_pool,
storage_account,
signed_tx,
[
rw_lock_contract.solana_address,
rw_lock_contract.balance_account_address,
user_account.balance_account_address,
],
1,
operator_keypair,
)
updated_slot = json.loads(sol_client.get_slot().to_json())["result"]
initial_time = time.time() # used for exiting in emergency case

while True:
current_slot = json.loads(sol_client.get_slot().to_json())["result"]
print(f"Initial slot: {initial_slot}; current slot: {current_slot} -> Diff: {current_slot - initial_slot}")
if current_slot - initial_slot > 100:
break
if time.time() - initial_time > 150:
assert current_slot - initial_slot > 100, "Timeout expired, less than 100 slots in 150 seconds"
time.sleep(0.4)

# Attempt to cancel transaction after initial timeout expiration
trx = Transaction()
trx.add(
make_Cancel(
evm_loader.loader_id,
storage_account,
operator_keypair,
operator_balance,
signed_tx.hash,
[
rw_lock_contract.solana_address,
rw_lock_contract.balance_account_address,
user_account.balance_account_address,
],
)
)
receipt_cancel = evm_loader.send_tx(trx, operator_keypair)
print(f"\n{receipt_cancel}\n")
print(f"\n{decode_logs(receipt_cancel.value.transaction.meta.log_messages)}\n")
check_holder_account_tag(storage_account, FINALIZED_STORAGE_ACCOUNT_INFO_LAYOUT, TAG_ACTIVE_STATE)

initial_time = time.time() # used for exiting in emergency case
while True:
current_slot = json.loads(sol_client.get_slot().to_json())["result"]
print(f"Updated slot: {updated_slot}; current slot: {current_slot} -> Diff: {current_slot - updated_slot}")
if current_slot - updated_slot > 100:
break
if time.time() - initial_time > 150:
assert current_slot - initial_slot > 100, "Timeout expired, less than 100 slots in 150 seconds"
time.sleep(0.4)

# Attempt to cancel transaction after updated timeout expiration
trx = Transaction()
trx.add(
make_Cancel(
evm_loader.loader_id,
storage_account,
operator_keypair,
operator_balance,
signed_tx.hash,
[
rw_lock_contract.solana_address,
rw_lock_contract.balance_account_address,
user_account.balance_account_address,
],
)
)
receipt_cancel = evm_loader.send_tx(trx, operator_keypair)
print(f"\n{receipt_cancel}\n")
print(f"\n{decode_logs(receipt_cancel.value.transaction.meta.log_messages)}\n")
check_holder_account_tag(storage_account, FINALIZED_STORAGE_ACCOUNT_INFO_LAYOUT, TAG_FINALIZED_STATE)
assert user_nonce_after_first_step == evm_loader.get_neon_nonce(user_account.eth_address)
Original file line number Diff line number Diff line change
Expand Up @@ -389,12 +389,12 @@ def test_incorrect_operator_account(
],
)

def test_operator_is_not_in_white_list(
def test_holder_invalid_owner_trx_execution(
self, sender_with_tokens, operator_keypair, evm_loader, treasury_pool, session_user, holder_acc
):
signed_tx = make_eth_transaction(evm_loader, session_user.eth_address, None, sender_with_tokens, 1)
evm_loader.write_transaction_to_holder_account(signed_tx, holder_acc, operator_keypair)
with pytest.raises(SolanaRPCException, match=InstructionAsserts.NOT_AUTHORIZED_OPERATOR):
with pytest.raises(SolanaRPCException, match=InstructionAsserts.INVALID_HOLDER_OWNER):
evm_loader.execute_transaction_steps_from_account(
sender_with_tokens.solana_account,
treasury_pool,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,11 +399,11 @@ def test_incorrect_operator_account(self, sender_with_tokens, evm_loader, treasu
],
)

def test_operator_is_not_in_white_list(
def test_holder_invalid_owner_trx_execution(
self, sender_with_tokens, evm_loader, treasury_pool, session_user, holder_acc
):
signed_tx = make_eth_transaction(evm_loader, session_user.eth_address, None, sender_with_tokens, 1)
with pytest.raises(solana.rpc.core.RPCException, match=InstructionAsserts.NOT_AUTHORIZED_OPERATOR):
with pytest.raises(solana.rpc.core.RPCException, match=InstructionAsserts.INVALID_HOLDER_OWNER):
evm_loader.execute_transaction_steps_from_instruction(
sender_with_tokens.solana_account,
treasury_pool,
Expand Down

0 comments on commit 6518da5

Please sign in to comment.