Skip to content

Commit

Permalink
tools/generate_wire.py: generate varlen arrays properly.
Browse files Browse the repository at this point in the history
These are now logically arrays of pointers.  This is much more natural,
and gets rid of the horrible utxo array converters.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell authored and cdecker committed Feb 8, 2018
1 parent ad8dfac commit 526d3a2
Show file tree
Hide file tree
Showing 14 changed files with 104 additions and 120 deletions.
19 changes: 11 additions & 8 deletions channeld/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -1064,15 +1064,15 @@ static u8 *got_commitsig_msg(const tal_t *ctx,
const tal_t *tmpctx = tal_tmpctx(ctx);
struct changed_htlc *changed;
struct fulfilled_htlc *fulfilled;
struct failed_htlc *failed;
const struct failed_htlc **failed;
struct added_htlc *added;
struct secret *shared_secret;
u8 *msg;

changed = tal_arr(tmpctx, struct changed_htlc, 0);
added = tal_arr(tmpctx, struct added_htlc, 0);
shared_secret = tal_arr(tmpctx, struct secret, 0);
failed = tal_arr(tmpctx, struct failed_htlc, 0);
failed = tal_arr(tmpctx, const struct failed_htlc *, 0);
fulfilled = tal_arr(tmpctx, struct fulfilled_htlc, 0);

for (size_t i = 0; i < tal_count(changed_htlcs); i++) {
Expand All @@ -1096,12 +1096,13 @@ static u8 *got_commitsig_msg(const tal_t *ctx,
f->id = htlc->id;
f->payment_preimage = *htlc->r;
} else {
struct failed_htlc *f;
struct failed_htlc **f;
assert(htlc->fail);
f = tal_arr_append(&failed);
f->id = htlc->id;
f->malformed = htlc->malformed;
f->failreason = cast_const(u8 *, htlc->fail);
*f = tal(failed, struct failed_htlc);
(*f)->id = htlc->id;
(*f)->malformed = htlc->malformed;
(*f)->failreason = cast_const(u8 *, htlc->fail);
}
} else {
struct changed_htlc *c = tal_arr_append(&changed);
Expand Down Expand Up @@ -2421,7 +2422,7 @@ static void init_channel(struct peer *peer)
enum htlc_state *hstates;
struct fulfilled_htlc *fulfilled;
enum side *fulfilled_sides;
struct failed_htlc *failed;
struct failed_htlc **failed;
enum side *failed_sides;
struct added_htlc *htlcs;
bool reconnected;
Expand Down Expand Up @@ -2521,7 +2522,9 @@ static void init_channel(struct peer *peer)

if (!channel_force_htlcs(peer->channel, htlcs, hstates,
fulfilled, fulfilled_sides,
failed, failed_sides))
cast_const2(const struct failed_htlc **,
failed),
failed_sides))
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Could not restore HTLCs");

