Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
frol committed Mar 6, 2020
1 parent a62a421 commit 2f22a6a
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 48 deletions.
13 changes: 12 additions & 1 deletion pytest/lib/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ def sign_and_serialize_transaction(receiverId, nonce, actions, blockHash, accoun

return BinarySerializer(tx_schema).serialize(signedTx)

def create_create_account_action():
createAccount = CreateAccount()
action = Action()
action.enum = 'createAccount'
action.createAccount = createAccount
return action

def create_payment_action(amount):
transfer = Transfer()
transfer.deposit = amount
Expand Down Expand Up @@ -189,6 +196,10 @@ def create_function_call_action(methodName, args, gas, deposit):
action.functionCall = functionCall
return action

def sign_create_account_tx(key, new_account_id, nonce, blockHash):
action = create_create_account_action()
return sign_and_serialize_transaction(new_account_id, nonce, [action], blockHash, key.account_id, key.decoded_pk(), key.decoded_sk())

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())
Expand All @@ -203,4 +214,4 @@ def sign_deploy_contract_tx(signer_key, code, nonce, blockHash):

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())
return sign_and_serialize_transaction(signer_key.account_id, nonce, [action], blockHash, signer_key.account_id, signer_key.decoded_pk(), signer_key.decoded_sk())
139 changes: 92 additions & 47 deletions pytest/tests/sanity/rpc_key_value_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Check that the key changes are observable via `changes` RPC call.


import sys, time
import sys
import base58, base64
import json
import threading
Expand All @@ -12,11 +12,54 @@

sys.path.append('lib')
from cluster import start_cluster
from transaction import sign_deploy_contract_tx, sign_function_call_tx
from transaction import sign_create_account_tx, sign_deploy_contract_tx, sign_function_call_tx
from utils import load_binary_file

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

status = nodes[0].get_status()
latest_block_hash = status['sync_info']['latest_block_hash']
new_account_id = 'rpc_key_value_changes'
create_account_tx = sign_create_account_tx(
nodes[0].signer_key,
new_account_id,
9,
base58.b58decode(latest_block_hash.encode('utf8'))
)
new_account_response = nodes[0].send_tx_and_wait(create_account_tx, 10)

state_changes_request = {
"block_id": new_account_response['result']['receipts_outcome'][0]['block_hash'],
"changes_type": "account_changes",
"account_id": new_account_id,
}
expected_changes_response = {
"block_hash": state_changes_request["block_id"],
"changes": [
{
"cause": {
"type": "receipt_processing",
"receipt_hash": new_account_response['result']['receipts_outcome'][0]['id']
},
"type": "account_update",
"change": {
"account_id": new_account_id,
"amount": "0",
"locked": "0",
"code_hash": "11111111111111111111111111111111",
"storage_usage": 100,
}
}
]
}

for node in nodes:
changes_response = nodes[0].get_changes(state_changes_request)['result']
del changes_response['changes'][0]['change']['storage_paid_at']
assert not deepdiff.DeepDiff(changes_response, expected_changes_response), \
"query same changes gives different results:\n%r" \
% changes_response

status = nodes[0].get_status()
latest_block_hash = status['sync_info']['latest_block_hash']
deploy_contract_tx = sign_deploy_contract_tx(
Expand All @@ -25,9 +68,7 @@
10,
base58.b58decode(latest_block_hash.encode('utf8'))
)
nodes[0].send_tx(deploy_contract_tx)

time.sleep(3)
nodes[0].send_tx_and_wait(deploy_contract_tx, 10)

status = nodes[1].get_status()
latest_block_hash = status['sync_info']['latest_block_hash']
Expand All @@ -43,24 +84,21 @@ def set_value_1():
base58.b58decode(latest_block_hash.encode('utf8'))
)
res = nodes[1].send_tx_and_wait(function_call_1_tx, 10)
print("SET VALUE 1: ", res)
function_call_1_thread = threading.Thread(target=set_value_1)
function_call_1_thread.start()
#print(res)

