Skip to content

Commit 3e53c6e

Browse files
committedApr 7, 2023
onchaind: have lightningd create our penalty txs.
Signed-off-by: Rusty Russell <[email protected]>
1 parent 36dd70e commit 3e53c6e

7 files changed

+181
-86
lines changed
 

‎lightningd/onchain_control.c

+62
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,10 @@ struct onchain_signing_info {
546546
struct {
547547
u64 commit_num;
548548
} htlc_timedout;
549+
/* WIRE_ONCHAIND_SPEND_PENALTY */
550+
struct {
551+
struct secret remote_per_commitment_secret;
552+
} spend_penalty;
549553
} u;
550554
};
551555

@@ -578,6 +582,19 @@ static u8 *sign_tx_to_us(const tal_t *ctx,
578582
info->channel->dbid);
579583
}
580584

585+
static u8 *sign_penalty(const tal_t *ctx,
586+
const struct bitcoin_tx *tx,
587+
const struct onchain_signing_info *info)
588+
{
589+
assert(info->msgtype == WIRE_ONCHAIND_SPEND_PENALTY);
590+
return towire_hsmd_sign_any_penalty_to_us(ctx,
591+
&info->u.spend_penalty.remote_per_commitment_secret,
592+
tx, info->wscript,
593+
0,
594+
&info->channel->peer->id,
595+
info->channel->dbid);
596+
}
597+
581598
/* Matches bitcoin_witness_sig_and_element! */
582599
static const struct onchain_witness_element **
583600
onchain_witness_sig_and_element(const tal_t *ctx, u8 **witness)
@@ -791,6 +808,47 @@ static void handle_onchaind_spend_to_us(struct channel *channel,
791808
__func__);
792809
}
793810

