Skip to content

Commit

Permalink
df: incorporate a few spec changes -- serial_id is now 64-bits
Browse files Browse the repository at this point in the history
And we pass 3-params for feerate so it's a 'pick a range' conversation.
  • Loading branch information
niftynei committed Oct 28, 2020
1 parent d464272 commit 97fd18f
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 64 deletions.
2 changes: 1 addition & 1 deletion channeld/channeld.c
Original file line number Diff line number Diff line change
Expand Up @@ -2114,7 +2114,7 @@ static void handle_tx_sigs(struct peer *peer, const u8 *msg)
for (size_t i = 0; i < peer->psbt->num_inputs; i++) {
struct wally_psbt_input *in =
&peer->psbt->inputs[i];
u16 in_serial;
u64 in_serial;
const struct witness_element **elem;

if (!psbt_get_serial_id(&in->unknowns, &in_serial)) {
Expand Down
2 changes: 1 addition & 1 deletion common/psbt_internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ psbt_to_witness_stacks(const tal_t *ctx,
enum tx_role side_to_stack)
{
size_t stack_index;
u16 serial_id;
u64 serial_id;
const struct witness_stack **stacks
= tal_arr(ctx, const struct witness_stack *, psbt->num_inputs);

Expand Down
42 changes: 21 additions & 21 deletions common/psbt_open.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
#include <common/pseudorand.h>
#include <common/utils.h>

bool psbt_get_serial_id(const struct wally_map *map, u16 *serial_id)
bool psbt_get_serial_id(const struct wally_map *map, u64 *serial_id)
{
size_t value_len;
beint16_t bev;
beint64_t bev;
void *result = psbt_get_lightning(map, PSBT_TYPE_SERIAL_ID, &value_len);
if (!result)
return false;
Expand All @@ -21,14 +21,14 @@ bool psbt_get_serial_id(const struct wally_map *map, u16 *serial_id)
return false;

memcpy(&bev, result, value_len);
*serial_id = be16_to_cpu(bev);
*serial_id = be64_to_cpu(bev);
return true;
}

static int compare_serials(const struct wally_map *map_a,
const struct wally_map *map_b)
{
u16 serial_left, serial_right;
u64 serial_left, serial_right;
bool ok;

ok = psbt_get_serial_id(map_a, &serial_left);
Expand Down Expand Up @@ -324,28 +324,28 @@ struct psbt_changeset *psbt_get_changeset(const tal_t *ctx,

void psbt_input_set_serial_id(const tal_t *ctx,
struct wally_psbt_input *input,
u16 serial_id)
u64 serial_id)
{
u8 *key = psbt_make_key(tmpctx, PSBT_TYPE_SERIAL_ID, NULL);
beint16_t bev = cpu_to_be16(serial_id);
beint64_t bev = cpu_to_be64(serial_id);

psbt_input_set_unknown(ctx, input, key, &bev, sizeof(bev));
}


void psbt_output_set_serial_id(const tal_t *ctx,
struct wally_psbt_output *output,
u16 serial_id)
u64 serial_id)
{
u8 *key = psbt_make_key(tmpctx, PSBT_TYPE_SERIAL_ID, NULL);
beint16_t bev = cpu_to_be16(serial_id);
beint64_t bev = cpu_to_be64(serial_id);
psbt_output_set_unknown(ctx, output, key, &bev, sizeof(bev));
}

int psbt_find_serial_input(struct wally_psbt *psbt, u16 serial_id)
int psbt_find_serial_input(struct wally_psbt *psbt, u64 serial_id)
{
for (size_t i = 0; i < psbt->num_inputs; i++) {
u16 in_serial;
u64 in_serial;
if (!psbt_get_serial_id(&psbt->inputs[i].unknowns, &in_serial))
continue;
if (in_serial == serial_id)
Expand All @@ -354,10 +354,10 @@ int psbt_find_serial_input(struct wally_psbt *psbt, u16 serial_id)
return -1;
}

int psbt_find_serial_output(struct wally_psbt *psbt, u16 serial_id)
int psbt_find_serial_output(struct wally_psbt *psbt, u64 serial_id)
{
for (size_t i = 0; i < psbt->num_outputs; i++) {
u16 out_serial;
u64 out_serial;
if (!psbt_get_serial_id(&psbt->outputs[i].unknowns, &out_serial))
continue;
if (out_serial == serial_id)
Expand All @@ -366,14 +366,14 @@ int psbt_find_serial_output(struct wally_psbt *psbt, u16 serial_id)
return -1;
}

static u16 get_random_serial(enum tx_role role)
static u64 get_random_serial(enum tx_role role)
{
return pseudorand(1 << 15) << 1 | role;
return pseudorand_u64() << 1 | role;
}

u16 psbt_new_input_serial(struct wally_psbt *psbt, enum tx_role role)
u64 psbt_new_input_serial(struct wally_psbt *psbt, enum tx_role role)
{
u16 serial_id;
u64 serial_id;

while ((serial_id = get_random_serial(role)) &&
psbt_find_serial_input(psbt, serial_id) != -1) {
Expand All @@ -383,9 +383,9 @@ u16 psbt_new_input_serial(struct wally_psbt *psbt, enum tx_role role)
return serial_id;
}

u16 psbt_new_output_serial(struct wally_psbt *psbt, enum tx_role role)
u64 psbt_new_output_serial(struct wally_psbt *psbt, enum tx_role role)
{
u16 serial_id;
u64 serial_id;

while ((serial_id = get_random_serial(role)) &&
psbt_find_serial_output(psbt, serial_id) != -1) {
Expand All @@ -397,7 +397,7 @@ u16 psbt_new_output_serial(struct wally_psbt *psbt, enum tx_role role)

bool psbt_has_required_fields(struct wally_psbt *psbt)
{
u16 serial_id;
u64 serial_id;
for (size_t i = 0; i < psbt->num_inputs; i++) {
struct wally_psbt_input *input = &psbt->inputs[i];

Expand Down Expand Up @@ -428,7 +428,7 @@ bool psbt_has_required_fields(struct wally_psbt *psbt)

bool psbt_side_finalized(const struct wally_psbt *psbt, enum tx_role role)
{
u16 serial_id;
u64 serial_id;
for (size_t i = 0; i < psbt->num_inputs; i++) {
if (!psbt_get_serial_id(&psbt->inputs[i].unknowns,
&serial_id)) {
Expand All @@ -446,7 +446,7 @@ bool psbt_side_finalized(const struct wally_psbt *psbt, enum tx_role role)
/* Adds serials to inputs + outputs that don't have one yet */
void psbt_add_serials(struct wally_psbt *psbt, enum tx_role role)
{
u16 serial_id;
u64 serial_id;
for (size_t i = 0; i < psbt->num_inputs; i++) {
/* Skip ones that already have a serial id */
if (psbt_get_serial_id(&psbt->inputs[i].unknowns, &serial_id))
Expand Down
14 changes: 7 additions & 7 deletions common/psbt_open.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct psbt_changeset {
* Returns false if serial_id is not present
*/
WARN_UNUSED_RESULT bool psbt_get_serial_id(const struct wally_map *map,
u16 *serial_id);
u64 *serial_id);

/* psbt_sort_by_serial_id - Sort PSBT by serial_ids
*
Expand Down Expand Up @@ -82,7 +82,7 @@ struct psbt_changeset *psbt_get_changeset(const tal_t *ctx,
*/
void psbt_input_set_serial_id(const tal_t *ctx,
struct wally_psbt_input *input,
u16 serial_id);
u64 serial_id);
/* psbt_output_set_serial_id - Sets a serial id on given output
*
* @ctx - tal context for allocations
Expand All @@ -91,7 +91,7 @@ void psbt_input_set_serial_id(const tal_t *ctx,
*/
void psbt_output_set_serial_id(const tal_t *ctx,
struct wally_psbt_output *output,
u16 serial_id);
u64 serial_id);

/* psbt_sort_by_serial_id - Sorts the inputs + outputs by serial_id
*
Expand All @@ -108,7 +108,7 @@ void psbt_sort_by_serial_id(struct wally_psbt *psbt);
*
* Returns index of input with matching serial if found or -1
*/
int psbt_find_serial_input(struct wally_psbt *psbt, u16 serial_id);
int psbt_find_serial_input(struct wally_psbt *psbt, u64 serial_id);

/* psbt_find_serial_output - Checks outputs for provided serial_id
*
Expand All @@ -117,7 +117,7 @@ int psbt_find_serial_input(struct wally_psbt *psbt, u16 serial_id);
*
* Returns index of output with matching serial if found or -1
*/
int psbt_find_serial_output(struct wally_psbt *psbt, u16 serial_id);
int psbt_find_serial_output(struct wally_psbt *psbt, u64 serial_id);

/* psbt_new_input_serial - Generate a new serial for an input for {role}
*
Expand All @@ -126,7 +126,7 @@ int psbt_find_serial_output(struct wally_psbt *psbt, u16 serial_id);
*
* Returns a new, unique serial of the correct parity for the specified {role}
*/
u16 psbt_new_input_serial(struct wally_psbt *psbt, enum tx_role role);
u64 psbt_new_input_serial(struct wally_psbt *psbt, enum tx_role role);

/* psbt_new_output_serial - Generate a new serial for an output for {role}
*
Expand All @@ -135,7 +135,7 @@ u16 psbt_new_input_serial(struct wally_psbt *psbt, enum tx_role role);
*
* Returns a new, unique serial of the correct parity for the specified {role}
*/
u16 psbt_new_output_serial(struct wally_psbt *psbt, enum tx_role role);
u64 psbt_new_output_serial(struct wally_psbt *psbt, enum tx_role role);

/* psbt_has_required_fields - Validates psbt field completion
*
Expand Down
6 changes: 3 additions & 3 deletions common/test/exp-run-psbt_diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ u64 fromwire_u64(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
/* Generated stub for fromwire_u8 */
u8 fromwire_u8(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_u8 called!\n"); abort(); }
/* Generated stub for pseudorand */
uint64_t pseudorand(uint64_t max UNNEEDED)
{ fprintf(stderr, "pseudorand called!\n"); abort(); }
/* Generated stub for pseudorand_u64 */
uint64_t pseudorand_u64(void)
{ fprintf(stderr, "pseudorand_u64 called!\n"); abort(); }
/* Generated stub for towire */
void towire(u8 **pptr UNNEEDED, const void *data UNNEEDED, size_t len UNNEEDED)
{ fprintf(stderr, "towire called!\n"); abort(); }
Expand Down
8 changes: 4 additions & 4 deletions lightningd/dual_open_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ static bool psbt_side_contribs_changed(struct wally_psbt *orig,
enum side opener_side)
{
struct psbt_changeset *cs;
u16 serial_id;
u64 serial_id;
bool ok;

cs = psbt_get_changeset(tmpctx, orig, new);
Expand Down Expand Up @@ -636,7 +636,7 @@ static void opener_psbt_changed(struct subd *dualopend,
const u8 *msg)
{
struct channel_id cid;
u16 funding_serial;
u64 funding_serial;
struct wally_psbt *psbt;
struct json_stream *response;
struct command *cmd = uc->fc->cmd;
Expand All @@ -656,7 +656,7 @@ static void opener_psbt_changed(struct subd *dualopend,
type_to_string(tmpctx, struct channel_id, &cid));
json_add_psbt(response, "psbt", psbt);
json_add_bool(response, "commitments_secured", false);
json_add_num(response, "funding_serial", funding_serial);
json_add_u64(response, "funding_serial", funding_serial);

uc->cid = cid;
uc->fc->inflight = true;
Expand Down Expand Up @@ -911,7 +911,7 @@ static void opener_commit_received(struct subd *dualopend,
static void accepter_psbt_changed(struct subd *dualopend,
const u8 *msg)
{
u16 unused;
u64 unused;
struct openchannel2_psbt_payload *payload =
tal(dualopend, struct openchannel2_psbt_payload);
payload->dualopend = dualopend;
Expand Down
Loading

0 comments on commit 97fd18f

Please sign in to comment.