Skip to content

Commit

Permalink
action dutch auction end support
Browse files Browse the repository at this point in the history
  • Loading branch information
abenso committed Jan 23, 2025
1 parent cda92ee commit 68e3ecf
Show file tree
Hide file tree
Showing 17 changed files with 7,879 additions and 8,032 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ file(GLOB_RECURSE LIB_SRC
${CMAKE_CURRENT_SOURCE_DIR}/app/src/plan/position_close.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/plan/position_withdraw.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/plan/action_dutch_auction_schedule.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/plan/action_dutch_auction_end.c
)

add_library(app_lib STATIC ${LIB_SRC})
Expand Down
2 changes: 2 additions & 0 deletions app/rust/src/parser/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ pub enum ParserError {
UndelegateClaimPlanError,
DelegatorVotePlanError,
PositionWithdrawPlanError,
DutchAuctionSchedulePlanError,
DutchAuctionEndPlanError,

// Chain related
InvalidChainId,
Expand Down
6 changes: 6 additions & 0 deletions app/rust/src/parser/plans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,12 @@ pub unsafe extern "C" fn rs_generic_action_hash(
data_to_hash,
);
}
ActionPlan::ActionDutchAuctionEnd => {
effect_hash = EffectHash::from_proto_effecting_data(
"/penumbra.core.component.auction.v1.ActionDutchAuctionEnd",
data_to_hash,
);
}
_ => {
return ParserError::UnexpectedData as u32;
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/common/parser_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ typedef enum {
parser_undelegate_claim_plan_error,
parser_delegator_vote_plan_error,
parser_position_withdraw_plan_error,
parser_action_dutch_auction_schedule_plan_error,
parser_action_dutch_auction_end_plan_error,

// Chain related
parser_invalid_chain_id,
Expand Down
10 changes: 8 additions & 2 deletions app/src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
#define POSITION_ID_BECH32_PREFIX "plpid"
#define POSITION_ID_LEN 32

#define AUCTION_ID_BECH32_PREFIX "pauctid"
#define AUCTION_ID_LEN 32

// Common BECH32m constants
#define CHECKSUM_LENGTH 8
#define BECH32_BITS_PER_CHAR 5
Expand Down Expand Up @@ -110,10 +113,13 @@
#define POSITION_OPEN_DISPLAY_MAX_LEN (2 * VALUE_DISPLAY_MAX_LEN + 110) // = 434

// Constant to use to allocate a buffer on the stack to hold the formatting of an position_close action
#define POSITION_CLOSE_DISPLAY_MAX_LEN 100 // = 100
#define POSITION_CLOSE_DISPLAY_MAX_LEN 100

// Constant to use to allocate a buffer on the stack to hold the formatting of an position_withdraw action
#define POSITION_WITHDRAW_DISPLAY_MAX_LEN 140 // = 140
#define POSITION_WITHDRAW_DISPLAY_MAX_LEN 140

// Constant to use to allocate a buffer on the stack to hold the formatting of an dutch_auction_schedule action
#define DUTCH_AUCTION_SCHEDULE_DISPLAY_MAX_LEN (4 * VALUE_DISPLAY_MAX_LEN + 154) // = 802

// Constant to use to allocate a buffer on the stack to hold the formatting of an dutch_auction_end action
#define DUTCH_AUCTION_END_DISPLAY_MAX_LEN 100
9 changes: 9 additions & 0 deletions app/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <zxmacros.h>
#include <zxtypes.h>

#include "action_dutch_auction_end.h"
#include "action_dutch_auction_schedule.h"
#include "coin.h"
#include "crypto.h"
Expand Down Expand Up @@ -131,6 +132,9 @@ parser_error_t parser_getNumItems(const parser_context_t *ctx, uint8_t *num_item
case penumbra_core_transaction_v1_ActionPlan_action_dutch_auction_schedule_tag:
CHECK_ERROR(action_dutch_auction_schedule_getNumItems(ctx, &action_num_items));
break;
case penumbra_core_transaction_v1_ActionPlan_action_dutch_auction_end_tag:
CHECK_ERROR(action_dutch_auction_end_getNumItems(ctx, &action_num_items));
break;
default:
return parser_unexpected_error;
}
Expand Down Expand Up @@ -248,6 +252,11 @@ parser_error_t parser_getItem(const parser_context_t *ctx, uint8_t displayIdx, c
ctx, &ctx->tx_obj->actions_plan[action_idx].action.action_dutch_auction_schedule, action_idx + 1, outKey,
outKeyLen, outVal, outValLen, pageIdx, pageCount))
break;
case penumbra_core_transaction_v1_ActionPlan_action_dutch_auction_end_tag:
CHECK_ERROR(action_dutch_auction_end_getItem(
ctx, &ctx->tx_obj->actions_plan[action_idx].action.action_dutch_auction_end, action_idx + 1, outKey,
outKeyLen, outVal, outValLen, pageIdx, pageCount))
break;
default:
return parser_unexpected_error;
}
Expand Down
6 changes: 6 additions & 0 deletions app/src/parser_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "parser_impl.h"

