From 8a8f81175dc6cfa4cb8dc65021caf7919cf6ae98 Mon Sep 17 00:00:00 2001 From: Karol Hosiawa Date: Sat, 23 Jan 2021 18:18:34 +0100 Subject: [PATCH] Added in_channel,out_channel,state params to listforwards --- lightningd/peer_htlcs.c | 31 ++++++++++++---- wallet/db_postgres_sqlgen.c | 8 ++-- wallet/db_sqlite3_sqlgen.c | 8 ++-- wallet/statements_gettextgen.po | 28 +++++++------- wallet/wallet.c | 65 +++++++++++++++++++++++++++++++-- wallet/wallet.h | 18 ++++++++- 6 files changed, 124 insertions(+), 34 deletions(-) diff --git a/lightningd/peer_htlcs.c b/lightningd/peer_htlcs.c index 4145047f9..827be6f4f 100644 --- a/lightningd/peer_htlcs.c +++ b/lightningd/peer_htlcs.c @@ -2596,11 +2596,15 @@ void json_format_forwarding_object(struct json_stream *response, json_object_end(response); } - -static void listforwardings_add_forwardings(struct json_stream *response, struct wallet *wallet) +static void listforwardings_add_forwardings(struct json_stream *response, + struct wallet *wallet, + enum forward_status status, + const struct short_channel_id *chan_in, + const struct short_channel_id *chan_out) { const struct forwarding *forwardings; - forwardings = wallet_forwarded_payments_get(wallet, tmpctx); + + forwardings = wallet_forwarded_payments_get(wallet, tmpctx, status, chan_in, chan_out); json_array_start(response, "forwards"); for (size_t i=0; ild->wallet); + listforwardings_add_forwardings(response, cmd->ld->wallet, status, chan_in, chan_out); return command_success(cmd, response); } @@ -2632,8 +2650,7 @@ static const struct json_command listforwards_command = { "listforwards", "channels", json_listforwards, - "List all forwarded payments and their information", false, - "List all forwarded payments and their information" + "List all forwarded payments and their information optionally filtering by [in_channel] [out_channel] and [state]" }; AUTODATA(json_command, &listforwards_command); diff --git a/wallet/db_postgres_sqlgen.c b/wallet/db_postgres_sqlgen.c index 7707b9dd4..27c67ee62 100644 --- a/wallet/db_postgres_sqlgen.c +++ b/wallet/db_postgres_sqlgen.c @@ -1695,9 +1695,9 @@ struct db_query db_postgres_queries[] = { .readonly = true, }, { - .name = "SELECT f.state, in_msatoshi, out_msatoshi, hin.payment_hash as payment_hash, in_channel_scid, out_channel_scid, f.received_time, f.resolved_time, f.failcode FROM forwarded_payments f LEFT JOIN channel_htlcs hin ON (f.in_htlc_id = hin.id)", - .query = "SELECT f.state, in_msatoshi, out_msatoshi, hin.payment_hash as payment_hash, in_channel_scid, out_channel_scid, f.received_time, f.resolved_time, f.failcode FROM forwarded_payments f LEFT JOIN channel_htlcs hin ON (f.in_htlc_id = hin.id)", - .placeholders = 0, + .name = "SELECT f.state, in_msatoshi, out_msatoshi, hin.payment_hash as payment_hash, in_channel_scid, out_channel_scid, f.received_time, f.resolved_time, f.failcode FROM forwarded_payments f LEFT JOIN channel_htlcs hin ON (f.in_htlc_id = hin.id) WHERE (1 = ? OR f.state = ?) AND (1 = ? OR f.in_channel_scid = ?) AND (1 = ? OR f.out_channel_scid = ?)", + .query = "SELECT f.state, in_msatoshi, out_msatoshi, hin.payment_hash as payment_hash, in_channel_scid, out_channel_scid, f.received_time, f.resolved_time, f.failcode FROM forwarded_payments f LEFT JOIN channel_htlcs hin ON (f.in_htlc_id = hin.id) WHERE (1 = $1 OR f.state = $2) AND (1 = $3 OR f.in_channel_scid = $4) AND (1 = $5 OR f.out_channel_scid = $6)", + .placeholders = 6, .readonly = true, }, { @@ -1792,4 +1792,4 @@ struct db_query db_postgres_queries[] = { #endif /* LIGHTNINGD_WALLET_GEN_DB_POSTGRES */ -// SHA256STAMP:759b19435d68328d597f6540e1f4f8c42ce22ef91dbf66c1098de0434462546c +// SHA256STAMP:4a878278ad7a1d427b80ff3a2abe34ae82cd73cf635ecf25e5cb20532c0604be diff --git a/wallet/db_sqlite3_sqlgen.c b/wallet/db_sqlite3_sqlgen.c index 4212510f4..ae8715eff 100644 --- a/wallet/db_sqlite3_sqlgen.c +++ b/wallet/db_sqlite3_sqlgen.c @@ -1695,9 +1695,9 @@ struct db_query db_sqlite3_queries[] = { .readonly = true, }, { - .name = "SELECT f.state, in_msatoshi, out_msatoshi, hin.payment_hash as payment_hash, in_channel_scid, out_channel_scid, f.received_time, f.resolved_time, f.failcode FROM forwarded_payments f LEFT JOIN channel_htlcs hin ON (f.in_htlc_id = hin.id)", - .query = "SELECT f.state, in_msatoshi, out_msatoshi, hin.payment_hash as payment_hash, in_channel_scid, out_channel_scid, f.received_time, f.resolved_time, f.failcode FROM forwarded_payments f LEFT JOIN channel_htlcs hin ON (f.in_htlc_id = hin.id)", - .placeholders = 0, + .name = "SELECT f.state, in_msatoshi, out_msatoshi, hin.payment_hash as payment_hash, in_channel_scid, out_channel_scid, f.received_time, f.resolved_time, f.failcode FROM forwarded_payments f LEFT JOIN channel_htlcs hin ON (f.in_htlc_id = hin.id) WHERE (1 = ? OR f.state = ?) AND (1 = ? OR f.in_channel_scid = ?) AND (1 = ? OR f.out_channel_scid = ?)", + .query = "SELECT f.state, in_msatoshi, out_msatoshi, hin.payment_hash as payment_hash, in_channel_scid, out_channel_scid, f.received_time, f.resolved_time, f.failcode FROM forwarded_payments f LEFT JOIN channel_htlcs hin ON (f.in_htlc_id = hin.id) WHERE (1 = ? OR f.state = ?) AND (1 = ? OR f.in_channel_scid = ?) AND (1 = ? OR f.out_channel_scid = ?)", + .placeholders = 6, .readonly = true, }, { @@ -1792,4 +1792,4 @@ struct db_query db_sqlite3_queries[] = { #endif /* LIGHTNINGD_WALLET_GEN_DB_SQLITE3 */ -// SHA256STAMP:759b19435d68328d597f6540e1f4f8c42ce22ef91dbf66c1098de0434462546c +// SHA256STAMP:4a878278ad7a1d427b80ff3a2abe34ae82cd73cf635ecf25e5cb20532c0604be diff --git a/wallet/statements_gettextgen.po b/wallet/statements_gettextgen.po index 5a7b1ac35..36342d2f9 100644 --- a/wallet/statements_gettextgen.po +++ b/wallet/statements_gettextgen.po @@ -1122,51 +1122,51 @@ msgstr "" msgid "SELECT CAST(COALESCE(SUM(in_msatoshi - out_msatoshi), 0) AS BIGINT)FROM forwarded_payments WHERE state = ?;" msgstr "" -#: wallet/wallet.c:3836 -msgid "SELECT f.state, in_msatoshi, out_msatoshi, hin.payment_hash as payment_hash, in_channel_scid, out_channel_scid, f.received_time, f.resolved_time, f.failcode FROM forwarded_payments f LEFT JOIN channel_htlcs hin ON (f.in_htlc_id = hin.id)" +#: wallet/wallet.c:3861 +msgid "SELECT f.state, in_msatoshi, out_msatoshi, hin.payment_hash as payment_hash, in_channel_scid, out_channel_scid, f.received_time, f.resolved_time, f.failcode FROM forwarded_payments f LEFT JOIN channel_htlcs hin ON (f.in_htlc_id = hin.id) WHERE (1 = ? OR f.state = ?) AND (1 = ? OR f.in_channel_scid = ?) AND (1 = ? OR f.out_channel_scid = ?)" msgstr "" -#: wallet/wallet.c:3924 +#: wallet/wallet.c:3983 msgid "SELECT t.id, t.rawtx, t.blockheight, t.txindex, t.type as txtype, c2.short_channel_id as txchan, a.location, a.idx as ann_idx, a.type as annotation_type, c.short_channel_id FROM transactions t LEFT JOIN transaction_annotations a ON (a.txid = t.id) LEFT JOIN channels c ON (a.channel = c.id) LEFT JOIN channels c2 ON (t.channel_id = c2.id) ORDER BY t.blockheight, t.txindex ASC" msgstr "" -#: wallet/wallet.c:4018 +#: wallet/wallet.c:4077 msgid "INSERT INTO penalty_bases ( channel_id, commitnum, txid, outnum, amount) VALUES (?, ?, ?, ?, ?);" msgstr "" -#: wallet/wallet.c:4043 +#: wallet/wallet.c:4102 msgid "SELECT commitnum, txid, outnum, amount FROM penalty_bases WHERE channel_id = ?" msgstr "" -#: wallet/wallet.c:4067 +#: wallet/wallet.c:4126 msgid "DELETE FROM penalty_bases WHERE channel_id = ? AND commitnum = ?" msgstr "" -#: wallet/wallet.c:4085 +#: wallet/wallet.c:4144 msgid "SELECT 1 FROM offers WHERE offer_id = ?;" msgstr "" -#: wallet/wallet.c:4098 +#: wallet/wallet.c:4157 msgid "INSERT INTO offers ( offer_id, bolt12, label, status) VALUES (?, ?, ?, ?);" msgstr "" -#: wallet/wallet.c:4125 +#: wallet/wallet.c:4184 msgid "SELECT bolt12, label, status FROM offers WHERE offer_id = ?;" msgstr "" -#: wallet/wallet.c:4153 +#: wallet/wallet.c:4212 msgid "SELECT offer_id FROM offers;" msgstr "" -#: wallet/wallet.c:4179 +#: wallet/wallet.c:4238 msgid "UPDATE offers SET status=? WHERE offer_id = ?;" msgstr "" -#: wallet/wallet.c:4190 +#: wallet/wallet.c:4249 msgid "UPDATE invoices SET state=? WHERE state=? AND local_offer_id = ?;" msgstr "" -#: wallet/wallet.c:4218 +#: wallet/wallet.c:4277 msgid "SELECT status FROM offers WHERE offer_id = ?;" msgstr "" @@ -1181,4 +1181,4 @@ msgstr "" #: wallet/test/run-wallet.c:1399 msgid "INSERT INTO channels (id) VALUES (1);" msgstr "" -# SHA256STAMP:c51857ae4385dd151d6a60f5ce99a5227a9a0cd01a66e0507bb81c04db07c39e +# SHA256STAMP:e50580171fbee6db0aa824ddc75693d353453c74f4d65c8ca4daf023d7858dc1 diff --git a/wallet/wallet.c b/wallet/wallet.c index c6ecf5f27..ba5761e25 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -10,13 +10,13 @@ #include #include #include +#include #include #include #include #include #include #include -#include #include #include #include @@ -3825,12 +3825,37 @@ struct amount_msat wallet_total_forward_fees(struct wallet *w) return total; } +bool string_to_forward_status(const char *status_str, enum forward_status *status) +{ + if (streq(status_str, "offered")) { + *status = FORWARD_OFFERED; + return true; + } else if (streq(status_str, "settled")) { + *status = FORWARD_SETTLED; + return true; + } else if (streq(status_str, "failed")) { + *status = FORWARD_FAILED; + return true; + } else if (streq(status_str, "local_failed")) { + *status = FORWARD_LOCAL_FAILED; + return true; + } + return false; +} + const struct forwarding *wallet_forwarded_payments_get(struct wallet *w, - const tal_t *ctx) + const tal_t *ctx, + enum forward_status status, + const struct short_channel_id *chan_in, + const struct short_channel_id *chan_out) { struct forwarding *results = tal_arr(ctx, struct forwarding, 0); size_t count = 0; struct db_stmt *stmt; + + // placeholder for any parameter, the value doesn't matter because it's discarded by sql + const int any = -1; + stmt = db_prepare_v2( w->db, SQL("SELECT" @@ -3844,7 +3869,41 @@ const struct forwarding *wallet_forwarded_payments_get(struct wallet *w, ", f.resolved_time" ", f.failcode " "FROM forwarded_payments f " - "LEFT JOIN channel_htlcs hin ON (f.in_htlc_id = hin.id)")); + "LEFT JOIN channel_htlcs hin ON (f.in_htlc_id = hin.id) " + "WHERE (1 = ? OR f.state = ?) AND " + "(1 = ? OR f.in_channel_scid = ?) AND " + "(1 = ? OR f.out_channel_scid = ?)")); + + if (status == FORWARD_ANY) { + // any status + db_bind_int(stmt, 0, 1); + db_bind_int(stmt, 1, any); + } else { + // specific forward status + db_bind_int(stmt, 0, 0); + db_bind_int(stmt, 1, status); + } + + if (chan_in) { + // specific in_channel + db_bind_int(stmt, 2, 0); + db_bind_u64(stmt, 3, chan_in->u64); + } else { + // any in_channel + db_bind_int(stmt, 2, 1); + db_bind_int(stmt, 3, any); + } + + if (chan_out) { + // specific out_channel + db_bind_int(stmt, 4, 0); + db_bind_u64(stmt, 5, chan_out->u64); + } else { + // any out_channel + db_bind_int(stmt, 4, 1); + db_bind_int(stmt, 5, any); + } + db_query_prepared(stmt); for (count=0; db_step(stmt); count++) { diff --git a/wallet/wallet.h b/wallet/wallet.h index 7ddf69ebe..eaff0adce 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -123,7 +124,11 @@ enum forward_status { FORWARD_OFFERED = 0, FORWARD_SETTLED = 1, FORWARD_FAILED = 2, - FORWARD_LOCAL_FAILED = 3 + FORWARD_LOCAL_FAILED = 3, + /* Special status used to express that we don't care in + * queries */ + FORWARD_ANY = 255 + }; static inline enum forward_status wallet_forward_status_in_db(enum forward_status s) @@ -141,6 +146,8 @@ static inline enum forward_status wallet_forward_status_in_db(enum forward_statu case FORWARD_LOCAL_FAILED: BUILD_ASSERT(FORWARD_LOCAL_FAILED == 3); return s; + case FORWARD_ANY: + break; } fatal("%s: %u is invalid", __func__, s); } @@ -156,10 +163,14 @@ static inline const char* forward_status_name(enum forward_status status) return "failed"; case FORWARD_LOCAL_FAILED: return "local_failed"; + case FORWARD_ANY: + return "any"; } abort(); } +bool string_to_forward_status(const char *status_str, enum forward_status *status); + struct forwarding { struct short_channel_id channel_in, channel_out; struct amount_msat msat_in, msat_out, fee; @@ -1259,7 +1270,10 @@ struct amount_msat wallet_total_forward_fees(struct wallet *w); * Retrieve a list of all forwarded_payments */ const struct forwarding *wallet_forwarded_payments_get(struct wallet *w, - const tal_t *ctx); + const tal_t *ctx, + enum forward_status state, + const struct short_channel_id *chan_in, + const struct short_channel_id *chan_out); /** * Load remote_ann_node_sig and remote_ann_bitcoin_sig