Skip to content

Commit

Permalink
Update testcases and some logic (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
Raj-RR1 authored May 2, 2024
1 parent 72c5bec commit d1071ea
Show file tree
Hide file tree
Showing 4 changed files with 496 additions and 20 deletions.
87 changes: 70 additions & 17 deletions app/src/substrate_types_V2.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,9 @@ parser_error_t _readPerbill_V2(parser_context_t* c, pd_Perbill_V2_t* v)

parser_error_t _readPercent_V2(parser_context_t* c, pd_Percent_V2_t* v)
{
return _readCompactInt(c, &v->value);
CHECK_INPUT()
CHECK_ERROR(_readUInt8(c, &v->value))
return parser_ok;
}

parser_error_t _readProxyType_V2(parser_context_t* c, pd_ProxyType_V2_t* v)
Expand Down Expand Up @@ -461,7 +463,10 @@ parser_error_t _readTimepoint_V2(parser_context_t* c, pd_Timepoint_V2_t* v)

parser_error_t _readTupleAccountIdData_V2(parser_context_t* c, pd_TupleAccountIdData_V2_t* v)
{
return parser_not_supported;
CHECK_INPUT()
CHECK_ERROR(_readAccountId_V2(c, &v->id));
CHECK_ERROR(_readData(c, &v->data));
return parser_ok;
}

parser_error_t _readTupleBalanceOfTBalanceOfTBlockNumber_V2(parser_context_t* c, pd_TupleBalanceOfTBalanceOfTBlockNumber_V2_t* v)
Expand Down Expand Up @@ -501,13 +506,12 @@ parser_error_t _readVote_V2(parser_context_t* c, pd_Vote_V2_t* v)
{
CHECK_INPUT()
CHECK_ERROR(_readUInt8(c, &v->value))

if (v->value & 0x7F) {
return parser_value_out_of_range;
const uint8_t aye = v->value & 0xF0;
const uint8_t conviction = v->value & 0x0F;
if ((aye == 0x80 || aye == 0x00) && conviction <= 0x06) {
return parser_ok;
}
v->value = (v->value & 0x80u) >> 7u;

return parser_ok;
return parser_value_out_of_range;
}

parser_error_t _readWeightLimit_V2(parser_context_t* c, pd_WeightLimit_V2_t* v)
Expand Down Expand Up @@ -1419,8 +1423,14 @@ parser_error_t _toStringPercent_V2(
uint8_t pageIdx,
uint8_t* pageCount)
{
// 9 but shift 2 to show as percentage
return _toStringCompactInt(&v->value, 7, "%", "", outValue, outValueLen, pageIdx, pageCount);
char bufferUI[50];
char bufferRatio[50];

uint64_to_str(bufferRatio, sizeof(bufferRatio), v->value);

snprintf(bufferUI, sizeof(bufferUI), "%s%%", bufferRatio);
pageString(outValue, outValueLen, bufferUI, pageIdx, pageCount);
return parser_ok;
}

parser_error_t _toStringProxyType_V2(
Expand Down Expand Up @@ -1610,7 +1620,33 @@ parser_error_t _toStringTupleAccountIdData_V2(
uint8_t* pageCount)
{
CLEAN_AND_CHECK()
return parser_print_not_supported;

// First measure number of pages
uint8_t pages[2] = { 0 };
CHECK_ERROR(_toStringAccountId_V2(&v->id, outValue, outValueLen, 0, &pages[0]))
CHECK_ERROR(_toStringData(&v->data, outValue, outValueLen, 0, &pages[1]))

*pageCount = 0;
for (uint8_t i = 0; i < (uint8_t)sizeof(pages); i++) {
*pageCount += pages[i];
}

if (pageIdx >= *pageCount) {
return parser_display_idx_out_of_range;
}

if (pageIdx < pages[0]) {
CHECK_ERROR(_toStringAccountId_V2(&v->id, outValue, outValueLen, pageIdx, &pages[0]))
return parser_ok;
}
pageIdx -= pages[0];

if (pageIdx < pages[1]) {
CHECK_ERROR(_toStringData(&v->data, outValue, outValueLen, pageIdx, &pages[1]))
return parser_ok;
}

return parser_display_idx_out_of_range;
}

parser_error_t _toStringTupleBalanceOfTBalanceOfTBlockNumber_V2(
Expand Down Expand Up @@ -1712,20 +1748,37 @@ parser_error_t _toStringVote_V2(
uint8_t pageIdx,
uint8_t* pageCount)
{
CLEAN_AND_CHECK()

CLEAN_AND_CHECK()
UNUSED(pageIdx);
*pageCount = 1;
switch (v->value) {
const uint8_t conviction = v->value & 0x0F;

switch (v->value & 0xF0) {
case 0x80:
snprintf(outValue, outValueLen, "Aye - ");
break;
case 0x00:
snprintf(outValue, outValueLen, "Nay - ");
break;
default:
return parser_unexpected_value;
}

switch (conviction) {
case 0:
snprintf(outValue, outValueLen, "Nay");
snprintf(outValue + 6, outValueLen - 6, "None");
break;
case 1:
snprintf(outValue, outValueLen, "Aye");
case 2:
case 3:
case 4:
case 5:
case 6:
snprintf(outValue + 6, outValueLen - 6, "Locked%dx", conviction);
break;
default:
return parser_unexpected_value;
}

return parser_ok;
}

Expand Down
6 changes: 3 additions & 3 deletions app/src/substrate_types_V2.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ typedef struct {
} pd_Timepoint_V2_t;

typedef struct {
// TODO: Not implemented
uint8_t _NOT_IMPLEMENTED__DO_NOT_USE;
pd_AccountId_V2_t id;
pd_Data_t data;
} pd_TupleAccountIdData_V2_t;

typedef struct {
Expand Down Expand Up @@ -234,7 +234,7 @@ typedef struct {
} pd_ConfigOpPerbill_V2_t;

typedef struct {
compactInt_t value;
uint8_t value;
} pd_Percent_V2_t;

typedef struct {
Expand Down
4 changes: 4 additions & 0 deletions deps/ledger-zxlib/include/zxmacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ extern void explicit_bzero(void *s, size_t n) __THROW __nonnull ((1));
#define __Z_UNUSED __attribute__((unused))
#define NV_ALIGN __attribute__ ((aligned(64)))

#ifndef UNUSED
#define UNUSED(x) (void)x
#endif

#if defined(LEDGER_SPECIFIC)
#include "bolos_target.h"
#endif
Expand Down
Loading

0 comments on commit d1071ea

Please sign in to comment.