Expand Down
21 changes: 11 additions & 10 deletions channeld/full_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ bool channel_force_htlcs(struct channel *channel,
const enum htlc_state *hstates,
const struct fulfilled_htlc *fulfilled,
const enum side *fulfilled_sides,
const struct failed_htlc *failed,
const struct failed_htlc **failed,
const enum side *failed_sides)
{
size_t i;
Expand Down Expand Up @@ -1002,29 +1002,29 @@ bool channel_force_htlcs(struct channel *channel,
for (i = 0; i < tal_count(failed); i++) {
struct htlc *htlc;
htlc = channel_get_htlc(channel, failed_sides[i],
failed[i].id);
failed[i]->id);
if (!htlc) {
status_trace("Fail %s HTLC %"PRIu64" not found",
failed_sides[i] == LOCAL ? "out" : "in",
failed[i].id);
failed[i]->id);
return false;
}
if (htlc->r) {
status_trace("Fail %s HTLC %"PRIu64" already fulfilled",
failed_sides[i] == LOCAL ? "out" : "in",
failed[i].id);
failed[i]->id);
return false;
}
if (htlc->fail) {
status_trace("Fail %s HTLC %"PRIu64" already failed",
failed_sides[i] == LOCAL ? "out" : "in",
failed[i].id);
failed[i]->id);
return false;
}
if (htlc->malformed) {
status_trace("Fail %s HTLC %"PRIu64" already malformed",
failed_sides[i] == LOCAL ? "out" : "in",
failed[i].id);
failed[i]->id);
return false;
}
if (!htlc_has(htlc, HTLC_REMOVING)) {
Expand All @@ -1034,11 +1034,12 @@ bool channel_force_htlcs(struct channel *channel,
htlc_state_name(htlc->state));
return false;
}
if (failed[i].malformed)
htlc->malformed = failed[i].malformed;
if (failed[i]->malformed)
htlc->malformed = failed[i]->malformed;
else
htlc->fail = tal_dup_arr(htlc, u8, failed[i].failreason,
tal_len(failed[i].failreason),
htlc->fail = tal_dup_arr(htlc, u8,
failed[i]->failreason,
tal_len(failed[i]->failreason),
0);
}

Expand Down
2 changes: 1 addition & 1 deletion channeld/full_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ bool channel_force_htlcs(struct channel *channel,
const enum htlc_state *hstates,
const struct fulfilled_htlc *fulfilled,
const enum side *fulfilled_sides,
const struct failed_htlc *failed,
const struct failed_htlc **failed,
const enum side *failed_sides);

/**
Expand Down
21 changes: 0 additions & 21 deletions common/utxo.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,3 @@ struct utxo *fromwire_utxo(const tal_t *ctx, const u8 **ptr, size_t *max)
}
return utxo;
}


struct utxo *from_utxoptr_arr(const tal_t *ctx, const struct utxo **utxos)
{
size_t i, n = tal_count(utxos);
struct utxo *utxo = tal_arr(ctx, struct utxo, n);

for (i = 0; i < n; i++)
utxo[i] = *utxos[i];
return utxo;
}

const struct utxo **to_utxoptr_arr(const tal_t *ctx, const struct utxo *utxos)
{
size_t i, n = tal_count(utxos);
const struct utxo **utxo = tal_arr(ctx, const struct utxo *, n);

for (i = 0; i < n; i++)
utxo[i] = &utxos[i];
return utxo;
}
5 changes: 0 additions & 5 deletions common/utxo.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,4 @@ struct utxo {

void towire_utxo(u8 **pptr, const struct utxo *utxo);
struct utxo *fromwire_utxo(const tal_t *ctx, const u8 **ptr, size_t *max);

/* build_utxos/funding_tx use array of pointers, but marshall code
* wants arr of structs */
struct utxo *from_utxoptr_arr(const tal_t *ctx, const struct utxo **utxos);
const struct utxo **to_utxoptr_arr(const tal_t *ctx, const struct utxo *utxos);
#endif /* LIGHTNING_COMMON_UTXO_H */
28 changes: 16 additions & 12 deletions gossipd/gossip.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <ccan/build_assert/build_assert.h>
#include <ccan/cast/cast.h>
#include <ccan/container_of/container_of.h>
#include <ccan/crypto/hkdf_sha256/hkdf_sha256.h>
#include <ccan/endian/endian.h>
Expand Down Expand Up @@ -1118,21 +1119,24 @@ static struct io_plan *getchannels_req(struct io_conn *conn, struct daemon *daem
return daemon_conn_read_next(conn, &daemon->master);
}

