Skip to content

Commit e2c88b2

Browse files
committed
channeld: split get_per_commitment_point usesinto separate functions
Changelog-None: channeld internal This factoring makes it much clearer which callers need the pubkey and which need the old_secret. It does not alter the unfortunate semantic that fetching future pubkeys beyond the next commit is an error (because the underlying hsmd api still implies revocation of index - 2). Using HSM_VERSION 6 will fix this semantic in hsmd.
1 parent fcf7e9f commit e2c88b2

File tree

1 file changed

+39
-34
lines changed

1 file changed

+39
-34
lines changed

channeld/channeld.c

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,53 +1429,60 @@ 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 implicitly revoke
1436+
* index - 2 it is an error to call this past the next commitment.
1437+
*/
1438+
static void get_per_commitment_point(u64 index, struct pubkey *point)
14351439
{
1436-
struct secret *s;
1440+
struct secret *unused;
14371441
const u8 *msg;
14381442

14391443
msg = hsm_req(tmpctx,
14401444
take(towire_hsmd_get_per_commitment_point(NULL, index)));
14411445

14421446
if (!fromwire_hsmd_get_per_commitment_point_reply(tmpctx, msg,
14431447
point,
1444-
&s))
1448+
&unused))
14451449
status_failed(STATUS_FAIL_HSM_IO,
14461450
"Bad per_commitment_point reply %s",
14471451
tal_hex(tmpctx, msg));
1452+
}
14481453

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-
}
1454+
/* Revoke the specified commitment, the old secret is returned and
1455+
* next commitment point are returned. This call is idempotent, it is
1456+
* fine to re-revoke a previously revoked commitment. It is an error
1457+
* to revoke a commitment beyond the next revocable commitment.
1458+
*/
1459+
static void revoke_commitment(u64 index, struct secret *old_secret, struct pubkey *point)
1460+
{
1461+
const u8 *msg;
1462+
1463+
msg = hsm_req(tmpctx,
1464+
take(towire_hsmd_revoke_commitment_tx(tmpctx, index)));
1465+
1466+
if (!fromwire_hsmd_revoke_commitment_tx_reply(msg, old_secret, point))
1467+
status_failed(STATUS_FAIL_HSM_IO,
1468+
"Reading revoke_commitment_tx reply: %s",
1469+
tal_hex(tmpctx, msg));
14571470
}
14581471

14591472
/* revoke_index == current index - 1 (usually; not for retransmission) */
14601473
static u8 *make_revocation_msg(const struct peer *peer, u64 revoke_index,
14611474
struct pubkey *point)
14621475
{
1463-
const u8 *msg;
14641476
struct secret old_commit_secret;
14651477

14661478
/* 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
1479+
* and fetch the revocation secret for the old one.
1480+
*
1481+
* After HSM_VERSION 5 we explicitly revoke the commitment in case
14701482
* the original revoke didn't complete. The hsmd_revoke_commitment_tx
14711483
* call is idempotent ...
14721484
*/
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));
1485+
revoke_commitment(revoke_index, &old_commit_secret, point);
14791486

14801487
return towire_revoke_and_ack(peer, &peer->channel_id, &old_commit_secret,
14811488
point);
@@ -2026,7 +2033,7 @@ static struct commitsig_info *handle_peer_commit_sig(struct peer *peer,
20262033
"error");
20272034
}
20282035

2029-
/* Validate the counterparty's signatures, returns prior per_commitment_secret. */
2036+
/* As of HSM_VERSION 5 returned old_secret is always NULL (revoke returns it instead) */
20302037
htlcs = collect_htlcs(NULL, htlc_map);
20312038
msg2 = towire_hsmd_validate_commitment_tx(NULL,
20322039
txs[0],
@@ -2088,7 +2095,7 @@ static struct commitsig_info *handle_peer_commit_sig(struct peer *peer,
20882095
tal_steal(commitsigs, result);
20892096
}
20902097

2091-
/* We no longer receive old_secret from hsmd_validate_commitment_tx */
2098+
/* After HSM_VERSION 5 old_secret is always NULL */
20922099
assert(!old_secret);
20932100

20942101
send_revocation(peer, &commit_sig, htlc_sigs, changed_htlcs, txs[0],
@@ -2741,7 +2748,7 @@ static struct commitsig *interactive_send_commitments(struct peer *peer,
27412748
/* Funding counts as 0th commit so we do inflight_index + 1 */
27422749
if (fromwire_peektype(msg) == WIRE_COMMITMENT_SIGNED) {
27432750
get_per_commitment_point(next_index_local - 1,
2744-
&my_current_per_commitment_point, NULL);
2751+
&my_current_per_commitment_point);
27452752

27462753
result = handle_peer_commit_sig(peer, msg,
27472754
inflight_index + 1,
@@ -4633,12 +4640,10 @@ static void check_current_dataloss_fields(struct peer *peer,
46334640
next_revocation_number);
46344641
if (next_revocation_number == 0)
46354642
memset(&old_commit_secret, 0, sizeof(old_commit_secret));
4636-
else {
4643+
else
4644+
{
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

Comments
 (0)