From 7e72b49021944b6b99ce6c5bb57eccc86c8c329b Mon Sep 17 00:00:00 2001 From: Kobi Gurkan Date: Fri, 23 Jul 2021 22:32:57 +0300 Subject: [PATCH 1/3] fix: allow invoking with array arguments and add some consistency checks --- src/starkware/starknet/cli/starknet_cli.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/starkware/starknet/cli/starknet_cli.py b/src/starkware/starknet/cli/starknet_cli.py index 9a9fcfb0..f2d81bf6 100755 --- a/src/starkware/starknet/cli/starknet_cli.py +++ b/src/starkware/starknet/cli/starknet_cli.py @@ -106,12 +106,30 @@ async def invoke_or_call(args, command_args, call: bool): raise ValueError('Invalid address format.') for abi_entry in abi: if abi_entry['type'] == 'function' and abi_entry['name'] == args.function: + previous_input = None + current_inputs_ptr = 0 + for input_desc in abi_entry['inputs']: + assert current_inputs_ptr < len(args.inputs), \ + f'Expected at least {current_inputs_ptr + 1}, got {len(args.inputs)}' + current_input_val = args.inputs[current_inputs_ptr] + if input_desc['type'] == 'felt': + current_inputs_ptr += 1 + elif input_desc['type'] == 'felt*': + assert current_inputs_ptr > 0, 'First input type cannot be felt*.' + assert previous_input['desc']['type'] == 'felt', \ + 'Current input is felt*, and previous input should have been ' \ + f'felt, but it was {previous_input["desc"]["type"]}' + + current_inputs_ptr += previous_input['val'] + else: + raise Exception(f'Unsupported type {input_desc["type"]}') + previous_input = { 'desc': input_desc, 'val': current_input_val } break else: raise Exception(f'Function {args.function} not found.') selector = get_selector_from_name(args.function) - assert len(args.inputs) == len(abi_entry['inputs']), \ - f'Wrong number of arguments. Expected {len(abi_entry["inputs"])}, got {len(args.inputs)}.' + assert len(args.inputs) == current_inputs_ptr, \ + f'Wrong number of arguments. Expected {current_inputs_ptr}, got {len(args.inputs)}.' calldata = args.inputs tx = InvokeFunction(contract_address=address, entry_point_selector=selector, calldata=calldata) From 0f348379ed9b634b65850191bfc29ea04d99cd7b Mon Sep 17 00:00:00 2001 From: Kobi Gurkan Date: Wed, 4 Aug 2021 22:51:58 +0300 Subject: [PATCH 2/3] fix: apply review suggestions --- src/starkware/starknet/cli/starknet_cli.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/starkware/starknet/cli/starknet_cli.py b/src/starkware/starknet/cli/starknet_cli.py index f2d81bf6..4aecb2c9 100755 --- a/src/starkware/starknet/cli/starknet_cli.py +++ b/src/starkware/starknet/cli/starknet_cli.py @@ -106,24 +106,22 @@ async def invoke_or_call(args, command_args, call: bool): raise ValueError('Invalid address format.') for abi_entry in abi: if abi_entry['type'] == 'function' and abi_entry['name'] == args.function: - previous_input = None + previous_felt_input = None current_inputs_ptr = 0 for input_desc in abi_entry['inputs']: - assert current_inputs_ptr < len(args.inputs), \ - f'Expected at least {current_inputs_ptr + 1}, got {len(args.inputs)}' - current_input_val = args.inputs[current_inputs_ptr] if input_desc['type'] == 'felt': + assert current_inputs_ptr < len(args.inputs), \ + f'Expected at least {current_inputs_ptr + 1} inputs, got {len(args.inputs)}' + previous_felt_input = args.inputs[current_inputs_ptr] current_inputs_ptr += 1 elif input_desc['type'] == 'felt*': - assert current_inputs_ptr > 0, 'First input type cannot be felt*.' - assert previous_input['desc']['type'] == 'felt', \ - 'Current input is felt*, and previous input should have been ' \ - f'felt, but it was {previous_input["desc"]["type"]}' + assert previous_felt_input is not None, \ + f'The array argument {input_desc["name"]} of type felt* must be preceded ' \ + 'by a length argument of type felt.' - current_inputs_ptr += previous_input['val'] + current_inputs_ptr += previous_felt_input else: raise Exception(f'Unsupported type {input_desc["type"]}') - previous_input = { 'desc': input_desc, 'val': current_input_val } break else: raise Exception(f'Function {args.function} not found.') From d498f22fa836fd8675380d8535dd5b6b99fe75d0 Mon Sep 17 00:00:00 2001 From: Kobi Gurkan Date: Thu, 5 Aug 2021 19:08:31 +0300 Subject: [PATCH 3/3] fix: resets previous_felt_input when encountering felt* --- src/starkware/starknet/cli/starknet_cli.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/starkware/starknet/cli/starknet_cli.py b/src/starkware/starknet/cli/starknet_cli.py index 4aecb2c9..4aac71b2 100755 --- a/src/starkware/starknet/cli/starknet_cli.py +++ b/src/starkware/starknet/cli/starknet_cli.py @@ -120,6 +120,7 @@ async def invoke_or_call(args, command_args, call: bool): 'by a length argument of type felt.' current_inputs_ptr += previous_felt_input + previous_felt_input = None else: raise Exception(f'Unsupported type {input_desc["type"]}') break