function_call_2_tx = sign_function_call_tx(
nodes[0].signer_key,
'setKeyValue',
json.dumps({"key": "my_key", "value": "my_value_2"}).encode('utf-8'),
100000000000,
100000000000,
20,
30,
base58.b58decode(latest_block_hash.encode('utf8'))
)
res = nodes[1].send_tx_and_wait(function_call_2_tx, 10)
print(res)
assert res['result']['receipts_outcome'][0]['outcome']['status'] == {'SuccessValue': ''}, "Expected successful execution, but the output was: %s" % res
print(nodes[1].get_chunk([res['result']['transaction_outcome']['block_hash'], 0]))
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()

# send method=changes params:={"block_id": block_hash, "changes_type": "data_changes", "account_id": account_id, "key_prefix": key_prefix_as_base64_encoded_string}
Expand All @@ -87,42 +125,49 @@ def set_value_1():
# }
#}

tx_block_hash = res['result']['transaction_outcome']['block_hash']
tx_block_hash = function_call_2_response['result']['transaction_outcome']['block_hash']
tx_account_id = nodes[0].signer_key.account_id

changes_response = nodes[0].get_changes(
{
"block_id": tx_block_hash,
"changes_type": "data_changes",
"account_id": tx_account_id,
"key_prefix_base64": base64.b64encode(b"my_key").decode('utf-8'),
}
)
print(changes_response)
changes = changes_response['result']['changes']
assert len(changes) == 2
for change in changes:
assert change['type'] == 'data_update'
assert change['cause']['type'] == 'receipt_processing'
assert change['change']['key_base64'] == base64.b64encode(b"my_key"), \
"changed key is expected to be 'my_key' as a base64 encoded string, but %r found" \
% change['change']['key_base64']

assert changes[0]['change']['value'] == base64.b64encode(b"my_value_1"), \
"changed value is expected to be 'my_value_1' as a base64 encoded string, but %r found" \
% changes[0]['change']['value']
assert changes[1]['change']['value'] == base64.b64encode(b"my_value_2"), \
"changed value is expected to be 'my_value_2' as a base64 encoded string, but %r found" \
% changes[1]['change']['value']


for node in nodes[1:]:
changes_response_from_another_node = node.get_changes(
state_changes_request = {
"block_id": tx_block_hash,
"changes_type": "data_changes",
"account_id": tx_account_id,
"key_prefix_base64": base64.b64encode(b"my_key").decode('utf-8'),
}

expected_changes_response = {
"block_hash": state_changes_request["block_id"],
"changes": [
{
'cause': {
'type': 'receipt_processing',
},
'type': 'data_update',
'change': {
'key_base64': base64.b64encode(b"my_key").decode('utf-8'),
'value_base64': base64.b64encode(b"my_value_1").decode('utf-8'),
}
},
{
"block_id": tx_block_hash,
"changes_type": "data_changes",
"account_id": tx_account_id,
"key_prefix": base64.b64encode(b"my_key").decode('utf-8')
'cause': {
'type': 'receipt_processing',
'receipt_hash': function_call_2_response['result']['receipts_outcome'][0]['id']
},
'type': 'data_update',
'change': {
'key_base64': base64.b64encode(b"my_key").decode('utf-8'),
'value_base64': base64.b64encode(b"my_value_2").decode('utf-8'),
}
}
)
assert not deepdiff.DeepDiff(changes_response_from_another_node, changes_response), "query same changes gives different result"
]
}


for node in nodes:
changes_response = node.get_changes(state_changes_request)['result']
# We fetch the transaction in a separate thread, so receiving the right receipt hash is a bit
# involved process, nevermind.
del changes_response['changes'][0]['cause']['receipt_hash']
assert not deepdiff.DeepDiff(changes_response, expected_changes_response), \
"query same changes gives different results:\n%r" \
% (changes_response)

0 comments on commit 2f22a6a

Please sign in to comment.