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

Add a status field to listpeers indicating where an HTLC is currently being held #4580

Merged
merged 4 commits into from
Jun 5, 2021
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
18 changes: 10 additions & 8 deletions lightningd/dual_open_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ struct rbf_channel_payload {
char *err_msg;
};

static void
rbf_channel_hook_serialize(struct rbf_channel_payload *payload,
struct json_stream *stream)
static void rbf_channel_hook_serialize(struct rbf_channel_payload *payload,
struct json_stream *stream,
struct plugin *plugin)
{
json_object_start(stream, "rbf_channel");
json_add_node_id(stream, "id", &payload->peer_id);
Expand Down Expand Up @@ -251,9 +251,9 @@ struct openchannel2_payload {
char *err_msg;
};

static void
openchannel2_hook_serialize(struct openchannel2_payload *payload,
struct json_stream *stream)
static void openchannel2_hook_serialize(struct openchannel2_payload *payload,
struct json_stream *stream,
struct plugin *plugin)
{
json_object_start(stream, "openchannel2");
json_add_node_id(stream, "id", &payload->peer_id);
Expand Down Expand Up @@ -295,7 +295,8 @@ struct openchannel2_psbt_payload {

static void
openchannel2_changed_hook_serialize(struct openchannel2_psbt_payload *payload,
struct json_stream *stream)
struct json_stream *stream,
struct plugin *plugin)
{
json_object_start(stream, "openchannel2_changed");
json_add_psbt(stream, "psbt", payload->psbt);
Expand All @@ -307,7 +308,8 @@ openchannel2_changed_hook_serialize(struct openchannel2_psbt_payload *payload,

static void
openchannel2_sign_hook_serialize(struct openchannel2_psbt_payload *payload,
struct json_stream *stream)
struct json_stream *stream,
struct plugin *plugin)
{
json_object_start(stream, "openchannel2_sign");
json_add_psbt(stream, "psbt", payload->psbt);
Expand Down
1 change: 1 addition & 0 deletions lightningd/htlc_end.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ struct htlc_in *new_htlc_in(const tal_t *ctx,
hin->msat = msat;
hin->cltv_expiry = cltv_expiry;
hin->payment_hash = *payment_hash;
hin->status = NULL;
if (shared_secret)
hin->shared_secret = tal_dup(hin, struct secret, shared_secret);
else
Expand Down
3 changes: 3 additions & 0 deletions lightningd/htlc_end.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ struct htlc_in {
struct secret blinding_ss;
/* true if we supplied the preimage */
bool *we_filled;

/* A simple text annotation shown in `listpeers` */
char *status;
Comment on lines +58 to +60
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be in the wrong commit?

};

struct htlc_out {
Expand Down
3 changes: 2 additions & 1 deletion lightningd/invoice.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ struct invoice_payment_hook_payload {

static void
invoice_payment_serialize(struct invoice_payment_hook_payload *payload,
struct json_stream *stream)
struct json_stream *stream,
struct plugin *plugin)
{
json_object_start(stream, "payment");
json_add_escaped_string(stream, "label", payload->label);
Expand Down
3 changes: 2 additions & 1 deletion lightningd/jsonrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,8 @@ struct rpc_command_hook_payload {
};

static void rpc_command_hook_serialize(struct rpc_command_hook_payload *p,
struct json_stream *s)
struct json_stream *s,
struct plugin *plugin)
{
const jsmntok_t *tok;
size_t i;
Expand Down
6 changes: 3 additions & 3 deletions lightningd/onion_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ struct onion_message_hook_payload {
struct tlv_onionmsg_payload *om;
};

static void
onion_message_serialize(struct onion_message_hook_payload *payload,
struct json_stream *stream)
static void onion_message_serialize(struct onion_message_hook_payload *payload,
struct json_stream *stream,
struct plugin *plugin)
{
json_object_start(stream, "onion_message");
if (payload->blinding_in)
Expand Down
6 changes: 3 additions & 3 deletions lightningd/opening_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -615,9 +615,9 @@ struct openchannel_hook_payload {
char *errmsg;
};

static void
openchannel_hook_serialize(struct openchannel_hook_payload *payload,
struct json_stream *stream)
static void openchannel_hook_serialize(struct openchannel_hook_payload *payload,
struct json_stream *stream,
struct plugin *plugin)
{
struct uncommitted_channel *uc = payload->openingd->channel;
json_object_start(stream, "openchannel");
Expand Down
7 changes: 5 additions & 2 deletions lightningd/peer_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,8 @@ static void json_add_htlcs(struct lightningd *ld,
channel->our_config.dust_limit, LOCAL,
channel->option_anchor_outputs))
json_add_bool(response, "local_trimmed", true);
if (hin->status != NULL)
json_add_string(response, "status", hin->status);
json_object_end(response);
}

Expand Down Expand Up @@ -1039,7 +1041,7 @@ struct peer_connected_hook_payload {

static void
peer_connected_serialize(struct peer_connected_hook_payload *payload,
struct json_stream *stream)
struct json_stream *stream, struct plugin *plugin)
{
const struct peer *p = payload->peer;
json_object_start(stream, "peer");
Expand Down Expand Up @@ -2782,7 +2784,8 @@ static void custommsg_final(struct custommsg_payload *payload STEALS)
}

static void custommsg_payload_serialize(struct custommsg_payload *payload,
struct json_stream *stream)
struct json_stream *stream,
struct plugin *plugin)
{
/* Backward compat for broken custommsg: if we get a custommsg
* from an old c-lightning node, then we must identify and
Expand Down
16 changes: 13 additions & 3 deletions lightningd/peer_htlcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1011,11 +1011,18 @@ static bool htlc_accepted_hook_deserialize(struct htlc_accepted_hook_payload *re
}

static void htlc_accepted_hook_serialize(struct htlc_accepted_hook_payload *p,
struct json_stream *s)
struct json_stream *s,
struct plugin *plugin)
{
const struct route_step *rs = p->route_step;
const struct htlc_in *hin = p->hin;
struct htlc_in *hin = p->hin;
s32 expiry = hin->cltv_expiry, blockheight = p->ld->topology->tip->height;

tal_free(hin->status);
hin->status =
tal_fmt(hin, "Waiting for the htlc_accepted hook of plugin %s",
plugin->shortname);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You presumably should initialize this to NULL everywhere hin is allocated?

json_object_start(s, "onion");

json_add_hex_talarr(s, "payload", rs->raw_payload);
Expand Down Expand Up @@ -1067,6 +1074,8 @@ htlc_accepted_hook_final(struct htlc_accepted_hook_payload *request STEALS)
struct htlc_in *hin = request->hin;
struct channel *channel = request->channel;

request->hin->status = tal_free(request->hin->status);

/* *Now* we barf if it failed to decode */
if (!request->payload) {
log_debug(channel->log,
Expand Down Expand Up @@ -2034,7 +2043,8 @@ struct commitment_revocation_payload {
};

static void commitment_revocation_hook_serialize(
struct commitment_revocation_payload *payload, struct json_stream *stream)
struct commitment_revocation_payload *payload, struct json_stream *stream,
struct plugin *plugin)
{
json_add_txid(stream, "commitment_txid", &payload->commitment_txid);
json_add_tx(stream, "penalty_tx", payload->penalty_tx);
Expand Down
7 changes: 6 additions & 1 deletion lightningd/plugin_hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ static void plugin_hook_callback(const char *buffer, const jsmntok_t *toks,
struct plugin_hook_call_link *last, *it;
bool in_transaction = false;

log_debug(r->ld->log, "Plugin %s returned from %s hook call",
r->plugin->shortname, r->hook->name);

if (r->ld->state == LD_STATE_SHUTDOWN) {
log_debug(r->ld->log,
"Abandoning plugin hook call due to shutdown");
Expand Down Expand Up @@ -227,12 +230,14 @@ static void plugin_hook_call_next(struct plugin_hook_request *ph_req)
assert(!list_empty(&ph_req->call_chain));
ph_req->plugin = list_top(&ph_req->call_chain, struct plugin_hook_call_link, list)->plugin;

log_debug(ph_req->ld->log, "Calling %s hook of plugin %s",
ph_req->hook->name, ph_req->plugin->shortname);
req = jsonrpc_request_start(NULL, hook->name,
plugin_get_log(ph_req->plugin),
NULL,
plugin_hook_callback, ph_req);

hook->serialize_payload(ph_req->cb_arg, req->stream);
hook->serialize_payload(ph_req->cb_arg, req->stream, ph_req->plugin);
jsonrpc_request_end(req);
plugin_request_send(ph_req->plugin, req);
}
Expand Down
16 changes: 8 additions & 8 deletions lightningd/plugin_hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ struct plugin_hook {
void (*final_cb)(void *arg);

/* To send the payload to the plugin */
void (*serialize_payload)(void *src, struct json_stream *dest);
void (*serialize_payload)(void *src, struct json_stream *dest,
struct plugin *plugin);

/* Which plugins have registered this hook? This is a `tal_arr`
* initialized at creation. */
Expand Down Expand Up @@ -91,20 +92,19 @@ bool plugin_hook_continue(void *arg, const char *buffer, const jsmntok_t *toks);
* an arbitrary extra argument used to maintain context.
*/
#define REGISTER_PLUGIN_HOOK(name, deserialize_cb, final_cb, \
serialize_payload, cb_arg_type) \
serialize_payload, cb_arg_type) \
struct plugin_hook name##_hook_gen = { \
stringify(name), \
typesafe_cb_cast( \
bool (*)(void *, const char *, const jsmntok_t *), \
bool (*)(cb_arg_type, const char *, const jsmntok_t *), \
deserialize_cb), \
typesafe_cb_cast(void (*)(void *STEALS), \
void (*)(cb_arg_type STEALS), final_cb), \
typesafe_cb_cast( \
void (*)(void *STEALS), \
void (*)(cb_arg_type STEALS), \
final_cb), \
typesafe_cb_cast(void (*)(void *, struct json_stream *), \
void (*)(cb_arg_type, struct json_stream *), \
serialize_payload), \
void (*)(void *, struct json_stream *, struct plugin *), \
void (*)(cb_arg_type, struct json_stream *, struct plugin *), \
serialize_payload), \
NULL, /* .plugins */ \
}; \
AUTODATA(hooks, &name##_hook_gen); \
Expand Down
7 changes: 7 additions & 0 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,13 @@ def test_htlc_accepted_hook_direct_restart(node_factory, executor):
f1 = executor.submit(l1.rpc.pay, i1)

l2.daemon.wait_for_log(r'Holding onto an incoming htlc for 10 seconds')

# Check that the status mentions the HTLC being held
l2.rpc.listpeers()
peers = l2.rpc.listpeers()['peers']
htlc_status = peers[0]['channels'][0]['htlcs'][0].get('status', None)
assert htlc_status == "Waiting for the htlc_accepted hook of plugin hold_htlcs.py"

needle = l2.daemon.logsearch_start
l2.restart()

Expand Down
2 changes: 1 addition & 1 deletion wallet/db_postgres_sqlgen.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion wallet/db_sqlite3_sqlgen.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading