Skip to content

Commit

Permalink
pytest: introduce helper load_test_contract function (near#4415)
Browse files Browse the repository at this point in the history
Issue: near#4408
  • Loading branch information
mina86 authored Jun 26, 2021
1 parent 9c10e80 commit b7d630f
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 44 deletions.
10 changes: 10 additions & 0 deletions pytest/lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,16 @@ def load_binary_file(filepath):
return bytearray(binaryfile.read())


def load_test_contract(filename='test_contract_rs.wasm'):
"""Loads a WASM file from near-test-contracts package.
This is just a convenience function around load_binary_file which loads
files from ../runtime/near-test-contracts/res directory. By default
test_contract_rs.wasm is loaded.
"""
return load_binary_file('../runtime/near-test-contracts/res/' + filename)


def compile_rust_contract(content):
empty_contract_rs = os.path.join(os.path.dirname(__file__),
'../empty-contract-rs')
Expand Down
8 changes: 3 additions & 5 deletions pytest/tests/contracts/deploy_call_smart_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
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, compile_rust_contract
from utils import load_binary_file, load_test_contract, compile_rust_contract

nodes = start_cluster(
4, 0, 4, None,
Expand All @@ -16,10 +16,8 @@
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-test-contracts/res/test_contract_rs.wasm'), 10, hash_)
tx = sign_deploy_contract_tx(nodes[0].signer_key, load_test_contract(),
10, hash_)
nodes[0].send_tx(tx)

time.sleep(3)
Expand Down
5 changes: 2 additions & 3 deletions pytest/tests/contracts/gibberish.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
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
from utils import load_test_contract

nodes = start_cluster(
3, 0, 4, None,
[["epoch_length", 1000], ["block_producer_kickout_threshold", 80]], {})

wasm_blob_1 = load_binary_file(
'../runtime/near-test-contracts/res/test_contract_rs.wasm')
wasm_blob_1 = load_test_contract()

status = nodes[0].get_status()
hash_ = status['sync_info']['latest_block_hash']
Expand Down
6 changes: 2 additions & 4 deletions pytest/tests/contracts/infinite_loops.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
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, compile_rust_contract
from utils import load_test_contract

nodes = start_cluster(
4, 0, 1, None,
Expand All @@ -17,9 +17,7 @@
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-test-contracts/res/test_contract_rs.wasm'), 10, hash_)
tx = sign_deploy_contract_tx(nodes[0].signer_key, load_test_contract(), 10, hash_)
nodes[0].send_tx(tx)

time.sleep(3)
Expand Down
7 changes: 2 additions & 5 deletions pytest/tests/mocknet/sanity.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import mocknet
from transaction import sign_payment_tx, sign_function_call_tx, sign_deploy_contract_tx
from utils import load_binary_file
from utils import load_test_contract

nodes = mocknet.get_nodes()
accounts = mocknet.accounts_from_nodes(nodes)
Expand Down Expand Up @@ -48,10 +48,7 @@
# Test contract deployment

tx = sign_deploy_contract_tx(
accounts[2],
load_binary_file(
'../runtime/near-test-contracts/res/test_contract_rs.wasm'),
nonces[2] + 1, last_block_hash_decoded)
accounts[2], load_test_contract(), nonces[2] + 1, last_block_hash_decoded)
nodes[0].send_tx_and_wait(tx, timeout=20)

tx2 = sign_function_call_tx(accounts[2], accounts[2].account_id,
Expand Down
8 changes: 3 additions & 5 deletions pytest/tests/sandbox/patch_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from cluster import start_cluster
from transaction import sign_deploy_contract_tx, sign_function_call_tx
from utils import load_binary_file
from utils import load_test_contract

CONFIG = {
'local': True,
Expand All @@ -27,10 +27,8 @@
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-test-contracts/res/test_contract_rs.wasm'), 10, hash_)
tx = sign_deploy_contract_tx(nodes[0].signer_key, load_test_contract(), 10,
hash_)
nodes[0].send_tx(tx)
time.sleep(3)

Expand Down
9 changes: 3 additions & 6 deletions pytest/tests/sanity/backward_compatible.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import branches
import cluster
from utils import load_binary_file
from utils import load_test_contract
from transaction import sign_deploy_contract_tx, sign_function_call_tx, sign_payment_tx, sign_create_account_with_full_access_key_and_balance_tx

def main():
Expand Down Expand Up @@ -75,11 +75,8 @@ def main():
block_height = status['sync_info']['latest_block_height']
nonce = block_height * 1_000_000 - 1

tx = sign_deploy_contract_tx(
new_signer_key,
load_binary_file(
'../runtime/near-test-contracts/res/test_contract_rs.wasm'), nonce,
block_hash)
tx = sign_deploy_contract_tx(new_signer_key, load_test_contract(), nonce,
block_hash)
res = stable_node.send_tx_and_wait(tx, timeout=20)
assert 'error' not in res, res

Expand Down
8 changes: 3 additions & 5 deletions pytest/tests/sanity/concurrent_function_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
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, compile_rust_contract
from utils import load_test_contract

nodes = start_cluster(
4, 0, 4, None,
Expand All @@ -19,10 +19,8 @@
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-test-contracts/res/test_contract_rs.wasm'), 10, hash_)
tx = sign_deploy_contract_tx(nodes[0].signer_key, load_test_contract(), 10,
hash_)
nodes[0].send_tx(tx)

time.sleep(3)
Expand Down
8 changes: 3 additions & 5 deletions pytest/tests/sanity/db_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import branches
import cluster
from utils import wait_for_blocks_or_timeout, load_binary_file
from utils import wait_for_blocks_or_timeout, load_test_contract
from transaction import sign_deploy_contract_tx, sign_function_call_tx

logging.basicConfig(level=logging.INFO)
Expand All @@ -27,10 +27,8 @@ def deploy_contract(node):
status = node.get_status()
hash_ = status['sync_info']['latest_block_hash']
hash_ = base58.b58decode(hash_.encode('utf8'))
tx = sign_deploy_contract_tx(
node.signer_key,
load_binary_file(
'../runtime/near-test-contracts/res/test_contract_rs.wasm'), 10, hash_)
tx = sign_deploy_contract_tx(node.signer_key, load_test_contract(), 10,
hash_)
node.send_tx_and_wait(tx, timeout=15)
wait_for_blocks_or_timeout(node, 3, 100)

Expand Down
9 changes: 3 additions & 6 deletions pytest/tests/sanity/upgradable.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import branches
import cluster
from utils import wait_for_blocks_or_timeout, load_binary_file
from utils import wait_for_blocks_or_timeout, load_test_contract
from transaction import sign_deploy_contract_tx, sign_function_call_tx, sign_payment_tx, \
sign_create_account_tx, sign_delete_account_tx, sign_create_account_with_full_access_key_and_balance_tx

Expand Down Expand Up @@ -77,11 +77,8 @@ def main():
# deploy a contract
status = nodes[0].get_status()
hash = status['sync_info']['latest_block_hash']
tx = sign_deploy_contract_tx(
nodes[0].signer_key,
load_binary_file(
'../runtime/near-test-contracts/res/test_contract_rs.wasm'), 1,
base58.b58decode(hash.encode('utf8')))
tx = sign_deploy_contract_tx(nodes[0].signer_key, load_test_contract(), 1,
base58.b58decode(hash.encode('utf8')))
res = nodes[0].send_tx_and_wait(tx, timeout=20)
assert 'error' not in res, res

Expand Down

0 comments on commit b7d630f

Please sign in to comment.