Skip to content

Commit

Permalink
add spend and output tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abenso committed Nov 18, 2024
1 parent afd1261 commit ecd652f
Show file tree
Hide file tree
Showing 15 changed files with 469 additions and 60 deletions.
2 changes: 2 additions & 0 deletions app/rust/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub const DETECTION_DATA_QTY: usize = 16;
pub const ACTION_DATA_QTY: usize = 16;
pub const MAX_CLUE_SUBKEYS: usize = 10;

pub const EFFECT_HASH_LEN: usize = 64;

// Nonces:
pub const NONCE_LEN: usize = 12;
pub const NONCE_NOTE: &[u8; NONCE_LEN] = &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
Expand Down
16 changes: 8 additions & 8 deletions app/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/
#![no_std]
// #![no_std]
#![no_builtins]
#![allow(dead_code)]
#![deny(unused_crate_dependencies)]
Expand Down Expand Up @@ -44,14 +44,14 @@ pub(crate) use utils::prf::{expand_fq, expand_fr};

fn debug(_msg: &str) {}

#[cfg(all(not(test), not(feature = "clippy"), not(feature = "fuzzing")))]
use core::panic::PanicInfo;
// #[cfg(all(not(test), not(feature = "clippy"), not(feature = "fuzzing")))]
// use core::panic::PanicInfo;

#[cfg(all(not(test), not(feature = "clippy"), not(feature = "fuzzing")))]
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}
// #[cfg(all(not(test), not(feature = "clippy"), not(feature = "fuzzing")))]
// #[panic_handler]
// fn panic(_info: &PanicInfo) -> ! {
// loop {}
// }

