Skip to content

Commit

Permalink
Add more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
andreibancioiu committed Oct 9, 2024
1 parent ed30157 commit 718c233
Showing 1 changed file with 116 additions and 13 deletions.
129 changes: 116 additions & 13 deletions systemtests/generate_testdata_on_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
TransactionsFactoryConfig,
TransferTransactionsFactory, UserSecretKey,
UserSigner)
from multiversx_sdk.abi import AddressValue, Serializer, StringValue, U32Value
from multiversx_sdk.network_providers.transactions import TransactionOnNetwork

from systemtests.config import CONFIGURATIONS, Configuration
Expand Down Expand Up @@ -74,39 +75,45 @@ def do_run(args: Any):
controller = Controller(configuration, accounts, memento)

print("## Intra-shard, simple MoveBalance with refund")
controller.send(controller.create_simple_move_balance_with_refund(
controller.send(controller.create_simple_move_balance(
sender=accounts.get_user(shard=0, index=0),
receiver=accounts.get_user(shard=0, index=1).address,
additional_gas_limit=42000,
), await_completion=True)

print("## Cross-shard, simple MoveBalance with refund")
controller.send(controller.create_simple_move_balance_with_refund(
controller.send(controller.create_simple_move_balance(
sender=accounts.get_user(shard=0, index=1),
receiver=accounts.get_user(shard=1, index=0).address,
additional_gas_limit=42000,
), await_completion=True)

print("## Intra-shard, invalid MoveBalance with refund")
controller.send(controller.create_invalid_move_balance_with_refund(
controller.send(controller.create_invalid_move_balance(
sender=accounts.get_user(shard=0, index=2),
receiver=accounts.get_user(shard=0, index=3).address,
additional_gas_limit=42000,
), await_completion=True)

print("## Cross-shard, invalid MoveBalance with refund")
controller.send(controller.create_invalid_move_balance_with_refund(
controller.send(controller.create_invalid_move_balance(
sender=accounts.get_user(shard=0, index=4),
receiver=accounts.get_user(shard=1, index=1).address,
additional_gas_limit=42000,
), await_completion=True)

print("## Intra-shard, sending value to non-payable contract")
controller.send(controller.create_simple_move_balance_with_refund(
controller.send(controller.create_simple_move_balance(
sender=accounts.get_user(shard=0, index=0),
receiver=accounts.get_contract_address("adder", 0, 0),
additional_gas_limit=42000,
), await_completion=True)

print("## Cross-shard, sending value to non-payable contract")
controller.send(controller.create_simple_move_balance_with_refund(
controller.send(controller.create_simple_move_balance(
sender=accounts.get_user(shard=0, index=1),
receiver=accounts.get_contract_address("adder", 1, 0),
additional_gas_limit=42000,
), await_completion=True)

print("## Intra-shard, native transfer within MultiESDTTransfer")
Expand Down Expand Up @@ -347,6 +354,70 @@ def do_run(args: Any):
parent_contract=accounts.get_contract_address("developerRewards", shard=1, index=0),
), await_completion=True)

print("## Relayed v3, intra-shard, a few identical transactions (same nonce)")
controller.send(controller.create_relayed_v3_with_inner_transactions(
relayer=accounts.get_user(shard=0, index=0),
inner_transactions=[
controller.create_native_transfer_within_multiesdt_transfer_and_execute(
sender=accounts.get_user(shard=0, index=3),
contract=accounts.get_contract_address("dummy", shard=0, index=0),
function="doSomething",
native_amount=42,
custom_amount=7,
seal_for_broadcast=True,
)
] * 5
), await_completion=True)

print("## Relayed v3, cross-shard, a few identical transactions (same nonce)")
controller.send(controller.create_relayed_v3_with_inner_transactions(
relayer=accounts.get_user(shard=0, index=0),
inner_transactions=[
controller.create_native_transfer_within_multiesdt_transfer_and_execute(
sender=accounts.get_user(shard=0, index=3),
contract=accounts.get_contract_address("dummy", shard=2, index=0),
function="doSomething",
native_amount=42,
custom_amount=7,
seal_for_broadcast=True,
)
] * 5
), await_completion=True)

print("## Relayed v3, cross-shard, a few identical transactions (same nonce) - move balances")
controller.send(controller.create_relayed_v3_with_inner_transactions(
relayer=accounts.get_user(shard=0, index=0),
inner_transactions=[
controller.create_simple_move_balance(
sender=accounts.get_user(shard=0, index=3),
receiver=accounts.get_user(shard=2, index=3).address)
] * 5
), await_completion=True)

print("## Intra-shard, transfer native within MultiESDTTransfer (fuzzy, with tx.value != 0)")
controller.send(controller.create_native_transfer_within_multiesdt_fuzzy(
sender=accounts.get_user(shard=0, index=4),
receiver=accounts.get_user(shard=0, index=5).address,
native_amount_as_value=42,
native_amount_in_data=[43, 44]
), await_completion=True)

print("## Intra-shard, transfer native within MultiESDTTransfer (fuzzy, with tx.value == 0)")
controller.send(controller.create_native_transfer_within_multiesdt_fuzzy(
sender=accounts.get_user(shard=0, index=4),
receiver=accounts.get_user(shard=0, index=5).address,
native_amount_as_value=0,
native_amount_in_data=[43, 44]
), await_completion=True)

print("## Cross-shard, transfer native within MultiESDTTransfer (fuzzy, with tx.value == 0)")
controller.send(controller.create_native_transfer_within_multiesdt_fuzzy(
sender=accounts.get_user(shard=0, index=4),
receiver=accounts.get_user(shard=1, index=5).address,
native_amount_as_value=0,
native_amount_in_data=[43, 44]
), await_completion=True)


class BunchOfAccounts:
def __init__(self, configuration: Configuration, memento: "Memento") -> None:
Expand Down Expand Up @@ -667,28 +738,28 @@ def do_change_contract_owner(self, contract: Address, new_owner: "Account"):
self.send(transaction)
self.await_completed([transaction])

def create_simple_move_balance_with_refund(self, sender: "Account", receiver: Address) -> Transaction:
def create_simple_move_balance(self, sender: "Account", receiver: Address, additional_gas_limit: int = 0) -> Transaction:
transaction = self.transfer_transactions_factory.create_transaction_for_native_token_transfer(
sender=sender.address,
receiver=receiver,
native_amount=42
)

transaction.gas_limit += 42000
transaction.gas_limit += additional_gas_limit

self.apply_nonce(transaction)
self.sign(transaction)

return transaction

def create_invalid_move_balance_with_refund(self, sender: "Account", receiver: Address) -> Transaction:
def create_invalid_move_balance(self, sender: "Account", receiver: Address, additional_gas_limit: int = 0) -> Transaction:
transaction = self.transfer_transactions_factory.create_transaction_for_native_token_transfer(
sender=sender.address,
receiver=receiver,
native_amount=1000000000000000000000000
)

transaction.gas_limit += 42000
transaction.gas_limit += additional_gas_limit

self.apply_nonce(transaction)
self.sign(transaction)
Expand All @@ -710,6 +781,35 @@ def create_native_transfer_within_multiesdt(self, sender: "Account", receiver: A

return transaction

def create_native_transfer_within_multiesdt_fuzzy(self, sender: "Account", receiver: Address, native_amount_as_value: int, native_amount_in_data: list[int]) -> Transaction:
serializer = Serializer(parts_separator="@")

data_args = [
AddressValue(receiver.pubkey),
U32Value(len(native_amount_in_data))
]

for amount in native_amount_in_data:
data_args.append(StringValue("EGLD-000000"))
data_args.append(U32Value(0))
data_args.append(U32Value(amount))

data = "MultiESDTNFTTransfer@" + serializer.serialize(data_args)

transaction = Transaction(
sender=sender.address.to_bech32(),
receiver=sender.address.to_bech32(),
gas_limit=5000000,
chain_id=self.configuration.network_id,
value=native_amount_as_value,
data=data.encode()
)

self.apply_nonce(transaction)
self.sign(transaction)

return transaction

def create_native_transfer_within_multiesdt_transfer_and_execute(self, sender: "Account", contract: Address, function: str, native_amount: int, custom_amount: int, seal_for_broadcast: bool = True) -> Transaction:
custom_currency = self.memento.get_custom_currencies()[0]

Expand Down Expand Up @@ -878,9 +978,12 @@ def create_relayed_v3_with_inner_transactions(self, relayer: "Account", inner_tr

for inner_transaction in inner_transactions:
inner_transaction.relayer = relayer.address.to_bech32()
self.apply_nonce(inner_transaction)

# Even if it was already signed, we need to sign it again (because it has been modified).
# If it was previously signed, we don't alter the nonce.
if not inner_transaction.signature:
self.apply_nonce(inner_transaction)

# Even if it was already signed, we need to sign it again (because the field "relayer" has been modified).
self.sign(inner_transaction)

transaction = self.relayed_transactions_factory.create_relayed_v3_transaction(
Expand Down Expand Up @@ -925,7 +1028,7 @@ def create_contract_call_with_move_balance_with_signal_error(self, sender: "Acco
transaction = self.contracts_transactions_factory.create_transaction_for_execute(
sender=sender.address,
contract=contract,
function="doSomething",
function="missingFunction",
gas_limit=5000000,
arguments=[1, 2, 3, 4, 5],
native_transfer_amount=amount
Expand Down

0 comments on commit 718c233

Please sign in to comment.