Skip to content

Commit

Permalink
pytest: convert rpc_tx_status.py to use test-contract-rs
Browse files Browse the repository at this point in the history
To reduce reliance on WASM blobs stored in repository,
convert the pytest/tests/sanity/rpc_tx_status.py test to
use test-contract-rs, which is built from source, rather
than hello.wasm contract.

Issue: near#4408
  • Loading branch information
mina86 committed Jun 26, 2021
1 parent a9391d6 commit 5f13947
Showing 1 changed file with 50 additions and 31 deletions.
81 changes: 50 additions & 31 deletions pytest/tests/sanity/rpc_tx_status.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,65 @@
import sys
import base58
import json
import struct
import sys

sys.path.append('lib')
from cluster import start_cluster, Key
from utils import load_binary_file
from utils import load_test_contract
import transaction


def submit_tx_and_check(node, tx):
def submit_tx_and_check(nodes, node_index, tx):
node = nodes[node_index]
res = node.send_tx_and_wait(tx, timeout=20)
assert 'error' not in res, res

tx_hash = res['result']['transaction']['hash']
query_res = nodes[0].json_rpc('EXPERIMENTAL_tx_status', [tx_hash, 'test0'])
assert 'error' not in query_res, query_res
receipt_id_from_outcomes = set(map(lambda x: x['id'], query_res['result']['receipts_outcome']))
receipt_id_from_receipts = set(map(lambda x: x['receipt_id'], query_res['result']['receipts']))
is_local_receipt = res['result']['transaction']['signer_id'] == res['result']['transaction']['receiver_id']

receipt_id_from_outcomes = set(
outcome['id'] for outcome in query_res['result']['receipts_outcome'])
receipt_id_from_receipts = set(
rec['receipt_id'] for rec in query_res['result']['receipts'])

is_local_receipt = (res['result']['transaction']['signer_id'] ==
res['result']['transaction']['receiver_id'])
if is_local_receipt:
receipt_id_from_outcomes.remove(res['result']['transaction_outcome']['outcome']['receipt_ids'][0])
assert receipt_id_from_outcomes == receipt_id_from_receipts, f'receipt id from outcomes {receipt_id_from_outcomes}, receipt id from receipts {receipt_id_from_receipts} '


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

status = nodes[0].get_status()
block_hash = status['sync_info']['latest_block_hash']
print("1")
payment_tx = transaction.sign_payment_tx(nodes[0].signer_key, 'test1', 100, 1,
base58.b58decode(block_hash.encode('utf8')))
submit_tx_and_check(nodes[0], payment_tx)

print("2")
deploy_contract_tx = transaction.sign_deploy_contract_tx(nodes[0].signer_key, load_binary_file('testdata/hello.wasm'), 2, base58.b58decode(block_hash.encode('utf8')))
submit_tx_and_check(nodes[0], deploy_contract_tx)

print("3")
function_call_tx = transaction.sign_function_call_tx(nodes[0].signer_key, nodes[0].signer_key.account_id, 'setKeyValue', json.dumps({
"key": "my_key",
"value": "my_value_1"
}).encode('utf-8'), 300000000000000, 0, 3, base58.b58decode(block_hash.encode('utf8')))
submit_tx_and_check(nodes[0], deploy_contract_tx)
receipt_id_from_outcomes.remove(
res['result']['transaction_outcome']['outcome']['receipt_ids'][0])

assert receipt_id_from_outcomes == receipt_id_from_receipts, (
f'receipt id from outcomes {receipt_id_from_outcomes}, '
f'receipt id from receipts {receipt_id_from_receipts}')


def test_tx_status():
nodes = start_cluster(4, 0, 1, None,
[["epoch_length", 1000],
["block_producer_kickout_threshold", 80],
["transaction_validity_period", 10000]],
{})

signer_key = nodes[0].signer_key
status = nodes[0].get_status()
block_hash = status['sync_info']['latest_block_hash']
encoded_block_hash = base58.b58decode(block_hash.encode('ascii'))

payment_tx = transaction.sign_payment_tx(
signer_key, 'test1', 100, 1, encoded_block_hash)
submit_tx_and_check(nodes, 0, payment_tx)

deploy_contract_tx = transaction.sign_deploy_contract_tx(
signer_key, load_test_contract(), 2, encoded_block_hash)
submit_tx_and_check(nodes, 0, deploy_contract_tx)

function_call_tx = transaction.sign_function_call_tx(
signer_key, signer_key.account_id,
'write_key_value', struct.pack('<QQ', 42, 24),
300000000000000, 0, 3, encoded_block_hash)
submit_tx_and_check(nodes, 0, deploy_contract_tx)


if __name__ == '__main__':
test_tx_status()

0 comments on commit 5f13947

Please sign in to comment.