Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove db accesses in listpeerchannels #7679

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 37 additions & 6 deletions lightningd/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ struct channel *new_unsaved_channel(struct peer *peer,
channel->ignore_fee_limits = ld->config.ignore_fee_limits;
channel->last_stable_connection = 0;
channel->stable_conn_timer = NULL;
/* Nothing happened yet */
memset(&channel->stats, 0, sizeof(channel->stats));
channel->state_changes = tal_arr(channel, struct channel_state_change *, 0);

/* No shachain yet */
channel->their_shachain.id = 0;
Expand Down Expand Up @@ -445,7 +448,9 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
bool ignore_fee_limits,
/* NULL or stolen */
struct peer_update *peer_update STEALS,
u64 last_stable_connection)
u64 last_stable_connection,
const struct channel_stats *stats,
struct channel_state_change **state_changes STEALS)
{
struct channel *channel = tal(peer->ld, struct channel);
struct amount_msat htlc_min, htlc_max;
Expand Down Expand Up @@ -602,6 +607,9 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
channel->ignore_fee_limits = ignore_fee_limits;
channel->last_stable_connection = last_stable_connection;
channel->stable_conn_timer = NULL;
channel->stats = *stats;
channel->state_changes = tal_steal(channel, state_changes);

/* Populate channel->channel_gossip */
channel_gossip_init(channel, take(peer_update));

Expand Down Expand Up @@ -826,14 +834,28 @@ void channel_set_last_tx(struct channel *channel,
channel->last_tx = tal_steal(channel, tx);
}

struct channel_state_change *new_channel_state_change(const tal_t *ctx,
struct timeabs timestamp,
enum channel_state old_state,
enum channel_state new_state,
enum state_change cause,
const char *message TAKES)
{
struct channel_state_change *c = tal(ctx, struct channel_state_change);
c->timestamp = timestamp;
c->old_state = old_state;
c->new_state = new_state;
c->cause = cause;
c->message = tal_strdup(c, message);
return c;
}

void channel_set_state(struct channel *channel,
enum channel_state old_state,
enum channel_state state,
enum state_change reason,
char *why)
{
struct timeabs timestamp;

/* set closer, if known */
if (channel_state_closing(state) && channel->closer == NUM_SIDES) {
if (reason == REASON_LOCAL) channel->closer = LOCAL;
Expand Down Expand Up @@ -861,10 +883,19 @@ void channel_set_state(struct channel *channel,

/* plugin notification channel_state_changed and DB entry */
if (state != old_state) { /* see issue #4029 */
timestamp = time_now();
struct channel_state_change *change;

change = new_channel_state_change(channel->state_changes,
time_now(),
old_state,
state,
reason,
why);
tal_arr_expand(&channel->state_changes, change);

wallet_state_change_add(channel->peer->ld->wallet,
channel->dbid,
timestamp,
change->timestamp,
old_state,
state,
reason,
Expand All @@ -873,7 +904,7 @@ void channel_set_state(struct channel *channel,
&channel->peer->id,
&channel->cid,
channel->scid,
timestamp,
change->timestamp,
old_state,
state,
reason,
Expand Down
34 changes: 33 additions & 1 deletion lightningd/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,23 @@ struct open_attempt {
const u8 *open_msg;
};

/* Statistics for a channel */
struct channel_stats {
u64 in_payments_offered, in_payments_fulfilled;
struct amount_msat in_msatoshi_offered, in_msatoshi_fulfilled;
u64 out_payments_offered, out_payments_fulfilled;
struct amount_msat out_msatoshi_offered, out_msatoshi_fulfilled;
};


struct channel_state_change {
struct timeabs timestamp;
enum channel_state old_state;
enum channel_state new_state;
enum state_change cause;
const char *message;
};

struct channel {
/* Inside peer->channels. */
struct list_node list;
Expand Down Expand Up @@ -314,6 +331,12 @@ struct channel {
/* Last time we had a stable connection, if any (0 = none) */
u64 last_stable_connection;
struct oneshot *stable_conn_timer;

/* Our stats */
struct channel_stats stats;

/* Our change history. */
struct channel_state_change **state_changes;
};

/* Is channel owned (and should be talking to peer) */
Expand Down Expand Up @@ -395,7 +418,9 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
bool ignore_fee_limits,
/* NULL or stolen */
struct peer_update *peer_update STEALS,
u64 last_stable_connection);
u64 last_stable_connection,
const struct channel_stats *stats,
struct channel_state_change **state_changes STEALS);

/* new_inflight - Create a new channel_inflight for a channel */
struct channel_inflight *new_inflight(struct channel *channel,
Expand All @@ -415,6 +440,13 @@ struct channel_inflight *new_inflight(struct channel *channel,
bool i_am_initiator,
bool force_sign_first);

struct channel_state_change *new_channel_state_change(const tal_t *ctx,
struct timeabs timestamp,
enum channel_state old_state,
enum channel_state new_state,
enum state_change cause,
const char *message TAKES);

/* Add a last_tx and sig to an inflight */
void inflight_set_last_tx(struct channel_inflight *inflight,
struct bitcoin_tx *last_tx STEALS,
Expand Down
9 changes: 0 additions & 9 deletions lightningd/channel_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,4 @@ enum state_change {
/* Note: This is very likely a conscious remote decision. */
REASON_ONCHAIN
};

struct state_change_entry {
struct timeabs timestamp;
enum channel_state old_state;
enum channel_state new_state;
enum state_change cause;
char *message;
};

#endif /* LIGHTNING_LIGHTNINGD_CHANNEL_STATE_H */
15 changes: 13 additions & 2 deletions lightningd/opening_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ wallet_commit_channel(struct lightningd *ld,
u32 lease_start_blockheight = 0; /* No leases on v1 */
struct timeabs timestamp;
bool any_active = peer_any_channel(uc->peer, channel_state_wants_peercomms, NULL);
struct channel_stats zero_channel_stats;

/* We can't have any payments yet */
memset(&zero_channel_stats, 0, sizeof(zero_channel_stats));

/* We cannot both be the fundee *and* have a `fundchannel_start`
* command running!
Expand Down Expand Up @@ -215,7 +219,9 @@ wallet_commit_channel(struct lightningd *ld,
ld->config.htlc_maximum_msat,
ld->config.ignore_fee_limits,
NULL,
0);
0,
&zero_channel_stats,
tal_arr(NULL, struct channel_state_change *, 0));

/* Now we finally put it in the database. */
wallet_channel_insert(ld->wallet, channel);
Expand Down Expand Up @@ -1456,6 +1462,7 @@ static struct channel *stub_chan(struct command *cmd,
struct short_channel_id *scid;
u32 blockht;
u32 feerate;
struct channel_stats zero_channel_stats;
u8 *dummy_sig = tal_hexdata(cmd,
"30450221009b2e0eef267b94c3899fb0dc73750"
"12e2cee4c10348a068fe78d1b82b4b1403602207"
Expand Down Expand Up @@ -1530,6 +1537,8 @@ static struct channel *stub_chan(struct command *cmd,
if (!mk_short_channel_id(scid, 1, 1, 1))
fatal("Failed to make short channel 1x1x1!");

memset(&zero_channel_stats, 0, sizeof(zero_channel_stats));

/* Channel Shell with Dummy data(mostly) */
channel = new_channel(peer, id,
NULL, /* No shachain yet */
Expand Down Expand Up @@ -1589,7 +1598,9 @@ static struct channel *stub_chan(struct command *cmd,
ld->config.htlc_maximum_msat,
false,
NULL,
0);
0,
&zero_channel_stats,
tal_arr(NULL, struct channel_state_change *, 0));

/* We don't want to gossip about this, ever. */
channel->channel_gossip = tal_free(channel->channel_gossip);
Expand Down
35 changes: 16 additions & 19 deletions lightningd/peer_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -818,10 +818,8 @@ static void NON_NULL_ARGS(1, 2, 4, 5) json_add_channel(struct command *cmd,
const struct peer *peer)
{
struct lightningd *ld = cmd->ld;
struct channel_stats channel_stats;
struct amount_msat funding_msat;
struct amount_sat peer_funded_sats;
struct state_change_entry *state_changes;
const struct peer_update *peer_update;
u32 feerate;

Expand Down Expand Up @@ -1162,19 +1160,19 @@ static void NON_NULL_ARGS(1, 2, 4, 5) json_add_channel(struct command *cmd,
json_add_num(response, "max_accepted_htlcs",
channel->our_config.max_accepted_htlcs);

state_changes = wallet_state_change_get(tmpctx, ld->wallet, channel->dbid);
json_array_start(response, "state_changes");
for (size_t i = 0; i < tal_count(state_changes); i++) {
for (size_t i = 0; i < tal_count(channel->state_changes); i++) {
const struct channel_state_change *change
= channel->state_changes[i];
json_object_start(response, NULL);
json_add_timeiso(response, "timestamp",
state_changes[i].timestamp);
json_add_timeiso(response, "timestamp", change->timestamp);
json_add_string(response, "old_state",
channel_state_str(state_changes[i].old_state));
channel_state_str(change->old_state));
json_add_string(response, "new_state",
channel_state_str(state_changes[i].new_state));
channel_state_str(change->new_state));
json_add_string(response, "cause",
channel_change_state_reason_str(state_changes[i].cause));
json_add_string(response, "message", state_changes[i].message);
channel_change_state_reason_str(change->cause));
json_add_string(response, "message", change->message);
json_object_end(response);
}
json_array_end(response);
Expand All @@ -1191,27 +1189,26 @@ static void NON_NULL_ARGS(1, 2, 4, 5) json_add_channel(struct command *cmd,
json_array_end(response);

/* Provide channel statistics */
wallet_channel_stats_load(ld->wallet, channel->dbid, &channel_stats);
json_add_u64(response, "in_payments_offered",
channel_stats.in_payments_offered);
channel->stats.in_payments_offered);
json_add_amount_msat(response,
"in_offered_msat",
channel_stats.in_msatoshi_offered);
channel->stats.in_msatoshi_offered);
json_add_u64(response, "in_payments_fulfilled",
channel_stats.in_payments_fulfilled);
channel->stats.in_payments_fulfilled);
json_add_amount_msat(response,
"in_fulfilled_msat",
channel_stats.in_msatoshi_fulfilled);
channel->stats.in_msatoshi_fulfilled);
json_add_u64(response, "out_payments_offered",
channel_stats.out_payments_offered);
channel->stats.out_payments_offered);
json_add_amount_msat(response,
"out_offered_msat",
channel_stats.out_msatoshi_offered);
channel->stats.out_msatoshi_offered);
json_add_u64(response, "out_payments_fulfilled",
channel_stats.out_payments_fulfilled);
channel->stats.out_payments_fulfilled);
json_add_amount_msat(response,
"out_fulfilled_msat",
channel_stats.out_msatoshi_fulfilled);
channel->stats.out_msatoshi_fulfilled);

json_add_htlcs(ld, response, channel);
json_object_end(response);
Expand Down
58 changes: 46 additions & 12 deletions lightningd/peer_htlcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,48 @@ static bool htlc_out_update_state(struct channel *channel,
return true;
}

static void cstat_accumulate(struct channel *c,
struct amount_msat *val,
struct amount_msat msat)
{
if (!amount_msat_add(val, *val, msat))
log_broken(c->log, "Adding %s to stat %s overflowed!",
fmt_amount_msat(tmpctx, *val),
fmt_amount_msat(tmpctx, msat));
}

static void channel_stats_incr_in_fulfilled(struct channel *c,
struct amount_msat msat)
{
c->stats.in_payments_fulfilled++;
cstat_accumulate(c, &c->stats.in_msatoshi_fulfilled, msat);
wallet_channel_stats_incr_in_fulfilled(c->peer->ld->wallet, c->dbid, msat);
}

static void channel_stats_incr_out_fulfilled(struct channel *c,
struct amount_msat msat)
{
c->stats.out_payments_fulfilled++;
cstat_accumulate(c, &c->stats.out_msatoshi_fulfilled, msat);
wallet_channel_stats_incr_out_fulfilled(c->peer->ld->wallet, c->dbid, msat);
}

static void channel_stats_incr_in_offered(struct channel *c,
struct amount_msat msat)
{
c->stats.in_payments_offered++;
cstat_accumulate(c, &c->stats.in_msatoshi_offered, msat);
wallet_channel_stats_incr_in_offered(c->peer->ld->wallet, c->dbid, msat);
}

static void channel_stats_incr_out_offered(struct channel *c,
struct amount_msat msat)
{
c->stats.out_payments_offered++;
cstat_accumulate(c, &c->stats.out_msatoshi_offered, msat);
wallet_channel_stats_incr_out_offered(c->peer->ld->wallet, c->dbid, msat);
}

/* BOLT #4:
* - if `blinding_point` is set in the incoming `update_add_htlc`:
* - MUST return an `invalid_onion_blinding` error.
Expand Down Expand Up @@ -332,7 +374,6 @@ void fulfill_htlc(struct htlc_in *hin, const struct preimage *preimage)
{
u8 *msg;
struct channel *channel = hin->key.channel;
struct wallet *wallet = channel->peer->ld->wallet;

if (hin->hstate != RCVD_ADD_ACK_REVOCATION) {
log_debug(channel->log,
Expand All @@ -349,9 +390,7 @@ void fulfill_htlc(struct htlc_in *hin, const struct preimage *preimage)
htlc_in_check(hin, __func__);

/* Update channel stats */
wallet_channel_stats_incr_in_fulfilled(wallet,
channel->dbid,
hin->msat);
channel_stats_incr_in_fulfilled(channel, hin->msat);

/* No owner? We'll either send to channeld in peer_htlcs, or
* onchaind in onchaind_tell_fulfill. */
Expand Down Expand Up @@ -1417,9 +1456,7 @@ static void fulfill_our_htlc_out(struct channel *channel, struct htlc_out *hout,
0, hout->failonion,
hout->failmsg, &we_filled);
/* Update channel stats */
wallet_channel_stats_incr_out_fulfilled(ld->wallet,
channel->dbid,
hout->msat);
channel_stats_incr_out_fulfilled(channel, hout->msat);

if (hout->am_origin)
payment_succeeded(ld, &hout->payment_hash, hout->partid, hout->groupid, preimage);
Expand Down Expand Up @@ -1892,9 +1929,7 @@ static bool update_out_htlc(struct channel *channel,
if (!hout->dbid) {
wallet_htlc_save_out(ld->wallet, channel, hout);
/* Update channel stats */
wallet_channel_stats_incr_out_offered(ld->wallet,
channel->dbid,
hout->msat);
channel_stats_incr_out_offered(channel, hout->msat);

if (hout->in) {
struct short_channel_id scid;
Expand Down Expand Up @@ -2131,8 +2166,7 @@ static bool channel_added_their_htlc(struct channel *channel,
/* Save an incoming htlc to the wallet */
wallet_htlc_save_in(ld->wallet, channel, hin);
/* Update channel stats */
wallet_channel_stats_incr_in_offered(ld->wallet, channel->dbid,
added->amount);
channel_stats_incr_in_offered(channel, added->amount);

log_debug(channel->log, "Adding their HTLC %"PRIu64, added->id);
connect_htlc_in(channel->peer->ld->htlcs_in, hin);
Expand Down
Loading
Loading