From e8212c781c13a8ba02ba57058b342c629215fbd9 Mon Sep 17 00:00:00 2001 From: abenso Date: Fri, 24 Jan 2025 11:04:04 -0300 Subject: [PATCH] fix error handler --- app/src/crypto.c | 2 +- app/src/memo.c | 1 - app/src/parser.c | 5 ++-- app/src/parser_interface.c | 47 ++++++++++++++------------------------ app/src/plan/swap.c | 3 +-- tests/parser_impl.cpp | 11 +++++++-- 6 files changed, 31 insertions(+), 38 deletions(-) diff --git a/app/src/crypto.c b/app/src/crypto.c index f49b149..e39d3ba 100644 --- a/app/src/crypto.c +++ b/app/src/crypto.c @@ -159,7 +159,7 @@ zxerr_t crypto_sign(parser_tx_t *tx_obj, uint8_t *signature, uint16_t signatureM nv_signature_init(); zxerr_t error = zxerr_invalid_crypto_settings; - + // compute spend key CATCH_ZX_ERROR(computeSpendKey(&keys)); diff --git a/app/src/memo.c b/app/src/memo.c index 43a5793..0e05ef0 100644 --- a/app/src/memo.c +++ b/app/src/memo.c @@ -36,7 +36,6 @@ parser_error_t memo_getItem(const parser_context_t *ctx, uint8_t displayIdx, cha return err; } - char short_address[100] = {0}; switch (displayIdx) { case 0: diff --git a/app/src/parser.c b/app/src/parser.c index 764af08..dcd88e8 100644 --- a/app/src/parser.c +++ b/app/src/parser.c @@ -34,6 +34,7 @@ #include "parameters.h" #include "parser_common.h" #include "parser_impl.h" +#include "parser_interface.h" #include "position_close.h" #include "position_open.h" #include "position_withdraw.h" @@ -42,7 +43,6 @@ #include "tx_metadata.h" #include "undelegate.h" #include "undelegate_claim.h" -#include "parser_interface.h" static uint8_t action_idx = 0; @@ -73,7 +73,8 @@ parser_error_t parser_computeEffectHash(parser_context_t *ctx) { // compute action hashes for (uint16_t i = 0; i < ctx->tx_obj->plan.actions.qty; i++) { - CHECK_ERROR(compute_action_hash(&ctx->tx_obj->actions_plan[i], &ctx->tx_obj->plan.memo.key, &ctx->tx_obj->plan.actions.hashes[i])); + CHECK_ERROR(compute_action_hash(&ctx->tx_obj->actions_plan[i], &ctx->tx_obj->plan.memo.key, + &ctx->tx_obj->plan.actions.hashes[i])); } // compute effect hash diff --git a/app/src/parser_interface.c b/app/src/parser_interface.c index 2a26cc3..db63a73 100644 --- a/app/src/parser_interface.c +++ b/app/src/parser_interface.c @@ -24,8 +24,9 @@ parser_error_t compute_effect_hash(transaction_plan_t *plan, uint8_t *effect_hash, uint16_t effect_hash_len) { if (plan == NULL || effect_hash == NULL) return parser_unexpected_error; - if (rs_compute_effect_hash(plan, effect_hash, effect_hash_len) != parser_ok) { - return parser_unexpected_error; + parser_error_t err = rs_compute_effect_hash(plan, effect_hash, effect_hash_len); + if (err != parser_ok) { + return err; } return parser_ok; @@ -34,8 +35,9 @@ parser_error_t compute_effect_hash(transaction_plan_t *plan, uint8_t *effect_has parser_error_t compute_parameters_hash(bytes_t *parameters_bytes, hash_t *output) { if (parameters_bytes == NULL || output == NULL) return parser_unexpected_error; - if (rs_parameter_hash(parameters_bytes, (uint8_t *)output, 64) != parser_ok) { - return parser_unexpected_error; + parser_error_t err = rs_parameter_hash(parameters_bytes, (uint8_t *)output, 64); + if (err != parser_ok) { + return err; } return parser_ok; @@ -44,22 +46,17 @@ parser_error_t compute_parameters_hash(bytes_t *parameters_bytes, hash_t *output parser_error_t compute_action_hash(action_t *action, bytes_t *memo_key, hash_t *output) { if (action == NULL || output == NULL) return parser_unexpected_error; + parser_error_t err = parser_unexpected_error; switch (action->action_type) { case penumbra_core_transaction_v1_ActionPlan_spend_tag: - if (rs_spend_action_hash(&action->action.spend, (uint8_t *)output, 64) != parser_ok) { - return parser_unexpected_error; - } + err = rs_spend_action_hash(&action->action.spend, (uint8_t *)output, 64); break; case penumbra_core_transaction_v1_ActionPlan_output_tag: - if (rs_output_action_hash(&action->action.output, memo_key, (uint8_t *)output, 64) != parser_ok) { - return parser_unexpected_error; - } + err = rs_output_action_hash(&action->action.output, memo_key, (uint8_t *)output, 64); break; #if defined(FULL_APP) case penumbra_core_transaction_v1_ActionPlan_swap_tag: - if (rs_swap_action_hash(&action->action.swap, (uint8_t *)output, 64) != parser_ok) { - return parser_unexpected_error; - } + err = rs_swap_action_hash(&action->action.swap, (uint8_t *)output, 64); break; #endif case penumbra_core_transaction_v1_ActionPlan_ics20_withdrawal_tag: @@ -69,34 +66,24 @@ parser_error_t compute_action_hash(action_t *action, bytes_t *memo_key, hash_t * case penumbra_core_transaction_v1_ActionPlan_position_close_tag: case penumbra_core_transaction_v1_ActionPlan_action_dutch_auction_schedule_tag: case penumbra_core_transaction_v1_ActionPlan_action_dutch_auction_end_tag: - if (rs_generic_action_hash(&action->action_data, action->action_type, (uint8_t *)output, 64) != parser_ok) { - return parser_unexpected_error; - } + err = rs_generic_action_hash(&action->action_data, action->action_type, (uint8_t *)output, 64); break; case penumbra_core_transaction_v1_ActionPlan_undelegate_claim_tag: - if (rs_undelegate_claim_action_hash(&action->action.undelegate_claim, (uint8_t *)output, 64) != parser_ok) { - return parser_unexpected_error; - } + err = rs_undelegate_claim_action_hash(&action->action.undelegate_claim, (uint8_t *)output, 64); break; case penumbra_core_transaction_v1_ActionPlan_delegator_vote_tag: - if (rs_delegator_vote_action_hash(&action->action.delegator_vote, (uint8_t *)output, 64) != parser_ok) { - return parser_unexpected_error; - } + err = rs_delegator_vote_action_hash(&action->action.delegator_vote, (uint8_t *)output, 64); break; case penumbra_core_transaction_v1_ActionPlan_position_withdraw_tag: - if (rs_position_withdraw_action_hash(&action->action.position_withdraw, (uint8_t *)output, 64) != parser_ok) { - return parser_unexpected_error; - } + err = rs_position_withdraw_action_hash(&action->action.position_withdraw, (uint8_t *)output, 64); break; case penumbra_core_transaction_v1_ActionPlan_action_dutch_auction_withdraw_tag: - if (rs_action_dutch_auction_withdraw_action_hash(&action->action.action_dutch_auction_withdraw, - (uint8_t *)output, 64) != parser_ok) { - return parser_unexpected_error; - } + err = rs_action_dutch_auction_withdraw_action_hash(&action->action.action_dutch_auction_withdraw, + (uint8_t *)output, 64); break; default: return parser_unexpected_error; } - return parser_ok; + return err; } diff --git a/app/src/plan/swap.c b/app/src/plan/swap.c index 46c105a..1847a00 100644 --- a/app/src/plan/swap.c +++ b/app/src/plan/swap.c @@ -32,8 +32,7 @@ parser_error_t decode_swap_plan(const bytes_t *data, swap_plan_t *swap) { CHECK_APP_CANARY() // Set up fixed size fields - fixed_size_field_t fee_blinding_arg, asset_1_arg, asset_2_arg, - fee_asset_id_arg, claim_address_arg, rseed_arg; + fixed_size_field_t fee_blinding_arg, asset_1_arg, asset_2_arg, fee_asset_id_arg, claim_address_arg, rseed_arg; setup_decode_fixed_field(&swap_plan.fee_blinding, &fee_blinding_arg, &swap->fee_blinding, 32); setup_decode_fixed_field(&swap_plan.swap_plaintext.trading_pair.asset_1.inner, &asset_1_arg, diff --git a/tests/parser_impl.cpp b/tests/parser_impl.cpp index a71869e..3f5bf52 100644 --- a/tests/parser_impl.cpp +++ b/tests/parser_impl.cpp @@ -38,7 +38,13 @@ TEST(SCALE, ReadBytes) { uint8_t buffer[6000]; auto bufferLen = parseHexString( buffer, sizeof(buffer), - "0abe020abb020aa8010a300a0a08caedc786f98cc3e50312220a2029ea9c2f3371f6a487e7e95c247041f4a356f983eb064e5d2b3bcf322ca96a10122042b70d0c3e32c65670ca200dbd3c3a9a9dd59ef896fee3579025b841050359431a520a50890bc98e3698aa4578e419b028da5672e627c280d8b06166f4c42d5366bccf1fcf3b296cd61e8d744a21f75f2fb697183e18595d8a79008539d8fb138b405db09db65cc42d54c0e772e5d42d5f20b52f109d94b78f8798391a20e5a37679976d378f1dabe0fd608211081bb9c43b1f53978f9cef43dbf7a3d0012220f229ec5a96f4445a12f4c314b6a789bb99391fecb49c9b91800c7c5c94ccb6012a20a9a2cf11e230952ddbf772551a29786b2d4fef65388b2f3ea22cec9efc0f54013220dfb0cd1034c290a673a6825cc6771d1c7b97c063b429ff089e890cb85175320c121f120f6b696b7a7262762d373935363239341a0c0a0a089ff59eb6b88a838f0b"); + "0abe020abb020aa8010a300a0a08caedc786f98cc3e50312220a2029ea9c2f3371f6a487e7e95c247041f4a356f983eb064e5d2b3bcf322ca96" + "a10122042b70d0c3e32c65670ca200dbd3c3a9a9dd59ef896fee3579025b841050359431a520a50890bc98e3698aa4578e419b028da5672e627" + "c280d8b06166f4c42d5366bccf1fcf3b296cd61e8d744a21f75f2fb697183e18595d8a79008539d8fb138b405db09db65cc42d54c0e772e5d42" + "d5f20b52f109d94b78f8798391a20e5a37679976d378f1dabe0fd608211081bb9c43b1f53978f9cef43dbf7a3d0012220f229ec5a96f4445a12" + "f4c314b6a789bb99391fecb49c9b91800c7c5c94ccb6012a20a9a2cf11e230952ddbf772551a29786b2d4fef65388b2f3ea22cec9efc0f54013" + "220dfb0cd1034c290a673a6825cc6771d1c7b97c063b429ff089e890cb85175320c121f120f6b696b7a7262762d373935363239341a0c0a0a08" + "9ff59eb6b88a838f0b"); err = parser_parse(&ctx, buffer, bufferLen, &tx_obj); ASSERT_EQ(err, parser_ok) << parser_getErrorDescription(err); @@ -47,7 +53,8 @@ TEST(SCALE, ReadBytes) { ASSERT_EQ(err, parser_ok) << parser_getErrorDescription(err); std::string expected = - "0e9fd733a3724555dd48a8ca91010b8d57b1291ee90784fc8cbfb2c152b1e762065a01b4fa9af5f0ee510ffcc2cb07987b2a4983e9481e6341ee94ddec213b4f"; + "0e9fd733a3724555dd48a8ca91010b8d57b1291ee90784fc8cbfb2c152b1e762065a01b4fa9af5f0ee510ffcc2cb07987b2a4983e9481e6341e" + "e94ddec213b4f"; char actual[129]; array_to_hexstr(actual, sizeof(actual), tx_obj.effect_hash, sizeof(tx_obj.effect_hash));