From 7bfd282319f0dad66c15d71552d5932e70bff6a5 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 23 May 2017 20:36:17 +0930 Subject: [PATCH] lightningd/utxo: helpers to translate from utxo * <-> utxo ** We need the former for marshalling, the latter for build_utxos and funding_tx. Signed-off-by: Rusty Russell --- lightningd/hsm/hsm.c | 6 ++---- lightningd/peer_control.c | 8 ++------ lightningd/utxo.c | 21 +++++++++++++++++++++ lightningd/utxo.h | 5 +++++ 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/lightningd/hsm/hsm.c b/lightningd/hsm/hsm.c index 490dd149c..b521c753d 100644 --- a/lightningd/hsm/hsm.c +++ b/lightningd/hsm/hsm.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -474,10 +475,7 @@ static void sign_funding_tx(struct daemon_conn *master, const u8 *msg) &remote_pubkey, &inputs)) status_failed(WIRE_HSMSTATUS_BAD_REQUEST, "Bad SIGN_FUNDING"); - /* FIXME: unmarshall gives array, not array of pointers. */ - utxomap = tal_arr(tmpctx, const struct utxo *, tal_count(inputs)); - for (i = 0; i < tal_count(inputs); i++) - utxomap[i] = &inputs[i]; + utxomap = to_utxoptr_arr(tmpctx, inputs); if (change_out) bitcoin_pubkey(&changekey, change_keyindex); diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 3d96b105b..4bda8cd8e 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -1377,14 +1377,12 @@ static bool opening_release_tx(struct subd *opening, const u8 *resp, struct funding_channel *fc) { u8 *msg; - size_t i; struct channel_config their_config; secp256k1_ecdsa_signature commit_sig; struct pubkey their_per_commit_point; struct basepoints theirbase; struct config *cfg = &fc->peer->ld->dstate.config; - /* FIXME: marshal code wants array, not array of pointers. */ - struct utxo *utxos = tal_arr(fc, struct utxo, tal_count(fc->utxomap)); + struct utxo *utxos; assert(tal_count(fds) == 1); fc->peer->fd = fds[0]; @@ -1407,9 +1405,7 @@ static bool opening_release_tx(struct subd *opening, const u8 *resp, log_debug(fc->peer->log, "Getting HSM to sign funding tx"); /* Get HSM to sign the funding tx. */ - for (i = 0; i < tal_count(fc->utxomap); i++) - utxos[i] = *fc->utxomap[i]; - + utxos = from_utxoptr_arr(fc, fc->utxomap); msg = towire_hsmctl_sign_funding(fc, fc->satoshi, fc->change, fc->change_keyindex, &fc->local_fundingkey, diff --git a/lightningd/utxo.c b/lightningd/utxo.c index b8c688f46..e66d80f19 100644 --- a/lightningd/utxo.c +++ b/lightningd/utxo.c @@ -18,3 +18,24 @@ void fromwire_utxo(const u8 **ptr, size_t *max, struct utxo *utxo) utxo->keyindex = fromwire_u32(ptr, max); utxo->is_p2sh = fromwire_bool(ptr, max); } + + +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; +} diff --git a/lightningd/utxo.h b/lightningd/utxo.h index 0b4ef0c61..fb76879b3 100644 --- a/lightningd/utxo.h +++ b/lightningd/utxo.h @@ -16,4 +16,9 @@ struct utxo { void towire_utxo(u8 **pptr, const struct utxo *utxo); void fromwire_utxo(const u8 **ptr, size_t *max, struct utxo *utxo); + +/* 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_LIGHTNINGD_UTXO_H */