Skip to content

Commit

Permalink
fix error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
abenso committed Jan 24, 2025
1 parent 8363fe3 commit e8212c7
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 38 deletions.
2 changes: 1 addition & 1 deletion app/src/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
1 change: 0 additions & 1 deletion app/src/memo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions app/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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;

Expand Down Expand Up @@ -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
Expand Down
47 changes: 17 additions & 30 deletions app/src/parser_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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:
Expand All @@ -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;
}
3 changes: 1 addition & 2 deletions app/src/plan/swap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 9 additions & 2 deletions tests/parser_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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));

Expand Down

0 comments on commit e8212c7

Please sign in to comment.