811+
static void handle_onchaind_spend_penalty(struct channel *channel,
812+
const u8 *msg)
813+
{
814+
struct lightningd *ld = channel->peer->ld;
815+
struct onchain_signing_info *info;
816+
struct bitcoin_outpoint out;
817+
struct amount_sat out_sats;
818+
u32 initial_feerate;
819+
u8 *stack_elem;
820+
821+
info = new_signing_info(msg, channel, WIRE_ONCHAIND_SPEND_PENALTY);
822+
/* We can always spend penalty txs immediately */
823+
info->minblock = 0;
824+
if (!fromwire_onchaind_spend_penalty(info, msg,
825+
&out, &out_sats,
826+
&info->u.spend_penalty.remote_per_commitment_secret,
827+
&stack_elem,
828+
&info->wscript)) {
829+
channel_internal_error(channel, "Invalid onchaind_spend_penalty %s",
830+
tal_hex(tmpctx, msg));
831+
return;
832+
}
833+
/* info->stack_elem is const void * */
834+
info->stack_elem = stack_elem;
835+
836+
/* FIXME: Be more sophisticated! */
837+
initial_feerate = penalty_feerate(ld->topology);
838+
if (!initial_feerate)
839+
initial_feerate = tx_feerate(channel->last_tx);
840+
841+
/* FIXME: deadline for HTLCs is actually a bit longer, but for
842+
* their output it's channel->our_config.to_self_delay after
843+
* the commitment tx is mined. */
844+
info->deadline_block = *channel->close_blockheight
845+
+ channel->our_config.to_self_delay;
846+
create_onchain_tx(channel, &out, out_sats,
847+
0, 0,
848+
initial_feerate, sign_penalty, info,
849+
__func__);
850+
}
851+
794852
static unsigned int onchain_msg(struct subd *sd, const u8 *msg, const int *fds UNUSED)
795853
{
796854
enum onchaind_wire t = fromwire_peektype(msg);
@@ -844,6 +902,10 @@ static unsigned int onchain_msg(struct subd *sd, const u8 *msg, const int *fds U
844902
handle_onchaind_spend_to_us(sd->channel, msg);
845903
break;
846904

905+
case WIRE_ONCHAIND_SPEND_PENALTY:
906+
handle_onchaind_spend_penalty(sd->channel, msg);
907+
break;
908+
847909
/* We send these, not receive them */
848910
case WIRE_ONCHAIND_INIT:
849911
case WIRE_ONCHAIND_SPENT:

‎onchaind/onchaind.c

+42-32
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "config.h"
22
#include <bitcoin/feerate.h>
33
#include <bitcoin/script.h>
4+
#include <ccan/array_size/array_size.h>
45
#include <ccan/asort/asort.h>
56
#include <ccan/cast/cast.h>
67
#include <ccan/mem/mem.h>
@@ -624,14 +625,6 @@ static u8 *remote_htlc_to_us(const tal_t *ctx,
624625
option_anchor_outputs);
625626
}
626627

627-
static u8 *penalty_to_us(const tal_t *ctx,
628-
struct bitcoin_tx *tx,
629-
const u8 *wscript)
630-
{
631-
return towire_hsmd_sign_penalty_to_us(ctx, remote_per_commitment_secret,
632-
tx, wscript);
633-
}
634-
635628
/*
636629
* This covers:
637630
* 1. to-us output spend (`<local_delayedsig> 0`)
@@ -927,6 +920,17 @@ static void propose_resolution_to_master(struct tracked_output *out,
927920
queue_until_msg(tmpctx, WIRE_ONCHAIND_SPEND_CREATED));
928921
}
929922

923+
/* Create and broadcast this tx now */
924+
static void propose_immediate_resolution(struct tracked_output *out,
925+
const u8 *send_message TAKES,
926+
enum tx_type tx_type)
927+
{
928+
/* We add 1 to blockheight (meaning you can broadcast it now) to avoid
929+
* having to check for < 0 in various places we print messages */
930+
propose_resolution_to_master(out, send_message, out->tx_blockheight+1,
931+
tx_type);
932+
}
933+
930934
static bool is_valid_sig(const u8 *e)
931935
{
932936
struct bitcoin_signature sig;
@@ -1417,11 +1421,10 @@ static void steal_htlc_tx(struct tracked_output *out,
14171421
enum tx_type htlc_tx_type,
14181422
const struct bitcoin_outpoint *htlc_outpoint)
14191423
{
1420-
struct bitcoin_tx *tx;
1421-
enum tx_type tx_type = OUR_PENALTY_TX;
14221424
struct tracked_output *htlc_out;
14231425
struct amount_asset asset;
14241426
struct amount_sat htlc_out_amt;
1427+
const u8 *msg;
14251428

14261429
u8 *wscript = bitcoin_wscript_htlc_tx(htlc_tx, to_self_delay[REMOTE],
14271430
&keyset->self_revocation_key,
@@ -1437,22 +1440,23 @@ static void steal_htlc_tx(struct tracked_output *out,
14371440
htlc_out_amt,
14381441
DELAYED_CHEAT_OUTPUT_TO_THEM,
14391442
&out->htlc, wscript, NULL);
1443+
1444+
/* mark commitment tx htlc output as 'resolved by them' */
1445+
resolved_by_other(out, &htlc_tx->txid, htlc_tx_type);
1446+
14401447
/* BOLT #3:
14411448
*
14421449
* To spend this via penalty, the remote node uses a witness stack
14431450
* `<revocationsig> 1`
14441451
*/
1445-
tx = tx_to_us(htlc_out, penalty_to_us, htlc_out,
1446-
BITCOIN_TX_RBF_SEQUENCE, 0,
1447-
&ONE, sizeof(ONE),
1448-
htlc_out->wscript,
1449-
&tx_type, penalty_feerate);
1450-
1451-
/* mark commitment tx htlc output as 'resolved by them' */
1452-
resolved_by_other(out, &htlc_tx->txid, htlc_tx_type);
1452+
msg = towire_onchaind_spend_penalty(NULL,
1453+
htlc_outpoint, htlc_out_amt,
1454+
remote_per_commitment_secret,
1455+
tal_dup(tmpctx, u8, &ONE),
1456+
htlc_out->wscript);
14531457

1454-
/* annnd done! */
1455-
propose_resolution(htlc_out, tx, 0, tx_type);
1458+
/* Spend this immediately. */
1459+
propose_immediate_resolution(htlc_out, take(msg), OUR_PENALTY_TX);
14561460
}
14571461

14581462
static void onchain_annotate_txout(const struct bitcoin_outpoint *outpoint,
@@ -1964,6 +1968,7 @@ static void wait_for_resolved(struct tracked_output **outs)
19641968
case WIRE_ONCHAIND_ANNOTATE_TXIN:
19651969
case WIRE_ONCHAIND_NOTIFY_COIN_MVT:
19661970
case WIRE_ONCHAIND_SPEND_TO_US:
1971+
case WIRE_ONCHAIND_SPEND_PENALTY:
19671972
break;
19681973
}
19691974
master_badmsg(-1, msg);
@@ -2753,9 +2758,7 @@ static void handle_our_unilateral(const struct tx_parts *tx,
27532758
* delay */
27542759
static void steal_to_them_output(struct tracked_output *out, u32 csv)
27552760
{
2756-
u8 *wscript;
2757-
struct bitcoin_tx *tx;
2758-
enum tx_type tx_type = OUR_PENALTY_TX;
2761+
const u8 *wscript, *msg;
27592762

27602763
/* BOLT #3:
27612764
*
@@ -2768,16 +2771,19 @@ static void steal_to_them_output(struct tracked_output *out, u32 csv)
27682771
&keyset->self_revocation_key,
27692772
&keyset->self_delayed_payment_key);
27702773

2771-
tx = tx_to_us(tmpctx, penalty_to_us, out, BITCOIN_TX_RBF_SEQUENCE, 0,
2772-
&ONE, sizeof(ONE), wscript, &tx_type, penalty_feerate);
2774+
msg = towire_onchaind_spend_penalty(NULL,
2775+
&out->outpoint, out->sat,
2776+
remote_per_commitment_secret,
2777+
tal_dup(tmpctx, u8, &ONE),
2778+
wscript);
27732779

2774-
propose_resolution(out, tx, 0, tx_type);
2780+
/* Spend this immediately. */
2781+
propose_immediate_resolution(out, take(msg), OUR_PENALTY_TX);
27752782
}
27762783

27772784
static void steal_htlc(struct tracked_output *out)
27782785
{
2779-
struct bitcoin_tx *tx;
2780-
enum tx_type tx_type = OUR_PENALTY_TX;
2786+
const u8 *msg;
27812787
u8 der[PUBKEY_CMPR_LEN];
27822788

27832789
/* BOLT #3:
@@ -2788,11 +2794,15 @@ static void steal_htlc(struct tracked_output *out)
27882794
* <revocation_sig> <revocationpubkey>
27892795
*/
27902796
pubkey_to_der(der, &keyset->self_revocation_key);
2791-
tx = tx_to_us(out, penalty_to_us, out, BITCOIN_TX_RBF_SEQUENCE, 0,
2792-
der, sizeof(der), out->wscript, &tx_type,
2793-
penalty_feerate);
27942797

2795-
propose_resolution(out, tx, 0, tx_type);
2798+
msg = towire_onchaind_spend_penalty(NULL,
2799+
&out->outpoint, out->sat,
2800+
remote_per_commitment_secret,
2801+
tal_dup_arr(tmpctx, u8, der, ARRAY_SIZE(der), 0),
2802+
out->wscript);
2803+
2804+
/* Spend this immediately. */
2805+
propose_immediate_resolution(out, take(msg), OUR_PENALTY_TX);
27962806
}
27972807

27982808
/* Tell wallet that we have discovered a UTXO from a to-remote output,

‎onchaind/onchaind_wire.csv

+10
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,16 @@ msgdata,onchaind_spend_to_us,commit_num,u64,
149149
msgdata,onchaind_spend_to_us,wscript_len,u32,
150150
msgdata,onchaind_spend_to_us,wscript,u8,wscript_len
151151

152+
# We tell lightningd to create, sign and broadcast this penalty tx:
153+
msgtype,onchaind_spend_penalty,5041
154+
msgdata,onchaind_spend_penalty,outpoint,bitcoin_outpoint,
155+
msgdata,onchaind_spend_penalty,outpoint_amount,amount_sat,
156+
msgdata,onchaind_spend_penalty,remote_per_commitment_secret,secret,
157+
msgdata,onchaind_spend_penalty,stack_elem_len,u16,
158+
msgdata,onchaind_spend_penalty,stack_elem,u8,stack_elem_len
159+
msgdata,onchaind_spend_penalty,wscript_len,u32,
160+
msgdata,onchaind_spend_penalty,wscript,u8,wscript_len
161+
152162
subtype,onchain_witness_element
153163
subtypedata,onchain_witness_element,is_signature,bool,
154164
subtypedata,onchain_witness_element,len,u32,

‎onchaind/test/run-grind_feerate-bug.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,6 @@ void towire_bool(u8 **pptr UNNEEDED, bool v UNNEEDED)
243243
/* Generated stub for towire_hsmd_get_per_commitment_point */
244244
u8 *towire_hsmd_get_per_commitment_point(const tal_t *ctx UNNEEDED, u64 n UNNEEDED)
245245
{ fprintf(stderr, "towire_hsmd_get_per_commitment_point called!\n"); abort(); }
246-
/* Generated stub for towire_hsmd_sign_delayed_payment_to_us */
247-
u8 *towire_hsmd_sign_delayed_payment_to_us(const tal_t *ctx UNNEEDED, u64 commit_num UNNEEDED, const struct bitcoin_tx *tx UNNEEDED, const u8 *wscript UNNEEDED)
248-
{ fprintf(stderr, "towire_hsmd_sign_delayed_payment_to_us called!\n"); abort(); }
249246
/* Generated stub for towire_hsmd_sign_penalty_to_us */
250247
u8 *towire_hsmd_sign_penalty_to_us(const tal_t *ctx UNNEEDED, const struct secret *revocation_secret UNNEEDED, const struct bitcoin_tx *tx UNNEEDED, const u8 *wscript UNNEEDED)
251248
{ fprintf(stderr, "towire_hsmd_sign_penalty_to_us called!\n"); abort(); }
@@ -285,6 +282,9 @@ u8 *towire_onchaind_missing_htlc_output(const tal_t *ctx UNNEEDED, const struct
285282
/* Generated stub for towire_onchaind_notify_coin_mvt */
286283
u8 *towire_onchaind_notify_coin_mvt(const tal_t *ctx UNNEEDED, const struct chain_coin_mvt *mvt UNNEEDED)
287284
{ fprintf(stderr, "towire_onchaind_notify_coin_mvt called!\n"); abort(); }
285+
/* Generated stub for towire_onchaind_spend_penalty */
286+
u8 *towire_onchaind_spend_penalty(const tal_t *ctx UNNEEDED, const struct bitcoin_outpoint *outpoint UNNEEDED, struct amount_sat outpoint_amount UNNEEDED, const struct secret *remote_per_commitment_secret UNNEEDED, const u8 *stack_elem UNNEEDED, const u8 *wscript UNNEEDED)
287+
{ fprintf(stderr, "towire_onchaind_spend_penalty called!\n"); abort(); }
288288
/* Generated stub for towire_onchaind_spend_to_us */
289289
u8 *towire_onchaind_spend_to_us(const tal_t *ctx UNNEEDED, const struct bitcoin_outpoint *outpoint UNNEEDED, struct amount_sat outpoint_amount UNNEEDED, u32 minblock UNNEEDED, u64 commit_num UNNEEDED, const u8 *wscript UNNEEDED)
290290
{ fprintf(stderr, "towire_onchaind_spend_to_us called!\n"); abort(); }

‎onchaind/test/run-grind_feerate.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,6 @@ void towire_bool(u8 **pptr UNNEEDED, bool v UNNEEDED)
272272
/* Generated stub for towire_hsmd_get_per_commitment_point */
273273
u8 *towire_hsmd_get_per_commitment_point(const tal_t *ctx UNNEEDED, u64 n UNNEEDED)
274274
{ fprintf(stderr, "towire_hsmd_get_per_commitment_point called!\n"); abort(); }
275-
/* Generated stub for towire_hsmd_sign_delayed_payment_to_us */
276-
u8 *towire_hsmd_sign_delayed_payment_to_us(const tal_t *ctx UNNEEDED, u64 commit_num UNNEEDED, const struct bitcoin_tx *tx UNNEEDED, const u8 *wscript UNNEEDED)
277-
{ fprintf(stderr, "towire_hsmd_sign_delayed_payment_to_us called!\n"); abort(); }
278275
/* Generated stub for towire_hsmd_sign_local_htlc_tx */
279276
u8 *towire_hsmd_sign_local_htlc_tx(const tal_t *ctx UNNEEDED, u64 commit_num UNNEEDED, const struct bitcoin_tx *tx UNNEEDED, const u8 *wscript UNNEEDED, bool option_anchor_outputs UNNEEDED)
280277
{ fprintf(stderr, "towire_hsmd_sign_local_htlc_tx called!\n"); abort(); }
@@ -317,6 +314,9 @@ u8 *towire_onchaind_missing_htlc_output(const tal_t *ctx UNNEEDED, const struct
317314
/* Generated stub for towire_onchaind_notify_coin_mvt */
318315
u8 *towire_onchaind_notify_coin_mvt(const tal_t *ctx UNNEEDED, const struct chain_coin_mvt *mvt UNNEEDED)
319316
{ fprintf(stderr, "towire_onchaind_notify_coin_mvt called!\n"); abort(); }
317+
/* Generated stub for towire_onchaind_spend_penalty */
318+
u8 *towire_onchaind_spend_penalty(const tal_t *ctx UNNEEDED, const struct bitcoin_outpoint *outpoint UNNEEDED, struct amount_sat outpoint_amount UNNEEDED, const struct secret *remote_per_commitment_secret UNNEEDED, const u8 *stack_elem UNNEEDED, const u8 *wscript UNNEEDED)
319+
{ fprintf(stderr, "towire_onchaind_spend_penalty called!\n"); abort(); }
320320
/* Generated stub for towire_onchaind_spend_to_us */
321321
u8 *towire_onchaind_spend_to_us(const tal_t *ctx UNNEEDED, const struct bitcoin_outpoint *outpoint UNNEEDED, struct amount_sat outpoint_amount UNNEEDED, u32 minblock UNNEEDED, u64 commit_num UNNEEDED, const u8 *wscript UNNEEDED)
322322
{ fprintf(stderr, "towire_onchaind_spend_to_us called!\n"); abort(); }

0 commit comments

Comments
 (0)
Please sign in to comment.