Added in_channel,out_channel,state params to listforwards

This commit is contained in:
Karol Hosiawa 2021-01-23 18:18:34 +01:00 committed by Rusty Russell
parent 8af5764a9c
commit 8a8f81175d
6 changed files with 124 additions and 34 deletions

View File

@ -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; i<tal_count(forwardings); i++) {
@ -2617,13 +2621,27 @@ static struct command_result *json_listforwards(struct command *cmd,
const jsmntok_t *obj UNNEEDED,
const jsmntok_t *params)
{
struct json_stream *response;
if (!param(cmd, buffer, params, NULL))
struct short_channel_id *chan_in;
struct short_channel_id *chan_out;
const char *status_str;
enum forward_status status = FORWARD_ANY;
if (!param(cmd, buffer, params,
p_opt("in_channel", param_short_channel_id, &chan_in),
p_opt("out_channel", param_short_channel_id, &chan_out),
p_opt("status", param_string, &status_str),
NULL))
return command_param_failed();
if (status_str && !string_to_forward_status(status_str, &status))
return command_fail(cmd, JSONRPC2_INVALID_PARAMS, "Unrecognized status: %s", status_str);
response = json_stream_success(cmd);
listforwardings_add_forwardings(response, cmd->ld->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);

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -10,13 +10,13 @@
#include <common/key_derive.h>
#include <common/memleak.h>
#include <common/onionreply.h>
#include <common/status.h>
#include <common/wireaddr.h>
#include <inttypes.h>
#include <lightningd/coin_mvts.h>
#include <lightningd/lightningd.h>
#include <lightningd/notification.h>
#include <lightningd/peer_control.h>
#include <lightningd/peer_htlcs.h>
#include <onchaind/onchaind_wiregen.h>
#include <string.h>
#include <wallet/db_common.h>
@ -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++) {

View File

@ -18,6 +18,7 @@
#include <lightningd/htlc_end.h>
#include <lightningd/invoice.h>
#include <lightningd/log.h>
#include <lightningd/peer_htlcs.h>
#include <onchaind/onchaind_wire.h>
#include <wally_bip32.h>
#include <wire/onion_wire.h>
@ -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