Skip to content

Commit

Permalink
global: change all type_to_string to fmt_X.
Browse files Browse the repository at this point in the history
This has the benefit of being shorter, as well as more reliable (you
will get a link error if we can't print it, not a runtime one!).

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Mar 20, 2024
1 parent d8c06dc commit 37d22f9
Show file tree
Hide file tree
Showing 108 changed files with 1,526 additions and 2,123 deletions.
281 changes: 111 additions & 170 deletions channeld/channeld.c

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions channeld/commit_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
* However, valgrind will warn us something wierd is happening */
SUPERVERBOSE("# to_remote amount %"PRIu64" P2WPKH(%s)\n",
amount.satoshis, /* Raw: BOLT 3 output match */
type_to_string(tmpctx, struct pubkey,
&keyset->other_payment_key));
fmt_pubkey(tmpctx, &keyset->other_payment_key));
n++;

to_remote = true;
Expand Down
66 changes: 25 additions & 41 deletions channeld/full_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,8 @@ static bool get_room_above_reserve(const struct channel *channel,
status_debug("%s cannot afford htlc: would make balance %s"
" below reserve %s",
side_to_str(side),
type_to_string(tmpctx, struct amount_msat,
remainder),
type_to_string(tmpctx, struct amount_sat,
&reserve));
fmt_amount_msat(tmpctx, *remainder),
fmt_amount_sat(tmpctx, reserve));
return false;
}
return true;
Expand Down Expand Up @@ -570,8 +568,8 @@ static bool local_opener_has_fee_headroom(const struct channel *channel,

status_debug("Adding HTLC would leave us only %s: we need %s for"
" another HTLC if fees increase by 100%% to %uperkw",
type_to_string(tmpctx, struct amount_msat, &remainder),
type_to_string(tmpctx, struct amount_sat, &fee),
fmt_amount_msat(tmpctx, remainder),
fmt_amount_sat(tmpctx, fee),
feerate + feerate);
return false;
}
Expand Down Expand Up @@ -775,10 +773,8 @@ static enum channel_add_err add_htlc(struct channel *channel,
if (channel->opener== sender) {
if (amount_msat_less_sat(remainder, fee)) {
status_debug("Cannot afford fee %s with %s above reserve",
type_to_string(tmpctx, struct amount_sat,
&fee),
type_to_string(tmpctx, struct amount_msat,
&remainder));
fmt_amount_sat(tmpctx, fee),
fmt_amount_msat(tmpctx, remainder));
return CHANNEL_ERR_CHANNEL_CAPACITY_EXCEEDED;
}

Expand Down Expand Up @@ -819,12 +815,8 @@ static enum channel_add_err add_htlc(struct channel *channel,
*htlc_fee = fee;
if (amount_msat_less_sat(remainder, fee)) {
status_debug("Funder could not afford own fee %s with %s above reserve",
type_to_string(tmpctx,
struct amount_sat,
&fee),
type_to_string(tmpctx,
struct amount_msat,
&remainder));
fmt_amount_sat(tmpctx, fee),
fmt_amount_msat(tmpctx, remainder));
return CHANNEL_ERR_CHANNEL_CAPACITY_EXCEEDED;
}
fee = fee_for_htlcs(channel,
Expand All @@ -836,13 +828,9 @@ static enum channel_add_err add_htlc(struct channel *channel,
if (htlc_fee && amount_sat_greater(fee, *htlc_fee))
*htlc_fee = fee;
if (amount_msat_less_sat(remainder, fee)) {
status_debug("Funder could not afford peer's fee %s with %s above reserve",
type_to_string(tmpctx,
struct amount_sat,
&fee),
type_to_string(tmpctx,
struct amount_msat,
&remainder));
status_debug("Funder could not afford peer's fee `%s with %s above reserve",
fmt_amount_sat(tmpctx, fee),
fmt_amount_msat(tmpctx, remainder));
return CHANNEL_ERR_CHANNEL_CAPACITY_EXCEEDED;
}
}
Expand Down Expand Up @@ -1192,8 +1180,8 @@ static int change_htlcs(struct channel *channel,
"%s: %s balance underflow: %s -> %"PRId64,
side_to_str(sidechanged),
side_to_str(i),
type_to_string(tmpctx, struct amount_msat,
&channel->view[sidechanged].owed[i]),
fmt_amount_msat(tmpctx,
channel->view[sidechanged].owed[i]),
owed[i].msat);
}
}
Expand Down Expand Up @@ -1349,8 +1337,7 @@ bool can_opener_afford_feerate(const struct channel *channel, u32 feerate_per_kw
&& !amount_sat_add(&fee, fee, AMOUNT_SAT(660)))
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Cannot add 660 sats to %s for anchor",
type_to_string(tmpctx, struct amount_sat,
&fee));
fmt_amount_sat(tmpctx, fee));

