From 8537e77ac7c8ed28677c6a6cda4cd81623528ad5 Mon Sep 17 00:00:00 2001 From: lisa neigut Date: Tue, 14 Apr 2020 14:01:18 -0500 Subject: [PATCH] coins: re-write API interface for htlc notices Wrap up more logic internally to the method call for htlcs. Also, don't touch part id if we're not the 'origin' Suggested-By: @rustyrussell --- common/coin_mvt.c | 2 +- common/coin_mvt.h | 6 +++--- lightningd/coin_mvts.c | 44 +++++++++++++++++++++++++++++++++++++++ lightningd/coin_mvts.h | 15 +++++++++++++ lightningd/notification.c | 2 +- lightningd/peer_htlcs.c | 23 ++++++++------------ wallet/test/run-wallet.c | 31 +++++++++++++++++---------- 7 files changed, 93 insertions(+), 30 deletions(-) diff --git a/common/coin_mvt.c b/common/coin_mvt.c index 85165ceba..74f7065d0 100644 --- a/common/coin_mvt.c +++ b/common/coin_mvt.c @@ -36,7 +36,7 @@ struct channel_coin_mvt *new_channel_coin_mvt(const tal_t *ctx, struct bitcoin_txid *funding_txid, u32 funding_outnum, struct sha256 payment_hash, - u32 part_id, + u64 *part_id, struct amount_msat amount, enum mvt_tag tag, bool is_credit, diff --git a/common/coin_mvt.h b/common/coin_mvt.h index 0d818b0be..8983c54e7 100644 --- a/common/coin_mvt.h +++ b/common/coin_mvt.h @@ -45,7 +45,7 @@ struct channel_coin_mvt { /* mutlti-part payments may share a payment hash, * so we should also record a 'part-id' for them */ - u32 part_id; + u64 *part_id; /* label / tag */ enum mvt_tag tag; @@ -86,7 +86,7 @@ struct chain_coin_mvt { /* differs depending on type!? */ struct mvt_id { struct sha256 *payment_hash; - u32 part_id; + u64 *part_id; const struct bitcoin_txid *tx_txid; const struct bitcoin_txid *output_txid; u32 vout; @@ -130,7 +130,7 @@ struct channel_coin_mvt *new_channel_coin_mvt(const tal_t *ctx, struct bitcoin_txid *funding_txid, u32 funding_outnum, struct sha256 payment_hash, - u32 part_id, + u64 *part_id, struct amount_msat amount, enum mvt_tag tag, bool is_credit, diff --git a/lightningd/coin_mvts.c b/lightningd/coin_mvts.c index 996944a46..5fb5f0265 100644 --- a/lightningd/coin_mvts.c +++ b/lightningd/coin_mvts.c @@ -36,6 +36,50 @@ void notify_chain_mvt(struct lightningd *ld, const struct chain_coin_mvt *mvt) notify_coin_mvt(ld, cm); } +struct channel_coin_mvt *new_channel_mvt_invoice_hin(const tal_t *ctx, + struct htlc_in *hin, + struct channel *channel) +{ + return new_channel_coin_mvt(ctx, &channel->funding_txid, + channel->funding_outnum, + hin->payment_hash, NULL, + hin->msat, INVOICE, + true, BTC); +} + +struct channel_coin_mvt *new_channel_mvt_routed_hin(const tal_t *ctx, + struct htlc_in *hin, + struct channel *channel) +{ + return new_channel_coin_mvt(ctx, &channel->funding_txid, + channel->funding_outnum, + hin->payment_hash, NULL, + hin->msat, ROUTED, + true, BTC); +} + +struct channel_coin_mvt *new_channel_mvt_invoice_hout(const tal_t *ctx, + struct htlc_out *hout, + struct channel *channel) +{ + return new_channel_coin_mvt(ctx, &channel->funding_txid, + channel->funding_outnum, + hout->payment_hash, &hout->partid, + hout->msat, INVOICE, + false, BTC); +} + +struct channel_coin_mvt *new_channel_mvt_routed_hout(const tal_t *ctx, + struct htlc_out *hout, + struct channel *channel) +{ + return new_channel_coin_mvt(ctx, &channel->funding_txid, + channel->funding_outnum, + hout->payment_hash, NULL, + hout->msat, ROUTED, + false, BTC); +} + void coin_mvts_init_count(struct lightningd *ld) { s64 count; diff --git a/lightningd/coin_mvts.h b/lightningd/coin_mvts.h index 84bf765ca..e64df0653 100644 --- a/lightningd/coin_mvts.h +++ b/lightningd/coin_mvts.h @@ -3,11 +3,26 @@ #include "config.h" #include +#include +#include #include void notify_channel_mvt(struct lightningd *ld, const struct channel_coin_mvt *mvt); void notify_chain_mvt(struct lightningd *ld, const struct chain_coin_mvt *mvt); +struct channel_coin_mvt *new_channel_mvt_invoice_hin(const tal_t *ctx, + struct htlc_in *hin, + struct channel *channel); +struct channel_coin_mvt *new_channel_mvt_routed_hin(const tal_t *ctx, + struct htlc_in *hin, + struct channel *channel); +struct channel_coin_mvt *new_channel_mvt_invoice_hout(const tal_t *ctx, + struct htlc_out *hout, + struct channel *channel); +struct channel_coin_mvt *new_channel_mvt_routed_hout(const tal_t *ctx, + struct htlc_out *hout, + struct channel *channel); + /* Initialize the coin movement counter on lightningd */ void coin_mvts_init_count(struct lightningd *ld); #endif /* LIGHTNING_LIGHTNINGD_COIN_MVTS_H */ diff --git a/lightningd/notification.c b/lightningd/notification.c index d03b5c8b5..60d76361e 100644 --- a/lightningd/notification.c +++ b/lightningd/notification.c @@ -373,7 +373,7 @@ static void json_mvt_id(struct json_stream *stream, enum mvt_type mvt_type, case CHANNEL_MVT: json_add_sha256(stream, "payment_hash", id->payment_hash); if (id->part_id) - json_add_u64(stream, "part_id", id->part_id); + json_add_u64(stream, "part_id", *id->part_id); return; } abort(); diff --git a/lightningd/peer_htlcs.c b/lightningd/peer_htlcs.c index 55a9b8cc4..186d1387f 100644 --- a/lightningd/peer_htlcs.c +++ b/lightningd/peer_htlcs.c @@ -1477,12 +1477,11 @@ static void remove_htlc_in(struct channel *channel, struct htlc_in *hin) channel->msat_to_us_max = channel->our_msat; /* Coins have definitively moved, log a movement */ - mvt = new_channel_coin_mvt(hin, &channel->funding_txid, - channel->funding_outnum, - hin->payment_hash, 0, hin->msat, - hin->we_filled ? INVOICE : ROUTED, - /* FIXME: variable unit ? */ - true, BTC); + if (hin->we_filled) + mvt = new_channel_mvt_invoice_hin(hin, hin, channel); + else + mvt = new_channel_mvt_routed_hin(hin, hin, channel); + notify_channel_mvt(channel->peer->ld, mvt); } @@ -1526,14 +1525,10 @@ static void remove_htlc_out(struct channel *channel, struct htlc_out *hout) channel->msat_to_us_min = channel->our_msat; /* Coins have definitively moved, log a movement */ - mvt = new_channel_coin_mvt(hout, &channel->funding_txid, - channel->funding_outnum, - hout->payment_hash, hout->partid, - hout->msat, - /* routed payments flow through... */ - hout->am_origin ? INVOICE : ROUTED, - /* FIXME: variable unit ? */ - false, BTC); + if (hout->am_origin) + mvt = new_channel_mvt_invoice_hout(hout, hout, channel); + else + mvt = new_channel_mvt_routed_hout(hout, hout, channel); notify_channel_mvt(channel->peer->ld, mvt); } diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 71d824481..d9042262e 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -397,17 +397,26 @@ struct chain_coin_mvt *new_chain_coin_mvt_sat(const tal_t *ctx UNNEEDED, bool is_credit UNNEEDED, enum mvt_unit_type unit UNNEEDED) { fprintf(stderr, "new_chain_coin_mvt_sat called!\n"); abort(); } -/* Generated stub for new_channel_coin_mvt */ -struct channel_coin_mvt *new_channel_coin_mvt(const tal_t *ctx UNNEEDED, - struct bitcoin_txid *funding_txid UNNEEDED, - u32 funding_outnum UNNEEDED, - struct sha256 payment_hash UNNEEDED, - u32 part_id UNNEEDED, - struct amount_msat amount UNNEEDED, - enum mvt_tag tag UNNEEDED, - bool is_credit UNNEEDED, - enum mvt_unit_type unit UNNEEDED) -{ fprintf(stderr, "new_channel_coin_mvt called!\n"); abort(); } +/* Generated stub for new_channel_mvt_invoice_hin */ +struct channel_coin_mvt *new_channel_mvt_invoice_hin(const tal_t *ctx UNNEEDED, + struct htlc_in *hin UNNEEDED, + struct channel *channel UNNEEDED) +{ fprintf(stderr, "new_channel_mvt_invoice_hin called!\n"); abort(); } +/* Generated stub for new_channel_mvt_invoice_hout */ +struct channel_coin_mvt *new_channel_mvt_invoice_hout(const tal_t *ctx UNNEEDED, + struct htlc_out *hout UNNEEDED, + struct channel *channel UNNEEDED) +{ fprintf(stderr, "new_channel_mvt_invoice_hout called!\n"); abort(); } +/* Generated stub for new_channel_mvt_routed_hin */ +struct channel_coin_mvt *new_channel_mvt_routed_hin(const tal_t *ctx UNNEEDED, + struct htlc_in *hin UNNEEDED, + struct channel *channel UNNEEDED) +{ fprintf(stderr, "new_channel_mvt_routed_hin called!\n"); abort(); } +/* Generated stub for new_channel_mvt_routed_hout */ +struct channel_coin_mvt *new_channel_mvt_routed_hout(const tal_t *ctx UNNEEDED, + struct htlc_out *hout UNNEEDED, + struct channel *channel UNNEEDED) +{ fprintf(stderr, "new_channel_mvt_routed_hout called!\n"); abort(); } /* Generated stub for notify_chain_mvt */ void notify_chain_mvt(struct lightningd *ld UNNEEDED, const struct chain_coin_mvt *mvt UNNEEDED) { fprintf(stderr, "notify_chain_mvt called!\n"); abort(); }