@@ -1429,53 +1429,61 @@ static void start_commit_timer(struct peer *peer)
14291429 send_commit_if_not_stfu , peer );
14301430}
14311431
1432- /* If old_secret is NULL, we don't care, otherwise it is filled in. */
1433- static void get_per_commitment_point (u64 index , struct pubkey * point ,
1434- struct secret * old_secret )
1432+ /* Fetch the requested point. The secret is no longer returned, use
1433+ * revoke_commitment.
1434+ *
1435+ * NOTE - Because the internals of this call also release the secret
1436+ * from a revoked commitment it is an error to call this past the next
1437+ * commitment.
1438+ */
1439+ static void get_per_commitment_point (u64 index , struct pubkey * point )
14351440{
1436- struct secret * s ;
1441+ struct secret * unused ;
14371442 const u8 * msg ;
14381443
14391444 msg = hsm_req (tmpctx ,
14401445 take (towire_hsmd_get_per_commitment_point (NULL , index )));
14411446
14421447 if (!fromwire_hsmd_get_per_commitment_point_reply (tmpctx , msg ,
14431448 point ,
1444- & s ))
1449+ & unused ))
14451450 status_failed (STATUS_FAIL_HSM_IO ,
14461451 "Bad per_commitment_point reply %s" ,
14471452 tal_hex (tmpctx , msg ));
1453+ }
14481454
1449- if (old_secret ) {
1450- if (!s )
1451- status_failed (STATUS_FAIL_HSM_IO ,
1452- "No secret in per_commitment_point_reply %"
1453- PRIu64 ,
1454- index );
1455- * old_secret = * s ;
1456- }
1455+ /* Revoke the specified commitment, the old secret is returned and
1456+ * next commitment point are returned. This call is idempotent, it is
1457+ * fine to re-revoke a previously revoked commitment. It is an error
1458+ * to revoke a commitment beyond the next revocable commitment.
1459+ */
1460+ static void revoke_commitment (u64 index , struct secret * old_secret , struct pubkey * point )
1461+ {
1462+ const u8 * msg ;
1463+
1464+ msg = hsm_req (tmpctx ,
1465+ take (towire_hsmd_revoke_commitment_tx (tmpctx , index )));
1466+
1467+ if (!fromwire_hsmd_revoke_commitment_tx_reply (msg , old_secret , point ))
1468+ status_failed (STATUS_FAIL_HSM_IO ,
1469+ "Reading revoke_commitment_tx reply: %s" ,
1470+ tal_hex (tmpctx , msg ));
14571471}
14581472
14591473/* revoke_index == current index - 1 (usually; not for retransmission) */
14601474static u8 * make_revocation_msg (const struct peer * peer , u64 revoke_index ,
14611475 struct pubkey * point )
14621476{
1463- const u8 * msg ;
14641477 struct secret old_commit_secret ;
14651478
14661479 /* Now that the master has persisted the new commitment advance the HSMD
1467- * and fetch the revocation secret for the old one. */
1468-
1469- / * After HSM_VERSION 5 we explicitly revoke the commitment in case
1480+ * and fetch the revocation secret for the old one.
1481+ *
1482+ * After HSM_VERSION 5 we explicitly revoke the commitment in case
14701483 * the original revoke didn't complete. The hsmd_revoke_commitment_tx
14711484 * call is idempotent ...
14721485 */
1473- msg = towire_hsmd_revoke_commitment_tx (tmpctx , revoke_index );
1474- msg = hsm_req (tmpctx , take (msg ));
1475- if (!fromwire_hsmd_revoke_commitment_tx_reply (msg , & old_commit_secret , point ))
1476- status_failed (STATUS_FAIL_HSM_IO ,
1477- "Reading revoke_commitment_tx reply: %s" ,
1478- tal_hex (tmpctx , msg ));
1486+ revoke_commitment (revoke_index , & old_commit_secret , point );
14791487
14801488 return towire_revoke_and_ack (peer , & peer -> channel_id , & old_commit_secret ,
14811489 point );
@@ -2026,7 +2034,7 @@ static struct commitsig_info *handle_peer_commit_sig(struct peer *peer,
20262034 "error" );
20272035 }
20282036
2029- /* Validate the counterparty's signatures, returns prior per_commitment_secret. */
2037+ /* As of HSM_VERSION 5 returned old_secret is always NULL (revoke returns it instead) */
20302038 htlcs = collect_htlcs (NULL , htlc_map );
20312039 msg2 = towire_hsmd_validate_commitment_tx (NULL ,
20322040 txs [0 ],
@@ -2088,7 +2096,7 @@ static struct commitsig_info *handle_peer_commit_sig(struct peer *peer,
20882096 tal_steal (commitsigs , result );
20892097 }
20902098
2091- /* We no longer receive old_secret from hsmd_validate_commitment_tx */
2099+ /* After HSM_VERSION 5 old_secret is always NULL */
20922100 assert (!old_secret );
20932101
20942102 send_revocation (peer , & commit_sig , htlc_sigs , changed_htlcs , txs [0 ],
@@ -2741,7 +2749,7 @@ static struct commitsig *interactive_send_commitments(struct peer *peer,
27412749 /* Funding counts as 0th commit so we do inflight_index + 1 */
27422750 if (fromwire_peektype (msg ) == WIRE_COMMITMENT_SIGNED ) {
27432751 get_per_commitment_point (next_index_local - 1 ,
2744- & my_current_per_commitment_point , NULL );
2752+ & my_current_per_commitment_point );
27452753
27462754 result = handle_peer_commit_sig (peer , msg ,
27472755 inflight_index + 1 ,
@@ -4635,10 +4643,7 @@ static void check_current_dataloss_fields(struct peer *peer,
46354643 memset (& old_commit_secret , 0 , sizeof (old_commit_secret ));
46364644 else {
46374645 struct pubkey unused ;
4638- /* This gets previous revocation number, since asking for
4639- * commitment point N gives secret for N-2 */
4640- get_per_commitment_point (next_revocation_number + 1 ,
4641- & unused , & old_commit_secret );
4646+ revoke_commitment (next_revocation_number - 1 , & old_commit_secret , & unused );
46424647 }
46434648
46444649 if (!secret_eq_consttime (& old_commit_secret ,
@@ -4773,7 +4778,7 @@ static void peer_reconnect(struct peer *peer,
47734778 /* Our current per-commitment point is the commitment point in the last
47744779 * received signed commitment */
47754780 get_per_commitment_point (peer -> next_index [LOCAL ] - 1 ,
4776- & my_current_per_commitment_point , NULL );
4781+ & my_current_per_commitment_point );
47774782
47784783 send_tlvs = NULL ;
47794784
@@ -5382,7 +5387,7 @@ static void handle_funding_depth(struct peer *peer, const u8 *msg)
53825387 /* Need to retrieve the first point again, even if we
53835388 * moved on, as channel_ready explicitly includes the
53845389 * first one. */
5385- get_per_commitment_point (1 , & point , NULL );
5390+ get_per_commitment_point (1 , & point );
53865391
53875392 msg = towire_channel_ready (NULL , & peer -> channel_id ,
53885393 & point , tlvs );
@@ -5969,7 +5974,7 @@ static void init_channel(struct peer *peer)
59695974 assert (peer -> next_index [REMOTE ] > 0 );
59705975
59715976 get_per_commitment_point (peer -> next_index [LOCAL ],
5972- & peer -> next_local_per_commit , NULL );
5977+ & peer -> next_local_per_commit );
59735978
59745979 peer -> channel = new_full_channel (peer , & peer -> channel_id ,
59755980 & funding ,
0 commit comments