Skip to content

Commit 04ce198

Browse files
committed
Extend hsmd API to pass HTLCs in hsmd_sign_commitment_tx and hsmd_sign_remote_commitment_tx.
1 parent fd31a57 commit 04ce198

17 files changed

+218
-110
lines changed

channeld/channeld.c

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

982+
// Collect the htlcs for call to hsmd.
983+
//
984+
// We use the existing_htlc to_wire routines, it's unfortunate that
985+
// we have to send a dummy onion_routing_packet ...
986+
//
987+
struct existing_htlc **htlcs = tal_arr(tmpctx, struct existing_htlc *, 0);
988+
u8 dummy_onion_routing_packet[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)];
989+
memset(dummy_onion_routing_packet, 0, sizeof(dummy_onion_routing_packet));
982990
size_t num_entries = tal_count(htlc_map);
983-
struct sha256 *rhashes = tal_arrz(tmpctx, struct sha256, num_entries);
984-
size_t nrhash = 0;
985991
for (size_t ndx = 0; ndx < num_entries; ++ndx) {
986-
if (htlc_map[ndx]) {
987-
memcpy(&rhashes[nrhash], &htlc_map[ndx]->rhash, sizeof(rhashes[nrhash]));
988-
++nrhash;
992+
struct htlc const *hh = htlc_map[ndx];
993+
if (hh) {
994+
status_debug("HTLC[%lu]=%" PRIu64 ", %s",
995+
ndx, hh->id, htlc_state_name(hh->state));
996+
struct existing_htlc *existing =
997+
new_existing_htlc(NULL,
998+
hh->id,
999+
hh->state,
1000+
hh->amount,
1001+
&hh->rhash,
1002+
hh->expiry.locktime,
1003+
dummy_onion_routing_packet,
1004+
NULL,
1005+
NULL,
1006+
NULL);
1007+
tal_arr_expand(&htlcs, tal_steal(htlcs, existing));
9891008
}
9901009
}
991-
tal_resize(&rhashes, nrhash);
9921010

9931011
msg = towire_hsmd_sign_remote_commitment_tx(NULL, txs[0],
9941012
&peer->channel->funding_pubkey[REMOTE],
9951013
&peer->remote_per_commit,
996-
peer->channel->option_static_remotekey,
997-
rhashes, commit_index);
998-
1014+
peer->channel->option_static_remotekey,
1015+
commit_index,
1016+
(const struct existing_htlc **) htlcs,
1017+
channel_feerate(peer->channel, REMOTE));
9991018
msg = hsm_req(tmpctx, take(msg));
10001019
if (!fromwire_hsmd_sign_tx_reply(msg, commit_sig))
10011020
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/hsmd_wiregen.c

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

hsmd/hsmd_wiregen.h

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

0 commit comments

Comments
 (0)