static void append_node(struct gossip_getnodes_entry **nodes,
static void append_node(const struct gossip_getnodes_entry ***nodes,
const struct node *n)
{
struct gossip_getnodes_entry *new;
size_t num_nodes = tal_count(*nodes);
tal_resize(nodes, num_nodes + 1);
(*nodes)[num_nodes].nodeid = n->id;
(*nodes)[num_nodes].last_timestamp = n->last_timestamp;

new = tal(*nodes, struct gossip_getnodes_entry);
new->nodeid = n->id;
new->last_timestamp = n->last_timestamp;
if (n->last_timestamp < 0) {
(*nodes)[num_nodes].addresses = NULL;
return;
new->addresses = NULL;
} else {
new->addresses = n->addresses;
new->alias = n->alias;
memcpy(new->color, n->rgb_color, 3);
}

(*nodes)[num_nodes].addresses = n->addresses;
(*nodes)[num_nodes].alias = n->alias;
memcpy((*nodes)[num_nodes].color, n->rgb_color, 3);
tal_resize(nodes, num_nodes + 1);
(*nodes)[num_nodes] = new;
}

static struct io_plan *getnodes(struct io_conn *conn, struct daemon *daemon,
Expand All @@ -1141,12 +1145,12 @@ static struct io_plan *getnodes(struct io_conn *conn, struct daemon *daemon,
tal_t *tmpctx = tal_tmpctx(daemon);
u8 *out;
struct node *n;
struct gossip_getnodes_entry *nodes;
const struct gossip_getnodes_entry **nodes;
struct pubkey *ids;

fromwire_gossip_getnodes_request(tmpctx, msg, NULL, &ids);

nodes = tal_arr(tmpctx, struct gossip_getnodes_entry, 0);
nodes = tal_arr(tmpctx, const struct gossip_getnodes_entry *, 0);
if (ids) {
for (size_t i = 0; i < tal_count(ids); i++) {
n = node_map_get(daemon->rstate->nodes, &ids[i].pubkey);
Expand Down
29 changes: 13 additions & 16 deletions hsmd/hsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <bitcoin/pubkey.h>
#include <bitcoin/script.h>
#include <bitcoin/tx.h>
#include <ccan/cast/cast.h>
#include <ccan/container_of/container_of.h>
#include <ccan/crypto/hkdf_sha256/hkdf_sha256.h>
#include <ccan/endian/endian.h>
Expand Down Expand Up @@ -610,8 +611,7 @@ static void sign_funding_tx(struct daemon_conn *master, const u8 *msg)
u64 satoshi_out, change_out;
u32 change_keyindex;
struct pubkey local_pubkey, remote_pubkey;
struct utxo *inputs;
const struct utxo **utxomap;
struct utxo **utxomap;
struct bitcoin_tx *tx;
u8 *wscript;
u16 outnum;
Expand All @@ -623,21 +623,20 @@ static void sign_funding_tx(struct daemon_conn *master, const u8 *msg)
if (!fromwire_hsm_sign_funding(tmpctx, msg, NULL,
&satoshi_out, &change_out,
&change_keyindex, &local_pubkey,
&remote_pubkey, &inputs))
&remote_pubkey, &utxomap))
master_badmsg(WIRE_HSM_SIGN_FUNDING, msg);

utxomap = to_utxoptr_arr(tmpctx, inputs);

if (change_out)
bitcoin_pubkey(&changekey, change_keyindex);

tx = funding_tx(tmpctx, &outnum, utxomap,
tx = funding_tx(tmpctx, &outnum,
cast_const2(const struct utxo **, utxomap),
satoshi_out, &local_pubkey, &remote_pubkey,
change_out, &changekey,
NULL);

scriptSigs = tal_arr(tmpctx, u8*, tal_count(inputs));
for (i = 0; i < tal_count(inputs); i++) {
scriptSigs = tal_arr(tmpctx, u8*, tal_count(utxomap));
for (i = 0; i < tal_count(utxomap); i++) {
struct pubkey inkey;
struct privkey inprivkey;
const struct utxo *in = utxomap[i];
Expand All @@ -657,14 +656,14 @@ static void sign_funding_tx(struct daemon_conn *master, const u8 *msg)

tx->input[i].witness = bitcoin_witness_p2wpkh(tx, &sig, &inkey);

if (inputs[i].is_p2sh)
if (utxomap[i]->is_p2sh)
scriptSigs[i] = bitcoin_scriptsig_p2sh_p2wpkh(tx, &inkey);
else
scriptSigs[i] = NULL;
}

/* Now complete the transaction by attaching the scriptSigs where necessary */
for (size_t i=0; i<tal_count(inputs); i++)
for (size_t i=0; i<tal_count(utxomap); i++)
tx->input[i].script = scriptSigs[i];

daemon_conn_send(master,
Expand All @@ -680,8 +679,7 @@ static void sign_withdrawal_tx(struct daemon_conn *master, const u8 *msg)
const tal_t *tmpctx = tal_tmpctx(master);
u64 satoshi_out, change_out;
u32 change_keyindex;
struct utxo *inutxos;
const struct utxo **utxos;
struct utxo **utxos;
u8 *wscript;
u8 **scriptSigs;
struct bitcoin_tx *tx;
Expand All @@ -691,7 +689,7 @@ static void sign_withdrawal_tx(struct daemon_conn *master, const u8 *msg)

if (!fromwire_hsm_sign_withdrawal(tmpctx, msg, NULL, &satoshi_out,
&change_out, &change_keyindex,
&scriptpubkey, &inutxos)) {
&scriptpubkey, &utxos)) {
status_broken("Failed to parse sign_withdrawal: %s",
tal_hex(trc, msg));
return;
Expand All @@ -704,11 +702,10 @@ static void sign_withdrawal_tx(struct daemon_conn *master, const u8 *msg)
return;
}

/* We need an array of pointers, since withdraw_tx permutes them */
utxos = to_utxoptr_arr(tmpctx, inutxos);
pubkey_from_der(ext.pub_key, sizeof(ext.pub_key), &changekey);
tx = withdraw_tx(
tmpctx, utxos, scriptpubkey, satoshi_out,
tmpctx, cast_const2(const struct utxo **, utxos),
scriptpubkey, satoshi_out,
&changekey, change_out, NULL);

scriptSigs = tal_arr(tmpctx, u8*, tal_count(utxos));
Expand Down
18 changes: 9 additions & 9 deletions lightningd/gossip_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ static void json_getnodes_reply(struct subd *gossip, const u8 *reply,
const int *fds,
struct command *cmd)
{
struct gossip_getnodes_entry *nodes;
struct gossip_getnodes_entry **nodes;
struct json_result *response = new_json_result(cmd);
size_t i, j;

Expand All @@ -205,21 +205,21 @@ static void json_getnodes_reply(struct subd *gossip, const u8 *reply,

for (i = 0; i < tal_count(nodes); i++) {
json_object_start(response, NULL);
json_add_pubkey(response, "nodeid", &nodes[i].nodeid);
if (nodes[i].last_timestamp < 0) {
json_add_pubkey(response, "nodeid", &nodes[i]->nodeid);
if (nodes[i]->last_timestamp < 0) {
json_object_end(response);
continue;
}
json_add_string(response, "alias",
tal_strndup(response, (char *)nodes[i].alias,
tal_len(nodes[i].alias)));
tal_strndup(response, (char *)nodes[i]->alias,
tal_len(nodes[i]->alias)));
json_add_hex(response, "color",
nodes[i].color, ARRAY_SIZE(nodes[i].color));
nodes[i]->color, ARRAY_SIZE(nodes[i]->color));
json_add_u64(response, "last_timestamp",
nodes[i].last_timestamp);
nodes[i]->last_timestamp);
json_array_start(response, "addresses");
for (j=0; j<tal_count(nodes[i].addresses); j++) {
json_add_address(response, NULL, &nodes[i].addresses[j]);
for (j=0; j<tal_count(nodes[i]->addresses); j++) {
json_add_address(response, NULL, &nodes[i]->addresses[j]);
}
json_array_end(response);
json_object_end(response);
Expand Down
Loading

0 comments on commit 526d3a2

Please sign in to comment.