Skip to content

Commit

Permalink
Optimize information display.
Browse files Browse the repository at this point in the history
  • Loading branch information
overcat committed May 11, 2024
1 parent e8366ba commit a568429
Show file tree
Hide file tree
Showing 88 changed files with 195 additions and 193 deletions.
266 changes: 98 additions & 168 deletions libstellar/formatter.c

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions libstellar/include/stellar/printer.h
Original file line number Diff line number Diff line change
Expand Up @@ -443,3 +443,34 @@ bool add_separator_to_number(char *out, size_t out_len);
* @return True if the decimal point was added successfully, false otherwise.
*/
bool add_decimal_point(char *out, size_t out_len, uint8_t decimals);

/**
* Print a string.
*
* @param out The output buffer.
* @param out_len The length of the output buffer.
* @param src The source buffer.
* @param src_size The size of the source buffer, don't include the null terminator.
*
* @return True if the string was printed successfully, false otherwise.
*/
bool print_string(char *out, size_t out_len, const uint8_t *src, size_t src_size);

/**
* Print a price.
*
* @param price The price to print.
* @param asset_a The first asset.
* @param asset_b The second asset.
* @param network_id The network ID to consider when printing the price.
* @param out The output buffer.
* @param out_len The length of the output buffer.
*
* @return True if the price was printed successfully, false otherwise.
*/
bool print_price(const price_t *price,
const asset_t *asset_a,
const asset_t *asset_b,
uint8_t network_id,
char *out,
size_t out_len);
79 changes: 60 additions & 19 deletions libstellar/printer.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ uint16_t crc16(const uint8_t *input_str, uint32_t num_bytes) {
}
return crc;
}