/* BOLT #2:
*
Expand All @@ -1366,10 +1353,9 @@ bool can_opener_afford_feerate(const struct channel *channel, u32 feerate_per_kw
channel->config[!channel->opener].channel_reserve))
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Cannot add fee %s and reserve %s",
type_to_string(tmpctx, struct amount_sat,
&fee),
type_to_string(tmpctx, struct amount_sat,
&channel->config[!channel->opener].channel_reserve));
fmt_amount_sat(tmpctx, fee),
fmt_amount_sat(tmpctx,
channel->config[!channel->opener].channel_reserve));

/* The amount we have needs to take into account that we're
* adding and removing htlcs */
Expand All @@ -1387,13 +1373,13 @@ bool can_opener_afford_feerate(const struct channel *channel, u32 feerate_per_kw
}

status_debug("We need %s at feerate %u for %zu untrimmed htlcs: we have %s/%s (will have %s)",
type_to_string(tmpctx, struct amount_sat, &needed),
fmt_amount_sat(tmpctx, needed),
feerate_per_kw, untrimmed,
type_to_string(tmpctx, struct amount_msat,
&channel->view[LOCAL].owed[channel->opener]),
type_to_string(tmpctx, struct amount_msat,
&channel->view[REMOTE].owed[channel->opener]),
type_to_string(tmpctx, struct amount_msat, &available));
fmt_amount_msat(tmpctx,
channel->view[LOCAL].owed[channel->opener]),
fmt_amount_msat(tmpctx,
channel->view[REMOTE].owed[channel->opener]),
fmt_amount_msat(tmpctx, available));
return amount_msat_greater_eq_sat(available, needed);
}

Expand Down Expand Up @@ -1620,11 +1606,9 @@ bool channel_force_htlcs(struct channel *channel,
" payment_hash=%s %s",
i, tal_count(htlcs),
htlcs[i]->id,
type_to_string(tmpctx, struct amount_msat,
&htlcs[i]->amount),
fmt_amount_msat(tmpctx, htlcs[i]->amount),
htlcs[i]->cltv_expiry,
type_to_string(tmpctx, struct sha256,
&htlcs[i]->payment_hash),
fmt_sha256(tmpctx, &htlcs[i]->payment_hash),
htlcs[i]->payment_preimage ? "(have preimage)"
: htlcs[i]->failed ? "(failed)" : "");

Expand Down
68 changes: 29 additions & 39 deletions channeld/test/run-commit_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,8 @@ static void report_htlcs(const struct bitcoin_tx *tx,
printf("# signature for output #%zi (%s for htlc #%"PRIu64")\n",
i, htlc_owner(htlc) == LOCAL ? "htlc-timeout" : "htlc-success", htlc->id);
printf("remote_htlc_signature = %s\n",
type_to_string(tmpctx, secp256k1_ecdsa_signature,
&remotehtlcsig[i].s));
fmt_secp256k1_ecdsa_signature(tmpctx,
&remotehtlcsig[i].s));