#include "action_dutch_auction_end.h"
#include "action_dutch_auction_schedule.h"
#include "delegate.h"
#include "delegator_vote.h"
Expand Down Expand Up @@ -139,6 +140,11 @@ bool decode_action(pb_istream_t *stream, const pb_field_t *field, void **arg) {
CHECK_ACTION_ERROR(decode_action_dutch_auction_schedule_plan(
&action_data_4, &decode_arg[actions_qty].action.action_dutch_auction_schedule));
break;
case penumbra_core_transaction_v1_ActionPlan_action_dutch_auction_end_tag:
decode_arg[actions_qty].action_data = action_data_3;
CHECK_ACTION_ERROR(decode_action_dutch_auction_end_plan(
&action_data_3, &decode_arg[actions_qty].action.action_dutch_auction_end));
break;
default:
return false;
}
Expand Down
1 change: 1 addition & 0 deletions app/src/parser_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ zxerr_t compute_action_hash(action_t *action, bytes_t *memo_key, hash_t *output)
case penumbra_core_transaction_v1_ActionPlan_position_open_tag:
case penumbra_core_transaction_v1_ActionPlan_position_close_tag:
case penumbra_core_transaction_v1_ActionPlan_action_dutch_auction_schedule_tag:
case penumbra_core_transaction_v1_ActionPlan_action_dutch_auction_end_tag:
if (rs_generic_action_hash(&action->action_data, action->action_type, (uint8_t *)output, 64) != parser_ok) {
return zxerr_encoding_failed;
}
Expand Down
10 changes: 10 additions & 0 deletions app/src/parser_txdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ typedef struct {
bytes_t nonce;
} dutch_auction_description_t;

typedef struct {
bytes_t inner;
} auction_id_t;

