psbt: have the unknown map 'add' be a 'set' instead

This commit is contained in:
niftynei
2020-10-09 14:37:15 -05:00
committed by Rusty Russell
parent a7d4b8780e
commit 4034d0c306
7 changed files with 90 additions and 45 deletions

View File

@@ -398,24 +398,24 @@ u8 *psbt_changeset_get_next(const tal_t *ctx, struct channel_id *cid,
return NULL;
}
void psbt_input_add_serial_id(const tal_t *ctx,
void psbt_input_set_serial_id(const tal_t *ctx,
struct wally_psbt_input *input,
u16 serial_id)
{
u8 *key = psbt_make_key(tmpctx, PSBT_TYPE_SERIAL_ID, NULL);
beint16_t bev = cpu_to_be16(serial_id);
psbt_input_add_unknown(ctx, input, key, &bev, sizeof(bev));
psbt_input_set_unknown(ctx, input, key, &bev, sizeof(bev));
}
void psbt_output_add_serial_id(const tal_t *ctx,
struct wally_psbt_output *output,
void psbt_output_set_serial_id(const tal_t *ctx,
struct wally_psbt_output *output,
u16 serial_id)
{
u8 *key = psbt_make_key(tmpctx, PSBT_TYPE_SERIAL_ID, NULL);
beint16_t bev = cpu_to_be16(serial_id);
psbt_output_add_unknown(ctx, output, key, &bev, sizeof(bev));
psbt_output_set_unknown(ctx, output, key, &bev, sizeof(bev));
}
int psbt_find_serial_input(struct wally_psbt *psbt, u16 serial_id)

View File

@@ -85,22 +85,22 @@ struct psbt_changeset *psbt_get_changeset(const tal_t *ctx,
u8 *psbt_changeset_get_next(const tal_t *ctx, struct channel_id *cid,
struct psbt_changeset *set);
/* psbt_input_add_serial_id - Adds a serial id to given input
/* psbt_input_set_serial_id - Sets a serial id on given input
*
* @ctx - tal context for allocations
* @input - to add serial_id to
* @serial_id - to add
* @input - to set serial_id on
* @serial_id - to set
*/
void psbt_input_add_serial_id(const tal_t *ctx,
void psbt_input_set_serial_id(const tal_t *ctx,
struct wally_psbt_input *input,
u16 serial_id);
/* psbt_output_add_serial_id - Adds a serial id to given output
u16 serial_id);
/* psbt_output_set_serial_id - Sets a serial id on given output
*
* @ctx - tal context for allocations
* @output - to add serial_id to
* @serial_id - to add
* @output - to set serial_id on
* @serial_id - to set
*/
void psbt_output_add_serial_id(const tal_t *ctx,
void psbt_output_set_serial_id(const tal_t *ctx,
struct wally_psbt_output *output,
u16 serial_id);

View File

@@ -117,7 +117,7 @@ static void add_in_out_with_serial(struct wally_psbt *psbt,
NULL, NULL, NULL);
if (!in)
abort();
psbt_input_add_serial_id(psbt, in, serial_id);
psbt_input_set_serial_id(psbt, in, serial_id);
script = tal_arr(tmpctx, u8, 20);
memset(script, default_value, 20);
@@ -125,7 +125,25 @@ static void add_in_out_with_serial(struct wally_psbt *psbt,
out = psbt_append_output(psbt, script, sat);
if (!out)
abort();
psbt_output_add_serial_id(psbt, out, serial_id);
psbt_output_set_serial_id(psbt, out, serial_id);
}
/* Try changing up the serial ids */
static void change_serials(void)
{
struct wally_psbt *psbt;
psbt = create_psbt(tmpctx, 1, 1, 0);
add_in_out_with_serial(psbt, 10, 1);
psbt_output_set_serial_id(psbt, &psbt->outputs[0], 2);
assert(psbt_find_serial_output(psbt, 2) == 0);
assert(psbt_find_serial_output(psbt, 10) == -1);
psbt_input_set_serial_id(psbt, &psbt->inputs[0], 4);
assert(psbt_find_serial_input(psbt, 4) == 0);
assert(psbt_find_serial_input(psbt, 10) == -1);
}
int main(int argc, const char *argv[])
@@ -195,8 +213,8 @@ int main(int argc, const char *argv[])
/* Add some extra unknown info to a PSBT */
u8 *key = psbt_make_key(tmpctx, 0x05, NULL);
char *val = tal_fmt(tmpctx, "hello");
psbt_input_add_unknown(end, &end->inputs[1], key, val, tal_bytelen(val));
psbt_input_add_unknown(start, &start->inputs[1], key, val, tal_bytelen(val));
psbt_input_set_unknown(end, &end->inputs[1], key, val, tal_bytelen(val));
psbt_input_set_unknown(start, &start->inputs[1], key, val, tal_bytelen(val));
/* Swap locations */
struct wally_map_item tmp;
@@ -208,6 +226,8 @@ int main(int argc, const char *argv[])
diff_count(start, end, 1, 1);
diff_count(end, start, 1, 1);
change_serials();
/* No memory leaks please */
common_shutdown();
return 0;