extern "C" {
fn check_app_canary();
Expand Down
3 changes: 2 additions & 1 deletion app/rust/src/parser/plans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::effect_hash::EffectHash;
use crate::parser::bytes::BytesC;
use crate::parser::parameters::TransactionParametersC;
use crate::ParserError;
use crate::constants::EFFECT_HASH_LEN;

#[repr(C)]
#[cfg_attr(any(feature = "derive-debug", test), derive(Debug))]
Expand Down Expand Up @@ -68,7 +69,7 @@ pub unsafe extern "C" fn rs_compute_transaction_plan(
crate::zlog("rs_compute_transaction_plan\x00");
let output = std::slice::from_raw_parts_mut(output, output_len);

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

Expand Down
5 changes: 0 additions & 5 deletions app/src/keys_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ extern "C" {
#include <stddef.h>
#include <stdint.h>

typedef struct {
uint8_t *ptr;
uint16_t len;
} bytes_t;

#define KEY_LEN 32
#define DIVERSIFIER_KEY_LEN 16
#define OUTGOING_VIEWING_KEY_LEN KEY_LEN
Expand Down
22 changes: 14 additions & 8 deletions app/src/parser_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static bool decode_detection_data(pb_istream_t *stream, const pb_field_t *field,
static uint16_t actions_qty = 0;
static uint16_t detection_data_qty = 0;

void print_buffer(Bytes_t *buffer, const char *title) {
void print_buffer(bytes_t *buffer, const char *title) {
#if defined(LEDGER_SPECIFIC)
ZEMU_LOGF(50, "%s\n", title);
char print[1000] = {0};
Expand All @@ -53,7 +53,7 @@ void print_string(const char *str) {
#endif
}

parser_error_t decode_output_plan(const Bytes_t *data, output_plan_t *output) {
parser_error_t decode_output_plan(const bytes_t *data, output_plan_t *output) {
penumbra_core_component_shielded_pool_v1_OutputPlan output_plan =
penumbra_core_component_shielded_pool_v1_OutputPlan_init_default;

Expand Down Expand Up @@ -86,7 +86,7 @@ parser_error_t decode_output_plan(const Bytes_t *data, output_plan_t *output) {
return parser_ok;
}

parser_error_t decode_delegate_plan(const Bytes_t *data, delegate_plan_t *delegate) {
parser_error_t decode_delegate_plan(const bytes_t *data, delegate_plan_t *delegate) {
penumbra_core_component_stake_v1_Delegate delegate_plan =
penumbra_core_component_stake_v1_Delegate_init_default;

Expand Down Expand Up @@ -115,7 +115,7 @@ parser_error_t decode_delegate_plan(const Bytes_t *data, delegate_plan_t *delega
return parser_ok;
}

parser_error_t decode_undelegate_plan(const Bytes_t *data, undelegate_plan_t *undelegate) {
parser_error_t decode_undelegate_plan(const bytes_t *data, undelegate_plan_t *undelegate) {
penumbra_core_component_stake_v1_Undelegate undelegate_plan =
penumbra_core_component_stake_v1_Undelegate_init_default;

Expand Down Expand Up @@ -160,7 +160,7 @@ bool decode_action(pb_istream_t *stream, const pb_field_t *field, void **arg) {
return false;
}

Bytes_t action_data;
bytes_t action_data;
action_data.ptr = stream->state + 3;
action_data.len = stream->bytes_left - 3;

Expand Down Expand Up @@ -223,7 +223,7 @@ bool decode_detection_data(pb_istream_t *stream, const pb_field_t *field, void *
}

parser_error_t _read(parser_context_t *c, parser_tx_t *v) {
Bytes_t data;
bytes_t data;
action_t actions_plan[ACTIONS_QTY];
data.ptr = c->buffer;
data.len = c->bufferLen;
Expand Down Expand Up @@ -316,9 +316,15 @@ parser_error_t _read(parser_context_t *c, parser_tx_t *v) {
}
v->plan.actions.qty = actions_qty;

compute_transaction_plan(&v->plan);
compute_transaction_plan(&v->plan, v->effect_hash, sizeof(v->effect_hash));

return parser_unexpected_error;
// TODO: only for testing
bytes_t effect_hash;
effect_hash.ptr = v->effect_hash;
effect_hash.len = sizeof(v->effect_hash);
print_buffer(&effect_hash, "effect_hash");

return parser_ok;
}

const char *parser_getErrorDescription(parser_error_t err) {
Expand Down
12 changes: 4 additions & 8 deletions app/src/parser_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,13 @@ void print_buffer_interface(uint8_t *buffer, size_t len, const char *title) {
#endif
}

parser_error_t compute_transaction_plan(transaction_plan_t *plan) {
if (plan == NULL) return parser_unexpected_error;
parser_error_t compute_transaction_plan(transaction_plan_t *plan, uint8_t *effect_hash, uint16_t effect_hash_len) {
if (plan == NULL || effect_hash == NULL) return parser_unexpected_error;

uint8_t output[300] = {0};
if (rs_compute_transaction_plan(plan, output, sizeof(output)) != parser_ok) {
if (rs_compute_transaction_plan(plan, effect_hash, effect_hash_len) != parser_ok) {
return parser_unexpected_error;
}

// TODO: only for testing
print_buffer_interface(output, 300, "output_bytes");

return parser_ok;
}

Expand Down Expand Up @@ -79,7 +75,7 @@ parser_error_t compute_output_action_hash(output_plan_t *plan, action_hash_t *ou
0xa1, 0xff, 0xba, 0x0c, 0x37, 0x93, 0x1f, 0x0a, 0x62, 0x61, 0x37, 0x52, 0x0d, 0xa6, 0x50, 0x63,
0x2d, 0x35, 0x85, 0x3b, 0xf5, 0x91, 0xb3, 0x6b, 0xb4, 0x28, 0x63, 0x0a, 0x4d, 0x87, 0xc4, 0xdc
};
Bytes_t memo = {0};
bytes_t memo = {0};

if (rs_output_action_hash(&sk_bytes, plan, &memo, (uint8_t *)output, 64) != parser_ok) {
return parser_unexpected_error;
Expand Down
2 changes: 1 addition & 1 deletion app/src/parser_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extern "C" {
#include "zxerror.h"
#include "zxmacros.h"

parser_error_t compute_transaction_plan(transaction_plan_t *plan);
parser_error_t compute_transaction_plan(transaction_plan_t *plan, uint8_t *effect_hash, uint16_t effect_hash_len);
parser_error_t compute_spend_action_hash(spend_plan_t *plan, action_hash_t *output);
parser_error_t compute_output_action_hash(output_plan_t *plan, action_hash_t *output);

Expand Down
6 changes: 3 additions & 3 deletions app/src/parser_pb_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,20 @@ bool decode_variable_field(pb_istream_t *stream, const pb_field_t *field, void *
return true;
}

void setup_decode_fixed_field(pb_callback_t *callback, fixed_size_field_t *arg, Bytes_t *bytes, uint16_t expected_size) {
void setup_decode_fixed_field(pb_callback_t *callback, fixed_size_field_t *arg, bytes_t *bytes, uint16_t expected_size) {
arg->bytes = bytes;
arg->expected_size = expected_size;
callback->funcs.decode = &decode_fixed_field;
callback->arg = arg;
}

void setup_decode_variable_field(pb_callback_t *callback, variable_size_field_t *arg, Bytes_t *bytes) {
void setup_decode_variable_field(pb_callback_t *callback, variable_size_field_t *arg, bytes_t *bytes) {
arg->bytes = bytes;
callback->funcs.decode = &decode_variable_field;
callback->arg = arg;
}

parser_error_t extract_data_from_tag(Bytes_t *in, Bytes_t *out, uint32_t tag) {
parser_error_t extract_data_from_tag(bytes_t *in, bytes_t *out, uint32_t tag) {
const uint8_t *start = NULL;
const uint8_t *end = NULL;
bool eof = false;
Expand Down
10 changes: 5 additions & 5 deletions app/src/parser_pb_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ extern "C" {
#include "zxtypes.h"

typedef struct {
Bytes_t *bytes;
bytes_t *bytes;
uint16_t expected_size;
} fixed_size_field_t;

typedef struct {
Bytes_t *bytes;
bytes_t *bytes;
} variable_size_field_t;

// Callback to parse binding fields in spend plans. all those fields are just
Expand All @@ -46,9 +46,9 @@ typedef struct {
bool decode_fixed_field(pb_istream_t *stream, const pb_field_t *field, void **arg);
bool decode_variable_field(pb_istream_t *stream, const pb_field_t *field, void **arg);

void setup_decode_fixed_field(pb_callback_t *callback, fixed_size_field_t *arg, Bytes_t *bytes, uint16_t expected_size);
void setup_decode_variable_field(pb_callback_t *callback, variable_size_field_t *arg, Bytes_t *bytes);
parser_error_t extract_data_from_tag(Bytes_t *in, Bytes_t *out, uint32_t tag);
void setup_decode_fixed_field(pb_callback_t *callback, fixed_size_field_t *arg, bytes_t *bytes, uint16_t expected_size);
void setup_decode_variable_field(pb_callback_t *callback, variable_size_field_t *arg, bytes_t *bytes);
parser_error_t extract_data_from_tag(bytes_t *in, bytes_t *out, uint32_t tag);

#ifdef __cplusplus
}
Expand Down
37 changes: 19 additions & 18 deletions app/src/parser_txdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ extern "C" {
typedef struct {
const uint8_t *ptr;
uint16_t len;
} Bytes_t;
} bytes_t;

typedef struct {
uint64_t lo;
uint64_t hi;
} amount_t;

typedef struct {
Bytes_t inner;
bytes_t inner;
} asset_id_t;

typedef struct {
Expand All @@ -54,21 +54,21 @@ typedef struct {
} value_t;

typedef struct {
Bytes_t inner;
bytes_t inner;
// Field bellow is a sort of optional
// and is a shortcut for the case address is already
// bech32m encoded
Bytes_t alt_bech32m;
bytes_t alt_bech32m;
} address_plan_t;

typedef struct {
value_t value;
Bytes_t rseed;
bytes_t rseed;
address_plan_t address;
} note_t;

typedef struct {
Bytes_t ik;
bytes_t ik;
} identity_key_t;

typedef struct {
Expand All @@ -79,19 +79,19 @@ typedef struct {
typedef struct {
note_t note;
uint64_t position;
Bytes_t randomizer;
Bytes_t value_blinding;
Bytes_t proof_blinding_r;
Bytes_t proof_blinding_s;
bytes_t randomizer;
bytes_t value_blinding;
bytes_t proof_blinding_r;
bytes_t proof_blinding_s;
} spend_plan_t;

typedef struct {
value_t value;
address_plan_t dest_address;
Bytes_t rseed;
Bytes_t value_blinding;
Bytes_t proof_blinding_r;
Bytes_t proof_blinding_s;
bytes_t rseed;
bytes_t value_blinding;
bytes_t proof_blinding_r;
bytes_t proof_blinding_s;
} output_plan_t;

typedef struct {
Expand All @@ -117,22 +117,22 @@ typedef struct {
} undelegate_plan_t;

typedef struct {
Bytes_t parameters;
bytes_t parameters;
} transaction_parameters_t;

typedef struct {
address_plan_t return_address;
Bytes_t text;
bytes_t text;
} memo_plain_text_t;

typedef struct {
memo_plain_text_t plaintext;
Bytes_t key;
bytes_t key;
} memo_plan_t;

typedef struct {
address_plan_t address;
Bytes_t rseed;
bytes_t rseed;
uint64_t precision_bits;
} clue_plan_t;

Expand Down Expand Up @@ -165,6 +165,7 @@ typedef struct {

typedef struct {
transaction_plan_t plan;
uint8_t effect_hash[64];
} parser_tx_t;

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion app/src/spend_plan.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "protobuf/penumbra/core/transaction/v1/transaction.pb.h"
#include "zxformat.h"

parser_error_t decode_spend_plan(const Bytes_t *data, spend_plan_t *output) {
parser_error_t decode_spend_plan(const bytes_t *data, spend_plan_t *output) {
penumbra_core_component_shielded_pool_v1_SpendPlan spend_plan =
penumbra_core_component_shielded_pool_v1_SpendPlan_init_default;

Expand Down
2 changes: 1 addition & 1 deletion app/src/spend_plan.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
extern "C" {
#endif

parser_error_t decode_spend_plan(const Bytes_t *input, spend_plan_t *spend_plan);
parser_error_t decode_spend_plan(const bytes_t *input, spend_plan_t *spend_plan);

#ifdef __cplusplus
}
Expand Down
2 changes: 1 addition & 1 deletion tests/parser_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ TEST(SCALE, ReadBytes) {
buffer, sizeof(buffer),
"0a9102128e020a300a0a089e92c9dbf28597800112220a2029ea9c2f3371f6a487e7e95c247041f4a356f983eb064e5d2b3bcf322ca96a1012520a5084506123bb61575ea404e7734087db179136b0e9cc1f62ff29880c5199fe8176c2a1b57c60b61d79b955d145b503c30d6e233b33ddc598b7f6404befdbb39b27c209cf3e7c43044106c9b90d500c74461a20e21e9d6e187cd382414758297fafb4b1a06a4dc6efb9299b2d70d72c0ceec288222099e2b31b8470c3b28a61342a4e8e9dc03aa89a2d1cdb55b19b4d82319df87e012a201be58b456e9d10eddfe2cb2839d554e1e36d25ae82b018355d0ce9b17e8e0a12322097c33e865ddc7b32075fe88f54f03a10a7a7758e354eb0efc49bcbb7fb2805040a9102128e020a300a0a08f89e99f6e2a8bea60812220a2029ea9c2f3371f6a487e7e95c247041f4a356f983eb064e5d2b3bcf322ca96a1012520a503b0c5b6250417576213e2a7eeee16e9a816b91cef8336be9c5b1d125f9e6e2769ccbc85fdd2bd1d29e92e78e659d5801964116ecc280156d2247a9433b5bb803682e26e9b6fa4dc59f9a3fb0c9ce662a1a20160054d1d9ce5b6db1fa846074dca4df8929f8123551eb600a52a693ee0815ad2220bb71525bc61fb4cdb82087468a0944361a5c394e7f24262f488e459224be9d032a20cac86a6cc8ae51f861e3cc2f023f256c89d298b0194936eb2253d5d4bdf1800c322026e955748103e77c098257ca9df191006d6bcc1ba7e0577a1d91dff264a8d0000a9102128e020a300a0a088bca90dc90ada4850512220a2029ea9c2f3371f6a487e7e95c247041f4a356f983eb064e5d2b3bcf322ca96a1012520a501a02f34fd21d6a56d6cd308717d955b580af5462dd56b06f95e9d1d3f41d172e5a5a64c34857aa962024132d576dc02b16f881198f2465b9565d475ac97040c96f7f742b4eda3028b5f8ad9a9ab4a19d1a203f9ea78658e69d1750a841138818dd7cc1c6cdd02456a522616e0ce44cb1211a22208c2fd85a84e184a6fa3d2f644f1f6efe1f53f22c60addb9b00db453364d018032a20fc29fc2dee02e0e42af1a7874da7988828c8b5685069e9faab7a79f1d9b7d40032209df349c1cdfd9d0dada6333b14282f93632cf1ee4578239640dd2e184eb9a7030abe020abb020aa8010a300a0a0891db8cc1cec2829a0112220a2029ea9c2f3371f6a487e7e95c247041f4a356f983eb064e5d2b3bcf322ca96a101220e406c5db8fd46de42bfa23021b24fba56aed33dd995c82af14ee66331655a5271a520a50890bc98e3698aa4578e419b028da5672e627c280d8b06166f4c42d5366bccf1fcf3b296cd61e8d744a21f75f2fb697183e18595d8a79008539d8fb138b405db09db65cc42d54c0e772e5d42d5f20b52f108495d5d38a89171a205434dbfd666bd395970e388f5b40e17ec166ab8a373919944a746e15926305012220b9b89bf8112d8f70501a485aa4d2eadd427e21d7a7c4c480335a5e1845fd44002a20e653e5cdaa0c1bf13a739544df995658622d44c44d2bcc1f270597e694c3220232209375bb05762a06306e8c3c81778a875a638d969df8f567bb80a37b982a176f120abe020abb020aa8010a300a0a089dd8f2fad1e2ed9e0812220a2029ea9c2f3371f6a487e7e95c247041f4a356f983eb064e5d2b3bcf322ca96a1012203ab4367b3b7a3957e045bf4b4f6ef49112883f20161e9ea815ff6c7e9b72a0281a520a50890bc98e3698aa4578e419b028da5672e627c280d8b06166f4c42d5366bccf1fcf3b296cd61e8d744a21f75f2fb697183e18595d8a79008539d8fb138b405db09db65cc42d54c0e772e5d42d5f20b52f1085d0e884e0d73c1a20b0b138c127a91b4ed70c1bfe86738d66da4179d24001bad903622a5cba86060422203a339b4017ef099f4fa9f0e9f538b4a412adf436aa4274042c5689b57a54d4002a20e22e518d5e3ca1364372bb41bf715a2b875913df69ef04d102cba287b376ae0132202f95351d4a308667fef2551c24ca71dfb38ebde9c4ea112a5fdf5aeaef49c70e125508eff9ada202123f6b6f62687462637676666b6665696c6571706766786e75656b6279666376762d323737383432363234373330323535303935353735323131303131353638391a0c0a0a08a8cbd3dbcd9be3aa0a");

parser_parse(&ctx, buffer, bufferLen, &tx_obj);
//parser_parse(&ctx, buffer, bufferLen, &tx_obj);
}
Loading

0 comments on commit ecd652f

Please sign in to comment.