typedef struct {
note_t note;
uint64_t position;
Expand Down Expand Up @@ -326,6 +330,11 @@ typedef struct {
dutch_auction_description_t description;
} action_dutch_auction_schedule_plan_t;

typedef struct {
bool has_auction_id;
auction_id_t auction_id;
} action_dutch_auction_end_plan_t;

typedef struct {
address_plan_t return_address;
bytes_t text;
Expand Down Expand Up @@ -362,6 +371,7 @@ typedef struct {
position_close_plan_t position_close;
position_withdraw_plan_t position_withdraw;
action_dutch_auction_schedule_plan_t action_dutch_auction_schedule;
action_dutch_auction_end_plan_t action_dutch_auction_end;
} action;
} action_t;

Expand Down
94 changes: 94 additions & 0 deletions app/src/plan/action_dutch_auction_end.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*******************************************************************************
* (c) 2018 - 2023 Zondax AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/
#include "action_dutch_auction_end.h"

#include "note.h"
#include "parser_pb_utils.h"
#include "ui_utils.h"
#include "zxformat.h"

parser_error_t decode_action_dutch_auction_end_plan(const bytes_t *data,
action_dutch_auction_end_plan_t *action_dutch_auction_end) {
penumbra_core_component_auction_v1_ActionDutchAuctionEnd action_dutch_auction_end_pb =
penumbra_core_component_auction_v1_ActionDutchAuctionEnd_init_default;

pb_istream_t stream = pb_istream_from_buffer(data->ptr, data->len);
CHECK_APP_CANARY()

// Set up fixed size fields
fixed_size_field_t auction_id_arg;
setup_decode_fixed_field(&action_dutch_auction_end_pb.auction_id.inner, &auction_id_arg,
&action_dutch_auction_end->auction_id.inner, ASSET_ID_LEN);

if (!pb_decode(&stream, penumbra_core_component_auction_v1_ActionDutchAuctionEnd_fields, &action_dutch_auction_end_pb)) {
return parser_action_dutch_auction_end_plan_error;
}

action_dutch_auction_end->has_auction_id = action_dutch_auction_end_pb.has_auction_id;

return parser_ok;
}

parser_error_t action_dutch_auction_end_getNumItems(const parser_context_t *ctx, uint8_t *num_items) {
UNUSED(ctx);
*num_items = 1;
return parser_ok;
}

parser_error_t action_dutch_auction_end_getItem(const parser_context_t *ctx,
const action_dutch_auction_end_plan_t *action_dutch_auction_end,
uint8_t actionIdx, char *outKey, uint16_t outKeyLen, char *outVal,
uint16_t outValLen, uint8_t pageIdx, uint8_t *pageCount) {
parser_error_t err = parser_no_data;
if (action_dutch_auction_end == NULL || outKey == NULL || outVal == NULL || outKeyLen == 0 || outValLen == 0) {
return err;
}

char bufferUI[DUTCH_AUCTION_END_DISPLAY_MAX_LEN] = {0};

snprintf(outKey, outKeyLen, "Action_%d", actionIdx);
CHECK_ERROR(action_dutch_auction_end_printValue(ctx, action_dutch_auction_end, bufferUI, sizeof(bufferUI)));
pageString(outVal, outValLen, bufferUI, pageIdx, pageCount);

return parser_ok;
}

parser_error_t action_dutch_auction_end_printValue(const parser_context_t *ctx,
const action_dutch_auction_end_plan_t *action_dutch_auction_end,
char *outVal, uint16_t outValLen) {
if (ctx == NULL || action_dutch_auction_end == NULL || outVal == NULL) {
return parser_no_data;
}

if (outValLen < DUTCH_AUCTION_END_DISPLAY_MAX_LEN) {
return parser_unexpected_buffer_end;
}

MEMZERO(outVal, outValLen);

// add action title
snprintf(outVal, outValLen, "DutchAuctionEnd Auction ID: ");
uint16_t written_value = strlen(outVal);

MEMCPY(outVal + written_value, action_dutch_auction_end->auction_id.inner.ptr,
action_dutch_auction_end->auction_id.inner.len);

CHECK_ERROR(encodeAuctionId(action_dutch_auction_end->auction_id.inner.ptr,
action_dutch_auction_end->auction_id.inner.len, outVal + written_value,
outValLen - written_value));

return parser_ok;
}
36 changes: 36 additions & 0 deletions app/src/plan/action_dutch_auction_end.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*******************************************************************************
* (c) 2018 - 2023 Zondax AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/
#pragma once

#include <zxmacros.h>

#include "parser_common.h"

#ifdef __cplusplus
extern "C" {
#endif

parser_error_t decode_action_dutch_auction_end_plan(const bytes_t *data, action_dutch_auction_end_plan_t *output);
parser_error_t action_dutch_auction_end_getNumItems(const parser_context_t *ctx, uint8_t *num_items);
parser_error_t action_dutch_auction_end_getItem(const parser_context_t *ctx, const action_dutch_auction_end_plan_t *output,
uint8_t displayIdx, char *outKey, uint16_t outKeyLen, char *outVal,
uint16_t outValLen, uint8_t actionIdx, uint8_t *pageCount);
parser_error_t action_dutch_auction_end_printValue(const parser_context_t *ctx,
const action_dutch_auction_end_plan_t *output, char *outVal,
uint16_t outValLen);
#ifdef __cplusplus
}
#endif
2 changes: 1 addition & 1 deletion app/src/plan/action_dutch_auction_schedule.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ parser_error_t decode_action_dutch_auction_schedule_plan(

if (!pb_decode(&stream, penumbra_core_component_auction_v1_ActionDutchAuctionSchedule_fields,
&action_dutch_auction_schedule_pb)) {
return parser_output_plan_error;
return parser_action_dutch_auction_schedule_plan_error;
}

action_dutch_auction_schedule->has_description = action_dutch_auction_schedule_pb.has_description;
Expand Down
9 changes: 9 additions & 0 deletions app/src/ui_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ parser_error_t encodePositionId(const uint8_t *position_id, uint16_t position_id
out, out_len);
}

parser_error_t encodeAuctionId(const uint8_t *auction_id, uint16_t auction_id_len, char *out, uint16_t out_len) {
// Validate input length
if (auction_id_len != AUCTION_ID_LEN) {
return parser_invalid_address;
}
return printBech32Encoded(AUCTION_ID_BECH32_PREFIX, sizeof(AUCTION_ID_BECH32_PREFIX) - 1, auction_id, auction_id_len,
out, out_len);
}

parser_error_t uint128_to_str(char *data, int dataLen, uint64_t high, uint64_t low) {
if (data == NULL) return parser_no_data;
if (dataLen < U128_STR_MAX_LEN) return parser_value_out_of_range;
Expand Down
1 change: 1 addition & 0 deletions app/src/ui_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ parser_error_t encodeAddress(const uint8_t *address, uint16_t address_len, char
parser_error_t printAssetId(const uint8_t *asset, uint16_t asset_len, char *out, uint16_t out_len);
parser_error_t encodeIdentityKey(const uint8_t *identity_key, uint16_t identity_key_len, char *out, uint16_t out_len);
parser_error_t encodePositionId(const uint8_t *position_id, uint16_t position_id_len, char *out, uint16_t out_len);
parser_error_t encodeAuctionId(const uint8_t *auction_id, uint16_t auction_id_len, char *out, uint16_t out_len);

/**
* Converts a 128-bit unsigned integer to its decimal string representation.
Expand Down
22 changes: 13 additions & 9 deletions tests/parser_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ TEST(SCALE, ReadBytes) {
uint8_t buffer[6000];
auto bufferLen = parseHexString(
buffer, sizeof(buffer),
"0aa701aa03a3010aa0010a300a0a088cc0b0b4f4beb4a30712220a2029ea9c2f3371f6a487e7e95c247041f4a356f983eb064e5d2b3bcf322ca"
"96a1012220a2029ea9c2f3371f6a487e7e95c247041f4a356f983eb064e5d2b3bcf322ca96a101a0a08c581b4d0d5dbc7ec05220a0886e68daa"
"c0b4a6f6092897da857c3098da857c38b79dcba30142200c5f7b2df77aabf7f38d26618485f51c51337c159528d013408c7601cdb310a60aa90"
"1aa03a5010aa2010a300a0a08ebea9d83c9fb9bc40512220a2029ea9c2f3371f6a487e7e95c247041f4a356f983eb064e5d2b3bcf322ca96a10"
"12220a2029ea9c2f3371f6a487e7e95c247041f4a356f983eb064e5d2b3bcf322ca96a101a0a08faa6cda885b0bea702220a08d99f9ca9e0c0c"
"3dc0428a4a5bca40130a5a5bca40138d5e7c3b50342209138cb498ea2007358ecedf67cd752780c1b9de4e0be7c57da199107ded3226f121a12"
"0a70656e756d6272612d311a0c0a0a08cdada9db83bc938804");
"0a27b203240a220a20d236a0af60136307f49dcf9d21d4d5bfc8025b4ada87a819cb0d6de82681b8e0121e08c6a70d120a70656e756d6272612"
"d311a0c0a0a0889aaa69f92f5d9c3042afa030ad5030a520a50caef612cb3e0aa9799cfad490583bac0ca8240ec27085df3409c6fb8721a7490"
"c7c4b1e566574b4278f257c6aa49cda0cd4d2d0ab42a889e0b40a183a7915312ebdde08f869eddac9d1445c387c3e35f12fe02205a716c31644"
"b7057343262535220204f20783051596d4a6c394448644e76325171203266302030307a423753306a515920316b207135435046733020736820"
"68544e793935696e2057716d30476558667a342020334f30346d536e45334e44366737652020306f3753697245386a4936204620336c3542776"
"c55204c6267506a6632203420204b415838363252342051367120206d3039642071204420354f205a776948356820706d7a205336316a204146"
"74566333322078203278205975303532206132584f53206c6b3553304120372020533820775a78334d786f36676d75543538205576767854376"
"22020472038344e4320635430474655636a6f2063384d346d2074663938323651656f6e496b73505220336c5565436957736976374f56366875"
"5639416342346d37207320207a59202056313679203439384c207420594c4f307473463662594e694636343174557948507632206920674c6f6"
"1505a6652324a346e202055634556323820203143204620584d68386320411220618b73a44f2dd0b8b5e444e7fc7df7bef6cdf33b1a4bad802c"
"4a48d5021a2678");

err = parser_parse(&ctx, buffer, bufferLen, &tx_obj);
ASSERT_EQ(err, parser_ok) << parser_getErrorDescription(err);
Expand All @@ -64,8 +68,8 @@ TEST(SCALE, ReadBytes) {
ASSERT_EQ(zxerr, zxerr_ok);

std::string expected =
"8a5be23d8930ab2cf2a5457846e0030f99dfe6e35c79c83068bf09dc057ce427827ae3c3cb95ad5e1115d21025af4e5e53f8e152c0ea2bad2a0"
"f5604894a84f5";
"9dd2c5349b49dc367bc7e51d639773dd1850f68f885ad33d2cb0417a790c854eb865387f99738cdaf951a03ddb2319d81563448c99f9061baa5"
"0d6e8423b7f97";
char actual[129];
array_to_hexstr(actual, sizeof(actual), tx_obj.effect_hash, sizeof(tx_obj.effect_hash));

Expand Down
Loading

0 comments on commit 68e3ecf

Please sign in to comment.