From dd2773dfc0ef33535cd81d7433b3793c52526829 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 23 Jul 2018 11:53:02 +0930 Subject: [PATCH] common/keyset: use struct basepoints rather than open-coding fields. Signed-off-by: Rusty Russell --- channeld/full_channel.c | 8 +- common/initial_channel.c | 8 +- common/keyset.c | 21 +++--- common/keyset.h | 11 ++- onchaind/onchain.c | 119 +++++++++--------------------- onchaind/test/run-grind_feerate.c | 8 +- 6 files changed, 56 insertions(+), 119 deletions(-) diff --git a/channeld/full_channel.c b/channeld/full_channel.c index ad85d5c94..fde10fe58 100644 --- a/channeld/full_channel.c +++ b/channeld/full_channel.c @@ -248,12 +248,8 @@ struct bitcoin_tx **channel_txs(const tal_t *ctx, struct keyset keyset; if (!derive_keyset(per_commitment_point, - &channel->basepoints[side].payment, - &channel->basepoints[!side].payment, - &channel->basepoints[side].htlc, - &channel->basepoints[!side].htlc, - &channel->basepoints[side].delayed_payment, - &channel->basepoints[!side].revocation, + &channel->basepoints[side], + &channel->basepoints[!side], &keyset)) return NULL; diff --git a/common/initial_channel.c b/common/initial_channel.c index dc8bd7407..f2ad81c18 100644 --- a/common/initial_channel.c +++ b/common/initial_channel.c @@ -75,12 +75,8 @@ struct bitcoin_tx *initial_channel_tx(const tal_t *ctx, assert(!channel->htlcs); if (!derive_keyset(per_commitment_point, - &channel->basepoints[side].payment, - &channel->basepoints[!side].payment, - &channel->basepoints[side].htlc, - &channel->basepoints[!side].htlc, - &channel->basepoints[side].delayed_payment, - &channel->basepoints[!side].revocation, + &channel->basepoints[side], + &channel->basepoints[!side], &keyset)) return NULL; diff --git a/common/keyset.c b/common/keyset.c index d54b5600b..b84e24e59 100644 --- a/common/keyset.c +++ b/common/keyset.c @@ -1,13 +1,10 @@ +#include #include #include bool derive_keyset(const struct pubkey *per_commitment_point, - const struct pubkey *self_payment_basepoint, - const struct pubkey *other_payment_basepoint, - const struct pubkey *self_htlc_basepoint, - const struct pubkey *other_htlc_basepoint, - const struct pubkey *self_delayed_basepoint, - const struct pubkey *other_revocation_basepoint, + const struct basepoints *self, + const struct basepoints *other, struct keyset *keyset) { /* BOLT #3: @@ -27,27 +24,27 @@ bool derive_keyset(const struct pubkey *per_commitment_point, * node's `htlc_basepoint`; and the `remote_delayedpubkey` uses the * remote node's `delayed_payment_basepoint`. */ - if (!derive_simple_key(self_payment_basepoint, + if (!derive_simple_key(&self->payment, per_commitment_point, &keyset->self_payment_key)) return false; - if (!derive_simple_key(other_payment_basepoint, + if (!derive_simple_key(&other->payment, per_commitment_point, &keyset->other_payment_key)) return false; - if (!derive_simple_key(self_htlc_basepoint, + if (!derive_simple_key(&self->htlc, per_commitment_point, &keyset->self_htlc_key)) return false; - if (!derive_simple_key(other_htlc_basepoint, + if (!derive_simple_key(&other->htlc, per_commitment_point, &keyset->other_htlc_key)) return false; - if (!derive_simple_key(self_delayed_basepoint, + if (!derive_simple_key(&self->delayed_payment, per_commitment_point, &keyset->self_delayed_payment_key)) return false; @@ -61,7 +58,7 @@ bool derive_keyset(const struct pubkey *per_commitment_point, * `revocation_basepoint` and the remote node's `per_commitment_point` * to derive a new `revocationpubkey` for the commitment. */ - if (!derive_revocation_key(other_revocation_basepoint, + if (!derive_revocation_key(&other->revocation, per_commitment_point, &keyset->self_revocation_key)) return false; diff --git a/common/keyset.h b/common/keyset.h index 7859876da..09f877093 100644 --- a/common/keyset.h +++ b/common/keyset.h @@ -4,6 +4,8 @@ #include #include +struct basepoints; + /* Keys needed to derive a particular commitment tx. */ struct keyset { struct pubkey self_revocation_key; @@ -12,12 +14,9 @@ struct keyset { struct pubkey self_payment_key, other_payment_key; }; +/* Self == owner of commitment tx, other == non-owner. */ bool derive_keyset(const struct pubkey *per_commitment_point, - const struct pubkey *self_payment_basepoint, - const struct pubkey *other_payment_basepoint, - const struct pubkey *self_htlc_basepoint, - const struct pubkey *other_htlc_basepoint, - const struct pubkey *self_delayed_basepoint, - const struct pubkey *other_revocation_basepoint, + const struct basepoints *self, + const struct basepoints *other, struct keyset *keyset); #endif /* LIGHTNING_COMMON_KEYSET_H */ diff --git a/onchaind/onchain.c b/onchaind/onchain.c index 7c748e019..5144daac3 100644 --- a/onchaind/onchain.c +++ b/onchaind/onchain.c @@ -1369,12 +1369,7 @@ static void handle_our_unilateral(const struct bitcoin_tx *tx, const struct bitcoin_txid *txid, const struct secrets *secrets, const struct sha256 *shaseed, - const struct pubkey *remote_revocation_basepoint, - const struct pubkey *remote_payment_basepoint, - const struct pubkey *local_payment_basepoint, - const struct pubkey *remote_htlc_basepoint, - const struct pubkey *local_htlc_basepoint, - const struct pubkey *local_delayed_payment_basepoint, + const struct basepoints basepoints[NUM_SIDES], const struct htlc_stub *htlcs, const bool *tell_if_missing, const bool *tell_immediately, @@ -1405,12 +1400,8 @@ static void handle_our_unilateral(const struct bitcoin_tx *tx, /* keyset is const, we need a non-const ptr to set it up */ keyset = ks = tal(tx, struct keyset); if (!derive_keyset(&local_per_commitment_point, - local_payment_basepoint, - remote_payment_basepoint, - local_htlc_basepoint, - remote_htlc_basepoint, - local_delayed_payment_basepoint, - remote_revocation_basepoint, + &basepoints[LOCAL], + &basepoints[REMOTE], ks)) status_failed(STATUS_FAIL_INTERNAL_ERROR, "Deriving keyset for %"PRIu64, commit_num); @@ -1438,7 +1429,7 @@ static void handle_our_unilateral(const struct bitcoin_tx *tx, &keyset->other_htlc_key)); if (!derive_simple_privkey(&secrets->delayed_payment_basepoint_secret, - local_delayed_payment_basepoint, + &basepoints[LOCAL].delayed_payment, &local_per_commitment_point, &delayed_payment_privkey)) status_failed(STATUS_FAIL_INTERNAL_ERROR, @@ -1446,7 +1437,7 @@ static void handle_our_unilateral(const struct bitcoin_tx *tx, commit_num); if (!derive_simple_privkey(&secrets->payment_basepoint_secret, - local_payment_basepoint, + &basepoints[LOCAL].payment, &local_per_commitment_point, &payment_privkey)) status_failed(STATUS_FAIL_INTERNAL_ERROR, @@ -1454,7 +1445,7 @@ static void handle_our_unilateral(const struct bitcoin_tx *tx, commit_num); if (!derive_simple_privkey(&secrets->htlc_basepoint_secret, - local_htlc_basepoint, + &basepoints[LOCAL].htlc, &local_per_commitment_point, &htlc_privkey)) status_failed(STATUS_FAIL_INTERNAL_ERROR, @@ -1674,12 +1665,7 @@ static void handle_their_cheat(const struct bitcoin_tx *tx, u32 tx_blockheight, const struct sha256 *revocation_preimage, const struct secrets *secrets, - const struct pubkey *local_revocation_basepoint, - const struct pubkey *local_payment_basepoint, - const struct pubkey *remote_payment_basepoint, - const struct pubkey *remote_htlc_basepoint, - const struct pubkey *local_htlc_basepoint, - const struct pubkey *remote_delayed_payment_basepoint, + const struct basepoints basepoints[NUM_SIDES], const struct htlc_stub *htlcs, const bool *tell_if_missing, const bool *tell_immediately, @@ -1729,27 +1715,23 @@ static void handle_their_cheat(const struct bitcoin_tx *tx, type_to_string(tmpctx, struct pubkey, &per_commitment_point), type_to_string(tmpctx, struct pubkey, - remote_payment_basepoint), + &basepoints[REMOTE].payment), type_to_string(tmpctx, struct pubkey, - local_payment_basepoint), + &basepoints[LOCAL].payment), type_to_string(tmpctx, struct pubkey, - remote_htlc_basepoint), + &basepoints[REMOTE].htlc), type_to_string(tmpctx, struct pubkey, - local_htlc_basepoint), + &basepoints[LOCAL].htlc), type_to_string(tmpctx, struct pubkey, - remote_delayed_payment_basepoint), + &basepoints[REMOTE].delayed_payment), type_to_string(tmpctx, struct pubkey, - local_revocation_basepoint)); + &basepoints[LOCAL].revocation)); /* keyset is const, we need a non-const ptr to set it up */ keyset = ks = tal(tx, struct keyset); if (!derive_keyset(&per_commitment_point, - remote_payment_basepoint, - local_payment_basepoint, - local_htlc_basepoint, - remote_htlc_basepoint, - remote_delayed_payment_basepoint, - local_revocation_basepoint, + &basepoints[REMOTE], + &basepoints[LOCAL], ks)) status_failed(STATUS_FAIL_INTERNAL_ERROR, "Deriving keyset for %"PRIu64, commit_num); @@ -1779,7 +1761,7 @@ static void handle_their_cheat(const struct bitcoin_tx *tx, revocation_privkey = tal(tx, struct privkey); if (!derive_revocation_privkey(&secrets->revocation_basepoint_secret, &per_commitment_secret, - local_revocation_basepoint, + &basepoints[LOCAL].revocation, &per_commitment_point, revocation_privkey)) status_failed(STATUS_FAIL_INTERNAL_ERROR, @@ -1906,12 +1888,7 @@ static void handle_their_unilateral(const struct bitcoin_tx *tx, const struct bitcoin_txid *txid, const struct secrets *secrets, const struct pubkey *remote_per_commitment_point, - const struct pubkey *local_revocation_basepoint, - const struct pubkey *local_payment_basepoint, - const struct pubkey *remote_payment_basepoint, - const struct pubkey *remote_htlc_basepoint, - const struct pubkey *local_htlc_basepoint, - const struct pubkey *remote_delayed_payment_basepoint, + const struct basepoints basepoints[NUM_SIDES], const struct htlc_stub *htlcs, const bool *tell_if_missing, const bool *tell_immediately, @@ -1950,27 +1927,23 @@ static void handle_their_unilateral(const struct bitcoin_tx *tx, type_to_string(tmpctx, struct pubkey, remote_per_commitment_point), type_to_string(tmpctx, struct pubkey, - remote_payment_basepoint), + &basepoints[REMOTE].payment), type_to_string(tmpctx, struct pubkey, - local_payment_basepoint), + &basepoints[LOCAL].payment), type_to_string(tmpctx, struct pubkey, - remote_htlc_basepoint), + &basepoints[REMOTE].htlc), type_to_string(tmpctx, struct pubkey, - local_htlc_basepoint), + &basepoints[LOCAL].htlc), type_to_string(tmpctx, struct pubkey, - remote_delayed_payment_basepoint), + &basepoints[REMOTE].delayed_payment), type_to_string(tmpctx, struct pubkey, - local_revocation_basepoint)); + &basepoints[LOCAL].revocation)); /* keyset is const, we need a non-const ptr to set it up */ keyset = ks = tal(tx, struct keyset); if (!derive_keyset(remote_per_commitment_point, - remote_payment_basepoint, - local_payment_basepoint, - remote_htlc_basepoint, - local_htlc_basepoint, - remote_delayed_payment_basepoint, - local_revocation_basepoint, + &basepoints[REMOTE], + &basepoints[LOCAL], ks)) status_failed(STATUS_FAIL_INTERNAL_ERROR, "Deriving keyset for %"PRIu64, commit_num); @@ -1998,7 +1971,7 @@ static void handle_their_unilateral(const struct bitcoin_tx *tx, &keyset->other_htlc_key)); if (!derive_simple_privkey(&secrets->payment_basepoint_secret, - local_payment_basepoint, + &basepoints[LOCAL].payment, remote_per_commitment_point, &payment_privkey)) status_failed(STATUS_FAIL_INTERNAL_ERROR, @@ -2006,7 +1979,7 @@ static void handle_their_unilateral(const struct bitcoin_tx *tx, commit_num); if (!derive_simple_privkey(&secrets->htlc_basepoint_secret, - local_htlc_basepoint, + &basepoints[LOCAL].htlc, remote_per_commitment_point, &htlc_privkey)) status_failed(STATUS_FAIL_INTERNAL_ERROR, @@ -2138,7 +2111,7 @@ int main(int argc, char *argv[]) struct secret seed; struct pubkey remote_per_commit_point, old_remote_per_commit_point; enum side funder; - struct basepoints basepoints, remote_basepoints; + struct basepoints basepoints[NUM_SIDES]; struct shachain shachain; struct bitcoin_tx *tx; struct secrets secrets; @@ -2173,7 +2146,7 @@ int main(int argc, char *argv[]) &scriptpubkey[REMOTE], &our_wallet_pubkey, &funder, - &remote_basepoints, + &basepoints[REMOTE], &tx, &tx_blockheight, &reasonable_depth, @@ -2184,7 +2157,7 @@ int main(int argc, char *argv[]) master_badmsg(WIRE_ONCHAIN_INIT, msg); } - derive_basepoints(&seed, NULL, &basepoints, &secrets, &shaseed); + derive_basepoints(&seed, NULL, &basepoints[LOCAL], &secrets, &shaseed); bitcoin_txid(tx, &txid); /* FIXME: Filter as we go, don't load them all into mem! */ @@ -2240,8 +2213,8 @@ int main(int argc, char *argv[]) */ struct sha256 revocation_preimage; commit_num = unmask_commit_number(tx, funder, - &basepoints.payment, - &remote_basepoints.payment); + &basepoints[LOCAL].payment, + &basepoints[REMOTE].payment); status_trace("commitnum = %"PRIu64 ", revocations_received = %"PRIu64, @@ -2251,12 +2224,7 @@ int main(int argc, char *argv[]) handle_our_unilateral(tx, tx_blockheight, &txid, &secrets, &shaseed, - &remote_basepoints.revocation, - &remote_basepoints.payment, - &basepoints.payment, - &remote_basepoints.htlc, - &basepoints.htlc, - &basepoints.delayed_payment, + basepoints, htlcs, tell_if_missing, tell_immediately, remote_htlc_sigs, @@ -2275,12 +2243,7 @@ int main(int argc, char *argv[]) tx_blockheight, &revocation_preimage, &secrets, - &basepoints.revocation, - &basepoints.payment, - &remote_basepoints.payment, - &basepoints.htlc, - &remote_basepoints.htlc, - &remote_basepoints.delayed_payment, + basepoints, htlcs, tell_if_missing, tell_immediately, outs); @@ -2298,12 +2261,7 @@ int main(int argc, char *argv[]) handle_their_unilateral(tx, tx_blockheight, &txid, &secrets, &old_remote_per_commit_point, - &basepoints.revocation, - &basepoints.payment, - &remote_basepoints.payment, - &remote_basepoints.htlc, - &basepoints.htlc, - &remote_basepoints.delayed_payment, + basepoints, htlcs, tell_if_missing, tell_immediately, @@ -2313,12 +2271,7 @@ int main(int argc, char *argv[]) handle_their_unilateral(tx, tx_blockheight, &txid, &secrets, &remote_per_commit_point, - &basepoints.revocation, - &basepoints.payment, - &remote_basepoints.payment, - &remote_basepoints.htlc, - &basepoints.htlc, - &remote_basepoints.delayed_payment, + basepoints, htlcs, tell_if_missing, tell_immediately, diff --git a/onchaind/test/run-grind_feerate.c b/onchaind/test/run-grind_feerate.c index 4f224b96c..726f6a110 100644 --- a/onchaind/test/run-grind_feerate.c +++ b/onchaind/test/run-grind_feerate.c @@ -27,12 +27,8 @@ bool derive_basepoints(const struct secret *seed UNNEEDED, { fprintf(stderr, "derive_basepoints called!\n"); abort(); } /* Generated stub for derive_keyset */ bool derive_keyset(const struct pubkey *per_commitment_point UNNEEDED, - const struct pubkey *self_payment_basepoint UNNEEDED, - const struct pubkey *other_payment_basepoint UNNEEDED, - const struct pubkey *self_htlc_basepoint UNNEEDED, - const struct pubkey *other_htlc_basepoint UNNEEDED, - const struct pubkey *self_delayed_basepoint UNNEEDED, - const struct pubkey *other_revocation_basepoint UNNEEDED, + const struct basepoints *self UNNEEDED, + const struct basepoints *other UNNEEDED, struct keyset *keyset UNNEEDED) { fprintf(stderr, "derive_keyset called!\n"); abort(); } /* Generated stub for derive_revocation_privkey */