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

feat: chain hook custommsg #4358

Merged
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
7 changes: 2 additions & 5 deletions doc/PLUGINS.md
Original file line number Diff line number Diff line change
Expand Up @@ -1218,11 +1218,8 @@ ignored by nodes (see ["it's ok to be odd" in the specification][oddok] for
details). The plugin must implement the parsing of the message, including the
type prefix, since c-lightning does not know how to parse the message.

The result for this hook is currently being discarded. For future uses of the
result we suggest just returning `{'result': 'continue'}`.
This will ensure backward
compatibility should the semantics be changed in future.

Because this is a chained hook, the daemon expects the result to be
`{'result': 'continue'}`. It will fail if something else is returned.

### `onion_message` and `onion_message_blinded`

Expand Down
33 changes: 26 additions & 7 deletions lightningd/peer_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -2410,10 +2410,28 @@ struct custommsg_payload {
const u8 *msg;
};

static void custommsg_callback(struct custommsg_payload *payload STEALS,
const char *buffer, const jsmntok_t *toks)
static bool custommsg_cb(struct custommsg_payload *payload,
const char *buffer, const jsmntok_t *toks)
{
tal_free(payload);
const jsmntok_t *t_res;

if (!toks || !buffer)
return true;

t_res = json_get_member(buffer, toks, "result");

/* fail */
if (!t_res || !json_tok_streq(buffer, t_res, "continue"))
fatal("Plugin returned an invalid response to the "
"custommsg hook: %s", buffer);

/* call next hook */
return true;
}

static void custommsg_final(struct custommsg_payload *payload STEALS)
{
tal_steal(tmpctx, payload);
}

static void custommsg_payload_serialize(struct custommsg_payload *payload,
Expand All @@ -2423,10 +2441,11 @@ static void custommsg_payload_serialize(struct custommsg_payload *payload,
json_add_node_id(stream, "peer_id", &payload->peer_id);
}

REGISTER_SINGLE_PLUGIN_HOOK(custommsg,
custommsg_callback,
custommsg_payload_serialize,
struct custommsg_payload *);
REGISTER_PLUGIN_HOOK(custommsg,
custommsg_cb,
custommsg_final,
custommsg_payload_serialize,
struct custommsg_payload *);

void handle_custommsg_in(struct lightningd *ld, const struct node_id *peer_id,
const u8 *msg)
Expand Down
3 changes: 2 additions & 1 deletion tests/plugins/custommsg.py → tests/plugins/custommsg_a.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

@plugin.hook('custommsg')
def on_custommsg(peer_id, message, plugin, **kwargs):
plugin.log("Got a custom message {msg} from peer {peer_id}".format(
plugin.log("Got custommessage_a {msg} from peer {peer_id}".format(
msg=message,
peer_id=peer_id
))
return {'result': 'continue'}


plugin.run()
16 changes: 16 additions & 0 deletions tests/plugins/custommsg_b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env python3
from pyln.client import Plugin

plugin = Plugin()


@plugin.hook('custommsg')
def on_custommsg(peer_id, message, plugin, **kwargs):
plugin.log("Got custommessage_b {msg} from peer {peer_id}".format(
msg=message,
peer_id=peer_id
))
return {'result': 'continue'}


plugin.run()
24 changes: 16 additions & 8 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2239,8 +2239,10 @@ def test_sendcustommsg(node_factory):
and we can't send to it.

"""
plugin = os.path.join(os.path.dirname(__file__), "plugins", "custommsg.py")
opts = {'log-level': 'io', 'plugin': plugin}
opts = {'log-level': 'io', 'plugin': [
os.path.join(os.path.dirname(__file__), "plugins", "custommsg_b.py"),
os.path.join(os.path.dirname(__file__), "plugins", "custommsg_a.py")
]}
l1, l2, l3, l4 = node_factory.get_nodes(4, opts=opts)
node_factory.join_nodes([l1, l2, l3])
l2.connect(l4)
Expand Down Expand Up @@ -2279,9 +2281,12 @@ def test_sendcustommsg(node_factory):
)
)
l1.daemon.wait_for_log(r'\[IN\] {}'.format(serialized))
l1.daemon.wait_for_log(
r'Got a custom message {serialized} from peer {peer_id}'.format(
serialized=serialized, peer_id=l2.info['id']))
l1.daemon.wait_for_logs([
r'Got custommessage_a {serialized} from peer {peer_id}'.format(
serialized=serialized, peer_id=l2.info['id']),
r'Got custommessage_b {serialized} from peer {peer_id}'.format(
serialized=serialized, peer_id=l2.info['id'])
])

# This should work since the peer is currently owned by `openingd`
l2.rpc.dev_sendcustommsg(l4.info['id'], msg)
Expand All @@ -2291,9 +2296,12 @@ def test_sendcustommsg(node_factory):
)
)
l4.daemon.wait_for_log(r'\[IN\] {}'.format(serialized))
l4.daemon.wait_for_log(
r'Got a custom message {serialized} from peer {peer_id}'.format(
serialized=serialized, peer_id=l2.info['id']))
l4.daemon.wait_for_logs([
r'Got custommessage_a {serialized} from peer {peer_id}'.format(
serialized=serialized, peer_id=l2.info['id']),
r'Got custommessage_b {serialized} from peer {peer_id}'.format(
serialized=serialized, peer_id=l2.info['id']),
])


def test_sendonionmessage(node_factory):
Expand Down
2 changes: 2 additions & 0 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,8 @@ def test_openchannel_hook_chaining(node_factory, bitcoind):
l1.rpc.fundchannel(l2.info['id'], 100005)

assert l2.daemon.wait_for_log(hook_msg + "reject for a reason")
# first plugin in the chain was called
assert l2.daemon.is_in_log("accept on principle")
# the third plugin must now not be called anymore
assert not l2.daemon.is_in_log("reject on principle")

Expand Down