Skip to content

Commit 838872e

Browse files
devrandomksedgwic
authored andcommitted
introduce simple_htlc for communicating HTLCs to hsmd
1 parent 882c2d9 commit 838872e

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
@@ -1039,27 +1039,20 @@ static struct bitcoin_signature *calc_commitsigs(const tal_t *ctx,
10391039
// We use the existing_htlc to_wire routines, it's unfortunate that
10401040
// we have to send a dummy onion_routing_packet ...
10411041
//
1042-
struct existing_htlc **htlcs = tal_arr(tmpctx, struct existing_htlc *, 0);
1043-
u8 dummy_onion_routing_packet[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)];
1044-
memset(dummy_onion_routing_packet, 0, sizeof(dummy_onion_routing_packet));
1042+
struct simple_htlc **htlcs = tal_arr(tmpctx, struct simple_htlc *, 0);
10451043
size_t num_entries = tal_count(htlc_map);
10461044
for (size_t ndx = 0; ndx < num_entries; ++ndx) {
10471045
struct htlc const *hh = htlc_map[ndx];
10481046
if (hh) {
10491047
status_debug("HTLC[%lu]=%" PRIu64 ", %s",
10501048
ndx, hh->id, htlc_state_name(hh->state));
1051-
struct existing_htlc *existing =
1052-
new_existing_htlc(NULL,
1053-
hh->id,
1054-
hh->state,
1055-
hh->amount,
1056-
&hh->rhash,
1057-
hh->expiry.locktime,
1058-
dummy_onion_routing_packet,
1059-
NULL,
1060-
NULL,
1061-
NULL);
1062-
tal_arr_expand(&htlcs, tal_steal(htlcs, existing));
1049+
struct simple_htlc *simple =
1050+
new_simple_htlc(NULL,
1051+
htlc_state_owner(hh->state),
1052+
hh->amount,
1053+
&hh->rhash,
1054+
hh->expiry.locktime);
1055+
tal_arr_expand(&htlcs, tal_steal(htlcs, simple));
10631056
}
10641057
}
10651058

@@ -1069,7 +1062,7 @@ static struct bitcoin_signature *calc_commitsigs(const tal_t *ctx,
10691062
channel_has(peer->channel,
10701063
OPT_STATIC_REMOTEKEY),
10711064
commit_index,
1072-
(const struct existing_htlc **) htlcs,
1065+
(const struct simple_htlc **) htlcs,
10731066
channel_feerate(peer->channel, REMOTE));
10741067
msg = hsm_req(tmpctx, take(msg));
10751068
if (!fromwire_hsmd_sign_tx_reply(msg, commit_sig))
@@ -1755,38 +1748,31 @@ static void handle_peer_commit_sig(struct peer *peer, const u8 *msg)
17551748

17561749
// Collect the htlcs for call to hsmd validate.
17571750
//
1758-
// We use the existing_htlc to_wire routines, it's unfortunate that
1751+
// We use the simple_htlc to_wire routines, it's unfortunate that
17591752
// we have to send a dummy onion_routing_packet ...
17601753
//
1761-
struct existing_htlc **htlcs = tal_arr(NULL, struct existing_htlc *, 0);
1762-
u8 dummy_onion_routing_packet[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)];
1763-
memset(dummy_onion_routing_packet, 0, sizeof(dummy_onion_routing_packet));
1754+
struct simple_htlc **htlcs = tal_arr(NULL, struct simple_htlc *, 0);
17641755
size_t num_entries = tal_count(htlc_map);
17651756
for (size_t ndx = 0; ndx < num_entries; ++ndx) {
17661757
struct htlc const *hh = htlc_map[ndx];
17671758
if (hh) {
17681759
status_debug("HTLC[%lu]=%" PRIu64 ", %s",
17691760
ndx, hh->id, htlc_state_name(hh->state));
1770-
struct existing_htlc *existing =
1771-
new_existing_htlc(NULL,
1772-
hh->id,
1773-
hh->state,
1774-
hh->amount,
1775-
&hh->rhash,
1776-
hh->expiry.locktime,
1777-
dummy_onion_routing_packet,
1778-
NULL,
1779-
NULL,
1780-
NULL);
1781-
tal_arr_expand(&htlcs, tal_steal(htlcs, existing));
1761+
struct simple_htlc *simple =
1762+
new_simple_htlc(NULL,
1763+
htlc_state_owner(hh->state),
1764+
hh->amount,
1765+
&hh->rhash,
1766+
hh->expiry.locktime);
1767+
tal_arr_expand(&htlcs, tal_steal(htlcs, simple));
17821768
}
17831769
}
17841770

