Skip to content

Commit eb61a11

Browse files
committed
closingd: add support for handling wrong_funding.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 62b3129 commit eb61a11

File tree

7 files changed

+135
-65
lines changed

7 files changed

+135
-65
lines changed

bitcoin/tx.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,19 @@ void bitcoin_tx_input_get_txid(const struct bitcoin_tx *tx, int innum,
399399
wally_tx_input_get_txid(&tx->wtx->inputs[innum], out);
400400
}
401401

402+
void bitcoin_tx_input_set_txid(struct bitcoin_tx *tx, int innum,
403+
const struct bitcoin_txid *txid,
404+
u32 index)
405+
{
406+
struct wally_tx_input *in;
407+
assert(innum < tx->wtx->num_inputs);
408+
409+
in = &tx->wtx->inputs[innum];
410+
BUILD_ASSERT(sizeof(struct bitcoin_txid) == sizeof(in->txhash));
411+
memcpy(in->txhash, txid, sizeof(struct bitcoin_txid));
412+
in->index = index;
413+
}
414+
402415
void wally_tx_input_get_txid(const struct wally_tx_input *in,
403416
struct bitcoin_txid *txid)
404417
{

bitcoin/tx.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,13 @@ void bitcoin_tx_input_get_txid(const struct bitcoin_tx *tx, int innum,
201201
void wally_tx_input_get_txid(const struct wally_tx_input *in,
202202
struct bitcoin_txid *txid);
203203

204+
/**
205+
* Overwrite the txhash and index in the wally_tx_input
206+
*/
207+
void bitcoin_tx_input_set_txid(struct bitcoin_tx *tx, int innum,
208+
const struct bitcoin_txid *txid,
209+
u32 index);
210+
204211
/**
205212
* Check a transaction for consistency.
206213
*

closingd/closingd.c

Lines changed: 71 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ static struct bitcoin_tx *close_tx(const tal_t *ctx,
4343
const struct amount_sat out[NUM_SIDES],
4444
enum side opener,
4545
struct amount_sat fee,
46-
struct amount_sat dust_limit)
46+
struct amount_sat dust_limit,
47+
const struct bitcoin_outpoint *wrong_funding)
4748
{
4849
struct bitcoin_tx *tx;
4950
struct amount_sat out_minus_fee[NUM_SIDES];
@@ -75,6 +76,7 @@ static struct bitcoin_tx *close_tx(const tal_t *ctx,
7576
out_minus_fee[LOCAL],
7677
out_minus_fee[REMOTE],
7778
dust_limit);
79+
assert(tx);
7880
if (!tx)
7981
peer_failed_err(pps, channel_id,
8082
"Both outputs below dust limit:"
@@ -88,6 +90,12 @@ static struct bitcoin_tx *close_tx(const tal_t *ctx,
8890
type_to_string(tmpctx, struct amount_sat, &dust_limit),
8991
type_to_string(tmpctx, struct amount_sat, &out[LOCAL]),
9092
type_to_string(tmpctx, struct amount_sat, &out[REMOTE]));
93+
94+
if (wrong_funding)
95+
bitcoin_tx_input_set_txid(tx, 0,
96+
&wrong_funding->txid,
97+
wrong_funding->n);
98+
9199
return tx;
92100
}
93101

@@ -148,13 +156,15 @@ static void do_reconnect(struct per_peer_state *pps,
148156
u64 revocations_received,
149157
const u8 *channel_reestablish,
150158
const u8 *final_scriptpubkey,
151-
const struct secret *last_remote_per_commit_secret)
159+
const struct secret *last_remote_per_commit_secret,
160+
const struct bitcoin_outpoint *wrong_funding)
152161
{
153162
u8 *msg;
154163
struct channel_id their_channel_id;
155164
u64 next_local_commitment_number, next_remote_revocation_number;
156165
struct pubkey my_current_per_commitment_point, next_commitment_point;
157166
struct secret their_secret;
167+
struct tlv_shutdown_tlvs *tlvs;
158168

159169
my_current_per_commitment_point = get_per_commitment_point(next_index[LOCAL]-1);
160170

@@ -218,8 +228,16 @@ static void do_reconnect(struct per_peer_state *pps,
218228
* - if it has sent a previous `shutdown`:
219229
* - MUST retransmit `shutdown`.
220230
*/
221-
/* FIXME: retransmit wrong_funding */
222-
msg = towire_shutdown(NULL, channel_id, final_scriptpubkey, NULL);
231+
if (wrong_funding) {
232+
tlvs = tlv_shutdown_tlvs_new(tmpctx);
233+
tlvs->wrong_funding
234+
= tal(tlvs, struct tlv_shutdown_tlvs_wrong_funding);
235+
tlvs->wrong_funding->txid = wrong_funding->txid;
236+
tlvs->wrong_funding->outnum = wrong_funding->n;
237+
} else
238+
tlvs = NULL;
239+
240+
msg = towire_shutdown(NULL, channel_id, final_scriptpubkey, tlvs);
223241
sync_crypto_write(pps, take(msg));
224242

225243
/* BOLT #2:
@@ -250,7 +268,8 @@ static void send_offer(struct per_peer_state *pps,
250268
const struct amount_sat out[NUM_SIDES],
251269
enum side opener,
252270
struct amount_sat our_dust_limit,
253-
struct amount_sat fee_to_offer)
271+
struct amount_sat fee_to_offer,
272+
const struct bitcoin_outpoint *wrong_funding)
254273
{
255274
struct bitcoin_tx *tx;
256275
struct bitcoin_signature our_sig;
@@ -269,7 +288,8 @@ static void send_offer(struct per_peer_state *pps,
269288
funding,
270289
funding_wscript,
271290
out,
272-
opener, fee_to_offer, our_dust_limit);
291+
opener, fee_to_offer, our_dust_limit,
292+
wrong_funding);
273293

274294
/* BOLT #3:
275295
*
@@ -329,6 +349,7 @@ receive_offer(struct per_peer_state *pps,
329349
enum side opener,
330350
struct amount_sat our_dust_limit,
331351
struct amount_sat min_fee_to_accept,
352+
const struct bitcoin_outpoint *wrong_funding,
332353
struct bitcoin_txid *closing_txid)
333354
{
334355
u8 *msg;
@@ -379,7 +400,8 @@ receive_offer(struct per_peer_state *pps,
379400
funding_txout,
380401
funding,
381402
funding_wscript,
382-
out, opener, received_fee, our_dust_limit);
403+
out, opener, received_fee, our_dust_limit,
404+
wrong_funding);
383405

384406
if (!check_tx_sig(tx, 0, NULL, funding_wscript,
385407
&funding_pubkey[REMOTE], &their_sig)) {
@@ -410,7 +432,8 @@ receive_offer(struct per_peer_state *pps,
410432
funding,
411433
funding_wscript,
412434
trimming_out,
413-
opener, received_fee, our_dust_limit);
435+
opener, received_fee, our_dust_limit,
436+
wrong_funding);
414437
if (!trimmed
415438
|| !check_tx_sig(trimmed, 0, NULL, funding_wscript,
416439
&funding_pubkey[REMOTE], &their_sig)) {
@@ -619,37 +642,39 @@ int main(int argc, char *argv[])
619642
enum side whose_turn;
620643
u8 *channel_reestablish;
621644
struct secret last_remote_per_commit_secret;
645+
struct bitcoin_outpoint *wrong_funding;
622646

623647
subdaemon_setup(argc, argv);
624648

625649
status_setup_sync(REQ_FD);
626650

627651
msg = wire_sync_read(tmpctx, REQ_FD);
628652
if (!fromwire_closingd_init(ctx, msg,
629-
&chainparams,
630-
&pps,
631-
&channel_id,
632-
&funding_txid, &funding_txout,
633-
&funding,
634-
&funding_pubkey[LOCAL],
635-
&funding_pubkey[REMOTE],
636-
&opener,
637-
&out[LOCAL],
638-
&out[REMOTE],
639-
&our_dust_limit,
640-
&min_fee_to_accept, &commitment_fee,
641-
&offer[LOCAL],
642-
&scriptpubkey[LOCAL],
643-
&scriptpubkey[REMOTE],
644-
&fee_negotiation_step,
645-
&fee_negotiation_step_unit,
646-
&reconnected,
647-
&next_index[LOCAL],
648-
&next_index[REMOTE],
649-
&revocations_received,
650-
&channel_reestablish,
651-
&last_remote_per_commit_secret,
652-
&dev_fast_gossip))
653+
&chainparams,
654+
&pps,
655+
&channel_id,
656+
&funding_txid, &funding_txout,
657+
&funding,
658+
&funding_pubkey[LOCAL],
659+
&funding_pubkey[REMOTE],
660+
&opener,
661+
&out[LOCAL],
662+
&out[REMOTE],
663+
&our_dust_limit,
664+
&min_fee_to_accept, &commitment_fee,
665+
&offer[LOCAL],
666+
&scriptpubkey[LOCAL],
667+
&scriptpubkey[REMOTE],
668+
&fee_negotiation_step,
669+
&fee_negotiation_step_unit,
670+
&reconnected,
671+
&next_index[LOCAL],
672+
&next_index[REMOTE],
673+
&revocations_received,
674+
&channel_reestablish,
675+
&last_remote_per_commit_secret,
676+
&dev_fast_gossip,
677+
&wrong_funding))
653678
master_badmsg(WIRE_CLOSINGD_INIT, msg);
654679

655680
/* stdin == requests, 3 == peer, 4 = gossip, 5 = gossip_store, 6 = hsmd */
@@ -670,6 +695,11 @@ int main(int argc, char *argv[])
670695
status_debug("fee = %s",
671696
type_to_string(tmpctx, struct amount_sat, &offer[LOCAL]));
672697
status_debug("fee negotiation step = %s", fee_negotiation_step_str);
698+
if (wrong_funding)
699+
status_unusual("Seting wrong_funding_txid to %s:%u",
700+
type_to_string(tmpctx, struct bitcoin_txid,
701+
&wrong_funding->txid),
702+
wrong_funding->n);
673703

674704
funding_wscript = bitcoin_redeem_2of2(ctx,
675705
&funding_pubkey[LOCAL],
@@ -679,7 +709,8 @@ int main(int argc, char *argv[])
679709
do_reconnect(pps, &channel_id,
680710
next_index, revocations_received,
681711
channel_reestablish, scriptpubkey[LOCAL],
682-
&last_remote_per_commit_secret);
712+
&last_remote_per_commit_secret,
713+
wrong_funding);
683714

684715
peer_billboard(
685716
true,
@@ -705,7 +736,8 @@ int main(int argc, char *argv[])
705736
scriptpubkey, &funding_txid, funding_txout,
706737
funding, out, opener,
707738
our_dust_limit,
708-
offer[LOCAL]);
739+
offer[LOCAL],
740+
wrong_funding);
709741
} else {
710742
if (i == 0)
711743
peer_billboard(false, "Waiting for their initial"
@@ -726,6 +758,7 @@ int main(int argc, char *argv[])
726758
out, opener,
727759
our_dust_limit,
728760
min_fee_to_accept,
761+
wrong_funding,
729762
&closing_txid);
730763
}
731764
}
@@ -754,7 +787,8 @@ int main(int argc, char *argv[])
754787
scriptpubkey, &funding_txid, funding_txout,
755788
funding, out, opener,
756789
our_dust_limit,
757-
offer[LOCAL]);
790+
offer[LOCAL],
791+
wrong_funding);
758792
} else {
759793
peer_billboard(false, "Waiting for another"
760794
" closing fee offer:"
@@ -770,6 +804,7 @@ int main(int argc, char *argv[])
770804
out, opener,
771805
our_dust_limit,
772806
min_fee_to_accept,
807+
wrong_funding,
773808
&closing_txid);
774809
}
775810

@@ -782,6 +817,7 @@ int main(int argc, char *argv[])
782817

783818
#if DEVELOPER
784819
/* We don't listen for master commands, so always check memleak here */
820+
tal_free(wrong_funding);
785821
closing_dev_memleak(ctx, scriptpubkey, funding_wscript);
786822
#endif
787823

closingd/closingd_wire.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ msgdata,closingd_init,channel_reestablish_len,u16,
3434
msgdata,closingd_init,channel_reestablish,u8,channel_reestablish_len
3535
msgdata,closingd_init,last_remote_secret,secret,
3636
msgdata,closingd_init,dev_fast_gossip,bool,
37+
msgdata,closingd_init,shutdown_wrong_funding,?bitcoin_outpoint,
3738

3839
# We received an offer, save signature.
3940
msgtype,closingd_received_signature,2002

closingd/closingd_wiregen.c

Lines changed: 15 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

closingd/closingd_wiregen.h

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)