Skip to content

Commit 4085982

Browse files
committed
Extend hsmd API to pass HTLCs in hsmd_sign_commitment_tx and hsmd_sign_remote_commitment_tx.
1 parent 7d3bf1d commit 4085982

File tree

12 files changed

+177
-75
lines changed

12 files changed

+177
-75
lines changed

channeld/channeld.c

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,23 +1061,42 @@ static struct bitcoin_signature *calc_commitsigs(const tal_t *ctx,
10611061
const u8 *msg;
10621062
struct bitcoin_signature *htlc_sigs;
10631063

1064+
// Collect the htlcs for call to hsmd.
1065+
//
1066+
// We use the existing_htlc to_wire routines, it's unfortunate that
1067+
// we have to send a dummy onion_routing_packet ...
1068+
//
1069+
struct existing_htlc **htlcs = tal_arr(tmpctx, struct existing_htlc *, 0);
1070+
u8 dummy_onion_routing_packet[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)];
1071+
memset(dummy_onion_routing_packet, 0, sizeof(dummy_onion_routing_packet));
10641072
size_t num_entries = tal_count(htlc_map);
1065-
struct sha256 *rhashes = tal_arrz(tmpctx, struct sha256, num_entries);
1066-
size_t nrhash = 0;
10671073
for (size_t ndx = 0; ndx < num_entries; ++ndx) {
1068-
if (htlc_map[ndx]) {
1069-
memcpy(&rhashes[nrhash], &htlc_map[ndx]->rhash, sizeof(rhashes[nrhash]));
1070-
++nrhash;
1074+
struct htlc const *hh = htlc_map[ndx];
1075+
if (hh) {
1076+
status_debug("HTLC[%lu]=%" PRIu64 ", %s",
1077+
ndx, hh->id, htlc_state_name(hh->state));
1078+
struct existing_htlc *existing =
1079+
new_existing_htlc(NULL,
1080+
hh->id,
1081+
hh->state,
1082+
hh->amount,
1083+
&hh->rhash,
1084+
hh->expiry.locktime,
1085+
dummy_onion_routing_packet,
1086+
NULL,
1087+
NULL,
1088+
NULL);
1089+
tal_arr_expand(&htlcs, tal_steal(htlcs, existing));
10711090
}
10721091
}
1073-
tal_resize(&rhashes, nrhash);
10741092

10751093
msg = towire_hsmd_sign_remote_commitment_tx(NULL, txs[0],
10761094
&peer->channel->funding_pubkey[REMOTE],
10771095
&peer->remote_per_commit,
1078-
peer->channel->option_static_remotekey,
1079-
rhashes, commit_index);
1080-
1096+
peer->channel->option_static_remotekey,
1097+
commit_index,
1098+
(const struct existing_htlc **) htlcs,
1099+
channel_feerate(peer->channel, REMOTE));
10811100
msg = hsm_req(tmpctx, take(msg));
10821101
if (!fromwire_hsmd_sign_tx_reply(msg, commit_sig))
10831102
status_failed(STATUS_FAIL_HSM_IO,

contrib/remote_hsmd/hsmd.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -751,15 +751,16 @@ static struct io_plan *handle_sign_commitment_tx(struct io_conn *conn,
751751
struct node_id peer_id;
752752
u64 dbid;
753753
struct bitcoin_tx *tx;
754-
struct bitcoin_signature sig;
755-
struct sha256 *rhashes;
754+
struct existing_htlc **htlc;
756755
u64 commit_num;
756+
u32 feerate;
757+
struct bitcoin_signature sig;
757758

758759
if (!fromwire_hsmd_sign_commitment_tx(tmpctx, msg_in,
759760
&peer_id, &dbid,
760761
&tx,
761762
&remote_funding_pubkey,
762-
&rhashes, &commit_num))
763+
&commit_num, &htlc, &feerate))
763764
return bad_req(conn, c, msg_in);
764765

765766
tx->chainparams = c->chainparams;
@@ -793,7 +794,7 @@ static struct io_plan *handle_sign_commitment_tx(struct io_conn *conn,
793794
// This is a unilateral close from our side.
794795
proxy_stat rv = proxy_handle_sign_commitment_tx(
795796
tx, &remote_funding_pubkey, &peer_id, dbid,
796-
rhashes, commit_num, &sig);
797+
htlc, commit_num, feerate, &sig);
797798
if (PROXY_PERMANENT(rv))
798799
status_failed(STATUS_FAIL_INTERNAL_ERROR,
799800
"proxy_%s failed: %s", __FUNCTION__,
@@ -893,15 +894,17 @@ static struct io_plan *handle_sign_remote_commitment_tx(struct io_conn *conn,
893894
struct bitcoin_signature sig;
894895
struct pubkey remote_per_commit;
895896
bool option_static_remotekey;
896-
struct sha256 *rhashes;
897897
u64 commit_num;
898+
struct existing_htlc **htlc;
899+
u32 feerate;
898900

899901
if (!fromwire_hsmd_sign_remote_commitment_tx(tmpctx, msg_in,
900902
&tx,
901903
&remote_funding_pubkey,
902904
&remote_per_commit,
903905
&option_static_remotekey,
904-
&rhashes, &commit_num))
906+
&commit_num,
907+
&htlc, &feerate))
905908
bad_req(conn, c, msg_in);
906909
tx->chainparams = c->chainparams;
907910

