Skip to content

Commit 810f19a

Browse files
devrandomksedgwic
authored andcommitted
introduce simple_htlc for communicating HTLCs to hsmd
1 parent e3bbc83 commit 810f19a

File tree

11 files changed

+110
-80
lines changed

11 files changed

+110
-80
lines changed

channeld/channeld.c

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,27 +1034,20 @@ static struct bitcoin_signature *calc_commitsigs(const tal_t *ctx,
10341034
// We use the existing_htlc to_wire routines, it's unfortunate that
10351035
// we have to send a dummy onion_routing_packet ...
10361036
//
1037-
struct existing_htlc **htlcs = tal_arr(tmpctx, struct existing_htlc *, 0);
1038-
u8 dummy_onion_routing_packet[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)];
1039-
memset(dummy_onion_routing_packet, 0, sizeof(dummy_onion_routing_packet));
1037+
struct simple_htlc **htlcs = tal_arr(tmpctx, struct simple_htlc *, 0);
10401038
size_t num_entries = tal_count(htlc_map);
10411039
for (size_t ndx = 0; ndx < num_entries; ++ndx) {
10421040
struct htlc const *hh = htlc_map[ndx];
10431041
if (hh) {
10441042
status_debug("HTLC[%lu]=%" PRIu64 ", %s",
10451043
ndx, hh->id, htlc_state_name(hh->state));
1046-
struct existing_htlc *existing =
1047-
new_existing_htlc(NULL,
1048-
hh->id,
1049-
hh->state,
1050-
hh->amount,
1051-
&hh->rhash,
1052-
hh->expiry.locktime,
1053-
dummy_onion_routing_packet,
1054-
NULL,
1055-
NULL,
1056-
NULL);
1057-
tal_arr_expand(&htlcs, tal_steal(htlcs, existing));
1044+
struct simple_htlc *simple =
1045+
new_simple_htlc(NULL,
1046+
htlc_state_owner(hh->state),
1047+
hh->amount,
1048+
&hh->rhash,
1049+
hh->expiry.locktime);
1050+
tal_arr_expand(&htlcs, tal_steal(htlcs, simple));
10581051
}
10591052
}
10601053

@@ -1064,7 +1057,7 @@ static struct bitcoin_signature *calc_commitsigs(const tal_t *ctx,
10641057
channel_has(peer->channel,
10651058
OPT_STATIC_REMOTEKEY),
10661059
commit_index,
1067-
(const struct existing_htlc **) htlcs,
1060+
(const struct simple_htlc **) htlcs,
10681061
channel_feerate(peer->channel, REMOTE));
10691062
msg = hsm_req(tmpctx, take(msg));
10701063
if (!fromwire_hsmd_sign_tx_reply(msg, commit_sig))
@@ -1727,38 +1720,31 @@ static void handle_peer_commit_sig(struct peer *peer, const u8 *msg)
17271720

17281721
// Collect the htlcs for call to hsmd validate.
17291722
//
1730-
// We use the existing_htlc to_wire routines, it's unfortunate that
1723+
// We use the simple_htlc to_wire routines, it's unfortunate that
17311724
// we have to send a dummy onion_routing_packet ...
17321725
//
1733-
struct existing_htlc **htlcs = tal_arr(NULL, struct existing_htlc *, 0);
1734-
u8 dummy_onion_routing_packet[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)];
1735-
memset(dummy_onion_routing_packet, 0, sizeof(dummy_onion_routing_packet));
1726+
struct simple_htlc **htlcs = tal_arr(NULL, struct simple_htlc *, 0);
17361727
size_t num_entries = tal_count(htlc_map);
17371728
for (size_t ndx = 0; ndx < num_entries; ++ndx) {
17381729
struct htlc const *hh = htlc_map[ndx];
17391730
if (hh) {
17401731
status_debug("HTLC[%lu]=%" PRIu64 ", %s",
17411732
ndx, hh->id, htlc_state_name(hh->state));
1742-
struct existing_htlc *existing =
1743-
new_existing_htlc(NULL,
1744-
hh->id,
1745-
hh->state,
1746-
hh->amount,
1747-
&hh->rhash,
1748-
hh->expiry.locktime,
1749-
dummy_onion_routing_packet,
1750-
NULL,
1751-
NULL,
1752-
NULL);
1753-
tal_arr_expand(&htlcs, tal_steal(htlcs, existing));
1733+
struct simple_htlc *simple =
1734+
new_simple_htlc(NULL,
1735+
htlc_state_owner(hh->state),
1736+
hh->amount,
1737+
&hh->rhash,
1738+
hh->expiry.locktime);
1739+
tal_arr_expand(&htlcs, tal_steal(htlcs, simple));
17541740
}
17551741
}
17561742

