Skip to content

Commit

Permalink
tests: make run-commit_tx output match the BOLT test vectors more clo…
Browse files Browse the repository at this point in the history
…sely

Still needs some massaging (we print HTLCs as we add them, rather then
in the final order, which requires a manual move in one test vector),
but this makes it more trivial to compare the output with the BOLT 3
text after lightning/bolts#852

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Apr 27, 2021
1 parent e2863a8 commit 35bf52d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ CCANDIR := ccan

# Where we keep the BOLT RFCs
BOLTDIR := ../lightning-rfc/
DEFAULT_BOLTVERSION := b201efe0546120c14bf154ce5f4e18da7243fe7a
DEFAULT_BOLTVERSION := 2f0cce1a12e889a544e48d7e39cd90d80cf1efb9
# Can be overridden on cmdline.
BOLTVERSION := $(DEFAULT_BOLTVERSION)

Expand Down
22 changes: 10 additions & 12 deletions channeld/commit_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ static void add_offered_htlc_out(struct bitcoin_tx *tx, size_t n,
option_anchor_outputs);
p2wsh = scriptpubkey_p2wsh(tx, wscript);
bitcoin_tx_add_output(tx, p2wsh, wscript, amount);
SUPERVERBOSE("# HTLC %" PRIu64 " offered %s wscript %s\n", htlc->id,
type_to_string(tmpctx, struct amount_sat, &amount),
SUPERVERBOSE("# HTLC #%" PRIu64 " offered amount %"PRIu64" wscript %s\n", htlc->id,
amount.satoshis, /* Raw: BOLT 3 output match */
tal_hex(wscript, wscript));
tal_free(wscript);
}
Expand All @@ -75,10 +75,9 @@ static void add_received_htlc_out(struct bitcoin_tx *tx, size_t n,

bitcoin_tx_add_output(tx, p2wsh, wscript, amount);

SUPERVERBOSE("# HTLC %"PRIu64" received %s wscript %s\n",
SUPERVERBOSE("# HTLC #%"PRIu64" received amount %"PRIu64" wscript %s\n",
htlc->id,
type_to_string(tmpctx, struct amount_sat,
&amount),
amount.satoshis, /* Raw: BOLT 3 output match */
tal_hex(wscript, wscript));
tal_free(wscript);
}
Expand Down Expand Up @@ -138,8 +137,8 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
base_fee = commit_tx_base_fee(feerate_per_kw, untrimmed,
option_anchor_outputs);

SUPERVERBOSE("# base commitment transaction fee = %s\n",
type_to_string(tmpctx, struct amount_sat, &base_fee));
SUPERVERBOSE("# base commitment transaction fee = %"PRIu64"\n",
base_fee.satoshis /* Raw: spec uses raw numbers */);

/* BOLT #3:
* If `option_anchor_outputs` applies to the commitment
Expand Down Expand Up @@ -244,8 +243,8 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
(*htlcmap)[n] = direct_outputs ? dummy_to_local : NULL;
/* We don't assign cltvs[n]: if we use it, order doesn't matter.
* However, valgrind will warn us something wierd is happening */
SUPERVERBOSE("# to-local amount %s wscript %s\n",
type_to_string(tmpctx, struct amount_sat, &amount),
SUPERVERBOSE("# to_local amount %"PRIu64" wscript %s\n",
amount.satoshis, /* Raw: BOLT 3 output match */
tal_hex(tmpctx, wscript));
n++;
to_local = true;
Expand Down Expand Up @@ -287,9 +286,8 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
(*htlcmap)[n] = direct_outputs ? dummy_to_remote : NULL;
/* We don't assign cltvs[n]: if we use it, order doesn't matter.
* However, valgrind will warn us something wierd is happening */
SUPERVERBOSE("# to-remote amount %s key %s\n",
type_to_string(tmpctx, struct amount_sat,
&amount),
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));
n++;
Expand Down
59 changes: 33 additions & 26 deletions channeld/test/run-commit_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ static void report_htlcs(const struct bitcoin_tx *tx,
keyset.other_htlc_key = *remote_htlckey;

for (i = 0; i < tal_count(htlc_map); i++) {
struct bitcoin_signature localhtlcsig;
const struct htlc *htlc = htlc_map[i];

if (!htlc)
Expand Down Expand Up @@ -281,29 +282,21 @@ static void report_htlcs(const struct bitcoin_tx *tx,
x_remote_htlcsecretkey, remote_htlckey,
SIGHASH_ALL,
&remotehtlcsig[i]);
printf("# signature for output %zi (htlc %"PRIu64")\n", i, htlc->id);
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, struct bitcoin_signature,
&remotehtlcsig[i]));
}

/* For any HTLC outputs, produce htlc_tx */
for (i = 0; i < tal_count(htlc_map); i++) {
struct bitcoin_signature localhtlcsig;
const struct htlc *htlc = htlc_map[i];

if (!htlc)
continue;
type_to_string(tmpctx, secp256k1_ecdsa_signature,
&remotehtlcsig[i].s));

