Skip to content

Commit eb0d042

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 c88c8ea commit eb0d042

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

common/jsonrpc_io.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ const char *jsonrpc_io_parse(const tal_t *ctx,
6464
*toks = NULL;
6565
*buf = NULL;
6666

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

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)