@@ -915,7 +918,7 @@ static struct io_plan *handle_sign_remote_commitment_tx(struct io_conn *conn,
915918
tx, &remote_funding_pubkey,
916919
&c->id, c->dbid,
917920
&remote_per_commit,
918-
rhashes, commit_num,
921+
htlc, commit_num, feerate,
919922
&sig);
920923
if (PROXY_PERMANENT(rv))
921924
status_failed(STATUS_FAIL_INTERNAL_ERROR,

contrib/remote_hsmd/proxy.cc

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -746,24 +746,27 @@ proxy_stat proxy_handle_sign_remote_commitment_tx(
746746
struct node_id *peer_id,
747747
u64 dbid,
748748
const struct pubkey *remote_per_commit,
749-
struct sha256 *rhashes, u64 commit_num,
749+
struct existing_htlc **htlcs,
750+
u64 commit_num, u32 feerate,
750751
struct bitcoin_signature *o_sig)
751752
{
752753
STATUS_DEBUG(
753754
"%s:%d %s { "
754755
"\"self_id\":%s, \"peer_id\":%s, \"dbid\":%" PRIu64 ", "
755756
"\"counterparty_funding_pubkey\":%s, "
756757
"\"remote_per_commit\":%s, \"tx\":%s, "
757-
"\"rhashes\":%s, \"commit_num\":%" PRIu64 " }",
758+
"\"htlcs\":%s, "
759+
"\"commit_num\":%" PRIu64 ", "
760+
"\"feerate\":%d }",
758761
__FILE__, __LINE__, __FUNCTION__,
759762
dump_node_id(&self_id).c_str(),
760763
dump_node_id(peer_id).c_str(),
761764
dbid,
762765
dump_pubkey(counterparty_funding_pubkey).c_str(),
763766
dump_pubkey(remote_per_commit).c_str(),
764767
dump_tx(tx).c_str(),
765-
dump_rhashes(rhashes, tal_count(rhashes)).c_str(),
766-
commit_num
768+
dump_htlcs((const struct existing_htlc **) htlcs, tal_count(htlcs)).c_str(),
769+
commit_num, feerate
767770
);
768771

769772
last_message = "";
@@ -773,8 +776,15 @@ proxy_stat proxy_handle_sign_remote_commitment_tx(
773776
marshal_pubkey(remote_per_commit,
774777
req.mutable_remote_per_commit_point());
775778
marshal_single_input_tx(tx, NULL, req.mutable_tx());
776-
marshal_rhashes(rhashes, req.mutable_payment_hashes());
779+
for (size_t ii = 0; ii < tal_count(htlcs); ++ii) {
780+
if (htlc_state_owner(htlcs[ii]->state) == REMOTE) {
781+
marshal_htlc(htlcs[ii], req.add_offered_htlcs());
782+
} else {
783+
marshal_htlc(htlcs[ii], req.add_received_htlcs());
784+
}
785+
}
777786
req.set_commit_num(commit_num);
787+
req.set_feerate_sat_per_kw(feerate);
778788

779789
ClientContext context;
780790
SignatureReply rsp;
@@ -1073,31 +1083,41 @@ proxy_stat proxy_handle_sign_commitment_tx(
10731083
const struct pubkey *counterparty_funding_pubkey,
10741084
struct node_id *peer_id,
10751085
u64 dbid,
1076-
struct sha256 *rhashes, u64 commit_num,
1086+
struct existing_htlc **htlcs,
1087+
u64 commit_num, u32 feerate,
10771088
struct bitcoin_signature *o_sig)
10781089
{
10791090
STATUS_DEBUG(
10801091
"%s:%d %s { "
10811092
"\"self_id\":%s, \"peer_id\":%s, \"dbid\":%" PRIu64 ", "
10821093
"\"counterparty_funding_pubkey\":%s, \"tx\":%s, "
1083-
"\"rhashes\":%s, \"commit_num\":%" PRIu64 " }",
1094+
"\"htlcs\":%s, "
1095+
"\"commit_num\":%" PRIu64 ", "
1096+
"\"feerate\":%d }",
10841097
__FILE__, __LINE__, __FUNCTION__,
10851098
dump_node_id(&self_id).c_str(),
10861099
dump_node_id(peer_id).c_str(),
10871100
dbid,
10881101
dump_pubkey(counterparty_funding_pubkey).c_str(),
10891102
dump_tx(tx).c_str(),
1090-
dump_rhashes(rhashes, tal_count(rhashes)).c_str(),
1091-
commit_num
1103+
dump_htlcs((const struct existing_htlc **) htlcs, tal_count(htlcs)).c_str(),
1104+
commit_num, feerate
10921105
);
10931106

10941107
last_message = "";
10951108
SignHolderCommitmentTxRequest req;
10961109
marshal_node_id(&self_id, req.mutable_node_id());
10971110
marshal_channel_nonce(peer_id, dbid, req.mutable_channel_nonce());
10981111
marshal_single_input_tx(tx, NULL, req.mutable_tx());
1099-
marshal_rhashes(rhashes, req.mutable_payment_hashes());
1112+
for (size_t ii = 0; ii < tal_count(htlcs); ++ii) {
1113+
if (htlc_state_owner(htlcs[ii]->state) == LOCAL) {
1114+
marshal_htlc(htlcs[ii], req.add_offered_htlcs());
1115+
} else {
1116+
marshal_htlc(htlcs[ii], req.add_received_htlcs());
1117+
}
1118+
}
11001119
req.set_commit_num(commit_num);
1120+
req.set_feerate_sat_per_kw(feerate);
11011121

11021122
ClientContext context;
11031123
SignatureReply rsp;

contrib/remote_hsmd/proxy.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ proxy_stat proxy_handle_sign_remote_commitment_tx(
9090
struct node_id *peer_id,
9191
u64 dbid,
9292
const struct pubkey *remote_per_commit,
93-
struct sha256 *rhashes,
93+
struct existing_htlc **htlc,
9494
u64 commit_num,
95+
u32 feerate,
9596
struct bitcoin_signature *o_sig);
9697

9798
proxy_stat proxy_handle_get_per_commitment_point(
@@ -128,8 +129,9 @@ proxy_stat proxy_handle_sign_commitment_tx(
128129
const struct pubkey *remote_funding_pubkey,
129130
struct node_id *peer_id,
130131
u64 dbid,
131-
struct sha256 *rhashes,
132+
struct existing_htlc **htlc,
132133
u64 commit_num,
134+
u32 feerate,
133135
struct bitcoin_signature *o_sig);
134136

135137
proxy_stat proxy_handle_validate_commitment_tx(

hsmd/hsmd_wire.csv

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,10 @@ msgdata,hsmd_sign_commitment_tx,peer_id,node_id,
133133
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,
136-
msgdata,hsmd_sign_commitment_tx,num_htlc_rhash,u16,
137-
msgdata,hsmd_sign_commitment_tx,htlc_rhash,sha256,num_htlc_rhash
138136
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
139+
msgdata,hsmd_sign_commitment_tx,feerate,u32,
139140

140141
msgtype,hsmd_sign_commitment_tx_reply,105
141142
msgdata,hsmd_sign_commitment_tx_reply,sig,bitcoin_signature,
@@ -200,9 +201,10 @@ msgdata,hsmd_sign_remote_commitment_tx,tx,bitcoin_tx,
200201
msgdata,hsmd_sign_remote_commitment_tx,remote_funding_key,pubkey,
201202
msgdata,hsmd_sign_remote_commitment_tx,remote_per_commit,pubkey,
202203
msgdata,hsmd_sign_remote_commitment_tx,option_static_remotekey,bool,
203-
msgdata,hsmd_sign_remote_commitment_tx,num_htlc_rhash,u16,
204-
msgdata,hsmd_sign_remote_commitment_tx,htlc_rhash,sha256,num_htlc_rhash
205204
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
207+
msgdata,hsmd_sign_remote_commitment_tx,feerate,u32,
206208

207209
# channeld asks HSM to sign remote HTLC tx.
208210
msgtype,hsmd_sign_remote_htlc_tx,20

hsmd/libhsmd.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,16 +1290,17 @@ static u8 *handle_sign_commitment_tx(struct hsmd_client *c, const u8 *msg_in)
12901290
struct secret channel_seed;
12911291
struct bitcoin_tx *tx;
12921292
struct bitcoin_signature sig;
1293-
struct sha256 *rhashes;
1293+
struct existing_htlc **htlc;
12941294
u64 commit_num;
1295+
u32 feerate;
12951296
struct secrets secrets;
12961297
const u8 *funding_wscript;
12971298

12981299
if (!fromwire_hsmd_sign_commitment_tx(tmpctx, msg_in,
12991300
&peer_id, &dbid,
13001301
&tx,
13011302
&remote_funding_pubkey,
1302-
&rhashes, &commit_num))
1303+
&commit_num, &htlc, &feerate))
13031304
return hsmd_status_malformed_request(c, msg_in);
13041305

13051306
tx->chainparams = c->chainparams;

0 commit comments

Comments
 (0)