sign_tx_input(htlc_tx[i], 0,
NULL,
wscript[i],
local_htlcsecretkey, local_htlckey,
SIGHASH_ALL,
&localhtlcsig);
printf("# local_signature = %s\n",
type_to_string(tmpctx, struct bitcoin_signature,
&localhtlcsig));
printf("# local_htlc_signature = %s\n",
type_to_string(tmpctx, secp256k1_ecdsa_signature,
&localhtlcsig.s));
if (htlc_owner(htlc) == LOCAL) {
htlc_timeout_tx_add_witness(htlc_tx[i],
local_htlckey,
Expand All @@ -324,7 +317,7 @@ static void report_htlcs(const struct bitcoin_tx *tx,
remote_revocation_key,
option_anchor_outputs);
}
printf("output htlc_%s_tx %"PRIu64": %s\n",
printf("htlc_%s_tx (htlc #%"PRIu64"): %s\n",
htlc_owner(htlc) == LOCAL ? "timeout" : "success",
htlc->id,
tal_hex(tmpctx, linearize_tx(tmpctx, htlc_tx[i])));
Expand Down Expand Up @@ -361,15 +354,15 @@ static void report(struct bitcoin_tx *tx,
SIGHASH_ALL,
&remotesig);
printf("remote_signature = %s\n",
type_to_string(tmpctx, struct bitcoin_signature, &remotesig));
type_to_string(tmpctx, secp256k1_ecdsa_signature, &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, struct bitcoin_signature, &localsig));
type_to_string(tmpctx, secp256k1_ecdsa_signature, &localsig.s));

witness =
bitcoin_witness_2of2(tx, &localsig, &remotesig,
Expand Down Expand Up @@ -801,7 +794,7 @@ int main(int argc, const char *argv[])
to_remote.millisatoshis = 3000000000;
feerate_per_kw = 0;
printf("\n"
"name: commitment tx with all 5 htlcs untrimmed (minimum feerate)\n"
"name: commitment tx with all five HTLCs untrimmed (minimum feerate)\n"
"to_local_msat: %"PRIu64"\n"
"to_remote_msat: %"PRIu64"\n"
"local_feerate_per_kw: %u\n",
Expand Down Expand Up @@ -901,12 +894,19 @@ int main(int argc, const char *argv[])
}
#endif
printf("\n"
"name: commitment tx with %zu output%s untrimmed (maximum feerate)\n"
"name: commitment tx with %s untrimmed (maximum feerate)\n"
"to_local_msat: %"PRIu64"\n"
"to_remote_msat: %"PRIu64"\n"
"local_feerate_per_kw: %u\n",
tx->wtx->num_outputs,
tx->wtx->num_outputs > 1 ? "s" : "",
/* Spec was "neatened" to change these numbers to words! */
tx->wtx->num_outputs == 7 ? "seven outputs"
: tx->wtx->num_outputs == 6 ? "six outputs"
: tx->wtx->num_outputs == 5 ? "five outputs"
: tx->wtx->num_outputs == 4 ? "four outputs"
: tx->wtx->num_outputs == 3 ? "three outputs"
: tx->wtx->num_outputs == 2 ? "two outputs"
: tx->wtx->num_outputs == 1 ? "one output"
: "no outputs???",
to_local.millisatoshis, to_remote.millisatoshis, feerate_per_kw-1);
/* Recalc with verbosity on */
print_superverbose = true;
Expand Down Expand Up @@ -942,12 +942,19 @@ int main(int argc, const char *argv[])
htlc_map);

printf("\n"
"name: commitment tx with %zu output%s untrimmed (minimum feerate)\n"
"name: commitment tx with %s untrimmed (minimum feerate)\n"
"to_local_msat: %"PRIu64"\n"
"to_remote_msat: %"PRIu64"\n"
"local_feerate_per_kw: %u\n",
newtx->wtx->num_outputs,
newtx->wtx->num_outputs > 1 ? "s" : "",
/* Spec was "neatened" to change these numbers to words! */
newtx->wtx->num_outputs == 7 ? "seven outputs"
: newtx->wtx->num_outputs == 6 ? "six outputs"
: newtx->wtx->num_outputs == 5 ? "five outputs"
: newtx->wtx->num_outputs == 4 ? "four outputs"
: newtx->wtx->num_outputs == 3 ? "three outputs"
: newtx->wtx->num_outputs == 2 ? "two outputs"
: newtx->wtx->num_outputs == 1 ? "one output"
: "no outputs???",
to_local.millisatoshis, to_remote.millisatoshis, feerate_per_kw);
/* Recalc with verbosity on */
print_superverbose = true;
Expand Down Expand Up @@ -1020,7 +1027,7 @@ int main(int argc, const char *argv[])
assert(feerate_per_kw == 9651936);

printf("\n"
"name: commitment tx with fee greater than opener amount\n"
"name: commitment tx with fee greater than funder amount\n"
"to_local_msat: %"PRIu64"\n"
"to_remote_msat: %"PRIu64"\n"
"local_feerate_per_kw: %u\n",
Expand Down
Loading

0 comments on commit 35bf52d

Please sign in to comment.