Skip to content

Commit

Permalink
pytest: convert rpc_state_changes.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_state_changes.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 b7d630f commit a9391d6
Showing 1 changed file with 45 additions and 47 deletions.
92 changes: 45 additions & 47 deletions pytest/tests/sanity/rpc_state_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
# and call various scenarios to trigger store changes.
# Check that the key changes are observable via `changes` RPC call.

import sys
import base58, base64
import json
import struct
import sys
import threading

import deepdiff

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

nodes = start_cluster(
Expand Down Expand Up @@ -325,18 +326,18 @@ def test_key_value_changes():
1. Deploy a contract.
2. Observe the code changes in the block where the transaction outcome "lands".
3. Send two transactions to be included into the same block setting and overriding the value of
the same key (`my_key`).
the same key.
4. Observe the changes in the block where the transaction outcome "lands".
"""

contract_key = nodes[0].signer_key
hello_smart_contract = load_binary_file('testdata/hello.wasm')
contract_blob = load_test_contract()

# Step 1
status = nodes[0].get_status()
latest_block_hash = status['sync_info']['latest_block_hash']
deploy_contract_tx = transaction.sign_deploy_contract_tx(
contract_key, hello_smart_contract, 10,
contract_key, contract_blob, 10,
base58.b58decode(latest_block_hash.encode('utf8')))
deploy_contract_response = nodes[0].send_tx_and_wait(deploy_contract_tx, 10)

Expand Down Expand Up @@ -401,7 +402,7 @@ def test_key_value_changes():
"account_id":
contract_key.account_id,
"code_base64":
base64.b64encode(hello_smart_contract).decode('utf-8'),
base64.b64encode(contract_blob).decode('utf-8'),
}
},]
}
Expand Down Expand Up @@ -429,40 +430,37 @@ def test_key_value_changes():
latest_block_hash = status['sync_info']['latest_block_hash']
function_caller_key = nodes[0].signer_key

def set_value_1():
function_call_1_tx = transaction.sign_function_call_tx(
function_caller_key, contract_key.account_id, 'setKeyValue',
json.dumps({
"key": "my_key",
"value": "my_value_1"
}).encode('utf-8'), 300000000000000, 100000000000, 20,
base58.b58decode(latest_block_hash.encode('utf8')))
nodes[1].send_tx_and_wait(function_call_1_tx, 10)

function_call_1_thread = threading.Thread(target=set_value_1)
function_call_1_thread.start()

function_call_2_tx = transaction.sign_function_call_tx(
function_caller_key, contract_key.account_id, 'setKeyValue',
json.dumps({
"key": "my_key",
"value": "my_value_2"
}).encode('utf-8'), 300000000000000, 100000000000, 30,
base58.b58decode(latest_block_hash.encode('utf8')))
function_call_2_response = nodes[1].send_tx_and_wait(function_call_2_tx, 10)
assert function_call_2_response['result']['receipts_outcome'][0]['outcome']['status'] == {'SuccessValue': ''}, \
"Expected successful execution, but the output was: %s" % function_call_2_response
function_call_1_thread.join()
key = struct.pack('<Q', 42)
key_base64 = base64.b64encode(key).decode('ascii')

tx_block_hash = function_call_2_response['result']['transaction_outcome'][
'block_hash']
def set_value(value, *, nounce):
args = key + struct.pack('<Q', value)
tx = transaction.sign_function_call_tx(
function_caller_key, contract_key.account_id,
'write_key_value', args, 300000000000000, 100000000000, nounce,
base58.b58decode(latest_block_hash.encode('utf8')))
response = nodes[1].send_tx_and_wait(tx, 10)
try:
status = response['result']['receipts_outcome'][0]['outcome'][
'status']
except (KeyError, IndexError):
status = ()
assert 'SuccessValue' in status, (
"Expected successful execution, but the output was: %s" % response)
return response

thread = threading.Thread(target=lambda: set_value(10, nounce=20))
thread.start()
response = set_value(20, nounce=30)
thread.join()

tx_block_hash = response['result']['transaction_outcome']['block_hash']

# Step 4
assert_changes_in_block_response(
request={"block_id": tx_block_hash},
expected_response={
"block_hash":
tx_block_hash,
"block_hash": tx_block_hash,
"changes": [
{
"type": "account_touched",
Expand All @@ -482,7 +480,7 @@ def set_value_1():
base_request = {
"block_id": block_hash,
"changes_type": "data_changes",
"key_prefix_base64": base64.b64encode(b"my_key").decode('utf-8'),
"key_prefix_base64": key_base64,
}
for request in [
# Test empty account_ids
Expand All @@ -502,7 +500,7 @@ def set_value_1():
**base_request,
"account_ids": [contract_key.account_id],
"key_prefix_base64":
base64.b64encode(b"my_key_with_extra").decode('utf-8'),
base64.b64encode(struct.pack('<Q', 24)).decode('ascii'),
},
]:
assert_changes_response(request=request,
Expand All @@ -513,39 +511,39 @@ def set_value_1():

# Test happy-path
expected_response = {
"block_hash":
tx_block_hash,
"block_hash": tx_block_hash,
"changes": [{
"cause": {
"type": "receipt_processing",
},
"type": "data_update",
"change": {
"account_id": contract_key.account_id,
"key_base64": base64.b64encode(b"my_key").decode('utf-8'),
"value_base64": base64.b64encode(b"my_value_1").decode('utf-8'),
"key_base64": key_base64,
"value_base64": base64.b64encode(
struct.pack('<Q', 10)).decode('ascii'),
}
}, {
"cause": {
"type":
"receipt_processing",
"receipt_hash":
function_call_2_response["result"]["receipts_outcome"][0]
["id"],
response["result"]["receipts_outcome"][0]["id"],
},
"type": "data_update",
"change": {
"account_id": contract_key.account_id,
"key_base64": base64.b64encode(b"my_key").decode('utf-8'),
"value_base64": base64.b64encode(b"my_value_2").decode('utf-8'),
"key_base64": key_base64,
"value_base64": base64.b64encode(
struct.pack('<Q', 20)).decode('ascii'),
}
}]
}

base_request = {
"block_id": tx_block_hash,
"changes_type": "data_changes",
"key_prefix_base64": base64.b64encode(b"my_key").decode('utf-8'),
"key_prefix_base64": key_base64,
}
for request in [
{
Expand All @@ -561,12 +559,12 @@ def set_value_1():
{
**base_request,
"account_ids": [contract_key.account_id],
"key_prefix_base64": base64.b64encode(b"").decode('utf-8'),
"key_prefix_base64": '',
},
{
**base_request,
"account_ids": [contract_key.account_id],
"key_prefix_base64": base64.b64encode(b"my_ke").decode('utf-8'),
"key_prefix_base64": base64.b64encode(key[:3]).decode('ascii'),
},
]:
assert_changes_response(
Expand Down

0 comments on commit a9391d6

Please sign in to comment.