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

plugin:added invoice creation event #3658

Merged
merged 3 commits into from
May 4, 2020
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
12 changes: 8 additions & 4 deletions contrib/plugins/helloworld.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ def on_disconnect(plugin, id, **kwargs):

@plugin.subscribe("invoice_payment")
def on_payment(plugin, invoice_payment, **kwargs):
plugin.log("Received invoice_payment event for label {}, preimage {},"
" and amount of {}".format(invoice_payment.get("label"),
invoice_payment.get("preimage"),
invoice_payment.get("msat")))
plugin.log("Received invoice_payment event for label {label}, preimage {preimage},"
" and amount of {msat}".format(**invoice_payment))


@plugin.subscribe("invoice_creation")
def on_invoice_creation(plugin, invoice_creation, **kwargs):
plugin.log("Received invoice_creation event for label {label}, preimage {preimage},"
" and amount of {msat}".format(**invoice_creation))


@plugin.hook("htlc_accepted")
Expand Down
16 changes: 15 additions & 1 deletion doc/PLUGINS.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ to a peer was lost.

### `invoice_payment`

A notification for topic `invoice_payment` is sent every time an invoie is paid.
A notification for topic `invoice_payment` is sent every time an invoice is paid.

```json
{
Expand All @@ -334,6 +334,20 @@ A notification for topic `invoice_payment` is sent every time an invoie is paid.
"msat": "10000msat"
}
}

```
### `invoice_creation`

A notification for topic `invoice_creation` is sent every time an invoice is created.

```json
{
"invoice_creation": {
"label": "unique-label-for-invoice",
"preimage": "0000000000000000000000000000000000000000000000000000000000000000",
"msat": "10000msat"
}
}
```

### `warning`
Expand Down
3 changes: 3 additions & 0 deletions lightningd/invoice.c
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,9 @@ static void gossipd_incoming_channels_reply(struct subd *gossipd,
json_add_u64(response, "expires_at", details->expiry_time);
json_add_string(response, "bolt11", details->bolt11);

notify_invoice_creation(info->cmd->ld, info->b11->msat,
info->payment_preimage, info->label);

/* Warn if there's not sufficient incoming capacity. */
if (tal_count(info->b11->routes) == 0) {
log_unusual(info->cmd->ld->log,
Expand Down
35 changes: 35 additions & 0 deletions lightningd/notification.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,41 @@ void notify_invoice_payment(struct lightningd *ld, struct amount_msat amount,
plugins_notify(ld->plugins, take(n));
}

static void invoice_creation_notification_serialize(struct json_stream *stream,
struct amount_msat *amount,
struct preimage preimage,
const struct json_escape *label)
{
json_object_start(stream, "invoice_creation");
if (amount != NULL)
json_add_string(
stream, "msat",
type_to_string(tmpctx, struct amount_msat, amount));

json_add_hex(stream, "preimage", &preimage, sizeof(preimage));
json_add_escaped_string(stream, "label", label);
json_object_end(stream);
}

REGISTER_NOTIFICATION(invoice_creation,
invoice_creation_notification_serialize)

void notify_invoice_creation(struct lightningd *ld, struct amount_msat *amount,
struct preimage preimage,
const struct json_escape *label)
{
void (*serialize)(struct json_stream *,
struct amount_msat *,
struct preimage,
const struct json_escape *) = invoice_creation_notification_gen.serialize;

struct jsonrpc_notification *n
= jsonrpc_notification_start(NULL, invoice_creation_notification_gen.topic);
serialize(n->stream, amount, preimage, label);
jsonrpc_notification_end(n);
plugins_notify(ld->plugins, take(n));
}

static void channel_opened_notification_serialize(struct json_stream *stream,
struct node_id *node_id,
struct amount_sat *funding_sat,
Expand Down
3 changes: 3 additions & 0 deletions lightningd/notification.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ void notify_warning(struct lightningd *ld, struct log_entry *l);
void notify_invoice_payment(struct lightningd *ld, struct amount_msat amount,
struct preimage preimage, const struct json_escape *label);

void notify_invoice_creation(struct lightningd *ld, struct amount_msat *amount,
struct preimage preimage, const struct json_escape *label);

void notify_channel_opened(struct lightningd *ld, struct node_id *node_id,
struct amount_sat *funding_sat, struct bitcoin_txid *funding_txid,
bool *funding_locked);
Expand Down
4 changes: 4 additions & 0 deletions lightningd/test/run-invoice-select-inchan.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ void notify_connect(struct lightningd *ld UNNEEDED, struct node_id *nodeid UNNEE
/* Generated stub for notify_disconnect */
void notify_disconnect(struct lightningd *ld UNNEEDED, struct node_id *nodeid UNNEEDED)
{ fprintf(stderr, "notify_disconnect called!\n"); abort(); }
/* Generated stub for notify_invoice_creation */
void notify_invoice_creation(struct lightningd *ld UNNEEDED, struct amount_msat *amount UNNEEDED,
struct preimage preimage UNNEEDED, const struct json_escape *label UNNEEDED)
{ fprintf(stderr, "notify_invoice_creation called!\n"); abort(); }
/* Generated stub for notify_invoice_payment */
void notify_invoice_payment(struct lightningd *ld UNNEEDED, struct amount_msat amount UNNEEDED,
struct preimage preimage UNNEEDED, const struct json_escape *label UNNEEDED)
Expand Down
18 changes: 18 additions & 0 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,24 @@ def test_invoice_payment_notification(node_factory):
.format(label, preimage, msats))


@unittest.skipIf(not DEVELOPER, "needs to deactivate shadow routing")
def test_invoice_creation_notification(node_factory):
"""
Test the 'invoice_creation' notification
"""
opts = [{}, {"plugin": os.path.join(os.getcwd(), "contrib/plugins/helloworld.py")}]
rbndg marked this conversation as resolved.
Show resolved Hide resolved
l1, l2 = node_factory.line_graph(2, opts=opts)

msats = 12345
preimage = '1' * 64
label = "a_descriptive_label"
l2.rpc.invoice(msats, label, 'description', preimage=preimage)

l2.daemon.wait_for_log(r"Received invoice_creation event for label {},"
" preimage {}, and amount of {}msat"
.format(label, preimage, msats))


def test_channel_opened_notification(node_factory):
"""
Test the 'channel_opened' notification sent at channel funding success.
Expand Down