From 3273d38c7d558a919a26844773a57b106dcf2c23 Mon Sep 17 00:00:00 2001 From: dribeiro-ShardLabs <94369106+dribeiro-ShardLabs@users.noreply.github.com> Date: Tue, 1 Feb 2022 16:29:29 +0000 Subject: [PATCH] Add events support, multiple small fixes (#33) * Added events * Fixed wrong name on tx_failure_reason object * Added check for signatures --- poetry.lock | 17 ++++++++++++++++- starknet_devnet/starknet_wrapper.py | 15 ++++++++++++--- starknet_devnet/transaction_wrapper.py | 8 +++++++- test/expected/deploy_receipt.json | 1 + test/expected/deploy_receipt_auth.json | 1 + test/expected/invoke_receipt.json | 1 + test/expected/invoke_receipt_auth.json | 1 + test/expected/invoke_receipt_event.json | 14 ++++++++++++++ test/test_cli.py | 24 +++++++++++++++++++----- test/util.py | 10 ++++++++++ 10 files changed, 82 insertions(+), 10 deletions(-) create mode 100644 test/expected/invoke_receipt_event.json diff --git a/poetry.lock b/poetry.lock index 5500b3a0c..f9444af26 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1116,7 +1116,7 @@ testing = ["pytest (>=4.6)", "pytest-checkdocs (>=2.4)", "pytest-flake8", "pytes [metadata] lock-version = "1.1" python-versions = "^3.7" -content-hash = "778e099eaea79ed10683fff2bd9d1724acb4e07648f7af9dc93f3fddc214460a" +content-hash = "2ac4ab70666f6407a977e8291f7f2eb0c9038c48cf3189d74a6ba28676a03421" [metadata.files] aiohttp = [ @@ -1466,6 +1466,9 @@ markupsafe = [ {file = "MarkupSafe-2.0.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:2d7d807855b419fc2ed3e631034685db6079889a1f01d5d9dac950f764da3dad"}, {file = "MarkupSafe-2.0.1-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_12_i686.manylinux2010_i686.whl", hash = "sha256:add36cb2dbb8b736611303cd3bfcee00afd96471b09cda130da3581cbdc56a6d"}, {file = "MarkupSafe-2.0.1-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:168cd0a3642de83558a5153c8bd34f175a9a6e7f6dc6384b9655d2697312a646"}, + {file = "MarkupSafe-2.0.1-cp310-cp310-musllinux_1_1_aarch64.whl", hash = "sha256:4dc8f9fb58f7364b63fd9f85013b780ef83c11857ae79f2feda41e270468dd9b"}, + {file = "MarkupSafe-2.0.1-cp310-cp310-musllinux_1_1_i686.whl", hash = "sha256:20dca64a3ef2d6e4d5d615a3fd418ad3bde77a47ec8a23d984a12b5b4c74491a"}, + {file = "MarkupSafe-2.0.1-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:cdfba22ea2f0029c9261a4bd07e830a8da012291fbe44dc794e488b6c9bb353a"}, {file = "MarkupSafe-2.0.1-cp310-cp310-win32.whl", hash = "sha256:99df47edb6bda1249d3e80fdabb1dab8c08ef3975f69aed437cb69d0a5de1e28"}, {file = "MarkupSafe-2.0.1-cp310-cp310-win_amd64.whl", hash = "sha256:e0f138900af21926a02425cf736db95be9f4af72ba1bb21453432a07f6082134"}, {file = "MarkupSafe-2.0.1-cp36-cp36m-macosx_10_9_x86_64.whl", hash = "sha256:f9081981fe268bd86831e5c75f7de206ef275defcb82bc70740ae6dc507aee51"}, @@ -1477,6 +1480,9 @@ markupsafe = [ {file = "MarkupSafe-2.0.1-cp36-cp36m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:bf5d821ffabf0ef3533c39c518f3357b171a1651c1ff6827325e4489b0e46c3c"}, {file = "MarkupSafe-2.0.1-cp36-cp36m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_12_i686.manylinux2010_i686.whl", hash = "sha256:0d4b31cc67ab36e3392bbf3862cfbadac3db12bdd8b02a2731f509ed5b829724"}, {file = "MarkupSafe-2.0.1-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:baa1a4e8f868845af802979fcdbf0bb11f94f1cb7ced4c4b8a351bb60d108145"}, + {file = "MarkupSafe-2.0.1-cp36-cp36m-musllinux_1_1_aarch64.whl", hash = "sha256:deb993cacb280823246a026e3b2d81c493c53de6acfd5e6bfe31ab3402bb37dd"}, + {file = "MarkupSafe-2.0.1-cp36-cp36m-musllinux_1_1_i686.whl", hash = "sha256:63f3268ba69ace99cab4e3e3b5840b03340efed0948ab8f78d2fd87ee5442a4f"}, + {file = "MarkupSafe-2.0.1-cp36-cp36m-musllinux_1_1_x86_64.whl", hash = "sha256:8d206346619592c6200148b01a2142798c989edcb9c896f9ac9722a99d4e77e6"}, {file = "MarkupSafe-2.0.1-cp36-cp36m-win32.whl", hash = "sha256:6c4ca60fa24e85fe25b912b01e62cb969d69a23a5d5867682dd3e80b5b02581d"}, {file = "MarkupSafe-2.0.1-cp36-cp36m-win_amd64.whl", hash = "sha256:b2f4bf27480f5e5e8ce285a8c8fd176c0b03e93dcc6646477d4630e83440c6a9"}, {file = "MarkupSafe-2.0.1-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:0717a7390a68be14b8c793ba258e075c6f4ca819f15edfc2a3a027c823718567"}, @@ -1488,6 +1494,9 @@ markupsafe = [ {file = "MarkupSafe-2.0.1-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:e9936f0b261d4df76ad22f8fee3ae83b60d7c3e871292cd42f40b81b70afae85"}, {file = "MarkupSafe-2.0.1-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_12_i686.manylinux2010_i686.whl", hash = "sha256:2a7d351cbd8cfeb19ca00de495e224dea7e7d919659c2841bbb7f420ad03e2d6"}, {file = "MarkupSafe-2.0.1-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:60bf42e36abfaf9aff1f50f52644b336d4f0a3fd6d8a60ca0d054ac9f713a864"}, + {file = "MarkupSafe-2.0.1-cp37-cp37m-musllinux_1_1_aarch64.whl", hash = "sha256:d6c7ebd4e944c85e2c3421e612a7057a2f48d478d79e61800d81468a8d842207"}, + {file = "MarkupSafe-2.0.1-cp37-cp37m-musllinux_1_1_i686.whl", hash = "sha256:f0567c4dc99f264f49fe27da5f735f414c4e7e7dd850cfd8e69f0862d7c74ea9"}, + {file = "MarkupSafe-2.0.1-cp37-cp37m-musllinux_1_1_x86_64.whl", hash = "sha256:89c687013cb1cd489a0f0ac24febe8c7a666e6e221b783e53ac50ebf68e45d86"}, {file = "MarkupSafe-2.0.1-cp37-cp37m-win32.whl", hash = "sha256:a30e67a65b53ea0a5e62fe23682cfe22712e01f453b95233b25502f7c61cb415"}, {file = "MarkupSafe-2.0.1-cp37-cp37m-win_amd64.whl", hash = "sha256:611d1ad9a4288cf3e3c16014564df047fe08410e628f89805e475368bd304914"}, {file = "MarkupSafe-2.0.1-cp38-cp38-macosx_10_9_universal2.whl", hash = "sha256:5bb28c636d87e840583ee3adeb78172efc47c8b26127267f54a9c0ec251d41a9"}, @@ -1500,6 +1509,9 @@ markupsafe = [ {file = "MarkupSafe-2.0.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:6fcf051089389abe060c9cd7caa212c707e58153afa2c649f00346ce6d260f1b"}, {file = "MarkupSafe-2.0.1-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_12_i686.manylinux2010_i686.whl", hash = "sha256:5855f8438a7d1d458206a2466bf82b0f104a3724bf96a1c781ab731e4201731a"}, {file = "MarkupSafe-2.0.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:3dd007d54ee88b46be476e293f48c85048603f5f516008bee124ddd891398ed6"}, + {file = "MarkupSafe-2.0.1-cp38-cp38-musllinux_1_1_aarch64.whl", hash = "sha256:aca6377c0cb8a8253e493c6b451565ac77e98c2951c45f913e0b52facdcff83f"}, + {file = "MarkupSafe-2.0.1-cp38-cp38-musllinux_1_1_i686.whl", hash = "sha256:04635854b943835a6ea959e948d19dcd311762c5c0c6e1f0e16ee57022669194"}, + {file = "MarkupSafe-2.0.1-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:6300b8454aa6930a24b9618fbb54b5a68135092bc666f7b06901f897fa5c2fee"}, {file = "MarkupSafe-2.0.1-cp38-cp38-win32.whl", hash = "sha256:023cb26ec21ece8dc3907c0e8320058b2e0cb3c55cf9564da612bc325bed5e64"}, {file = "MarkupSafe-2.0.1-cp38-cp38-win_amd64.whl", hash = "sha256:984d76483eb32f1bcb536dc27e4ad56bba4baa70be32fa87152832cdd9db0833"}, {file = "MarkupSafe-2.0.1-cp39-cp39-macosx_10_9_universal2.whl", hash = "sha256:2ef54abee730b502252bcdf31b10dacb0a416229b72c18b19e24a4509f273d26"}, @@ -1512,6 +1524,9 @@ markupsafe = [ {file = "MarkupSafe-2.0.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:c47adbc92fc1bb2b3274c4b3a43ae0e4573d9fbff4f54cd484555edbf030baf1"}, {file = "MarkupSafe-2.0.1-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_12_i686.manylinux2010_i686.whl", hash = "sha256:37205cac2a79194e3750b0af2a5720d95f786a55ce7df90c3af697bfa100eaac"}, {file = "MarkupSafe-2.0.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:1f2ade76b9903f39aa442b4aadd2177decb66525062db244b35d71d0ee8599b6"}, + {file = "MarkupSafe-2.0.1-cp39-cp39-musllinux_1_1_aarch64.whl", hash = "sha256:4296f2b1ce8c86a6aea78613c34bb1a672ea0e3de9c6ba08a960efe0b0a09047"}, + {file = "MarkupSafe-2.0.1-cp39-cp39-musllinux_1_1_i686.whl", hash = "sha256:9f02365d4e99430a12647f09b6cc8bab61a6564363f313126f775eb4f6ef798e"}, + {file = "MarkupSafe-2.0.1-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:5b6d930f030f8ed98e3e6c98ffa0652bdb82601e7a016ec2ab5d7ff23baa78d1"}, {file = "MarkupSafe-2.0.1-cp39-cp39-win32.whl", hash = "sha256:10f82115e21dc0dfec9ab5c0223652f7197feb168c940f3ef61563fc2d6beb74"}, {file = "MarkupSafe-2.0.1-cp39-cp39-win_amd64.whl", hash = "sha256:693ce3f9e70a6cf7d2fb9e6c9d8b204b6b39897a2c4a1aa65728d5ac97dcc1d8"}, {file = "MarkupSafe-2.0.1.tar.gz", hash = "sha256:594c67807fb16238b30c44bdf74f36c02cdf22d1c8cda91ef8a0ed8dabf5620a"}, diff --git a/starknet_devnet/starknet_wrapper.py b/starknet_devnet/starknet_wrapper.py index 891c1eec4..8ff16495c 100644 --- a/starknet_devnet/starknet_wrapper.py +++ b/starknet_devnet/starknet_wrapper.py @@ -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 @@ -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 } @@ -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] diff --git a/starknet_devnet/transaction_wrapper.py b/starknet_devnet/transaction_wrapper.py index 04225cd3a..1900a377a 100644 --- a/starknet_devnet/transaction_wrapper.py +++ b/starknet_devnet/transaction_wrapper.py @@ -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(), @@ -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 @@ -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, diff --git a/test/expected/deploy_receipt.json b/test/expected/deploy_receipt.json index 4fdc250fa..8a425f09c 100644 --- a/test/expected/deploy_receipt.json +++ b/test/expected/deploy_receipt.json @@ -1,6 +1,7 @@ { "block_hash": "0x0", "block_number": 0, + "events": [], "execution_resources": { "builtin_instance_counter": { "bitwise_builtin": 0, diff --git a/test/expected/deploy_receipt_auth.json b/test/expected/deploy_receipt_auth.json index f7c05861d..00754fab0 100644 --- a/test/expected/deploy_receipt_auth.json +++ b/test/expected/deploy_receipt_auth.json @@ -1,6 +1,7 @@ { "block_hash": "0x0", "block_number": 0, + "events": [], "execution_resources": { "builtin_instance_counter": { "bitwise_builtin": 0, diff --git a/test/expected/invoke_receipt.json b/test/expected/invoke_receipt.json index ebddf9f05..fa49e87dc 100644 --- a/test/expected/invoke_receipt.json +++ b/test/expected/invoke_receipt.json @@ -1,6 +1,7 @@ { "block_hash": "0x1", "block_number": 1, + "events": [], "execution_resources": { "builtin_instance_counter": { "bitwise_builtin": 0, diff --git a/test/expected/invoke_receipt_auth.json b/test/expected/invoke_receipt_auth.json index 97d958516..84d19139b 100644 --- a/test/expected/invoke_receipt_auth.json +++ b/test/expected/invoke_receipt_auth.json @@ -1,6 +1,7 @@ { "block_hash": "0x1", "block_number": 1, + "events": [], "execution_resources": { "builtin_instance_counter": { "bitwise_builtin": 0, diff --git a/test/expected/invoke_receipt_event.json b/test/expected/invoke_receipt_event.json new file mode 100644 index 000000000..f906f140e --- /dev/null +++ b/test/expected/invoke_receipt_event.json @@ -0,0 +1,14 @@ +{ + "events": [ + { + "data": [ + 0, + 10 + ], + "from_address": 3588522635734560402301506863768658247518049453423086523878030385873504815898, + "keys": [ + 1744303484486821561902174603220722448499782664094942993128426674277214273437 + ] + } + ] +} diff --git a/test/test_cli.py b/test/test_cli.py index cf86be7e5..4528c5b79 100644 --- a/test/test_cli.py +++ b/test/test_cli.py @@ -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) @@ -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) diff --git a/test/util.py b/test/util.py index fa69bb344..1175ac7ba 100644 --- a/test/util.py +++ b/test/util.py @@ -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 = [