sign_tx_input(htlc_tx[i], 0,
NULL,
Expand All @@ -340,8 +340,8 @@ static void report_htlcs(const struct bitcoin_tx *tx,
SIGHASH_ALL,
&localhtlcsig);
printf("# local_htlc_signature = %s\n",
type_to_string(tmpctx, secp256k1_ecdsa_signature,
&localhtlcsig.s));
fmt_secp256k1_ecdsa_signature(tmpctx,
&localhtlcsig.s));
if (htlc_owner(htlc) == LOCAL) {
htlc_timeout_tx_add_witness(htlc_tx[i],
local_htlckey,
Expand Down Expand Up @@ -403,15 +403,15 @@ static void report(struct bitcoin_tx *tx,
SIGHASH_ALL,
&remotesig);
printf("remote_signature = %s\n",
type_to_string(tmpctx, secp256k1_ecdsa_signature, &remotesig.s));
fmt_secp256k1_ecdsa_signature(tmpctx, &remotesig.s));
sign_tx_input(tx, 0,
NULL,
wscript,
local_funding_privkey, local_funding_pubkey,
SIGHASH_ALL,
&localsig);
printf("# local_signature = %s\n",
type_to_string(tmpctx, secp256k1_ecdsa_signature, &localsig.s));
fmt_secp256k1_ecdsa_signature(tmpctx, &localsig.s));

witness =
bitcoin_witness_2of2(tx, &localsig, &remotesig,
Expand Down Expand Up @@ -618,51 +618,42 @@ int main(int argc, const char *argv[])
local_funding_privkey.secret = secret_from_hex("30ff4956bbdd3222d44cc5e8a1261dab1e07957bdac5ae88fe3261ef321f374901");
x_remote_funding_privkey.secret = secret_from_hex("1552dfba4f6cf29a62a0af13c8d6981d36d0ef8d61ba10fb0fe90da7634d7e1301");
SUPERVERBOSE("INTERNAL: remote_funding_privkey: %s01\n",
type_to_string(tmpctx, struct privkey,
&x_remote_funding_privkey));
fmt_privkey(tmpctx, &x_remote_funding_privkey));
x_local_payment_basepoint_secret = secret_from_hex("1111111111111111111111111111111111111111111111111111111111111111");
SUPERVERBOSE("INTERNAL: local_payment_basepoint_secret: %s\n",
type_to_string(tmpctx, struct secret,
&x_local_payment_basepoint_secret));
fmt_secret(tmpctx, &x_local_payment_basepoint_secret));
x_remote_revocation_basepoint_secret = secret_from_hex("2222222222222222222222222222222222222222222222222222222222222222");
SUPERVERBOSE("INTERNAL: remote_revocation_basepoint_secret: %s\n",
type_to_string(tmpctx, struct secret,
&x_remote_revocation_basepoint_secret));
fmt_secret(tmpctx, &x_remote_revocation_basepoint_secret));
x_local_delayed_payment_basepoint_secret = secret_from_hex("3333333333333333333333333333333333333333333333333333333333333333");
SUPERVERBOSE("INTERNAL: local_delayed_payment_basepoint_secret: %s\n",
type_to_string(tmpctx, struct secret,
&x_local_delayed_payment_basepoint_secret));
fmt_secret(tmpctx, &x_local_delayed_payment_basepoint_secret));
x_remote_payment_basepoint_secret = secret_from_hex("4444444444444444444444444444444444444444444444444444444444444444");
SUPERVERBOSE("INTERNAL: remote_payment_basepoint_secret: %s\n",
type_to_string(tmpctx, struct secret,
&x_remote_payment_basepoint_secret));
fmt_secret(tmpctx, &x_remote_payment_basepoint_secret));
x_local_per_commitment_secret = secret_from_hex("0x1f1e1d1c1b1a191817161514131211100f0e0d0c0b0a09080706050403020100");
SUPERVERBOSE("x_local_per_commitment_secret: %s\n",
type_to_string(tmpctx, struct secret,
&x_local_per_commitment_secret));
fmt_secret(tmpctx, &x_local_per_commitment_secret));

