Skip to content

Commit

Permalink
Add events support, multiple small fixes (0xSpaceShard#33)
Browse files Browse the repository at this point in the history
* Added events
* Fixed wrong name on tx_failure_reason object
* Added check for signatures
  • Loading branch information
dribeiro-ShardLabs committed Feb 1, 2022
1 parent 2eab61e commit 3273d38
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 10 deletions.
17 changes: 16 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 12 additions & 3 deletions starknet_devnet/starknet_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,17 @@ async def invoke(self, transaction: InvokeFunction):
state = await self.__get_state()
invoke_transaction: InternalInvokeFunction = InternalInvokeFunction.from_external(transaction, state.general_config)

signature_list = []
if hasattr(invoke_transaction, 'signature'):
signature_list = invoke_transaction.signature

try:
contract_wrapper = self.__get_contract_wrapper(invoke_transaction.contract_address)
adapted_result, execution_info = await contract_wrapper.call_or_invoke(
Choice.INVOKE,
entry_point_selector=invoke_transaction.entry_point_selector,
calldata=invoke_transaction.calldata,
signature=invoke_transaction.signature
signature=signature_list
)
status = TxStatus.ACCEPTED_ON_L2
error_message = None
Expand All @@ -165,11 +169,16 @@ async def invoke(self, transaction: InvokeFunction):
async def call(self, transaction: InvokeFunction):
"""Perform call according to specifications in `transaction`."""
contract_wrapper = self.__get_contract_wrapper(transaction.contract_address)

signature_list = []
if hasattr(transaction, 'signature'):
signature_list = transaction.signature

adapted_result, _ = await contract_wrapper.call_or_invoke(
Choice.CALL,
entry_point_selector=transaction.entry_point_selector,
calldata=transaction.calldata,
signature=transaction.signature
signature=signature_list
)

return { "result": adapted_result }
Expand All @@ -189,7 +198,7 @@ def get_transaction_status(self, transaction_hash: str):
if "block_hash" in transaction:
ret["block_hash"] = transaction["block_hash"]

failure_key = "transaction_failure_reason"
failure_key = "tx_failure_reason"
if failure_key in transaction:
ret[failure_key] = transaction[failure_key]

Expand Down
8 changes: 7 additions & 1 deletion starknet_devnet/transaction_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ def __init__(
):
self.transaction_hash = tx_details.transaction_hash

events = []

if hasattr(execution_info, 'raw_events'):
events = execution_info.raw_events

self.transaction = {
"status": status.name,
"transaction": tx_details.to_dict(),
Expand All @@ -56,6 +61,7 @@ def __init__(
self.receipt = {
"execution_resources": execution_info.call_info.cairo_usage,
"l2_to_l1_messages": execution_info.l2_to_l1_messages,
"events": events,
"status": status.name,
"transaction_hash": tx_details.transaction_hash,
"transaction_index": 0 # always the first (and only) tx in the block
Expand All @@ -71,7 +77,7 @@ def set_failure_reason(self, error_message: str):
assert error_message
assert self.transaction
assert self.receipt
failure_key = "transaction_failure_reason"
failure_key = "tx_failure_reason"
self.transaction[failure_key] = self.receipt[failure_key] = {
"code": StarknetErrorCode.TRANSACTION_FAILED.name,
"error_message": error_message,
Expand Down
1 change: 1 addition & 0 deletions test/expected/deploy_receipt.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"block_hash": "0x0",
"block_number": 0,
"events": [],
"execution_resources": {
"builtin_instance_counter": {
"bitwise_builtin": 0,
Expand Down
1 change: 1 addition & 0 deletions test/expected/deploy_receipt_auth.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"block_hash": "0x0",
"block_number": 0,
"events": [],
"execution_resources": {
"builtin_instance_counter": {
"bitwise_builtin": 0,
Expand Down
1 change: 1 addition & 0 deletions test/expected/invoke_receipt.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"block_hash": "0x1",
"block_number": 1,
"events": [],
"execution_resources": {
"builtin_instance_counter": {
"bitwise_builtin": 0,
Expand Down
1 change: 1 addition & 0 deletions test/expected/invoke_receipt_auth.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"block_hash": "0x1",
"block_number": 1,
"events": [],
"execution_resources": {
"builtin_instance_counter": {
"bitwise_builtin": 0,
Expand Down
14 changes: 14 additions & 0 deletions test/expected/invoke_receipt_event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"events": [
{
"data": [
0,
10
],
"from_address": 3588522635734560402301506863768658247518049453423086523878030385873504815898,
"keys": [
1744303484486821561902174603220722448499782664094942993128426674277214273437
]
}
]
}
24 changes: 19 additions & 5 deletions test/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@
assert_negative_block_input,
run_devnet_in_background,
assert_block, assert_contract_code, assert_equal, assert_failing_deploy, assert_receipt, assert_salty_deploy,
assert_storage, assert_transaction, assert_tx_status,
assert_storage, assert_transaction, assert_tx_status, assert_events,
call, deploy, invoke
)

ARTIFACTS_PATH = "starknet-hardhat-example/starknet-artifacts/contracts"
CONTRACT_PATH = f"{ARTIFACTS_PATH}/contract.cairo/contract.json"
ABI_PATH = f"{ARTIFACTS_PATH}/contract.cairo/contract_abi.json"
EVENTS_CONTRACT_PATH = f"{ARTIFACTS_PATH}/events.cairo/events.json"
EVENTS_ABI_PATH = f"{ARTIFACTS_PATH}/events.cairo/events_abi.json"
FAILING_CONTRACT_PATH = f"{ARTIFACTS_PATH}/always_fail.cairo/always_fail.json"

EXPECTED_SALTY_DEPLOY_ADDRESS="0x07ef082652cf5e336e98971981f2ef9a32d5673c822898c344b213f51449cb1a"

run_devnet_in_background(sleep_seconds=1)
deploy_info = deploy(CONTRACT_PATH, ["0"])
print("Deployment:", deploy_info)
Expand Down Expand Up @@ -62,12 +66,22 @@
)
assert_equal(value, "40 60", "Checking complex input failed!")

# check deploy when a salt is provided, and use the same contract to test events
assert_salty_deploy(
contract_path=CONTRACT_PATH,
inputs=["0"],
contract_path=EVENTS_CONTRACT_PATH,
salt="0x99",
expected_address="0x0116c1e1281f88c68d7ef61dc7b49bd1d7c4a3dcbe821b1c868735fd712947f0",
expected_tx_hash="0x073a803440143419cbabaf7484c6654dfb0deb4b0f6861190cb6c10c77a959bf"
inputs=None,
expected_address=EXPECTED_SALTY_DEPLOY_ADDRESS,
expected_tx_hash="0x03e3c1a20f6b175b812bb14df175fb8a9e352ea7b38d7b942489968a8a4a9dd0"
)

salty_invoke_tx_hash = invoke(
function="increase_balance",
address=EXPECTED_SALTY_DEPLOY_ADDRESS,
abi_path=EVENTS_ABI_PATH,
inputs=["10"]
)

assert_events(salty_invoke_tx_hash,"test/expected/invoke_receipt_event.json")

assert_failing_deploy(contract_path=FAILING_CONTRACT_PATH)
10 changes: 10 additions & 0 deletions test/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ def assert_receipt(tx_hash, expected_path):
expected_receipt.pop(ignorable_key)
assert_equal(receipt, expected_receipt)

def assert_events(tx_hash, expected_path):
"""Asserts the content of the events element of the receipt of tx with tx_hash."""
output = my_run([
"starknet", "get_transaction_receipt",
"--hash", tx_hash
])
receipt = json.loads(output.stdout)
expected_receipt = load_json_from_path(expected_path)
assert_equal(receipt["events"], expected_receipt["events"])

def get_block(block_number=None, parse=False):
"""Get the block with block_number. If no number provided, return the last."""
args = [
Expand Down

0 comments on commit 3273d38

Please sign in to comment.