Skip to content

Commit

Permalink
plugin: Fix the custommsg hook not to include the internal prefix
Browse files Browse the repository at this point in the history
We were always prefixing the `message` field with the internal type
prefix 0x0407, followed by the length prefix. Neither is needed since
the type being constant is of no interest to the plugin and the length
being implicit due to the JSON-encoding.

Reported-by: Ilya Evdokimov
Changelog-Fixed: plugin: The `custommsg` hook no longer includes the internal type prefix and length prefix in its `payload`
Changelog-Deprecated: plugin: The `message` field on the `custommsg` hook is deprecated in favor of the `payload` field, which skips the internal prefix.
  • Loading branch information
cdecker authored and rustyrussell committed Mar 2, 2021
1 parent 804c2c2 commit ebb1b19
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 18 deletions.
2 changes: 1 addition & 1 deletion doc/PLUGINS.md
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,7 @@ The payload for a call follows this format:
```json
{
"peer_id": "02df5ffe895c778e10f7742a6c5b8a0cefbe9465df58b92fadeb883752c8107c8f",
"message": "1337ffffffff"
"payload": "1337ffffffff"
}
```

Expand Down
11 changes: 10 additions & 1 deletion lightningd/peer_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -2454,7 +2454,16 @@ static void custommsg_final(struct custommsg_payload *payload STEALS)
static void custommsg_payload_serialize(struct custommsg_payload *payload,
struct json_stream *stream)
{
json_add_hex_talarr(stream, "message", payload->msg);
if (deprecated_apis) {
json_add_hex_talarr(stream, "message", payload->msg);
json_add_string(
stream, "warning",
"The `message` field is deprecated and has been replaced "
"with the payload` field which skips the internal type and "
"the length prefix. Please update to use that instead.");
}
json_add_hex(stream, "payload", payload->msg + 4,
tal_bytelen(payload->msg) - 4);
json_add_node_id(stream, "peer_id", &payload->peer_id);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/plugins/custommsg_a.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@


@plugin.hook('custommsg')
def on_custommsg(peer_id, message, plugin, **kwargs):
def on_custommsg(peer_id, payload, plugin, message=None, **kwargs):
plugin.log("Got custommessage_a {msg} from peer {peer_id}".format(
msg=message,
msg=payload,
peer_id=peer_id
))
return {'result': 'continue'}
Expand Down
4 changes: 2 additions & 2 deletions tests/plugins/custommsg_b.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@


@plugin.hook('custommsg')
def on_custommsg(peer_id, message, plugin, **kwargs):
def on_custommsg(peer_id, payload, plugin, message=None, **kwargs):
plugin.log("Got custommessage_b {msg} from peer {peer_id}".format(
msg=message,
msg=payload,
peer_id=peer_id
))
return {'result': 'continue'}
Expand Down
16 changes: 8 additions & 8 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2284,10 +2284,10 @@ def test_sendcustommsg(node_factory):
)
l1.daemon.wait_for_log(r'\[IN\] {}'.format(serialized))
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'])
r'Got custommessage_a {msg} from peer {peer_id}'.format(
msg=msg, peer_id=l2.info['id']),
r'Got custommessage_b {msg} from peer {peer_id}'.format(
msg=msg, peer_id=l2.info['id'])
])

# This should work since the peer is currently owned by `openingd`
Expand All @@ -2299,10 +2299,10 @@ def test_sendcustommsg(node_factory):
)
l4.daemon.wait_for_log(r'\[IN\] {}'.format(serialized))
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']),
r'Got custommessage_a {msg} from peer {peer_id}'.format(
msg=msg, peer_id=l2.info['id']),
r'Got custommessage_b {msg} from peer {peer_id}'.format(
msg=msg, peer_id=l2.info['id']),
])


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.

4 changes: 2 additions & 2 deletions wallet/statements_gettextgen.po

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

4 changes: 4 additions & 0 deletions wallet/test/run-wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ void json_add_amount_sat_compat(struct json_stream *result UNNEEDED,
void json_add_bool(struct json_stream *result UNNEEDED, const char *fieldname UNNEEDED,
bool value UNNEEDED)
{ fprintf(stderr, "json_add_bool called!\n"); abort(); }
/* Generated stub for json_add_hex */
void json_add_hex(struct json_stream *result UNNEEDED, const char *fieldname UNNEEDED,
const void *data UNNEEDED, size_t len UNNEEDED)
{ fprintf(stderr, "json_add_hex called!\n"); abort(); }
/* Generated stub for json_add_hex_talarr */
void json_add_hex_talarr(struct json_stream *result UNNEEDED,
const char *fieldname UNNEEDED,
Expand Down

0 comments on commit ebb1b19

Please sign in to comment.