Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ const struct wally_map_item *map_find_equal_integer(const struct wally_map *lhs,
/* Clamp initial witness stack allocation sizing */
#define MAX_WITNESS_ITEMS_ALLOC 100u /* Non-Taproot standardness limit */

/* Allows allocating a larger witness for e.g deserializing */
struct wally_tx_witness_stack;
int tx_witness_stack_init_alloc(size_t allocation_len,
size_t max_allocation_len,
struct wally_tx_witness_stack **output);

/* Absolute maximum number of inputs and outputs for BTC.
* Liquid numbers are smaller; we use the upper limit */
#define TX_MAX_INPUTS (TX_MAX_INPUTS_ALLOC * 10)
Expand Down
3 changes: 2 additions & 1 deletion src/pullpush.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ int pull_witness(const unsigned char **cursor, size_t *max,
/* Not enough bytes remaining for num_witnesses empty witnesses */
return WALLY_EINVAL;
}
ret = wally_tx_witness_stack_init_alloc(num_witnesses, witness_out);
ret = tx_witness_stack_init_alloc(num_witnesses, num_witnesses,
witness_out);

for (i = 0; ret == WALLY_OK && i < num_witnesses; ++i) {
const unsigned char *wit;
Expand Down
28 changes: 20 additions & 8 deletions src/transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ int wally_tx_witness_stack_clone_alloc(const struct wally_tx_witness_stack *stac
if (!stack)
return WALLY_EINVAL;

ret = wally_tx_witness_stack_init_alloc(stack->items_allocation_len, output);
ret = tx_witness_stack_init_alloc(stack->items_allocation_len,
stack->items_allocation_len, output);
for (i = 0; ret == WALLY_OK && i < stack->num_items; ++i) {
ret = wally_tx_witness_stack_set(*output, i,
stack->items[i].witness,
Expand All @@ -151,15 +152,16 @@ int wally_tx_witness_stack_clone_alloc(const struct wally_tx_witness_stack *stac
return ret;
}

int wally_tx_witness_stack_init_alloc(size_t allocation_len,
struct wally_tx_witness_stack **output)
int tx_witness_stack_init_alloc(size_t allocation_len,
size_t max_allocation_len,
struct wally_tx_witness_stack **output)
{
OUTPUT_CHECK;
OUTPUT_ALLOC(struct wally_tx_witness_stack);

if (allocation_len) {
if (allocation_len > MAX_WITNESS_ITEMS_ALLOC)
allocation_len = MAX_WITNESS_ITEMS_ALLOC;
if (allocation_len > max_allocation_len)
allocation_len = max_allocation_len;
(*output)->items = wally_calloc(allocation_len * sizeof(struct wally_tx_witness_item));
if (!(*output)->items) {
wally_free(*output);
Expand All @@ -172,6 +174,16 @@ int wally_tx_witness_stack_init_alloc(size_t allocation_len,
return WALLY_OK;
}

int wally_tx_witness_stack_init_alloc(size_t allocation_len,
struct wally_tx_witness_stack **output)
{
/* The public interface is limited to pre-allocating enough
* witness items for a standard tx, and will be slow if adding more
*/
return tx_witness_stack_init_alloc(allocation_len,
MAX_WITNESS_ITEMS_ALLOC, output);
}

static int tx_witness_stack_free(struct wally_tx_witness_stack *stack,
bool free_parent)
{
Expand Down Expand Up @@ -2371,7 +2383,7 @@ static int witness_stack_from_bytes(const unsigned char *bytes, struct wally_tx_
const unsigned char *p = bytes;
p += varint_from_bytes(p, &num_witnesses);
if (num_witnesses) {
ret = wally_tx_witness_stack_init_alloc(num_witnesses, witness);
ret = tx_witness_stack_init_alloc(num_witnesses, num_witnesses, witness);
if (ret != WALLY_OK)
goto cleanup;

Expand Down Expand Up @@ -2485,8 +2497,8 @@ static int tx_from_bytes(const unsigned char *bytes, size_t bytes_len,
p += varint_from_bytes(p, &num_witnesses);
if (!num_witnesses)
continue;
ret = wally_tx_witness_stack_init_alloc(num_witnesses,
&(*output)->inputs[i].witness);
ret = tx_witness_stack_init_alloc(num_witnesses, num_witnesses,
&(*output)->inputs[i].witness);
if (ret != WALLY_OK)
goto fail;

Expand Down
Loading