From 674a2c7554436713f23bf66a880a77cbe8b83d0e Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 8 Feb 2018 11:55:12 +1030 Subject: [PATCH] tools/generate_wire.py: make bitcoin_tx a varlen structs Now it allocates naturally. Signed-off-by: Rusty Russell --- lightningd/peer_control.c | 20 ++++++++------------ lightningd/peer_htlcs.c | 4 ++-- onchaind/onchain.c | 7 +++---- tools/generate-wire.py | 1 + wallet/walletrpc.c | 4 +--- wire/fromwire.c | 6 ++++-- wire/wire.h | 3 ++- 7 files changed, 21 insertions(+), 24 deletions(-) diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index dd93c2fe5..1e7292453 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -1150,8 +1150,7 @@ static void handle_onchain_broadcast_tx(struct peer *peer, const u8 *msg) { struct bitcoin_tx *tx; - tx = tal(msg, struct bitcoin_tx); - if (!fromwire_onchain_broadcast_tx(msg, NULL, tx)) { + if (!fromwire_onchain_broadcast_tx(msg, msg, NULL, &tx)) { peer_internal_error(peer, "Invalid onchain_broadcast_tx"); return; } @@ -1581,12 +1580,12 @@ static void opening_got_hsm_funding_sig(struct funding_channel *fc, const struct crypto_state *cs, u64 gossip_index) { - struct bitcoin_tx *tx = tal(fc, struct bitcoin_tx); + struct bitcoin_tx *tx; u8 *linear; u64 change_satoshi; struct json_result *response = new_json_result(fc->cmd); - if (!fromwire_hsm_sign_funding_reply(resp, NULL, tx)) + if (!fromwire_hsm_sign_funding_reply(fc, resp, NULL, &tx)) fatal("HSM gave bad sign_funding_reply %s", tal_hex(fc, resp)); @@ -1788,9 +1787,9 @@ static bool better_closing_fee(struct peer *peer, const struct bitcoin_tx *tx) static void peer_received_closing_signature(struct peer *peer, const u8 *msg) { secp256k1_ecdsa_signature sig; - struct bitcoin_tx *tx = tal(msg, struct bitcoin_tx); + struct bitcoin_tx *tx; - if (!fromwire_closing_received_signature(msg, NULL, &sig, tx)) { + if (!fromwire_closing_received_signature(msg, msg, NULL, &sig, &tx)) { peer_internal_error(peer, "Bad closing_received_signature %s", tal_hex(peer, msg)); return; @@ -2220,14 +2219,13 @@ static void opening_funder_finished(struct subd *opening, const u8 *resp, /* At this point, we care about peer */ fc->peer->channel_info = channel_info = tal(fc->peer, struct channel_info); - remote_commit = tal(resp, struct bitcoin_tx); /* This is a new channel_info->their_config so set its ID to 0 */ fc->peer->channel_info->their_config.id = 0; - if (!fromwire_opening_funder_reply(resp, NULL, + if (!fromwire_opening_funder_reply(resp, resp, NULL, &channel_info->their_config, - remote_commit, + &remote_commit, &remote_commit_sig, &cs, &gossip_index, @@ -2340,8 +2338,6 @@ static void opening_fundee_finished(struct subd *opening, log_debug(peer->log, "Got opening_fundee_finish_response"); assert(tal_count(fds) == 2); - remote_commit = tal(reply, struct bitcoin_tx); - /* At this point, we care about peer */ peer->channel_info = channel_info = tal(peer, struct channel_info); /* This is a new channel_info->their_config, set its ID to 0 */ @@ -2350,7 +2346,7 @@ static void opening_fundee_finished(struct subd *opening, peer->funding_txid = tal(peer, struct bitcoin_txid); if (!fromwire_opening_fundee_reply(tmpctx, reply, NULL, &channel_info->their_config, - remote_commit, + &remote_commit, &remote_commit_sig, &cs, &gossip_index, diff --git a/lightningd/peer_htlcs.c b/lightningd/peer_htlcs.c index 3c6fc006f..d1634528c 100644 --- a/lightningd/peer_htlcs.c +++ b/lightningd/peer_htlcs.c @@ -1081,7 +1081,7 @@ void peer_got_commitsig(struct peer *peer, const u8 *msg) struct fulfilled_htlc *fulfilled; struct failed_htlc **failed; struct changed_htlc *changed; - struct bitcoin_tx *tx = tal(msg, struct bitcoin_tx); + struct bitcoin_tx *tx; size_t i; if (!fromwire_channel_got_commitsig(msg, msg, NULL, @@ -1094,7 +1094,7 @@ void peer_got_commitsig(struct peer *peer, const u8 *msg) &fulfilled, &failed, &changed, - tx)) { + &tx)) { peer_internal_error(peer, "bad fromwire_channel_got_commitsig %s", tal_hex(peer, msg)); diff --git a/onchaind/onchain.c b/onchaind/onchain.c index ca74a4276..ba35926e0 100644 --- a/onchaind/onchain.c +++ b/onchaind/onchain.c @@ -943,7 +943,7 @@ static void wait_for_resolved(struct tracked_output **outs) while (!all_irrevocably_resolved(outs)) { u8 *msg = wire_sync_read(outs, REQ_FD); struct bitcoin_txid txid; - struct bitcoin_tx *tx = tal(msg, struct bitcoin_tx); + struct bitcoin_tx *tx; u32 input_num, depth, tx_blockheight; struct preimage preimage; @@ -952,7 +952,7 @@ static void wait_for_resolved(struct tracked_output **outs) if (fromwire_onchain_depth(msg, NULL, &txid, &depth)) tx_new_depth(outs, &txid, depth); - else if (fromwire_onchain_spent(msg, NULL, tx, &input_num, + else if (fromwire_onchain_spent(msg, msg, NULL, &tx, &input_num, &tx_blockheight)) output_spent(&outs, tx, input_num, tx_blockheight); else if (fromwire_onchain_known_preimage(msg, NULL, &preimage)) @@ -1982,7 +1982,6 @@ int main(int argc, char *argv[]) missing_htlc_msgs = tal_arr(ctx, u8 *, 0); msg = wire_sync_read(ctx, REQ_FD); - tx = tal(ctx, struct bitcoin_tx); if (!fromwire_onchain_init(ctx, msg, NULL, &seed, &shachain, &funding_amount_satoshi, @@ -2001,7 +2000,7 @@ int main(int argc, char *argv[]) &remote_payment_basepoint, &remote_htlc_basepoint, &remote_delayed_payment_basepoint, - tx, + &tx, &tx_blockheight, &reasonable_depth, &remote_htlc_sigs, diff --git a/tools/generate-wire.py b/tools/generate-wire.py index 260ce4a8d..6a52669a5 100755 --- a/tools/generate-wire.py +++ b/tools/generate-wire.py @@ -32,6 +32,7 @@ varlen_structs = [ 'gossip_getnodes_entry', 'failed_htlc', 'utxo', + 'bitcoin_tx', ] class FieldType(object): diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index 70c9444c9..466e8c40d 100644 --- a/wallet/walletrpc.c +++ b/wallet/walletrpc.c @@ -289,9 +289,7 @@ static void json_withdraw(struct command *cmd, msg = hsm_sync_read(cmd, cmd->ld); - tx = tal(withdraw, struct bitcoin_tx); - - if (!fromwire_hsm_sign_withdrawal_reply(msg, NULL, tx)) + if (!fromwire_hsm_sign_withdrawal_reply(msg, msg, NULL, &tx)) fatal("HSM gave bad sign_withdrawal_reply %s", tal_hex(withdraw, msg)); diff --git a/wire/fromwire.c b/wire/fromwire.c index f16222d92..ddff51ed3 100644 --- a/wire/fromwire.c +++ b/wire/fromwire.c @@ -225,7 +225,9 @@ void derive_channel_id(struct channel_id *channel_id, channel_id->id[sizeof(*channel_id)-1] ^= txout; } -void fromwire_bitcoin_tx(const u8 **cursor, size_t *max, struct bitcoin_tx *tx) +/* FIXME: Simply rename pull_bitcoin_tx, remove pull_bitcoin_tx_onto */ +struct bitcoin_tx *fromwire_bitcoin_tx(const tal_t *ctx, + const u8 **cursor, size_t *max) { - pull_bitcoin_tx_onto(tx, cursor, max, tx); + return pull_bitcoin_tx(ctx, cursor, max); } diff --git a/wire/wire.h b/wire/wire.h index ca17ffd82..167f342ec 100644 --- a/wire/wire.h +++ b/wire/wire.h @@ -87,5 +87,6 @@ void fromwire_pad(const u8 **cursor, size_t *max, size_t num); void fromwire_u8_array(const u8 **cursor, size_t *max, u8 *arr, size_t num); -void fromwire_bitcoin_tx(const u8 **cursor, size_t *max, struct bitcoin_tx *tx); +struct bitcoin_tx *fromwire_bitcoin_tx(const tal_t *ctx, + const u8 **cursor, size_t *max); #endif /* LIGHTNING_WIRE_WIRE_H */