Skip to content

Commit

Permalink
improve error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
abenso committed Jan 24, 2025
1 parent e8212c7 commit 9e79478
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 90 deletions.
142 changes: 68 additions & 74 deletions app/rust/src/parser/plans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub unsafe extern "C" fn rs_compute_effect_hash(
let output = std::slice::from_raw_parts_mut(output, output_len);

if output.len() < EFFECT_HASH_LEN {
return ParserError::UnexpectedData as u32;
return ParserError::InvalidLength as u32;
}

let plan_hash_result = plan.effect_hash();
Expand All @@ -105,8 +105,8 @@ pub unsafe extern "C" fn rs_parameter_hash(
crate::zlog("rs_parameter_hash\x00");
let output = std::slice::from_raw_parts_mut(output, output_len);

if output.len() < 64 {
return ParserError::Ok as u32;
if output.len() < EFFECT_HASH_LEN {
return ParserError::InvalidLength as u32;
}

let effect_hash: EffectHash;
Expand Down Expand Up @@ -137,21 +137,21 @@ pub unsafe extern "C" fn rs_spend_action_hash(
crate::zlog("rs_spend_action_hash\x00");
let output = std::slice::from_raw_parts_mut(output, output_len);

if output.len() < 64 {
return ParserError::Ok as u32;
if output.len() < EFFECT_HASH_LEN {
return ParserError::InvalidLength as u32;
}

let Ok(fvk) = c_fvk_bytes() else {
return ParserError::InvalidFvk as u32;
};
let body_hash_bytes = plan.effect_hash(&fvk);

if let Ok(body_hash_bytes) = body_hash_bytes {
let body_hash_array = body_hash_bytes.as_array();
let copy_len: usize = core::cmp::min(output.len(), body_hash_array.len());
output[..copy_len].copy_from_slice(&body_hash_array[..copy_len]);
} else {
return ParserError::SpendPlanError as u32;
match plan.effect_hash(&fvk) {
Ok(body_hash_bytes) => {
let body_hash_array = body_hash_bytes.as_array();
let copy_len: usize = core::cmp::min(output.len(), body_hash_array.len());
output[..copy_len].copy_from_slice(&body_hash_array[..copy_len]);
}
Err(err) => return err as u32,
}

ParserError::Ok as u32
Expand All @@ -169,8 +169,8 @@ pub unsafe extern "C" fn rs_output_action_hash(
crate::zlog("rs_output_action_hash\x00");
let output = std::slice::from_raw_parts_mut(output, output_len);

if output.len() < 64 {
return ParserError::Ok as u32;
if output.len() < EFFECT_HASH_LEN {
return ParserError::InvalidLength as u32;
}

let Ok(fvk) = c_fvk_bytes() else {
Expand All @@ -179,14 +179,13 @@ pub unsafe extern "C" fn rs_output_action_hash(

let memo_key_bytes = memo_key.get_bytes().unwrap_or(&[0u8; 32]);

let body_hash_bytes = plan.effect_hash(&fvk, memo_key_bytes);

if let Ok(body_hash_bytes) = body_hash_bytes {
let body_hash_array = body_hash_bytes.as_array();
let copy_len: usize = core::cmp::min(output.len(), body_hash_array.len());
match plan.effect_hash(&fvk, memo_key_bytes) {
Ok(body_hash_bytes) => {
let body_hash_array = body_hash_bytes.as_array();
let copy_len: usize = core::cmp::min(output.len(), body_hash_array.len());
output[..copy_len].copy_from_slice(&body_hash_array[..copy_len]);
} else {
return ParserError::OutputPlanError as u32;
}
Err(err) => return err as u32,
}

ParserError::Ok as u32
Expand All @@ -203,22 +202,21 @@ pub unsafe extern "C" fn rs_swap_action_hash(
crate::zlog("rs_swap_action_hash\x00");
let output = std::slice::from_raw_parts_mut(output, output_len);

if output.len() < 64 {
return ParserError::Ok as u32;
if output.len() < EFFECT_HASH_LEN {
return ParserError::InvalidLength as u32;
}

let Ok(fvk) = c_fvk_bytes() else {
return ParserError::InvalidFvk as u32;
};

let body_hash_bytes = plan.effect_hash(&fvk);

if let Ok(body_hash_bytes) = body_hash_bytes {
let body_hash_array = body_hash_bytes.as_array();
let copy_len: usize = core::cmp::min(output.len(), body_hash_array.len());
output[..copy_len].copy_from_slice(&body_hash_array[..copy_len]);
} else {
return ParserError::SwapPlanError as u32;
match plan.effect_hash(&fvk) {
Ok(body_hash_bytes) => {
let body_hash_array = body_hash_bytes.as_array();
let copy_len: usize = core::cmp::min(output.len(), body_hash_array.len());
output[..copy_len].copy_from_slice(&body_hash_array[..copy_len]);
}
Err(err) => return err as u32,
}

ParserError::Ok as u32
Expand All @@ -235,18 +233,17 @@ pub unsafe extern "C" fn rs_undelegate_claim_action_hash(
crate::zlog("rs_undelegate_claim_action_hash\x00");
let output = std::slice::from_raw_parts_mut(output, output_len);

if output.len() < 64 {
return ParserError::Ok as u32;
if output.len() < EFFECT_HASH_LEN {
return ParserError::InvalidLength as u32;
}

let body_hash_bytes = plan.effect_hash();

if let Ok(body_hash_bytes) = body_hash_bytes {
let body_hash_array = body_hash_bytes.as_array();
let copy_len: usize = core::cmp::min(output.len(), body_hash_array.len());
output[..copy_len].copy_from_slice(&body_hash_array[..copy_len]);
} else {
return ParserError::UndelegateClaimPlanError as u32;
match plan.effect_hash() {
Ok(body_hash_bytes) => {
let body_hash_array = body_hash_bytes.as_array();
let copy_len: usize = core::cmp::min(output.len(), body_hash_array.len());
output[..copy_len].copy_from_slice(&body_hash_array[..copy_len]);
}
Err(err) => return err as u32,
}

ParserError::Ok as u32
Expand All @@ -263,22 +260,21 @@ pub unsafe extern "C" fn rs_delegator_vote_action_hash(
crate::zlog("rs_delegator_vote_action_hash\x00");
let output = std::slice::from_raw_parts_mut(output, output_len);

if output.len() < 64 {
return ParserError::Ok as u32;
if output.len() < EFFECT_HASH_LEN {
return ParserError::InvalidLength as u32;
}

let Ok(fvk) = c_fvk_bytes() else {
return ParserError::InvalidFvk as u32;
};

let body_hash_bytes = plan.effect_hash(&fvk);

if let Ok(body_hash_bytes) = body_hash_bytes {
let body_hash_array = body_hash_bytes.as_array();
let copy_len: usize = core::cmp::min(output.len(), body_hash_array.len());
output[..copy_len].copy_from_slice(&body_hash_array[..copy_len]);
} else {
return ParserError::DelegatorVotePlanError as u32;
match plan.effect_hash(&fvk) {
Ok(body_hash_bytes) => {
let body_hash_array = body_hash_bytes.as_array();
let copy_len: usize = core::cmp::min(output.len(), body_hash_array.len());
output[..copy_len].copy_from_slice(&body_hash_array[..copy_len]);
}
Err(err) => return err as u32,
}

ParserError::Ok as u32
Expand All @@ -295,18 +291,17 @@ pub unsafe extern "C" fn rs_position_withdraw_action_hash(
crate::zlog("rs_position_withdraw_action_hash\x00");
let output = std::slice::from_raw_parts_mut(output, output_len);

if output.len() < 64 {
return ParserError::Ok as u32;
if output.len() < EFFECT_HASH_LEN {
return ParserError::InvalidLength as u32;
}

let body_hash_bytes = plan.effect_hash();

if let Ok(body_hash_bytes) = body_hash_bytes {
let body_hash_array = body_hash_bytes.as_array();
let copy_len: usize = core::cmp::min(output.len(), body_hash_array.len());
output[..copy_len].copy_from_slice(&body_hash_array[..copy_len]);
} else {
return ParserError::PositionWithdrawPlanError as u32;
match plan.effect_hash() {
Ok(body_hash_bytes) => {
let body_hash_array = body_hash_bytes.as_array();
let copy_len: usize = core::cmp::min(output.len(), body_hash_array.len());
output[..copy_len].copy_from_slice(&body_hash_array[..copy_len]);
}
Err(err) => return err as u32,
}

ParserError::Ok as u32
Expand All @@ -323,18 +318,17 @@ pub unsafe extern "C" fn rs_action_dutch_auction_withdraw_action_hash(
crate::zlog("rs_action_dutch_auction_withdraw_action_hash\x00");
let output = std::slice::from_raw_parts_mut(output, output_len);

if output.len() < 64 {
return ParserError::Ok as u32;
if output.len() < EFFECT_HASH_LEN {
return ParserError::InvalidLength as u32;
}

let body_hash_bytes = plan.effect_hash();

if let Ok(body_hash_bytes) = body_hash_bytes {
let body_hash_array = body_hash_bytes.as_array();
let copy_len: usize = core::cmp::min(output.len(), body_hash_array.len());
output[..copy_len].copy_from_slice(&body_hash_array[..copy_len]);
} else {
return ParserError::DutchAuctionWithdrawPlanError as u32;
match plan.effect_hash() {
Ok(body_hash_bytes) => {
let body_hash_array = body_hash_bytes.as_array();
let copy_len: usize = core::cmp::min(output.len(), body_hash_array.len());
output[..copy_len].copy_from_slice(&body_hash_array[..copy_len]);
}
Err(err) => return err as u32,
}

ParserError::Ok as u32
Expand All @@ -352,8 +346,8 @@ pub unsafe extern "C" fn rs_generic_action_hash(
crate::zlog("rs_generic_action_hash\x00");
let output = std::slice::from_raw_parts_mut(output, output_len);

if output.len() < 64 {
return ParserError::Ok as u32;
if output.len() < EFFECT_HASH_LEN {
return ParserError::InvalidLength as u32;
}

let action_type = ActionPlan::from(action_type);
Expand Down Expand Up @@ -403,7 +397,7 @@ pub unsafe extern "C" fn rs_generic_action_hash(
);
}
_ => {
return ParserError::UnexpectedData as u32;
return ParserError::InvalidActionType as u32;
}
}

Expand Down
3 changes: 1 addition & 2 deletions app/rust/src/parser/plans/action_dutch_auction_withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ impl ActionDutchAuctionWithdrawPlanC {
.to_proto_action_dutch_auction_withdraw(),
);

let hash = state.finalize();
Ok(EffectHash(*hash.as_array()))
Ok(EffectHash(*state.finalize().as_array()))
}

pub fn to_action(&self) -> Result<ActionDutchAuctionWithdraw, ParserError> {
Expand Down
3 changes: 1 addition & 2 deletions app/rust/src/parser/plans/delegator_vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ impl DelegatorVotePlanC {
state.update(&[0x3a, 0x22, 0x0a, 0x20]);
state.update(&body.rk.to_bytes());

let hash = state.finalize();
Ok(EffectHash(*hash.as_array()))
Ok(EffectHash(*state.finalize().as_array()))
}

pub fn delegator_vote_body(&self, fvk: &FullViewingKey) -> Result<Body, ParserError> {
Expand Down
3 changes: 1 addition & 2 deletions app/rust/src/parser/plans/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ impl OutputPlanC {
state.update(&body.wrapped_memo_key.to_proto());
state.update(&body.ovk_wrapped_key.to_proto());

let hash = state.finalize();
Ok(EffectHash(*hash.as_array()))
Ok(EffectHash(*state.finalize().as_array()))
} else {
Err(ParserError::InvalidLength)
}
Expand Down
3 changes: 1 addition & 2 deletions app/rust/src/parser/plans/position_withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ impl PositionWithdrawPlanC {
let len = encode_varint(position_withdraw.sequence, &mut encoded[pos..]);
state.update(&encoded[..len + 1]);

let hash = state.finalize();
Ok(EffectHash(*hash.as_array()))
Ok(EffectHash(*state.finalize().as_array()))
}

pub fn position_withdraw(&self) -> Result<PositionWithdraw, ParserError> {
Expand Down
3 changes: 1 addition & 2 deletions app/rust/src/parser/plans/spend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ impl SpendPlanC {

state.update(&body.nullifier.to_proto());

let hash = state.finalize();
Ok(EffectHash(*hash.as_array()))
Ok(EffectHash(*state.finalize().as_array()))
}

pub fn spend_body(&self, fvk: &FullViewingKey) -> Result<Body, ParserError> {
Expand Down
3 changes: 1 addition & 2 deletions app/rust/src/parser/plans/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ impl SwapPlanC {
state.update(&body.fee_commitment.to_proto_swap());
state.update(&body.payload.to_proto());

let hash = state.finalize();
Ok(EffectHash(*hash.as_array()))
Ok(EffectHash(*state.finalize().as_array()))
}

pub fn swap_body(&self, fvk: &FullViewingKey) -> Result<Body, ParserError> {
Expand Down
3 changes: 1 addition & 2 deletions app/rust/src/parser/plans/undelegate_claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ impl UndelegateClaimPlanC {
let len = encode_varint(body.unbonding_start_height, &mut encoded[pos..]);
state.update(&encoded[..len + 1]);

let hash = state.finalize();
Ok(EffectHash(*hash.as_array()))
Ok(EffectHash(*state.finalize().as_array()))
}

pub fn undelegate_claim_body(&self) -> Result<Body, ParserError> {
Expand Down
2 changes: 1 addition & 1 deletion app/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ parser_error_t parser_getItem(const parser_context_t *ctx, uint8_t displayIdx, c
outKeyLen, outVal, outValLen, pageIdx, pageCount))
break;
default:
return parser_unexpected_error;
return parser_invalid_action_type;
}
}

Expand Down
1 change: 1 addition & 0 deletions app/src/parser_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ bool decode_action(pb_istream_t *stream, const pb_field_t *field, void **arg) {
&action_data_4, &decode_arg[actions_qty].action.action_dutch_auction_withdraw));
break;
default:
decode_error = parser_invalid_action_type;
return false;
}
actions_qty++;
Expand Down
2 changes: 1 addition & 1 deletion app/src/parser_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ parser_error_t compute_action_hash(action_t *action, bytes_t *memo_key, hash_t *
(uint8_t *)output, 64);
break;
default:
return parser_unexpected_error;
return parser_invalid_action_type;
}

return err;
Expand Down

0 comments on commit 9e79478

Please sign in to comment.