bool encode_key(const uint8_t *in, uint8_t version_byte, char *out, uint8_t out_len) {
if (in == NULL || out_len < 56 + 1) {
return false;
Expand Down Expand Up @@ -241,14 +242,11 @@ bool print_ed25519_signed_payload(const ed25519_signed_payload_t *signed_payload
};

if (num_chars_l > 0) {
if (out_len < num_chars_l + num_chars_r + 2) {
if (out_len < num_chars_l + num_chars_r + 2 + 1) {
return false;
}
return print_summary(tmp, out, out_len, num_chars_l, num_chars_r);
} else {
if (out_len < ED25519_SIGNED_PAYLOAD_MAX_LENGTH) {
return false;
}
if (strlcpy(out, tmp, out_len) >= out_len) {
return false;
}
Expand Down Expand Up @@ -289,17 +287,15 @@ bool print_claimable_balance_id(const claimable_balance_id_t *claimable_balance_
size_t out_len,
uint8_t num_chars_l,
uint8_t num_chars_r) {
if (out_len < 36 * 2 + 1) {
if (out_len < (4 + CLAIMABLE_BALANCE_ID_SIZE) * 2 + 1) {
return false;
}
uint8_t data[36];
// enum is 1 byte
data[0] = '\0';
data[1] = '\0';
data[2] = '\0';
data[3] = claimable_balance_id_t->type;
uint8_t data[(4 + CLAIMABLE_BALANCE_ID_SIZE)];
for (uint8_t i = 0; i < 4; i++) {
data[i] = claimable_balance_id_t->type >> (8 * (3 - i));
}
memcpy(data + 4, claimable_balance_id_t->v0, CLAIMABLE_BALANCE_ID_SIZE);
return print_binary(data, 36, out, out_len, num_chars_l, num_chars_r);
return print_binary(data, sizeof(data), out, out_len, num_chars_l, num_chars_r);
}

bool print_uint64_num(uint64_t num, char *out, size_t out_len) {
Expand Down Expand Up @@ -405,8 +401,12 @@ bool print_asset(const asset_t *asset, uint8_t network_id, char *out, size_t out
return false;
}
if (asset->type != ASSET_TYPE_NATIVE) {
strlcat(out, "@", out_len);
strlcat(out, asset_issuer, out_len);
if (strlcat(out, "@", out_len) >= out_len) {
return false;
}
if (strlcat(out, asset_issuer, out_len) >= out_len) {
return false;
}
}
return true;
}
Expand Down Expand Up @@ -512,8 +512,12 @@ bool print_amount(uint64_t amount,
if (!print_asset(asset, network_id, asset_info, 23)) {
return false;
};
strlcat(out, " ", out_len);
strlcat(out, asset_info, out_len);
if (strlcat(out, " ", out_len) >= out_len) {
return false;
}
if (strlcat(out, asset_info, out_len) >= out_len) {
return false;
}
}
return true;
}
Expand Down Expand Up @@ -765,7 +769,6 @@ bool add_separator_to_number(char *out, size_t out_len) {

// If there is a decimal point, append the part after the decimal point
if (decimal_point) {
// strlcpy(temp + new_length, decimal_point, NUMBER_WITH_COMMAS_MAX_LENGTH - new_length);
if (strlcpy(temp + new_length, decimal_point, NUMBER_WITH_COMMAS_MAX_LENGTH - new_length) >=
NUMBER_WITH_COMMAS_MAX_LENGTH - new_length) {
return false;
Expand Down Expand Up @@ -874,8 +877,9 @@ bool print_scv_symbol(const scv_symbol_t *scv_symbol, char *out, size_t out_len)
if (!is_printable_binary(scv_symbol->symbol, scv_symbol->size)) {
return false;
}
memcpy(out, scv_symbol->symbol, scv_symbol->size);
out[scv_symbol->size] = '\0';
if (!print_string(out, out_len, scv_symbol->symbol, scv_symbol->size)) {
return false;
}
return true;
}

Expand Down Expand Up @@ -936,3 +940,40 @@ bool print_scv_string(const scv_string_t *scv_string, char *out, size_t out_len)

return true;
}

bool print_string(char *out, size_t out_len, const uint8_t *src, size_t src_size) {
if (out == NULL || src == NULL || out_len == 0) {
return false;
}
if (src_size + 1 > out_len) {
return false;
}
memcpy(out, src, src_size);
out[src_size] = '\0';
return true;
}

bool print_price(const price_t *price,
const asset_t *asset_a,
const asset_t *asset_b,
uint8_t network_id,
char *out,
size_t out_len) {
uint64_t scaled_price = ((uint64_t) price->n * 10000000) / price->d;
if (!print_amount(scaled_price, NULL, network_id, out, out_len)) {
return false;
}

if (asset_a != NULL && asset_b != NULL) {
char tmp_asset_code[ASSET_CODE_MAX_LENGTH] = {0};
if (strlcat(out, " ", out_len) >= out_len ||
!print_asset_name(asset_a, network_id, tmp_asset_code, sizeof(tmp_asset_code)) ||
strlcat(out, tmp_asset_code, out_len) >= out_len ||
strlcat(out, "/", out_len) >= out_len ||
!print_asset_name(asset_b, network_id, tmp_asset_code, sizeof(tmp_asset_code)) ||
strlcat(out, tmp_asset_code, out_len) >= out_len) {
return false;
}
}
return true;
}
2 changes: 1 addition & 1 deletion tests_unit/testcases/opAccountMerge.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ Sequence Num; 103720918407102568
Valid Before (UTC); 2022-12-12 04:12:12
Tx Source; GDUTHC..XM2FN7
Operation Type; Account Merge
Merge Account; GDUTHCF37UX32EMANXIL2WOOVEDZ47GHBTT3DYKU6EKM37SOIZXM2FN7
Send; All Funds
Destination; GDRMNAIPTNIJWJSL6JOF76CJORN47TDVMWERTXO2G2WKOMXGNHUFL5QX
Op Source; GDUTHC..XM2FN7
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ Sequence Num; 103720918407102568
Valid Before (UTC); 2022-12-12 04:12:12
Tx Source; GDUTHC..XM2FN7
Operation Type; Account Merge
Merge Account; GDUTHCF37UX32EMANXIL2WOOVEDZ47GHBTT3DYKU6EKM37SOIZXM2FN7
Send; All Funds
Destination; MDRMNAIPTNIJWJSL6JOF76CJORN47TDVMWERTXO2G2WKOMXGNHUFKAAAAAAAAABHCCLDW
Op Source; GDUTHC..XM2FN7
2 changes: 1 addition & 1 deletion tests_unit/testcases/opExtendFootprintTtl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ Network; Testnet
Max Fee; 0.0040914 XLM
Sequence Num; 12842214208045061
Tx Source; GDUTHC..XM2FN7
Soroban; Extend Footprint TTL
Operation Type; Extend Footprint TTL
2 changes: 1 addition & 1 deletion tests_unit/testcases/opManageBuyOfferCreate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Max Fee; 0.00001 XLM
Sequence Num; 103720918407102568
Valid Before (UTC); 2022-12-12 04:12:12
Tx Source; GDUTHC..XM2FN7
Create Offer; Type Active
Create Offer;
Sell; BTC@GAT..MTCH
Buy; 988,448,111.2222 XLM
Price; 0.0001011 BTC/XLM
Expand Down
2 changes: 1 addition & 1 deletion tests_unit/testcases/opManageSellOfferCreate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Max Fee; 0.00001 XLM
Sequence Num; 103720918407102568
Valid Before (UTC); 2022-12-12 04:12:12
Tx Source; GDUTHC..XM2FN7
Create Offer; Type Active
Create Offer;
Buy; XLM
Sell; 988,448,423.2134 BTC@GAT..MTCH
Price; 0.0001234 XLM/BTC
Expand Down
2 changes: 1 addition & 1 deletion tests_unit/testcases/opRestoreFootprint.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ Max Fee; 0.00001 XLM
Sequence Num; 103720918407102568
Valid Before (UTC); 2022-12-12 04:12:12
Tx Source; GDUTHC..XM2FN7
Soroban; Restore Footprint
Operation Type; Restore Footprint
Op Source; GDUTHC..XM2FN7
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests_zemu/snapshots/s-op-account-merge/00007.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/s-op-account-merge/00008.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/s-op-account-merge/00009.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/s-op-account-merge/00010.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/s-op-account-merge/00011.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/s-op-account-merge/00012.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/s-op-account-merge/00013.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/s-op-account-merge/00014.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed tests_zemu/snapshots/s-op-account-merge/00015.png
Binary file not shown.
Binary file removed tests_zemu/snapshots/s-op-account-merge/00016.png
Binary file not shown.
Binary file removed tests_zemu/snapshots/s-op-account-merge/00017.png
Binary file not shown.
Binary file modified tests_zemu/snapshots/s-op-extend-footprint-ttl/00005.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/s-op-manage-buy-offer-create/00006.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/s-op-manage-sell-offer-create/00006.png
Binary file modified tests_zemu/snapshots/s-op-restore-footprint/00006.png
Diff not rendered.
Binary file modified tests_zemu/snapshots/sp-op-account-merge/00007.png
Binary file modified tests_zemu/snapshots/sp-op-account-merge/00008.png
Binary file modified tests_zemu/snapshots/sp-op-account-merge/00009.png
Binary file modified tests_zemu/snapshots/sp-op-account-merge/00010.png
Binary file modified tests_zemu/snapshots/sp-op-account-merge/00011.png
Binary file modified tests_zemu/snapshots/sp-op-account-merge/00012.png
Binary file removed tests_zemu/snapshots/sp-op-account-merge/00013.png
Diff not rendered.
Binary file modified tests_zemu/snapshots/sp-op-extend-footprint-ttl/00005.png
Binary file modified tests_zemu/snapshots/sp-op-manage-buy-offer-create/00006.png
Binary file modified tests_zemu/snapshots/sp-op-manage-sell-offer-create/00006.png
Binary file modified tests_zemu/snapshots/sp-op-restore-footprint/00006.png
Binary file modified tests_zemu/snapshots/stax-op-account-merge/00002.png
Binary file modified tests_zemu/snapshots/stax-op-account-merge/00003.png
Binary file modified tests_zemu/snapshots/stax-op-extend-footprint-ttl/00001.png
Binary file modified tests_zemu/snapshots/stax-op-manage-buy-offer-create/00001.png
Binary file modified tests_zemu/snapshots/stax-op-manage-buy-offer-create/00002.png
Binary file modified tests_zemu/snapshots/stax-op-manage-buy-offer-create/00003.png
Binary file modified tests_zemu/snapshots/stax-op-manage-buy-offer-create/00004.png
Diff not rendered.
Binary file modified tests_zemu/snapshots/stax-op-manage-sell-offer-create/00001.png
Binary file modified tests_zemu/snapshots/stax-op-manage-sell-offer-create/00002.png
Binary file modified tests_zemu/snapshots/stax-op-manage-sell-offer-create/00003.png
Binary file modified tests_zemu/snapshots/stax-op-manage-sell-offer-create/00004.png
Diff not rendered.
Binary file modified tests_zemu/snapshots/stax-op-restore-footprint/00002.png
Diff not rendered.
Binary file modified tests_zemu/snapshots/x-op-account-merge/00007.png
Binary file modified tests_zemu/snapshots/x-op-account-merge/00008.png
Binary file modified tests_zemu/snapshots/x-op-account-merge/00009.png
Binary file modified tests_zemu/snapshots/x-op-account-merge/00010.png
Binary file modified tests_zemu/snapshots/x-op-account-merge/00011.png
Binary file modified tests_zemu/snapshots/x-op-account-merge/00012.png
Binary file removed tests_zemu/snapshots/x-op-account-merge/00013.png
Diff not rendered.
Binary file modified tests_zemu/snapshots/x-op-extend-footprint-ttl/00005.png
Binary file modified tests_zemu/snapshots/x-op-manage-buy-offer-create/00006.png
Binary file modified tests_zemu/snapshots/x-op-manage-sell-offer-create/00006.png
Binary file modified tests_zemu/snapshots/x-op-restore-footprint/00006.png

0 comments on commit a568429

Please sign in to comment.