mirror of
https://github.com/aljazceru/lightning.git
synced 2026-02-21 22:14:20 +01:00
bkpr: pass the node_id over for channel_opens, add to account
it's nice to know what node your channel was opened with. in theory we could use listpeers to merge the data after the fact, except that channels disappear after they've been closed for a bit. it's better to just save the info. we print it out in `listbalances`, as that's a great place account level information
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include <common/json_param.h>
|
||||
#include <common/json_stream.h>
|
||||
#include <common/memleak.h>
|
||||
#include <common/node_id.h>
|
||||
#include <common/type_to_string.h>
|
||||
#include <db/exec.h>
|
||||
#include <errno.h>
|
||||
@@ -489,6 +490,12 @@ static struct command_result *json_list_balances(struct command *cmd,
|
||||
json_object_start(res, NULL);
|
||||
|
||||
json_add_string(res, "account", accts[i]->name);
|
||||
if (accts[i]->peer_id) {
|
||||
json_add_node_id(res, "peer_id", accts[i]->peer_id);
|
||||
json_add_bool(res, "account_resolved",
|
||||
accts[i]->onchain_resolved_block > 0);
|
||||
}
|
||||
|
||||
json_array_start(res, "balances");
|
||||
for (size_t j = 0; j < tal_count(balances); j++) {
|
||||
json_object_start(res, NULL);
|
||||
@@ -549,6 +556,18 @@ static bool new_missed_channel_account(struct command *cmd,
|
||||
assert(peer_arr_tok->type == JSMN_ARRAY);
|
||||
/* There should only be one peer */
|
||||
json_for_each_arr(i, curr_peer, peer_arr_tok) {
|
||||
const char *err;
|
||||
struct node_id peer_id;
|
||||
|
||||
err = json_scan(cmd, buf, curr_peer, "{id:%}",
|
||||
JSON_SCAN(json_to_node_id, &peer_id));
|
||||
|
||||
if (err)
|
||||
plugin_err(cmd->plugin,
|
||||
"failure scanning listpeer"
|
||||
" result: %s", err);
|
||||
|
||||
json_get_member(buf, curr_peer, "id");
|
||||
chan_arr_tok = json_get_member(buf, curr_peer,
|
||||
"channels");
|
||||
assert(chan_arr_tok->type == JSMN_ARRAY);
|
||||
@@ -557,7 +576,6 @@ static bool new_missed_channel_account(struct command *cmd,
|
||||
struct amount_msat amt, remote_amt, push_amt,
|
||||
push_credit, push_debit;
|
||||
char *opener, *chan_id;
|
||||
const char *err;
|
||||
enum mvt_tag *tags;
|
||||
bool ok;
|
||||
|
||||
@@ -624,7 +642,8 @@ static bool new_missed_channel_account(struct command *cmd,
|
||||
chain_ev->credit = amt;
|
||||
db_begin_transaction(db);
|
||||
log_chain_event(db, acct, chain_ev);
|
||||
maybe_update_account(db, acct, chain_ev, tags, 0);
|
||||
maybe_update_account(db, acct, chain_ev,
|
||||
tags, 0, &peer_id);
|
||||
maybe_update_onchain_fees(cmd, db, &opt.txid);
|
||||
|
||||
/* We won't count the close's fees if we're
|
||||
@@ -1120,6 +1139,7 @@ parse_and_log_chain_move(struct command *cmd,
|
||||
struct chain_event *e = tal(cmd, struct chain_event);
|
||||
struct sha256 *payment_hash = tal(cmd, struct sha256);
|
||||
struct bitcoin_txid *spending_txid = tal(cmd, struct bitcoin_txid);
|
||||
struct node_id *peer_id;
|
||||
struct account *acct;
|
||||
u32 closed_count;
|
||||
const char *err;
|
||||
@@ -1180,6 +1200,17 @@ parse_and_log_chain_move(struct command *cmd,
|
||||
err = tal_free(err);
|
||||
}
|
||||
|
||||
peer_id = tal(cmd, struct node_id);
|
||||
err = json_scan(tmpctx, buf, params,
|
||||
"{coin_movement:"
|
||||
"{peer_id:%}}",
|
||||
JSON_SCAN(json_to_node_id, peer_id));
|
||||
|
||||
if (err) {
|
||||
peer_id = tal_free(peer_id);
|
||||
err = tal_free(err);
|
||||
}
|
||||
|
||||
err = json_scan(tmpctx, buf, params,
|
||||
"{coin_movement:"
|
||||
"{output_count:%}}",
|
||||
@@ -1219,7 +1250,8 @@ parse_and_log_chain_move(struct command *cmd,
|
||||
|
||||
/* This event *might* have implications for account;
|
||||
* update as necessary */
|
||||
maybe_update_account(db, acct, e, tags, closed_count);
|
||||
maybe_update_account(db, acct, e, tags, closed_count,
|
||||
peer_id);
|
||||
|
||||
/* Can we calculate any onchain fees now? */
|
||||
err = maybe_update_onchain_fees(cmd, db,
|
||||
|
||||
@@ -1152,7 +1152,8 @@ void maybe_update_account(struct db *db,
|
||||
struct account *acct,
|
||||
struct chain_event *e,
|
||||
const enum mvt_tag *tags,
|
||||
u32 closed_count)
|
||||
u32 closed_count,
|
||||
struct node_id *peer_id)
|
||||
{
|
||||
struct db_stmt *stmt;
|
||||
bool updated = false;
|
||||
@@ -1200,6 +1201,11 @@ void maybe_update_account(struct db *db,
|
||||
}
|
||||
}
|
||||
|
||||
if (peer_id) {
|
||||
updated = true;
|
||||
acct->peer_id = tal_dup(acct, struct node_id, peer_id);
|
||||
}
|
||||
|
||||
if (closed_count > 0) {
|
||||
updated = true;
|
||||
acct->closed_count = closed_count;
|
||||
@@ -1216,6 +1222,7 @@ void maybe_update_account(struct db *db,
|
||||
", we_opened = ?"
|
||||
", leased = ?"
|
||||
", closed_count = ?"
|
||||
", peer_id = ?"
|
||||
" WHERE"
|
||||
" name = ?"));
|
||||
|
||||
@@ -1232,8 +1239,12 @@ void maybe_update_account(struct db *db,
|
||||
db_bind_int(stmt, 2, acct->we_opened ? 1 : 0);
|
||||
db_bind_int(stmt, 3, acct->leased ? 1 : 0);
|
||||
db_bind_int(stmt, 4, acct->closed_count);
|
||||
if (acct->peer_id)
|
||||
db_bind_node_id(stmt, 5, acct->peer_id);
|
||||
else
|
||||
db_bind_null(stmt, 5);
|
||||
|
||||
db_bind_text(stmt, 5, acct->name);
|
||||
db_bind_text(stmt, 6, acct->name);
|
||||
|
||||
db_exec_prepared_v2(take(stmt));
|
||||
}
|
||||
|
||||
@@ -164,7 +164,8 @@ void maybe_update_account(struct db *db,
|
||||
struct account *acct,
|
||||
struct chain_event *e,
|
||||
const enum mvt_tag *tags,
|
||||
u32 closed_count);
|
||||
u32 closed_count,
|
||||
struct node_id *peer_id);
|
||||
|
||||
/* Update our onchain fees now? */
|
||||
char *maybe_update_onchain_fees(const tal_t *ctx,
|
||||
|
||||
@@ -251,7 +251,9 @@ static bool accountseq(struct account *a1, struct account *a2)
|
||||
{
|
||||
CHECK(a1->db_id == a2->db_id);
|
||||
CHECK(streq(a1->name, a2->name));
|
||||
CHECK(node_id_eq(a1->peer_id, a2->peer_id));
|
||||
CHECK((a1->peer_id != NULL) == (a2->peer_id != NULL));
|
||||
if (a1->peer_id)
|
||||
CHECK(node_id_eq(a1->peer_id, a2->peer_id));
|
||||
CHECK(a1->is_wallet == a2->is_wallet);
|
||||
CHECK(a1->we_opened == a2->we_opened);
|
||||
CHECK(a1->leased == a2->leased);
|
||||
@@ -507,7 +509,7 @@ static bool test_onchain_fee_chan_close(const tal_t *ctx, struct plugin *p)
|
||||
'X', 0, '*');
|
||||
log_chain_event(db, acct, ev);
|
||||
tags[0] = CHANNEL_OPEN;
|
||||
maybe_update_account(db, acct, ev, tags, 0);
|
||||
maybe_update_account(db, acct, ev, tags, 0, NULL);
|
||||
|
||||
ev = make_chain_event(ctx, "channel_close",
|
||||
AMOUNT_MSAT(0),
|
||||
@@ -519,7 +521,7 @@ static bool test_onchain_fee_chan_close(const tal_t *ctx, struct plugin *p)
|
||||
|
||||
/* Update the account to have the right info! */
|
||||
tags[0] = CHANNEL_CLOSE;
|
||||
maybe_update_account(db, acct, ev, tags, close_output_count);
|
||||
maybe_update_account(db, acct, ev, tags, close_output_count, NULL);
|
||||
|
||||
log_chain_event(db, acct,
|
||||
make_chain_event(ctx, "delayed_to_us",
|
||||
@@ -1169,15 +1171,16 @@ static bool test_account_balances(const tal_t *ctx, struct plugin *p)
|
||||
static bool test_account_crud(const tal_t *ctx, struct plugin *p)
|
||||
{
|
||||
struct db *db = db_setup(ctx, p, tmp_dsn(ctx));
|
||||
struct node_id peer_id;
|
||||
struct node_id *peer_id;
|
||||
struct account *acct, *acct2, **acct_list;
|
||||
struct chain_event *ev1;
|
||||
enum mvt_tag *tags;
|
||||
char *name = tal_fmt(ctx, "example");
|
||||
|
||||
memset(&peer_id, 3, sizeof(struct node_id));
|
||||
peer_id = tal(ctx, struct node_id);
|
||||
memset(peer_id, 3, sizeof(struct node_id));
|
||||
|
||||
acct = new_account(ctx, name, &peer_id);
|
||||
acct = new_account(ctx, name, NULL);
|
||||
CHECK(!acct->is_wallet);
|
||||
|
||||
db_begin_transaction(db);
|
||||
@@ -1192,7 +1195,7 @@ static bool test_account_crud(const tal_t *ctx, struct plugin *p)
|
||||
CHECK(tal_count(acct_list) == 1);
|
||||
accountseq(acct_list[0], acct);
|
||||
|
||||
acct = new_account(ctx, tal_fmt(ctx, "wallet"), &peer_id);
|
||||
acct = new_account(ctx, tal_fmt(ctx, "wallet"), NULL);
|
||||
CHECK(acct->is_wallet);
|
||||
|
||||
db_begin_transaction(db);
|
||||
@@ -1239,7 +1242,7 @@ static bool test_account_crud(const tal_t *ctx, struct plugin *p)
|
||||
/* should not update the account info */
|
||||
tags[0] = PUSHED;
|
||||
tags[1] = PENALTY;
|
||||
maybe_update_account(db, acct, ev1, tags, 0);
|
||||
maybe_update_account(db, acct, ev1, tags, 0, peer_id);
|
||||
acct2 = find_account(ctx, db, "wallet");
|
||||
accountseq(acct, acct2);
|
||||
|
||||
@@ -1248,7 +1251,7 @@ static bool test_account_crud(const tal_t *ctx, struct plugin *p)
|
||||
CHECK(acct->open_event_db_id == NULL);
|
||||
tags[0] = CHANNEL_OPEN;
|
||||
tags[1] = LEASED;
|
||||
maybe_update_account(db, acct, ev1, tags, 2);
|
||||
maybe_update_account(db, acct, ev1, tags, 2, peer_id);
|
||||
acct2 = find_account(ctx, db, "wallet");
|
||||
accountseq(acct, acct2);
|
||||
CHECK(acct->leased);
|
||||
@@ -1259,7 +1262,7 @@ static bool test_account_crud(const tal_t *ctx, struct plugin *p)
|
||||
tags[1] = OPENER;
|
||||
CHECK(acct->closed_event_db_id == NULL);
|
||||
CHECK(!acct->we_opened);
|
||||
maybe_update_account(db, acct, ev1, tags, 0);
|
||||
maybe_update_account(db, acct, ev1, tags, 0, NULL);
|
||||
acct2 = find_account(ctx, db, "wallet");
|
||||
accountseq(acct, acct2);
|
||||
CHECK(acct->closed_event_db_id != NULL);
|
||||
|
||||
Reference in New Issue
Block a user