Skip to content

Commit 7401722

Browse files
fix: rework the paginator API
Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent bc2e33e commit 7401722

File tree

9 files changed

+88
-70
lines changed

9 files changed

+88
-70
lines changed

common/json_param.c

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#include "ccan/tal/tal.h"
21
#include "config.h"
3-
#include "jsmn.h"
42
#include <assert.h>
53
#include <bitcoin/address.h>
64
#include <bitcoin/base58.h>
@@ -19,6 +17,8 @@
1917
#include <common/route.h>
2018
#include <stdbool.h>
2119
#include <stddef.h>
20+
#include <stdint.h>
21+
#include <stdio.h>
2222

2323
struct param {
2424
const char *name;
@@ -342,7 +342,7 @@ const char *param_subcommand(struct command *cmd, const char *buffer,
342342
}
343343

344344
bool param(struct command *cmd, const char *buffer,
345-
const jsmntok_t tokens[], ...)
345+
const jsmntok_t *tokens, ...)
346346
{
347347
struct param *params = tal_arr(tmpctx, struct param, 0);
348348
const char *name;
@@ -357,15 +357,8 @@ bool param(struct command *cmd, const char *buffer,
357357
if (streq(name, "")) {
358358
allow_extra = true;
359359
continue;
360-
} else if (streq(name, "paginator")) {
361-
struct command_result *result;
362-
if ((result = cbx(cmd, name, buffer, NULL, arg)))
363-
return result;
364-
/* we allow extra but we already made a check that are
365-
* paginator keys, so this should be safe! */
366-
allow_extra = true;
367-
continue;
368360
}
361+
369362
if (!param_add(&params, name, style, cbx, arg)) {
370363
/* We really do ignore this return! */
371364
struct command_result *ignore;
@@ -1111,23 +1104,33 @@ struct command_result *param_pubkey(struct command *cmd, const char *name,
11111104
}
11121105

11131106
struct command_result *param_paginator(struct command *cmd, const char *name,
1114-
const char *buffer, const jsmntok_t *tok)
1107+
const char *buffer, const jsmntok_t *tok,
1108+
struct jsonrpc_paginator **paginator)
11151109
{
1116-
/*
1117-
const jsmntok_t *tok_tmp;
1118-
1119-
tok_tmp = json_get_member(buffer, tok, "batch");
1120-
if (tok_tmp)
1121-
return NULL;
1122-
tok_tmp = json_get_member(buffer, tok, "offset");
1123-
if (tok_tmp)
1110+
const jsmntok_t *batch_tok, *offset_tok, *limit_tok;
1111+
u64 *limit, *offset;
1112+
const char **batch;
1113+
1114+
batch = NULL;
1115+
batch_tok = json_get_member(buffer, tok, "batch");
1116+
if (batch_tok)
1117+
json_to_strarr(cmd, buffer, batch_tok, &batch);
1118+
1119+
offset = tal(cmd, uint64_t);
1120+
offset_tok = json_get_member(buffer, tok, "offset");
1121+
if (offset_tok)
1122+
json_to_u64(buffer, offset_tok, offset);
1123+
1124+
limit = tal(cmd, uint64_t);
1125+
limit_tok = json_get_member(buffer, tok, "limit");
1126+
if (limit_tok)
1127+
json_to_u64(buffer, limit_tok, limit);
1128+
1129+
if (batch || (limit && offset)) {
1130+
*paginator = new_paginator(cmd, batch, limit, offset);
1131+
assert(paginator);
11241132
return NULL;
1125-
1126-
tok_tmp = json_get_member(buffer, tok, "limit");
1127-
if (tok_tmp)
1128-
return NULL;
1129-
1133+
}
11301134
return command_fail_badparam(cmd, name, buffer, tok,
1131-
"paginator request format in the wrong way!");*/
1132-
return NULL;
1135+
"paginator request format in the wrong way!");
11331136
}

common/json_param.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ enum param_style {
8888

8989
/** Check if this is a valid paginator input */
9090
struct command_result *param_paginator(struct command *cmd, const char *name,
91-
const char *buffer, const jsmntok_t *tok);
91+
const char *buffer, const jsmntok_t *tok,
92+
struct jsonrpc_paginator **paginator);
9293

9394
/*
9495
* Add a required parameter.
@@ -149,7 +150,16 @@ struct command_result *param_paginator(struct command *cmd, const char *name,
149150
/* Special flag for 'check' which allows any parameters. */
150151
#define p_opt_any() "", PARAM_OPTIONAL, NULL, NULL
151152

152-
#define p_paginator() "paginator", PARAM_OPTIONAL, param_paginator, NULL
153+
#define p_paginator(arg) \
154+
"paginator", \
155+
PARAM_OPTIONAL, \
156+
(param_cbx)(param_paginator), \
157+
({ *arg = NULL; \
158+
(arg) + 0*sizeof((param_paginator)((struct command *)NULL, \
159+
(const char *)NULL, \
160+
(const char *)NULL, \
161+
(const jsmntok_t *)NULL, \
162+
(arg)) == (struct command_result *)NULL); })
153163

154164

155165
/* All the helper routines. */

common/json_parse.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,3 +711,28 @@ json_tok_channel_id(const char *buffer, const jsmntok_t *tok,
711711
return hex_decode(buffer + tok->start, tok->end - tok->start,
712712
cid, sizeof(*cid));
713713
}
714+
715+
bool json_to_strarr(const tal_t *ctx, const char *buffer,
716+
const jsmntok_t *tok, const char ***arr)
717+
{
718+
const jsmntok_t *curr;
719+
size_t i;
720+
721+
if (tok->type != JSMN_ARRAY)
722+
return false;
723+
724+
*arr = tal_arr(ctx, const char *, 0);
725+
json_for_each_arr(i, curr, tok) {
726+
struct json_escape *esc;
727+
const char *str;
728+
729+
if (curr->type != JSMN_STRING)
730+
return false;
731+
esc = json_escape_string_(ctx, buffer + curr->start,
732+
curr->end - curr->start);
733+
str = json_escape_unescape(ctx, esc);
734+
tal_arr_expand(arr, str);
735+
}
736+
737+
return NULL;
738+
}

common/json_parse.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ json_to_blinded_path(const tal_t *ctx, const char *buffer, const jsmntok_t *tok)
124124
bool json_tok_channel_id(const char *buffer, const jsmntok_t *tok,
125125
struct channel_id *cid);
126126

127+
bool json_to_strarr(const tal_t *ctx, const char *buffer,
128+
const jsmntok_t *tok, const char ***arr);
129+
127130
/* Guide is % for a token: each must be followed by JSON_SCAN().
128131
* Returns NULL on error (asserts() on bad guide). */
129132
const char *json_scan(const tal_t *ctx,

common/jsonrpc_paginator.h

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
* Welcome in this wanderful experience of writing
33
* a paginator for the core lightning JSON RPC 2.0 in C!
44
*
5-
*
65
* I will try to keep the code as simple as possible,
76
* so if you had any dout on what I'm trying to do, blame me
87
* that I was not able to do my job.
98
*
10-
*
119
* In short, the goal of this paginator is offer a struct
1210
* that it is used to grep the paginator information by
1311
* pre processin the JSON RPC request.
@@ -18,16 +16,14 @@
1816
* if the paginator will be enbaled on the listnodes, the
1917
* call will be
2018
*
21-
* cli | rest | grpc -> json_paginator_listnodes -> json_listnodes + struct jsonrpc_paginator -> sjon response { .... }
19+
* cli | rest | grpc -> json_paginator_listnodes -> json_listnodes + struct jsonrpc_paginator -> jon response { .... }
2220
*
2321
* Done, this should be all!
24-
*
25-
*
26-
* Now let see how this is implemented.
27-
* */
22+
*/
2823
#ifndef JSONRPC_PAGINATOR_H
2924
#define JSONRPC_PAGINATOR_H
3025

26+
#include <assert.h>
3127
#include <ccan/tal/tal.h>
3228
#include <ccan/short_types/short_types.h>
3329

@@ -50,25 +46,27 @@
5046
struct jsonrpc_paginator {
5147
/** reall usefult for access to gossip map */
5248
const char **batch;
53-
/** query the database */
49+
/** query the database, for more complexy
50+
* query please use the sql plugins */
5451
const u64 *offset;
5552
const u64 *limit;
5653
/* FIXME: more smarter one? like sort_by = "json key"
5754
* but this required to have a mapping between json_keys and sql keys
5855
* maybe we had already somethings in the sql plugin? */
5956
};
6057

61-
/** new_paginator - helper function to create a new paginator from a list of parameter.
58+
/**
59+
* new_paginator - helper function to create a new paginator from a list of parameter.
6260
* This is simple enought to be avoided, but write simple code inside the C macros
6361
* is frustating, so let use this insteand.
6462
*
6563
* BTW: I love C Macros, really!
66-
* */
64+
*/
6765
static inline struct jsonrpc_paginator *new_paginator(const tal_t *ctx, const char **batch,
6866
const u64 *limit, const u64 *offset)
6967
{
7068
struct jsonrpc_paginator *paginator = NULL;
71-
if (batch || limit || offset) {
69+
if (batch || (limit && offset)) {
7270
paginator = tal(ctx, struct jsonrpc_paginator);
7371
paginator->batch = batch;
7472
paginator->limit = limit;
@@ -77,5 +75,4 @@ static inline struct jsonrpc_paginator *new_paginator(const tal_t *ctx, const ch
7775
}
7876
return NULL;
7977
}
80-
8178
#endif // JSONRPC_PAGINATOR_H

common/test/run-json_filter.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ const char *json_scan(const tal_t *ctx UNNEEDED,
7272
bool json_to_channel_id(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
7373
struct channel_id *cid UNNEEDED)
7474
{ fprintf(stderr, "json_to_channel_id called!\n"); abort(); }
75+
bool json_to_strarr(const tal_t *ctx UNNEEDED, const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, const char ***arr UNNEEDED)
76+
{ fprintf(stderr, "json_to_strarr called"); abort();}
7577
/* Generated stub for json_to_millionths */
7678
bool json_to_millionths(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
7779
u64 *millionths UNNEEDED)

common/test/run-json_remove.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ bool json_to_txid(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
133133
bool json_to_u16(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
134134
uint16_t *num UNNEEDED)
135135
{ fprintf(stderr, "json_to_u16 called!\n"); abort(); }
136+
bool json_to_strarr(const tal_t *ctx UNNEEDED, const char *buffer UNNEEDED,
137+
const jsmntok_t *tok UNNEEDED, const char ***arr UNNEEDED)
138+
{ fprintf(stderr, "json_to_strarr called"); abort();}
136139
/* Generated stub for json_tok_bin_from_hex */
137140
u8 *json_tok_bin_from_hex(const tal_t *ctx UNNEEDED, const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED)
138141
{ fprintf(stderr, "json_tok_bin_from_hex called!\n"); abort(); }

plugins/libplugin.h

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -487,23 +487,4 @@ const jsmntok_t *jsonrpc_request_sync(const tal_t *ctx, struct plugin *plugin,
487487
const char *method,
488488
const struct json_out *params TAKES,
489489
const char **resp);
490-
491-
#define PAGINATOR(callback) \
492-
static struct command_result* callback##_paginator(struct command *cmd, \
493-
const char *buffer, \
494-
const jsmntok_t *params) \
495-
{ \
496-
const char **batch; \
497-
u64 *limit, *offset; \
498-
if (!param(cmd, buffer, params, \
499-
p_opt("batch", param_arr_str, &batch), \
500-
p_opt("limit", param_u64, &limit), \
501-
p_opt("offset", param_u64, &offset), \
502-
p_opt_any(), \
503-
NULL)) \
504-
return command_param_failed(); \
505-
cmd->paginator = new_paginator(cmd, batch, limit, offset); \
506-
return callback(cmd, buffer, params); \
507-
}
508-
509490
#endif /* LIGHTNING_PLUGINS_LIBPLUGIN_H */

plugins/topology.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
#include "ccan/list/list.h"
2-
#include "ccan/strmap/strmap.h"
3-
#include "ccan/tal/tal.h"
4-
#include "common/node_id.h"
51
#include "config.h"
62
#include <assert.h>
73
#include <ccan/array_size/array_size.h>
@@ -431,13 +427,13 @@ static bool node_in_paginator(struct command *cmd, struct node_id *node_id)
431427
for (i = 0; i < tal_count(p->batch); i++) {
432428
id = p->batch[i];
433429
if (strcmp(id, target_id) == 0)
434-
return true;
430+
goto done;
435431
plugin_log(cmd->plugin, LOG_DBG, "not match %s != %s", target_id, id);
436432
}
437433
}
438434
return false;
439435
}
440-
tal_free(p);
436+
done:
441437
return true;
442438
}
443439

@@ -525,7 +521,7 @@ static struct command_result *json_listnodes(struct command *cmd,
525521

526522
if (!param(cmd, buffer, params,
527523
p_opt("id", param_node_id, &id),
528-
p_paginator(),
524+
p_paginator(&cmd->paginator),
529525
NULL))
530526
return command_param_failed();
531527

@@ -670,8 +666,6 @@ static const char *init(struct plugin *p,
670666
return NULL;
671667
}
672668

673-
PAGINATOR(json_listnodes);
674-
675669
static const struct plugin_command commands[] = {
676670
{
677671
"getroute",
@@ -698,7 +692,7 @@ static const struct plugin_command commands[] = {
698692
"network",
699693
"List all known nodes in the network",
700694
"Show node {id} (or all known nods, if not specified)",
701-
json_listnodes_paginator,
695+
json_listnodes,
702696
},
703697
{
704698
"listincoming",

0 commit comments

Comments
 (0)