Skip to content

Commit

Permalink
Stop storing duplicate data for items
Browse files Browse the repository at this point in the history
  • Loading branch information
0xPxt committed Aug 13, 2024
1 parent aaf4e42 commit e033635
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 163 deletions.
216 changes: 112 additions & 104 deletions app/src/items.c

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions app/src/items.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

typedef struct {
char key[25];
parsed_json_t json;
parser_error_t (*toString)(parsed_json_t json_obj, char *outVal, uint16_t *outValLen);
uint16_t json_token_index;
parser_error_t (*toString)(uint16_t token_index, char *outVal, uint16_t *outValLen);
} item_t;

typedef struct {
Expand Down
36 changes: 0 additions & 36 deletions app/src/json/json_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,42 +189,6 @@ parser_error_t object_get_value(const parsed_json_t *json,
if (key_token.start <= prev_element_end) {
continue;
}

if (value_token.type == JSMN_OBJECT) {
// An object was found, look inside it
parsed_json_t json_obj;
uint16_t token_index_before_recursion = *token_index;

json_parse(&json_obj, json->buffer + value_token.start, value_token.end - value_token.start);

if (object_get_value(&json_obj, 0, key_name, token_index) == parser_ok) {
*token_index = *token_index + token_index_before_recursion;
return parser_ok;
}
} else if (value_token.type == JSMN_ARRAY) {
// An array was found, look inside it
parsed_json_t json_array;
parsed_json_t json_array_element;
uint16_t token_index_before_object_recursion = 0;
uint16_t token_index_before_array_iteration = 0;
uint16_t element_count = 0;

json_parse(&json_array, json->buffer + value_token.start, value_token.end - value_token.start);

CHECK_ERROR(array_get_element_count(&json_array, 0, &element_count))

for (int i = 0; i < element_count; i++) {
CHECK_ERROR(array_get_nth_element(&json_array, 0, i, &token_index_before_array_iteration))

json_parse(&json_array_element, json_array.buffer + json_array.tokens[token_index_before_array_iteration].start, json_array.tokens[token_index_before_array_iteration].end - json_array.tokens[token_index_before_array_iteration].start);

if (object_get_value(&json_array_element, 0, key_name, &token_index_before_object_recursion) == parser_ok) {
*token_index = *token_index + token_index_before_object_recursion + token_index_before_array_iteration;
return parser_ok;
}
}
}

prev_element_end = value_token.end;

if (((uint16_t) strlen(key_name)) == (key_token.end - key_token.start)) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ parser_error_t parser_getItem(const parser_context_t *ctx, uint8_t displayIdx, c
cleanOutput(outKey, outKeyLen, outVal, outValLen);

snprintf(outKey, outKeyLen, "%s", item_array->items[displayIdx].key);
item_array->items[displayIdx].toString(item_array->items[displayIdx].json, outVal, &outValLen);
item_array->items[displayIdx].toString(item_array->items[displayIdx].json_token_index, outVal, &outValLen);

return parser_ok;
}
43 changes: 25 additions & 18 deletions app/src/parser_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,32 @@ parser_error_t parser_isTransfer(parsed_json_t *json_obj) {

uint16_t parser_getNumberOfClistElements() {
uint16_t number_of_elements = 0;
parsed_json_t json_obj;
parsed_json_t *json_all = &parser_tx_obj.tx_json.json;
uint16_t token_index = 0;

parser_getJsonValue(&token_index, JSON_SIGNERS);
array_get_nth_element(&json_all, token_index, 0, &token_index);
parser_getJsonValue(&token_index, JSON_CLIST);

CHECK_ERROR(array_get_element_count(json_all, token_index, &number_of_elements));

parser_getJsonValue(&json_obj, JSON_META);
parser_getJsonValue(&json_obj, JSON_CLIST);
CHECK_ERROR(array_get_element_count(&json_obj, 0, &number_of_elements));
return number_of_elements;
}

// TODO: join all these functions into a parametrized one
parser_error_t parser_getJsonValue(parsed_json_t *json_obj, const char *key) {
parser_error_t parser_getJsonValue(uint16_t *json_token_index, const char *key) {
parsed_json_t json_obj;
uint16_t token_index = 0;
object_get_value(&parser_tx_obj.tx_json.json, 0, key, &token_index);
json_parse(json_obj, parser_tx_obj.tx_json.json.buffer + parser_tx_obj.tx_json.json.tokens[token_index].start, parser_tx_obj.tx_json.json.tokens[token_index].end - parser_tx_obj.tx_json.json.tokens[token_index].start);

if (MEMCMP("null", json_obj->buffer, json_obj->bufferLen) == 0) {
object_get_value(&parser_tx_obj.tx_json.json, *json_token_index, key, &token_index);

json_parse(&json_obj, parser_tx_obj.tx_json.json.buffer + parser_tx_obj.tx_json.json.tokens[token_index].start, parser_tx_obj.tx_json.json.tokens[token_index].end - parser_tx_obj.tx_json.json.tokens[token_index].start);

if (MEMCMP("null", json_obj.buffer, json_obj.bufferLen) == 0) {
return parser_no_data;
}

*json_token_index = token_index;

return parser_ok;
}

Expand All @@ -85,19 +93,18 @@ parser_error_t parser_getNthClistElement(parsed_json_t *json_obj, uint8_t clist_
return parser_ok;
}

parser_error_t parser_getGasObject(parsed_json_t *json_obj) {
parser_error_t parser_getGasObject(uint16_t *json_token_index) {
uint16_t token_index = 0;
parsed_json_t *json_clist = json_obj;
parsed_json_t temp_json_obj;
parsed_json_t *json_all = &parser_tx_obj.tx_json.json;
uint16_t name_token_index = 0;

for (uint16_t i = 0; i < parser_getNumberOfClistElements(); i++) {
array_get_nth_element(json_clist, 0, i, &token_index);
json_parse(&temp_json_obj, json_clist->buffer + json_clist->tokens[token_index].start, json_clist->tokens[token_index].end - json_clist->tokens[token_index].start);
array_get_nth_element(json_all, *json_token_index, i, &token_index);

object_get_value(&temp_json_obj, 0, "name", &token_index);
if (MEMCMP("coin.GAS", temp_json_obj.buffer + temp_json_obj.tokens[token_index].start,
temp_json_obj.tokens[token_index].end - temp_json_obj.tokens[token_index].start) == 0) {
*json_obj = temp_json_obj;
object_get_value(json_all, token_index, "name", &name_token_index);
if (MEMCMP("coin.GAS", json_all->buffer + json_all->tokens[name_token_index].start,
json_all->tokens[name_token_index].end - json_all->tokens[name_token_index].start) == 0) {
*json_token_index = token_index;
return parser_ok;
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/parser_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ parser_error_t parser_getTransferTo(char **to, uint16_t *to_len);
parser_error_t parser_getTransferAmount(char **amount, uint16_t *amount_len);
uint16_t parser_getNumberOfClistElements();
uint16_t parser_getNumberOfTransfers();
parser_error_t parser_getJsonValue(parsed_json_t *json_obj, const char *key);
parser_error_t parser_getJsonValue(uint16_t *json_token_index, const char *key);
parser_error_t parser_getNthClistElement(parsed_json_t *json_obj, uint8_t clist_array_idx);
parser_error_t parser_getGasObject(parsed_json_t *json_obj);
parser_error_t parser_getGasObject(uint16_t *json_token_index);
parser_error_t parser_getChainId(parsed_json_t *json_obj);

#ifdef __cplusplus
Expand Down
13 changes: 13 additions & 0 deletions tests/testcases.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"6 | On Chain : 0",
"7 | Using Gas : at most 600 at price 1.0e-5",
"8 | Transaction hash : fPSCfMUaoK1N31qwhwBFUPwG-YR_guPP894uixsNZgk",
"9 | Sign for Address : 8d5d63bb1071a8dfc5c09ac96cfa50341a74eb91b6ea9ee5724cde09ef758bf2"
],
"output_expert": [
"0 | Signing : Transaction",
Expand All @@ -24,6 +25,7 @@
"6 | On Chain : 0",
"7 | Using Gas : at most 600 at price 1.0e-5",
"8 | Transaction hash : fPSCfMUaoK1N31qwhwBFUPwG-YR_guPP894uixsNZgk",
"9 | Sign for Address : 8d5d63bb1071a8dfc5c09ac96cfa50341a74eb91b6ea9ee5724cde09ef758bf2"
]
},
{
Expand All @@ -40,6 +42,7 @@
"6 | On Chain : 0",
"7 | Using Gas : at most 600 at price 1.0e-6",
"8 | Transaction hash : anrl4cUVN53NFJCQ9tH4szt-ZzlCQ_SZuDI7e8OLyco",
"9 | Sign for Address : 8d5d63bb1071a8dfc5c09ac96cfa50341a74eb91b6ea9ee5724cde09ef758bf2"
],
"output_expert": [
"0 | Signing : Transaction",
Expand All @@ -51,6 +54,7 @@
"6 | On Chain : 0",
"7 | Using Gas : at most 600 at price 1.0e-6",
"8 | Transaction hash : anrl4cUVN53NFJCQ9tH4szt-ZzlCQ_SZuDI7e8OLyco",
"9 | Sign for Address : 8d5d63bb1071a8dfc5c09ac96cfa50341a74eb91b6ea9ee5724cde09ef758bf2"
]
},
{
Expand All @@ -66,6 +70,7 @@
"5 | On Chain : 0",
"6 | Using Gas : at most 600 at price 1.0e-5",
"7 | Transaction hash : epv3lSVeZCWEYpPZet-ddYqpFSekJiIcw2azMb-Cn8w",
"8 | Sign for Address : 8d5d63bb1071a8dfc5c09ac96cfa50341a74eb91b6ea9ee5724cde09ef758bf2"
],
"output_expert": [
"0 | Signing : Transaction",
Expand All @@ -76,6 +81,7 @@
"5 | On Chain : 0",
"6 | Using Gas : at most 600 at price 1.0e-5",
"7 | Transaction hash : epv3lSVeZCWEYpPZet-ddYqpFSekJiIcw2azMb-Cn8w",
"8 | Sign for Address : 8d5d63bb1071a8dfc5c09ac96cfa50341a74eb91b6ea9ee5724cde09ef758bf2"
]
},
{
Expand All @@ -92,6 +98,7 @@
"6 | On Chain : 0",
"7 | Using Gas : at most 600 at price 1.0e-6",
"8 | Transaction hash : u4kRsc0DEmRbOOG2gePtMADMTOGGtRsXrMQ2R4bAvk4",
"9 | Sign for Address : 8d5d63bb1071a8dfc5c09ac96cfa50341a74eb91b6ea9ee5724cde09ef758bf2"
],
"output_expert": [
"0 | Signing : Transaction",
Expand All @@ -103,6 +110,7 @@
"6 | On Chain : 0",
"7 | Using Gas : at most 600 at price 1.0e-6",
"8 | Transaction hash : u4kRsc0DEmRbOOG2gePtMADMTOGGtRsXrMQ2R4bAvk4",
"9 | Sign for Address : 8d5d63bb1071a8dfc5c09ac96cfa50341a74eb91b6ea9ee5724cde09ef758bf2"
]
},
{
Expand All @@ -119,6 +127,7 @@
"6 | On Chain : 0",
"7 | Using Gas : at most 600 at price 1.0e-5",
"8 | Transaction hash : FmmZBoFdyW_0T7oD1fXldK_MgKyvxTd4B3i7ew7VnMY",
"9 | Sign for Address : 8d5d63bb1071a8dfc5c09ac96cfa50341a74eb91b6ea9ee5724cde09ef758bf2"
],
"output_expert": [
"0 | Signing : Transaction",
Expand All @@ -130,6 +139,7 @@
"6 | On Chain : 0",
"7 | Using Gas : at most 600 at price 1.0e-5",
"8 | Transaction hash : FmmZBoFdyW_0T7oD1fXldK_MgKyvxTd4B3i7ew7VnMY",
"9 | Sign for Address : 8d5d63bb1071a8dfc5c09ac96cfa50341a74eb91b6ea9ee5724cde09ef758bf2"
]
},
{
Expand All @@ -149,6 +159,7 @@
"9 | On Chain : 0",
"10 | Using Gas : at most 600 at price 1.0e-6",
"11 | Transaction hash : cYmajadc0EPG3ifvKR1Yd_-wlG79UZirK47JOREfZhk",
"12 | Sign for Address : 8d5d63bb1071a8dfc5c09ac96cfa50341a74eb91b6ea9ee5724cde09ef758bf2"
],
"output_expert": [
"0 | Signing : Transaction",
Expand All @@ -163,6 +174,8 @@
"9 | On Chain : 0",
"10 | Using Gas : at most 600 at price 1.0e-6",
"11 | Transaction hash : cYmajadc0EPG3ifvKR1Yd_-wlG79UZirK47JOREfZhk",
"12 | Sign for Address : 8d5d63bb1071a8dfc5c09ac96cfa50341a74eb91b6ea9ee5724cde09ef758bf2"
]
}
]

0 comments on commit e033635

Please sign in to comment.