Skip to content

Commit 8b31955

Browse files
committed
common: add brace hack for jsonrpc_async_parse.
This is a trick from bcli: we ask bitcoind for the block, and it hands us a 2MB hex blob (which we read in multiple parts). Our parser wades through it all, but a quick search for '}' makes it much faster. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 8d02977 commit 8b31955

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

common/jsonrpc_io.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ const char *jsonrpc_io_parse(const tal_t *ctx,
6969
*toks = NULL;
7070
*buf = NULL;
7171

72+
/* Our JSON parser is pretty good at incremental parsing, but
73+
* `getrawblock` gives a giant 2MB token, which forces it to re-parse
74+
* every time until we have all of it. However, we can't complete a
75+
* JSON object without a '}', so we do a cheaper check here.
76+
*/
77+
if (!memchr(membuf_elems(&json_in->membuf), '}',
78+
membuf_num_elems(&json_in->membuf)))
79+
return NULL;
80+
7281
if (!json_parse_input(&json_in->parser, &json_in->toks,
7382
membuf_elems(&json_in->membuf),
7483
json_in->bytes_unparsed,

tests/test_misc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -951,8 +951,8 @@ def test_malformed_rpc(node_factory):
951951
obj, _ = l1.rpc._readobj(sock, b'')
952952
assert obj['error']['code'] == -32600
953953

954-
# Complete crap (this makes it hang up!)
955-
sock.sendall(b'[]')
954+
# Complete crap: needs } to even try parsing, and also this makes it hang up!
955+
sock.sendall(b'[]}')
956956
obj, _ = l1.rpc._readobj(sock, b'')
957957
assert obj['error']['code'] == -32600
958958

0 commit comments

Comments
 (0)