if (!pubkey_from_secret(&x_remote_revocation_basepoint_secret,
&x_remote_revocation_basepoint))
abort();
SUPERVERBOSE("# From remote_revocation_basepoint_secret\n"
"INTERNAL: remote_revocation_basepoint: %s\n",
type_to_string(tmpctx, struct pubkey,
&x_remote_revocation_basepoint));
fmt_pubkey(tmpctx, &x_remote_revocation_basepoint));

if (!pubkey_from_secret(&x_local_delayed_payment_basepoint_secret,
&x_local_delayed_payment_basepoint))
abort();
SUPERVERBOSE("# From local_delayed_payment_basepoint_secret\n"
"INTERNAL: local_delayed_payment_basepoint: %s\n",
type_to_string(tmpctx, struct pubkey,
&x_local_delayed_payment_basepoint));
fmt_pubkey(tmpctx, &x_local_delayed_payment_basepoint));

if (!pubkey_from_secret(&x_local_per_commitment_secret,
&x_local_per_commitment_point))
abort();
SUPERVERBOSE("INTERNAL: local_per_commitment_point: %s\n",
type_to_string(tmpctx, struct pubkey,
&x_local_per_commitment_point));
fmt_pubkey(tmpctx, &x_local_per_commitment_point));

if (!pubkey_from_secret(&x_local_payment_basepoint_secret,
&local_payment_basepoint))
Expand All @@ -684,7 +675,7 @@ int main(int argc, const char *argv[])
&x_remote_htlcsecretkey))
abort();
SUPERVERBOSE("INTERNAL: remote_secretkey: %s\n",
type_to_string(tmpctx, struct privkey, &x_remote_htlcsecretkey));
fmt_privkey(tmpctx, &x_remote_htlcsecretkey));

if (!derive_simple_privkey(&x_local_delayed_payment_basepoint_secret,
&x_local_delayed_payment_basepoint,
Expand All @@ -693,46 +684,45 @@ int main(int argc, const char *argv[])
abort();
SUPERVERBOSE("# From local_delayed_payment_basepoint_secret, local_per_commitment_point and local_delayed_payment_basepoint\n"
"INTERNAL: local_delayed_secretkey: %s\n",
type_to_string(tmpctx, struct privkey,
&x_local_delayed_secretkey));
fmt_privkey(tmpctx, &x_local_delayed_secretkey));

/* These two needed to calculate obscuring factor */
printf("local_payment_basepoint: %s\n",
type_to_string(tmpctx, struct pubkey, &local_payment_basepoint));
fmt_pubkey(tmpctx, &local_payment_basepoint));
printf("remote_payment_basepoint: %s\n",
type_to_string(tmpctx, struct pubkey,&remote_payment_basepoint));
fmt_pubkey(tmpctx,&remote_payment_basepoint));
cn_obscurer = commit_number_obscurer(&local_payment_basepoint,
&remote_payment_basepoint);
printf("# obscured commitment transaction number = 0x%"PRIx64" ^ %"PRIu64"\n",
cn_obscurer, commitment_number);


printf("local_funding_privkey: %s01\n",
type_to_string(tmpctx, struct privkey, &local_funding_privkey));
fmt_privkey(tmpctx, &local_funding_privkey));
if (!pubkey_from_privkey(&local_funding_privkey, &local_funding_pubkey))
abort();
printf("local_funding_pubkey: %s\n",
type_to_string(tmpctx, struct pubkey, &local_funding_pubkey));
fmt_pubkey(tmpctx, &local_funding_pubkey));

if (!pubkey_from_privkey(&x_remote_funding_privkey, &remote_funding_pubkey))
abort();
printf("remote_funding_pubkey: %s\n",
type_to_string(tmpctx, struct pubkey, &remote_funding_pubkey));
fmt_pubkey(tmpctx, &remote_funding_pubkey));

if (!derive_simple_privkey(&x_local_htlc_basepoint_secret,
&local_payment_basepoint,
&x_local_per_commitment_point,
&local_htlcsecretkey))
abort();
printf("local_secretkey: %s\n",
type_to_string(tmpctx, struct privkey, &local_htlcsecretkey));
fmt_privkey(tmpctx, &local_htlcsecretkey));

