Browse Source

coin moves: record moves for unilateral closes

nifty/pset-pre
lisa neigut 5 years ago
committed by Rusty Russell
parent
commit
19268afba5
  1. 56
      onchaind/onchaind.c

56
onchaind/onchaind.c

@ -200,6 +200,17 @@ static struct amount_sat record_chain_fees_tx(const struct bitcoin_txid *txid,
return fees; return fees;
} }
static void add_amt(struct amount_sat *sum, struct amount_sat amt)
{
if (!amount_sat_add(sum, *sum, amt))
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"unable to add %s to %s",
type_to_string(tmpctx, struct amount_sat,
&amt),
type_to_string(tmpctx, struct amount_sat,
sum));
}
static void record_mutual_closure(const struct bitcoin_txid *txid, static void record_mutual_closure(const struct bitcoin_txid *txid,
struct amount_sat our_out, struct amount_sat our_out,
int output_num) int output_num)
@ -243,6 +254,35 @@ static void record_mutual_closure(const struct bitcoin_txid *txid,
send_coin_mvt(take(mvt)); send_coin_mvt(take(mvt));
} }
static void record_chain_fees_unilateral(const struct bitcoin_txid *txid,
struct amount_sat funding,
struct amount_sat their_outs,
struct amount_sat our_outs)
{
struct amount_msat trimmed;
status_debug("chain_movements...recording chain fees for unilateral."
" our msat balance %s, funding %s,"
" their_outs %s, our outs %s",
type_to_string(tmpctx, struct amount_msat, &our_msat),
type_to_string(tmpctx, struct amount_sat, &funding),
type_to_string(tmpctx, struct amount_sat, &their_outs),
type_to_string(tmpctx, struct amount_sat, &our_outs));
/* we need to figure out what we paid in fees, total.
* this encompasses the actual chain fees + any trimmed outputs */
if (!amount_msat_sub_sat(&trimmed, our_msat, our_outs))
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"unable to subtract %s from %s",
type_to_string(tmpctx, struct amount_msat,
&our_msat),
type_to_string(tmpctx, struct amount_sat,
&our_outs));
status_debug("logging 'chain fees' for unilateral (trimmed) %s",
type_to_string(tmpctx, struct amount_msat, &trimmed));
update_ledger_chain_fees_msat(txid, trimmed);
}
static void record_coin_loss(const struct bitcoin_txid *txid, static void record_coin_loss(const struct bitcoin_txid *txid,
struct tracked_output *out) struct tracked_output *out)
{ {
@ -1986,6 +2026,7 @@ static void handle_our_unilateral(const struct bitcoin_tx *tx,
struct pubkey local_per_commitment_point; struct pubkey local_per_commitment_point;
struct keyset *ks; struct keyset *ks;
size_t i; size_t i;
struct amount_sat their_outs = AMOUNT_SAT(0), our_outs = AMOUNT_SAT(0);
init_reply("Tracking our own unilateral close"); init_reply("Tracking our own unilateral close");
onchain_annotate_txin(txid, 0, TX_CHANNEL_UNILATERAL); onchain_annotate_txin(txid, 0, TX_CHANNEL_UNILATERAL);
@ -2128,6 +2169,7 @@ static void handle_our_unilateral(const struct bitcoin_tx *tx,
tx_type); tx_type);
script[LOCAL] = NULL; script[LOCAL] = NULL;
add_amt(&our_outs, amt);
continue; continue;
} }
if (script[REMOTE] if (script[REMOTE]
@ -2147,6 +2189,7 @@ static void handle_our_unilateral(const struct bitcoin_tx *tx,
NULL, NULL, NULL); NULL, NULL, NULL);
ignore_output(out); ignore_output(out);
script[REMOTE] = NULL; script[REMOTE] = NULL;
add_amt(&their_outs, amt);
continue; continue;
} }
@ -2179,6 +2222,7 @@ static void handle_our_unilateral(const struct bitcoin_tx *tx,
out, matches, out, matches,
htlcs, htlcs,
htlc_scripts); htlc_scripts);
add_amt(&our_outs, amt);
} else { } else {
out = new_tracked_output(tx->chainparams, out = new_tracked_output(tx->chainparams,
&outs, txid, &outs, txid,
@ -2197,6 +2241,7 @@ static void handle_our_unilateral(const struct bitcoin_tx *tx,
/* Tells us which htlc to use */ /* Tells us which htlc to use */
which_htlc = resolve_their_htlc(out, matches, htlcs, which_htlc = resolve_their_htlc(out, matches, htlcs,
htlc_scripts); htlc_scripts);
add_amt(&their_outs, amt);
} }
out->htlc = htlcs[which_htlc]; out->htlc = htlcs[which_htlc];
out->wscript = tal_steal(out, htlc_scripts[which_htlc]); out->wscript = tal_steal(out, htlc_scripts[which_htlc]);
@ -2210,6 +2255,9 @@ static void handle_our_unilateral(const struct bitcoin_tx *tx,
note_missing_htlcs(htlc_scripts, htlcs, note_missing_htlcs(htlc_scripts, htlcs,
tell_if_missing, tell_immediately); tell_if_missing, tell_immediately);
record_chain_fees_unilateral(txid, outs[0]->sat,
their_outs, our_outs);
wait_for_resolved(tx->chainparams, outs); wait_for_resolved(tx->chainparams, outs);
} }
@ -2554,6 +2602,7 @@ static void handle_their_unilateral(const struct bitcoin_tx *tx,
u8 *remote_wscript, *script[NUM_SIDES]; u8 *remote_wscript, *script[NUM_SIDES];
struct keyset *ks; struct keyset *ks;
size_t i; size_t i;
struct amount_sat their_outs = AMOUNT_SAT(0), our_outs = AMOUNT_SAT(0);
init_reply("Tracking their unilateral close"); init_reply("Tracking their unilateral close");
onchain_annotate_txin(txid, 0, TX_CHANNEL_UNILATERAL | TX_THEIRS); onchain_annotate_txin(txid, 0, TX_CHANNEL_UNILATERAL | TX_THEIRS);
@ -2693,6 +2742,7 @@ static void handle_their_unilateral(const struct bitcoin_tx *tx,
i, amt, i, amt,
OUTPUT_TO_US, NULL, NULL, NULL); OUTPUT_TO_US, NULL, NULL, NULL);
ignore_output(out); ignore_output(out);
record_channel_withdrawal(txid, out);
tell_wallet_to_remote(tx, i, txid, tell_wallet_to_remote(tx, i, txid,
tx_blockheight, tx_blockheight,
@ -2700,6 +2750,7 @@ static void handle_their_unilateral(const struct bitcoin_tx *tx,
remote_per_commitment_point, remote_per_commitment_point,
option_static_remotekey); option_static_remotekey);
script[LOCAL] = NULL; script[LOCAL] = NULL;
add_amt(&our_outs, amt);
continue; continue;
} }
if (script[REMOTE] if (script[REMOTE]
@ -2719,6 +2770,7 @@ static void handle_their_unilateral(const struct bitcoin_tx *tx,
DELAYED_OUTPUT_TO_THEM, DELAYED_OUTPUT_TO_THEM,
NULL, NULL, NULL); NULL, NULL, NULL);
ignore_output(out); ignore_output(out);
add_amt(&their_outs, amt);
continue; continue;
} }
@ -2747,6 +2799,7 @@ static void handle_their_unilateral(const struct bitcoin_tx *tx,
matches, matches,
htlcs, htlcs,
htlc_scripts); htlc_scripts);
add_amt(&our_outs, amt);
} else { } else {
out = new_tracked_output(tx->chainparams, out = new_tracked_output(tx->chainparams,
&outs, txid, &outs, txid,
@ -2764,6 +2817,7 @@ static void handle_their_unilateral(const struct bitcoin_tx *tx,
*/ */
which_htlc = resolve_their_htlc(out, matches, htlcs, which_htlc = resolve_their_htlc(out, matches, htlcs,
htlc_scripts); htlc_scripts);
add_amt(&their_outs, amt);
} }
out->htlc = htlcs[which_htlc]; out->htlc = htlcs[which_htlc];
out->wscript = tal_steal(out, htlc_scripts[which_htlc]); out->wscript = tal_steal(out, htlc_scripts[which_htlc]);
@ -2772,6 +2826,8 @@ static void handle_their_unilateral(const struct bitcoin_tx *tx,
note_missing_htlcs(htlc_scripts, htlcs, note_missing_htlcs(htlc_scripts, htlcs,
tell_if_missing, tell_immediately); tell_if_missing, tell_immediately);
record_chain_fees_unilateral(txid, outs[0]->sat,
their_outs, our_outs);
wait_for_resolved(tx->chainparams, outs); wait_for_resolved(tx->chainparams, outs);
} }

Loading…
Cancel
Save