17571743
// Validate the counterparty's signatures, returns old_secret.
17581744
const u8 * msg2 =
17591745
towire_hsmd_validate_commitment_tx(NULL,
17601746
txs[0],
1761-
(const struct existing_htlc **) htlcs,
1747+
(const struct simple_htlc **) htlcs,
17621748
peer->next_index[LOCAL],
17631749
channel_feerate(peer->channel, LOCAL),
17641750
&commit_sig,

common/htlc_wire.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ struct failed_htlc *failed_htlc_dup(const tal_t *ctx,
2525
return newf;
2626
}
2727

28+
struct simple_htlc *new_simple_htlc(const tal_t *ctx,
29+
enum side side,
30+
struct amount_msat amount,
31+
const struct sha256 *payment_hash,
32+
u32 cltv_expiry)
33+
{
34+
struct simple_htlc *simple = tal(NULL, struct simple_htlc);
35+
simple->side = side;
36+
simple->amount = amount;
37+
simple->payment_hash = *payment_hash;
38+
simple->cltv_expiry = cltv_expiry;
39+
return simple;
40+
}
41+
2842
struct existing_htlc *new_existing_htlc(const tal_t *ctx,
2943
u64 id,
3044
enum htlc_state state,
@@ -106,6 +120,14 @@ void towire_existing_htlc(u8 **pptr, const struct existing_htlc *existing)
106120
towire_bool(pptr, false);
107121
}
108122

123+
void towire_simple_htlc(u8 **pptr, const struct simple_htlc *simple)
124+
{
125+
towire_side(pptr, simple->side);
126+
towire_amount_msat(pptr, simple->amount);
127+
towire_sha256(pptr, &simple->payment_hash);
128+
towire_u32(pptr, simple->cltv_expiry);
129+
}
130+
109131
void towire_fulfilled_htlc(u8 **pptr, const struct fulfilled_htlc *fulfilled)
110132
{
111133
towire_u64(pptr, fulfilled->id);
@@ -202,6 +224,18 @@ struct existing_htlc *fromwire_existing_htlc(const tal_t *ctx,
202224
return existing;
203225
}
204226

227+
struct simple_htlc *fromwire_simple_htlc(const tal_t *ctx,
228+
const u8 **cursor, size_t *max)
229+
{
230+
struct simple_htlc *simple = tal(ctx, struct simple_htlc);
231+
232+
simple->side = fromwire_side(cursor, max);
233+
simple->amount = fromwire_amount_msat(cursor, max);
234+
fromwire_sha256(cursor, max, &simple->payment_hash);
235+
simple->cltv_expiry = fromwire_u32(cursor, max);
236+
return simple;
237+
}
238+
205239
void fromwire_fulfilled_htlc(const u8 **cursor, size_t *max,
206240
struct fulfilled_htlc *fulfilled)
207241
{

common/htlc_wire.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ struct changed_htlc {
6262
u64 id;
6363
};
6464

65+
/* For signing */
66+
struct simple_htlc {
67+
enum side side;
68+
struct amount_msat amount;
69+
struct sha256 payment_hash;
70+
u32 cltv_expiry;
71+
};
72+
6573
struct existing_htlc *new_existing_htlc(const tal_t *ctx,
6674
u64 id,
6775
enum htlc_state state,
@@ -73,10 +81,17 @@ struct existing_htlc *new_existing_htlc(const tal_t *ctx,
7381
const struct preimage *preimage TAKES,
7482
const struct failed_htlc *failed TAKES);
7583

84+
struct simple_htlc *new_simple_htlc(const tal_t *ctx,
85+
enum side side,
86+
struct amount_msat amount,
87+
const struct sha256 *payment_hash,
88+
u32 cltv_expiry);
89+
7690
struct failed_htlc *failed_htlc_dup(const tal_t *ctx, const struct failed_htlc *f TAKES);
7791

7892
void towire_added_htlc(u8 **pptr, const struct added_htlc *added);
7993
void towire_existing_htlc(u8 **pptr, const struct existing_htlc *existing);
94+
void towire_simple_htlc(u8 **pptr, const struct simple_htlc *simple);
8095
void towire_fulfilled_htlc(u8 **pptr, const struct fulfilled_htlc *fulfilled);
8196
void towire_failed_htlc(u8 **pptr, const struct failed_htlc *failed);
8297
void towire_changed_htlc(u8 **pptr, const struct changed_htlc *changed);
@@ -88,6 +103,8 @@ void fromwire_added_htlc(const u8 **cursor, size_t *max,
88103
struct added_htlc *added);
89104
struct existing_htlc *fromwire_existing_htlc(const tal_t *ctx,
90105
const u8 **cursor, size_t *max);
106+
struct simple_htlc *fromwire_simple_htlc(const tal_t *ctx,
107+
const u8 **cursor, size_t *max);
91108
void fromwire_fulfilled_htlc(const u8 **cursor, size_t *max,
92109
struct fulfilled_htlc *fulfilled);
93110
struct failed_htlc *fromwire_failed_htlc(const tal_t *ctx, const u8 **cursor,

hsmd/hsmd_wire.csv

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ msgdata,hsmd_sign_commitment_tx,channel_dbid,u64,
136136
msgdata,hsmd_sign_commitment_tx,tx,bitcoin_tx,
137137
msgdata,hsmd_sign_commitment_tx,remote_funding_key,pubkey,
138138
msgdata,hsmd_sign_commitment_tx,commit_num,u64,
139-
msgdata,hsmd_sign_commitment_tx,num_existing_htlcs,u16,
140-
msgdata,hsmd_sign_commitment_tx,htlcs,existing_htlc,num_existing_htlcs
139+
msgdata,hsmd_sign_commitment_tx,num_htlcs,u16,
140+
msgdata,hsmd_sign_commitment_tx,htlcs,simple_htlc,num_htlcs
141141
msgdata,hsmd_sign_commitment_tx,feerate,u32,
142142

143143
msgtype,hsmd_sign_commitment_tx_reply,105
@@ -147,8 +147,8 @@ msgdata,hsmd_sign_commitment_tx_reply,sig,bitcoin_signature,
147147
#include <common/htlc_wire.h>
148148
msgtype,hsmd_validate_commitment_tx,35
149149
msgdata,hsmd_validate_commitment_tx,tx,bitcoin_tx,
150-
msgdata,hsmd_validate_commitment_tx,num_existing_htlcs,u16,
151-
msgdata,hsmd_validate_commitment_tx,htlcs,existing_htlc,num_existing_htlcs
150+
msgdata,hsmd_validate_commitment_tx,num_htlcs,u16,
151+
msgdata,hsmd_validate_commitment_tx,htlcs,simple_htlc,num_htlcs
152152
msgdata,hsmd_validate_commitment_tx,commit_num,u64,
153153
msgdata,hsmd_validate_commitment_tx,feerate,u32,
154154
msgdata,hsmd_validate_commitment_tx,sig,bitcoin_signature,
@@ -204,8 +204,8 @@ msgdata,hsmd_sign_remote_commitment_tx,remote_funding_key,pubkey,
204204
msgdata,hsmd_sign_remote_commitment_tx,remote_per_commit,pubkey,
205205
msgdata,hsmd_sign_remote_commitment_tx,option_static_remotekey,bool,
206206
msgdata,hsmd_sign_remote_commitment_tx,commit_num,u64,
207-
msgdata,hsmd_sign_remote_commitment_tx,num_existing_htlcs,u16,
208-
msgdata,hsmd_sign_remote_commitment_tx,htlcs,existing_htlc,num_existing_htlcs
207+
msgdata,hsmd_sign_remote_commitment_tx,num_htlcs,u16,
208+
msgdata,hsmd_sign_remote_commitment_tx,htlcs,simple_htlc,num_htlcs
209209
msgdata,hsmd_sign_remote_commitment_tx,feerate,u32,
210210

211211
# channeld asks HSM to sign remote HTLC tx.

hsmd/libhsmd.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,7 +1201,7 @@ static u8 *handle_sign_remote_commitment_tx(struct hsmd_client *c, const u8 *msg
12011201
struct pubkey remote_per_commit;
12021202
bool option_static_remotekey;
12031203
u64 commit_num;
1204-
struct existing_htlc **htlc;
1204+
struct simple_htlc **htlc;
12051205
u32 feerate;
12061206

12071207
if (!fromwire_hsmd_sign_remote_commitment_tx(tmpctx, msg_in,
@@ -1295,7 +1295,7 @@ static u8 *handle_sign_commitment_tx(struct hsmd_client *c, const u8 *msg_in)
12951295
struct secret channel_seed;
12961296
struct bitcoin_tx *tx;
12971297
struct bitcoin_signature sig;
1298-
struct existing_htlc **htlc;
1298+
struct simple_htlc **htlc;
12991299
u64 commit_num;
13001300
u32 feerate;
13011301
struct secrets secrets;
@@ -1342,7 +1342,7 @@ static u8 *handle_sign_commitment_tx(struct hsmd_client *c, const u8 *msg_in)
13421342
static u8 *handle_validate_commitment_tx(struct hsmd_client *c, const u8 *msg_in)
13431343
{
13441344
struct bitcoin_tx *tx;
1345-
struct existing_htlc **htlc;
1345+
struct simple_htlc **htlc;
13461346
u64 commit_num;
13471347
u32 feerate;
13481348
struct bitcoin_signature sig;

lightningd/peer_control.c

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,15 @@ u8 *p2wpkh_for_keyidx(const tal_t *ctx, struct lightningd *ld, u64 keyidx)
175175
return scriptpubkey_p2wpkh(ctx, &shutdownkey);
176176
}
177177

178-
static struct existing_htlc **collect_htlcs(struct channel *channel, u32 local_feerate) {
178+
static struct simple_htlc **collect_htlcs(struct channel *channel, u32 local_feerate) {
179179
// Collect the htlcs for call to hsmd.
180180
//
181181
// We use the existing_htlc to_wire routines, it's unfortunate that
182182
// we have to send a dummy onion_routing_packet ...
183183
//
184184
struct htlc_in_map *htlcs_in = &channel->peer->ld->htlcs_in;
185185
struct htlc_out_map *htlcs_out = &channel->peer->ld->htlcs_out;
186-
struct existing_htlc **htlcs = tal_arr(tmpctx, struct existing_htlc *, 0);
186+
struct simple_htlc **htlcs = tal_arr(tmpctx, struct simple_htlc *, 0);
187187
u8 dummy_onion_routing_packet[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)];
188188
memset(dummy_onion_routing_packet, 0, sizeof(dummy_onion_routing_packet));
189189

@@ -200,18 +200,14 @@ static struct existing_htlc **collect_htlcs(struct channel *channel, u32 local_f
200200
channel->our_config.dust_limit, LOCAL,
201201
channel_has(channel, OPT_ANCHOR_OUTPUTS)))
202202
continue;
203-
struct existing_htlc *existing =
204-
new_existing_htlc(NULL,
205-
hin->dbid,
206-
hin->hstate,
207-
hin->msat,
208-
&hin->payment_hash,
209-
hin->cltv_expiry,
210-
dummy_onion_routing_packet,
211-
NULL,
212-
NULL,
213-
NULL);
214-
tal_arr_expand(&htlcs, tal_steal(htlcs, existing));
203+
struct simple_htlc *simple =
204+
new_simple_htlc(NULL,
205+
htlc_state_owner(hin->hstate),
206+
hin->msat,
207+
&hin->payment_hash,
208+
hin->cltv_expiry
209+
);
210+
tal_arr_expand(&htlcs, tal_steal(htlcs, simple));
215211
}
216212
const struct htlc_out *hout;
217213
struct htlc_out_map_iter outi;
@@ -226,18 +222,14 @@ static struct existing_htlc **collect_htlcs(struct channel *channel, u32 local_f
226222
channel->our_config.dust_limit, LOCAL,
227223
channel_has(channel, OPT_ANCHOR_OUTPUTS)))
228224
continue;
229-
struct existing_htlc *existing =
230-
new_existing_htlc(NULL,
231-
hout->dbid,
232-
hout->hstate,
233-
hout->msat,
234-
&hout->payment_hash,
235-
hout->cltv_expiry,
236-
dummy_onion_routing_packet,
237-
NULL,
238-
NULL,
239-
NULL);
240-
tal_arr_expand(&htlcs, tal_steal(htlcs, existing));
225+
struct simple_htlc *simple =
226+
new_simple_htlc(NULL,
227+
htlc_state_owner(hout->hstate),
228+
hout->msat,
229+
&hout->payment_hash,
230+
hout->cltv_expiry
231+
);
232+
tal_arr_expand(&htlcs, tal_steal(htlcs, simple));
241233
}
242234
return htlcs;
243235
}
@@ -252,7 +244,7 @@ static void sign_last_tx(struct channel *channel,
252244

253245
u64 commit_index = channel->next_index[LOCAL] - 1;
254246
u32 local_feerate = get_feerate(channel->fee_states, channel->opener, LOCAL);
255-
struct existing_htlc **htlcs = collect_htlcs(channel, local_feerate);
247+
struct simple_htlc **htlcs = collect_htlcs(channel, local_feerate);
256248

257249
assert(!last_tx->wtx->inputs[0].witness);
258250
msg = towire_hsmd_sign_commitment_tx(tmpctx,
@@ -262,7 +254,7 @@ static void sign_last_tx(struct channel *channel,
262254
&channel->channel_info
263255
.remote_fundingkey,
264256
commit_index,
265-
(const struct existing_htlc **) htlcs,
257+
(const struct simple_htlc **) htlcs,
266258
local_feerate);
267259

268260
if (!wire_sync_write(ld->hsm_fd, take(msg)))

lightningd/test/run-invoice-select-inchan.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ u8 *towire_errorfmt(const tal_t *ctx UNNEEDED,
656656
u8 *towire_hsmd_sign_bolt12(const tal_t *ctx UNNEEDED, const wirestring *messagename UNNEEDED, const wirestring *fieldname UNNEEDED, const struct sha256 *merkleroot UNNEEDED, const u8 *publictweak UNNEEDED)
657657
{ fprintf(stderr, "towire_hsmd_sign_bolt12 called!\n"); abort(); }
658658
/* Generated stub for towire_hsmd_sign_commitment_tx */
659-
u8 *towire_hsmd_sign_commitment_tx(const tal_t *ctx UNNEEDED, const struct node_id *peer_id UNNEEDED, u64 channel_dbid UNNEEDED, const struct bitcoin_tx *tx UNNEEDED, const struct pubkey *remote_funding_key UNNEEDED, u64 commit_num UNNEEDED, const struct existing_htlc **htlcs UNNEEDED, u32 feerate UNNEEDED)
659+
u8 *towire_hsmd_sign_commitment_tx(const tal_t *ctx UNNEEDED, const struct node_id *peer_id UNNEEDED, u64 channel_dbid UNNEEDED, const struct bitcoin_tx *tx UNNEEDED, const struct pubkey *remote_funding_key UNNEEDED, u64 commit_num UNNEEDED, const struct simple_htlc **htlcs UNNEEDED, u32 feerate UNNEEDED)
660660
{ fprintf(stderr, "towire_hsmd_sign_commitment_tx called!\n"); abort(); }
661661
/* Generated stub for towire_hsmd_sign_invoice */
662662
u8 *towire_hsmd_sign_invoice(const tal_t *ctx UNNEEDED, const u8 *u5bytes UNNEEDED, const u8 *hrp UNNEEDED)

openingd/dualopend.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,7 +1889,7 @@ static u8 *accepter_commits(struct state *state,
18891889
}
18901890

18911891
/* Make HSM sign it */
1892-
struct existing_htlc **htlcs = tal_arr(tmpctx, struct existing_htlc *, 0);
1892+
struct simple_htlc **htlcs = tal_arr(tmpctx, struct simple_htlc *, 0);
18931893
u32 feerate = 0; // unused since there are no htlcs
18941894
u64 commit_num = 0;
18951895
msg = towire_hsmd_sign_remote_commitment_tx(NULL,
@@ -1898,7 +1898,7 @@ static u8 *accepter_commits(struct state *state,
18981898
&state->first_per_commitment_point[REMOTE],
18991899
true,
19001900
commit_num,
1901-
(const struct existing_htlc **) htlcs,
1901+
(const struct simple_htlc **) htlcs,
19021902
feerate);
19031903
wire_sync_write(HSM_FD, take(msg));
19041904
msg = wire_sync_read(tmpctx, HSM_FD);
@@ -2455,7 +2455,7 @@ static u8 *opener_commits(struct state *state,
24552455
* witness script. It also needs the amount of the funding output,
24562456
* as segwit signatures commit to that as well, even though it doesn't
24572457
* explicitly appear in the transaction itself. */
2458-
struct existing_htlc **htlcs = tal_arr(tmpctx, struct existing_htlc *, 0);
2458+
struct simple_htlc **htlcs = tal_arr(tmpctx, struct simple_htlc *, 0);
24592459
u32 feerate = 0; // unused since there are no htlcs
24602460
u64 commit_num = 0;
24612461
msg = towire_hsmd_sign_remote_commitment_tx(NULL,
@@ -2464,7 +2464,7 @@ static u8 *opener_commits(struct state *state,
24642464
&state->first_per_commitment_point[REMOTE],
24652465
true,
24662466
commit_num,
2467-
(const struct existing_htlc **) htlcs,
2467+
(const struct simple_htlc **) htlcs,
24682468
feerate);
24692469

24702470
wire_sync_write(HSM_FD, take(msg));

0 commit comments

Comments
 (0)