diff --git a/pytest/lib/transaction.py b/pytest/lib/transaction.py index b3ad6ff9c44..59cfbdc020d 100644 --- a/pytest/lib/transaction.py +++ b/pytest/lib/transaction.py @@ -170,6 +170,25 @@ def create_staking_action(amount, pk): action.stake = stake return action +def create_deploy_contract_action(code): + deployContract = DeployContract() + deployContract.code = code + action = Action() + action.enum = 'deployContract' + action.deployContract = deployContract + return action + +def create_function_call_action(methodName, args, gas, deposit): + functionCall = FunctionCall() + functionCall.methodName = methodName + functionCall.args = args + functionCall.gas = gas + functionCall.deposit = deposit + action = Action() + action.enum = 'functionCall' + action.functionCall = functionCall + return action + def sign_payment_tx(key, to, amount, nonce, blockHash): action = create_payment_action(amount) return sign_and_serialize_transaction(to, nonce, [action], blockHash, key.account_id, key.decoded_pk(), key.decoded_sk()) @@ -178,3 +197,10 @@ def sign_staking_tx(signer_key, validator_key, amount, nonce, blockHash): action = create_staking_action(amount, validator_key.decoded_pk()) return sign_and_serialize_transaction(signer_key.account_id, nonce, [action], blockHash, signer_key.account_id, signer_key.decoded_pk(), signer_key.decoded_sk()) +def sign_deploy_contract_tx(signer_key, code, nonce, blockHash): + action = create_deploy_contract_action(code) + return sign_and_serialize_transaction(signer_key.account_id, nonce, [action], blockHash, signer_key.account_id, signer_key.decoded_pk(), signer_key.decoded_sk()) + +def sign_function_call_tx(signer_key, methodName, args, gas, deposit, nonce, blockHash): + action = create_function_call_action(methodName, args, gas, deposit) + return sign_and_serialize_transaction(signer_key.account_id, nonce, [action], blockHash, signer_key.account_id, signer_key.decoded_pk(), signer_key.decoded_sk()) \ No newline at end of file diff --git a/pytest/lib/utils.py b/pytest/lib/utils.py index d77c8d32cde..a5654ca960b 100644 --- a/pytest/lib/utils.py +++ b/pytest/lib/utils.py @@ -117,3 +117,7 @@ def count(self, pattern): return ret else: raise NotImplementedError() + +def load_binary_file(filepath): + with open(filepath, "rb") as binaryfile: + return bytearray(binaryfile.read()) diff --git a/pytest/tests/sanity/block_production.py b/pytest/tests/sanity/block_production.py index 3bbe38d4a1b..c01abaf5576 100644 --- a/pytest/tests/sanity/block_production.py +++ b/pytest/tests/sanity/block_production.py @@ -2,6 +2,13 @@ # Ensures that the nodes remained in sync throughout the process # Sets epoch length to 10 +# Local: +# python tests/sanity/block_production.py +# Remote: +# NEAR_PYTEST_CONFIG=remote.json python tests/sanity/block_production.py + +# Same for all tests that call start_cluster with a None config + import sys, time sys.path.append('lib') @@ -12,12 +19,8 @@ TIMEOUT = 150 BLOCKS = 50 -# Local: nodes = start_cluster(4, 0, 4, None, [["epoch_length", 10], ["block_producer_kickout_threshold", 80]], {}) -# Remote: -# NEAR_PYTEST_CONFIG=remote.json python tests/sanity/block_production.py - started = time.time() max_height = 0 diff --git a/pytest/tests/sanity/deploy_call_smart_contract.py b/pytest/tests/sanity/deploy_call_smart_contract.py new file mode 100644 index 00000000000..24ee2f27f7a --- /dev/null +++ b/pytest/tests/sanity/deploy_call_smart_contract.py @@ -0,0 +1,27 @@ +# Spins up four nodes, deploy an smart contract to one node, +# Call a smart contract method in another node + +import sys, time +import base58 + +sys.path.append('lib') +from cluster import start_cluster +from transaction import sign_deploy_contract_tx, sign_function_call_tx +from utils import load_binary_file + +nodes = start_cluster(4, 0, 4, None, [["epoch_length", 10], ["block_producer_kickout_threshold", 80]], {}) + +status = nodes[0].get_status() +hash_ = status['sync_info']['latest_block_hash'] +hash_ = base58.b58decode(hash_.encode('utf8')) +tx = sign_deploy_contract_tx(nodes[0].signer_key, load_binary_file('../runtime/near-vm-runner/tests/res/test_contract_rs.wasm'), 10, hash_) +nodes[0].send_tx(tx) + +time.sleep(3) + +status2 = nodes[1].get_status() +hash_2 = status2['sync_info']['latest_block_hash'] +hash_2 = base58.b58decode(hash_2.encode('utf8')) +tx2 = sign_function_call_tx(nodes[0].signer_key, 'log_something', [], 100000000000, 100000000000, 20, hash_2) +res = nodes[1].send_tx_and_wait(tx2, 10) +assert res['result']['receipts_outcome'][0]['outcome']['logs'][0] == 'hello' \ No newline at end of file