if (!derive_simple_key(&local_payment_basepoint,
&x_local_per_commitment_point,
&localkey))
abort();
printf("localkey: %s\n",
type_to_string(tmpctx, struct pubkey, &localkey));
fmt_pubkey(tmpctx, &localkey));

if (option_static_remotekey)
remotekey = remote_payment_basepoint;
Expand All @@ -743,7 +733,7 @@ int main(int argc, const char *argv[])
abort();
}
printf("remotekey: %s\n",
type_to_string(tmpctx, struct pubkey, &remotekey));
fmt_pubkey(tmpctx, &remotekey));

if (!pubkey_from_privkey(&local_htlcsecretkey, &local_htlckey))
abort();
Expand All @@ -753,14 +743,14 @@ int main(int argc, const char *argv[])
abort();
assert(pubkey_eq(&tmpkey, &local_htlckey));
printf("local_htlckey: %s\n",
type_to_string(tmpctx, struct pubkey, &local_htlckey));
fmt_pubkey(tmpctx, &local_htlckey));

if (!derive_simple_key(&remote_htlc_basepoint,
&x_local_per_commitment_point,
&remote_htlckey))
abort();
printf("remote_htlckey: %s\n",
type_to_string(tmpctx, struct pubkey, &remote_htlckey));
fmt_pubkey(tmpctx, &remote_htlckey));

if (!pubkey_from_privkey(&x_local_delayed_secretkey, &local_delayedkey))
abort();
Expand All @@ -770,14 +760,14 @@ int main(int argc, const char *argv[])
abort();
assert(pubkey_eq(&tmpkey, &local_delayedkey));
printf("local_delayedkey: %s\n",
type_to_string(tmpctx, struct pubkey, &local_delayedkey));
fmt_pubkey(tmpctx, &local_delayedkey));

if (!derive_revocation_key(&x_remote_revocation_basepoint,
&x_local_per_commitment_point,
&remote_revocation_key))
abort();
printf("remote_revocation_key: %s\n",
type_to_string(tmpctx, struct pubkey, &remote_revocation_key));
fmt_pubkey(tmpctx, &remote_revocation_key));

wscript = bitcoin_redeem_2of2(tmpctx, &local_funding_pubkey,
&remote_funding_pubkey);
Expand Down
9 changes: 4 additions & 5 deletions channeld/watchtower.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ penalty_tx_create(const tal_t *ctx,
if (!pubkey_from_secret(&remote_per_commitment_secret, &remote_per_commitment_point))
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Failed derive from per_commitment_secret %s",
type_to_string(tmpctx, struct secret,
&remote_per_commitment_secret));
fmt_secret(tmpctx, &remote_per_commitment_secret));

if (!derive_keyset(&remote_per_commitment_point,
&basepoints[REMOTE],
Expand Down Expand Up @@ -95,8 +94,8 @@ penalty_tx_create(const tal_t *ctx,
if (!amount_sat_add(&min_out, dust_limit, fee))
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Cannot add dust_limit %s and fee %s",
type_to_string(tmpctx, struct amount_sat, &dust_limit),
type_to_string(tmpctx, struct amount_sat, &fee));
fmt_amount_sat(tmpctx, dust_limit),
fmt_amount_sat(tmpctx, fee));

if (amount_sat_less(to_them_sats, min_out)) {
/* FIXME: We should use SIGHASH_NONE so others can take it */
Expand All @@ -112,7 +111,7 @@ penalty_tx_create(const tal_t *ctx,
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"TX can't afford minimal feerate"
"; setting output to %s",
type_to_string(tmpctx, struct amount_sat, &amt));
fmt_amount_sat(tmpctx, amt));
}
bitcoin_tx_output_set_amount(tx, 0, amt);
bitcoin_tx_finalize(tx);
Expand Down
Loading

0 comments on commit 37d22f9

Please sign in to comment.