From 0e5a83349e05f255c2b53aecd53e0fbef23b77b0 Mon Sep 17 00:00:00 2001 From: dwightguth Date: Fri, 16 Aug 2019 10:24:23 -0400 Subject: [PATCH] Batch processing of web3 requests (#441) * tests * web3: batch processing * web3: fix everett's comment --- tests/web3/batch_all_notification.in.json | 13 ++++++ tests/web3/batch_all_notification.out.json | 0 tests/web3/batch_empty_array.in.json | 1 + tests/web3/batch_empty_array.out.json | 1 + tests/web3/batch_invalid_array.in.json | 1 + tests/web3/batch_invalid_array.out.json | 1 + tests/web3/batch_invalid_array_2.in.json | 1 + tests/web3/batch_invalid_array_2.out.json | 1 + tests/web3/batch_partial_notification.in.json | 10 +++++ .../web3/batch_partial_notification.out.json | 1 + tests/web3/batch_partially_valid.in.json | 6 +++ tests/web3/batch_partially_valid.out.json | 1 + web3.md | 44 +++++++++++++++++++ 13 files changed, 81 insertions(+) create mode 100644 tests/web3/batch_all_notification.in.json create mode 100644 tests/web3/batch_all_notification.out.json create mode 100644 tests/web3/batch_empty_array.in.json create mode 100644 tests/web3/batch_empty_array.out.json create mode 100644 tests/web3/batch_invalid_array.in.json create mode 100644 tests/web3/batch_invalid_array.out.json create mode 100644 tests/web3/batch_invalid_array_2.in.json create mode 100644 tests/web3/batch_invalid_array_2.out.json create mode 100644 tests/web3/batch_partial_notification.in.json create mode 100644 tests/web3/batch_partial_notification.out.json create mode 100644 tests/web3/batch_partially_valid.in.json create mode 100644 tests/web3/batch_partially_valid.out.json diff --git a/tests/web3/batch_all_notification.in.json b/tests/web3/batch_all_notification.in.json new file mode 100644 index 0000000000..6e21ed7f11 --- /dev/null +++ b/tests/web3/batch_all_notification.in.json @@ -0,0 +1,13 @@ +[{ + "jsonrpc": "2.0", + "method": "net_version", + "params": [] +},{ + "jsonrpc": "2.0", + "method": "net_version", + "params": [] +},{ + "jsonrpc": "2.0", + "method": "net_version", + "params": [] +}] diff --git a/tests/web3/batch_all_notification.out.json b/tests/web3/batch_all_notification.out.json new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/web3/batch_empty_array.in.json b/tests/web3/batch_empty_array.in.json new file mode 100644 index 0000000000..fe51488c70 --- /dev/null +++ b/tests/web3/batch_empty_array.in.json @@ -0,0 +1 @@ +[] diff --git a/tests/web3/batch_empty_array.out.json b/tests/web3/batch_empty_array.out.json new file mode 100644 index 0000000000..1ae3cf0152 --- /dev/null +++ b/tests/web3/batch_empty_array.out.json @@ -0,0 +1 @@ +{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"Invalid Request"}} \ No newline at end of file diff --git a/tests/web3/batch_invalid_array.in.json b/tests/web3/batch_invalid_array.in.json new file mode 100644 index 0000000000..7660873d10 --- /dev/null +++ b/tests/web3/batch_invalid_array.in.json @@ -0,0 +1 @@ +[1] diff --git a/tests/web3/batch_invalid_array.out.json b/tests/web3/batch_invalid_array.out.json new file mode 100644 index 0000000000..d55cf16355 --- /dev/null +++ b/tests/web3/batch_invalid_array.out.json @@ -0,0 +1 @@ +[{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"Invalid Request"}}] \ No newline at end of file diff --git a/tests/web3/batch_invalid_array_2.in.json b/tests/web3/batch_invalid_array_2.in.json new file mode 100644 index 0000000000..6ed63af507 --- /dev/null +++ b/tests/web3/batch_invalid_array_2.in.json @@ -0,0 +1 @@ +[1,2] diff --git a/tests/web3/batch_invalid_array_2.out.json b/tests/web3/batch_invalid_array_2.out.json new file mode 100644 index 0000000000..3789f4e8a0 --- /dev/null +++ b/tests/web3/batch_invalid_array_2.out.json @@ -0,0 +1 @@ +[{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"Invalid Request"}},{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"Invalid Request"}}] \ No newline at end of file diff --git a/tests/web3/batch_partial_notification.in.json b/tests/web3/batch_partial_notification.in.json new file mode 100644 index 0000000000..3706b8533e --- /dev/null +++ b/tests/web3/batch_partial_notification.in.json @@ -0,0 +1,10 @@ +[{ + "jsonrpc": "2.0", + "method": "net_version", + "params": [] +},{ + "jsonrpc": "2.0", + "id": 1, + "method": "net_version", + "params": [] +}] diff --git a/tests/web3/batch_partial_notification.out.json b/tests/web3/batch_partial_notification.out.json new file mode 100644 index 0000000000..cdb29fbc1a --- /dev/null +++ b/tests/web3/batch_partial_notification.out.json @@ -0,0 +1 @@ +[{"jsonrpc":"2.0","id":1,"result":"28346"}] \ No newline at end of file diff --git a/tests/web3/batch_partially_valid.in.json b/tests/web3/batch_partially_valid.in.json new file mode 100644 index 0000000000..7c1f6c07f4 --- /dev/null +++ b/tests/web3/batch_partially_valid.in.json @@ -0,0 +1,6 @@ +[{ + "jsonrpc": "2.0", + "id": 1, + "method": "net_version", + "params": [] +},{}] diff --git a/tests/web3/batch_partially_valid.out.json b/tests/web3/batch_partially_valid.out.json new file mode 100644 index 0000000000..95e242f054 --- /dev/null +++ b/tests/web3/batch_partially_valid.out.json @@ -0,0 +1 @@ +[{"jsonrpc":"2.0","id":1,"result":"28346"},{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"Invalid Request"}}] \ No newline at end of file diff --git a/web3.md b/web3.md index 7d90e6fd69..28900865c8 100644 --- a/web3.md +++ b/web3.md @@ -22,7 +22,9 @@ module WEB3 0:JSON "":JSON [ .JSONList ] + undef + .List syntax JSON ::= Int | Bool | "null" | "undef" @@ -53,6 +55,7 @@ module WEB3 // ----------------------------- rule getRequest() => #loadRPCCall(#getRequest(SOCK)) ... SOCK + _ => undef syntax IOJSON ::= #getRequest(Int) [function, hook(JSON.read)] // -------------------------------------------------------------- @@ -71,15 +74,56 @@ module WEB3 rule #loadRPCCall(#EOF) => #shutdownWrite(SOCK) ~> #close(SOCK) ~> accept() ... SOCK + rule #loadRPCCall([ _, _ ] #as J) => #loadFromBatch ... + _ => J + _ => .List + + rule #loadRPCCall(_:String #Or null #Or _:Int #Or [ .JSONList ]) => #sendResponse("error": {"code": -32600, "message": "Invalid Request"}) ... + _ => null + + syntax KItem ::= "#loadFromBatch" + // --------------------------------- + rule #loadFromBatch ~> _ => #loadRPCCall(J) + [ J , JS => JS ] + + rule #loadFromBatch ~> _ => #putResponse(List2JSON(RESPONSE), SOCK) ~> getRequest() + [ .JSONList ] + SOCK + RESPONSE + requires size(RESPONSE) >Int 0 + + rule #loadFromBatch ~> _ => getRequest() + [ .JSONList ] + .List + + syntax JSON ::= List2JSON(List) [function] + | List2JSON(List, JSONList) [function, klabel(List2JSONAux)] + // -------------------------------------------------------------------------- + rule List2JSON(L) => List2JSON(L, .JSONList) + + rule List2JSON(L ListItem(J), JS) => List2JSON(L, (J, JS)) + rule List2JSON(.List, JS) => [ JS ] + syntax KItem ::= #sendResponse( JSON ) // -------------------------------------- rule #sendResponse(J) ~> _ => #putResponse({ "jsonrpc": "2.0", "id": CALLID, J }, SOCK) ~> getRequest() CALLID SOCK + undef requires CALLID =/=K undef rule #sendResponse(_) ~> _ => getRequest() undef + undef + + rule #sendResponse(J) ~> _ => #loadFromBatch + CALLID + [ _ ] + ... .List => ListItem({ "jsonrpc": "2.0", "id": CALLID, J }) + + rule #sendResponse(_) ~> _ => #loadFromBatch + undef + [ _ ] syntax KItem ::= "#checkRPCCall" // --------------------------------