17851771
// Validate the counterparty's signatures, returns old_secret.
17861772
const u8 * msg2 =
17871773
towire_hsmd_validate_commitment_tx(NULL,
17881774
txs[0],
1789-
(const struct existing_htlc **) htlcs,
1775+
(const struct simple_htlc **) htlcs,
17901776
peer->next_index[LOCAL],
17911777
channel_feerate(peer->channel, LOCAL),
17921778
&commit_sig,

common/htlc_wire.c

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

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

125+
void towire_simple_htlc(u8 **pptr, const struct simple_htlc *simple)
126+
{
127+
towire_side(pptr, simple->side);
128+
towire_amount_msat(pptr, simple->amount);
129+
towire_sha256(pptr, &simple->payment_hash);
130+
towire_u32(pptr, simple->cltv_expiry);
131+
}
132+
111133
void towire_fulfilled_htlc(u8 **pptr, const struct fulfilled_htlc *fulfilled)
112134
{
113135
towire_u64(pptr, fulfilled->id);
@@ -205,6 +227,18 @@ struct existing_htlc *fromwire_existing_htlc(const tal_t *ctx,
205227
return existing;
206228
}
207229

230+
struct simple_htlc *fromwire_simple_htlc(const tal_t *ctx,
231+
const u8 **cursor, size_t *max)
232+
{
233+
struct simple_htlc *simple = tal(ctx, struct simple_htlc);
234+
235+
simple->side = fromwire_side(cursor, max);
236+
simple->amount = fromwire_amount_msat(cursor, max);
237+
fromwire_sha256(cursor, max, &simple->payment_hash);
238+
simple->cltv_expiry = fromwire_u32(cursor, max);
239+
return simple;
240+
}
241+
208242
void fromwire_fulfilled_htlc(const u8 **cursor, size_t *max,
209243
struct fulfilled_htlc *fulfilled)
210244
{

common/htlc_wire.h

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

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

85+
struct simple_htlc *new_simple_htlc(const tal_t *ctx,
86+
enum side side,
87+
struct amount_msat amount,
88+
const struct sha256 *payment_hash,
89+
u32 cltv_expiry);
90+
7791
void towire_added_htlc(u8 **pptr, const struct added_htlc *added);
7892
void towire_existing_htlc(u8 **pptr, const struct existing_htlc *existing);
93+
void towire_simple_htlc(u8 **pptr, const struct simple_htlc *simple);
7994
void towire_fulfilled_htlc(u8 **pptr, const struct fulfilled_htlc *fulfilled);
8095
void towire_failed_htlc(u8 **pptr, const struct failed_htlc *failed);
8196
void towire_changed_htlc(u8 **pptr, const struct changed_htlc *changed);
@@ -86,6 +101,8 @@ void fromwire_added_htlc(const u8 **cursor, size_t *max,
86101
struct added_htlc *added);
87102
struct existing_htlc *fromwire_existing_htlc(const tal_t *ctx,
88103
const u8 **cursor, size_t *max);
104+
struct simple_htlc *fromwire_simple_htlc(const tal_t *ctx,
105+
const u8 **cursor, size_t *max);
89106
void fromwire_fulfilled_htlc(const u8 **cursor, size_t *max,
90107
struct fulfilled_htlc *fulfilled);
91108
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
@@ -134,8 +134,8 @@ msgdata,hsmd_sign_commitment_tx,channel_dbid,u64,
134134
msgdata,hsmd_sign_commitment_tx,tx,bitcoin_tx,
135135
msgdata,hsmd_sign_commitment_tx,remote_funding_key,pubkey,
136136
msgdata,hsmd_sign_commitment_tx,commit_num,u64,
137-
msgdata,hsmd_sign_commitment_tx,num_existing_htlcs,u16,
138-
msgdata,hsmd_sign_commitment_tx,htlcs,existing_htlc,num_existing_htlcs
137+
msgdata,hsmd_sign_commitment_tx,num_htlcs,u16,
138+
msgdata,hsmd_sign_commitment_tx,htlcs,simple_htlc,num_htlcs
139139
msgdata,hsmd_sign_commitment_tx,feerate,u32,
140140

141141
msgtype,hsmd_sign_commitment_tx_reply,105
@@ -145,8 +145,8 @@ msgdata,hsmd_sign_commitment_tx_reply,sig,bitcoin_signature,
145145
#include <common/htlc_wire.h>
146146
msgtype,hsmd_validate_commitment_tx,35
147147
msgdata,hsmd_validate_commitment_tx,tx,bitcoin_tx,
148-
msgdata,hsmd_validate_commitment_tx,num_existing_htlcs,u16,
149-
msgdata,hsmd_validate_commitment_tx,htlcs,existing_htlc,num_existing_htlcs
148+
msgdata,hsmd_validate_commitment_tx,num_htlcs,u16,
149+
msgdata,hsmd_validate_commitment_tx,htlcs,simple_htlc,num_htlcs
150150
msgdata,hsmd_validate_commitment_tx,commit_num,u64,
151151
msgdata,hsmd_validate_commitment_tx,feerate,u32,
152152
msgdata,hsmd_validate_commitment_tx,sig,bitcoin_signature,
@@ -202,8 +202,8 @@ msgdata,hsmd_sign_remote_commitment_tx,remote_funding_key,pubkey,
202202
msgdata,hsmd_sign_remote_commitment_tx,remote_per_commit,pubkey,
203203
msgdata,hsmd_sign_remote_commitment_tx,option_static_remotekey,bool,
204204
msgdata,hsmd_sign_remote_commitment_tx,commit_num,u64,
205-
msgdata,hsmd_sign_remote_commitment_tx,num_existing_htlcs,u16,
206-
msgdata,hsmd_sign_remote_commitment_tx,htlcs,existing_htlc,num_existing_htlcs
205+
msgdata,hsmd_sign_remote_commitment_tx,num_htlcs,u16,
206+
msgdata,hsmd_sign_remote_commitment_tx,htlcs,simple_htlc,num_htlcs
207207
msgdata,hsmd_sign_remote_commitment_tx,feerate,u32,
208208

209209
# 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
@@ -1206,7 +1206,7 @@ static u8 *handle_sign_remote_commitment_tx(struct hsmd_client *c, const u8 *msg
12061206
struct pubkey remote_per_commit;
12071207
bool option_static_remotekey;
12081208
u64 commit_num;
1209-
struct existing_htlc **htlc;
1209+
struct simple_htlc **htlc;
12101210
u32 feerate;
12111211

12121212
if (!fromwire_hsmd_sign_remote_commitment_tx(tmpctx, msg_in,
@@ -1300,7 +1300,7 @@ static u8 *handle_sign_commitment_tx(struct hsmd_client *c, const u8 *msg_in)
13001300
struct secret channel_seed;
13011301
struct bitcoin_tx *tx;
13021302
struct bitcoin_signature sig;
1303-
struct existing_htlc **htlc;
1303+
struct simple_htlc **htlc;
13041304
u64 commit_num;
13051305
u32 feerate;
13061306
struct secrets secrets;
@@ -1347,7 +1347,7 @@ static u8 *handle_sign_commitment_tx(struct hsmd_client *c, const u8 *msg_in)
13471347
static u8 *handle_validate_commitment_tx(struct hsmd_client *c, const u8 *msg_in)
13481348
{
13491349
struct bitcoin_tx *tx;
1350-
struct existing_htlc **htlc;
1350+
struct simple_htlc **htlc;
13511351
u64 commit_num;
13521352
u32 feerate;
13531353
struct bitcoin_signature sig;

lightningd/peer_control.c

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

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

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

254246
u64 commit_index = channel->next_index[LOCAL] - 1;
255247
u32 local_feerate = get_feerate(channel->fee_states, channel->opener, LOCAL);
256-
struct existing_htlc **htlcs = collect_htlcs(channel, local_feerate);
248+
struct simple_htlc **htlcs = collect_htlcs(channel, local_feerate);
257249

258250
assert(!last_tx->wtx->inputs[0].witness);
259251
msg = towire_hsmd_sign_commitment_tx(tmpctx,
@@ -263,7 +255,7 @@ static void sign_last_tx(struct channel *channel,
263255
&channel->channel_info
264256
.remote_fundingkey,
265257
commit_index,
266-
(const struct existing_htlc **) htlcs,
258+
(const struct simple_htlc **) htlcs,
267259
local_feerate);
268260

269261
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
@@ -657,7 +657,7 @@ u8 *towire_errorfmt(const tal_t *ctx UNNEEDED,
657657
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)
658658
{ fprintf(stderr, "towire_hsmd_sign_bolt12 called!\n"); abort(); }
659659
/* Generated stub for towire_hsmd_sign_commitment_tx */
660-
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)
660+
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)
661661
{ fprintf(stderr, "towire_hsmd_sign_commitment_tx called!\n"); abort(); }
662662
/* Generated stub for towire_hsmd_sign_invoice */
663663
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
@@ -1918,7 +1918,7 @@ static u8 *accepter_commits(struct state *state,
19181918
}
19191919

19201920
/* Make HSM sign it */
1921-
struct existing_htlc **htlcs = tal_arr(tmpctx, struct existing_htlc *, 0);
1921+
struct simple_htlc **htlcs = tal_arr(tmpctx, struct simple_htlc *, 0);
19221922
u32 feerate = 0; // unused since there are no htlcs
19231923
u64 commit_num = 0;
19241924
msg = towire_hsmd_sign_remote_commitment_tx(NULL,
@@ -1927,7 +1927,7 @@ static u8 *accepter_commits(struct state *state,
19271927
&state->first_per_commitment_point[REMOTE],
19281928
true,
19291929
commit_num,
1930-
(const struct existing_htlc **) htlcs,
1930+
(const struct simple_htlc **) htlcs,
19311931
feerate);
19321932
wire_sync_write(HSM_FD, take(msg));
19331933
msg = wire_sync_read(tmpctx, HSM_FD);
@@ -2517,7 +2517,7 @@ static u8 *opener_commits(struct state *state,
25172517
* witness script. It also needs the amount of the funding output,
25182518
* as segwit signatures commit to that as well, even though it doesn't
25192519
* explicitly appear in the transaction itself. */
2520-
struct existing_htlc **htlcs = tal_arr(tmpctx, struct existing_htlc *, 0);
2520+
struct simple_htlc **htlcs = tal_arr(tmpctx, struct simple_htlc *, 0);
25212521
u32 feerate = 0; // unused since there are no htlcs
25222522
u64 commit_num = 0;
25232523
msg = towire_hsmd_sign_remote_commitment_tx(NULL,
@@ -2526,7 +2526,7 @@ static u8 *opener_commits(struct state *state,
25262526
&state->first_per_commitment_point[REMOTE],
25272527
true,
25282528
commit_num,
2529-
(const struct existing_htlc **) htlcs,
2529+
(const struct simple_htlc **) htlcs,
25302530
feerate);
25312531

25322532
wire_sync_write(HSM_FD, take(msg));

0 commit comments

Comments
 (0)