Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
406a4fce
Unverified
Commit
406a4fce
authored
Aug 03, 2022
by
Matthew Slipper
Committed by
GitHub
Aug 04, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
proxyd: Unwrap single RPC batches (#3165)
* proxyd: Unwrap single RPC batches * Update backend.go
parent
96103eda
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
41 additions
and
7 deletions
+41
-7
little-squids-invent.md
.changeset/little-squids-invent.md
+5
-0
backend.go
proxyd/backend.go
+26
-6
mock_backend_test.go
proxyd/integration_tests/mock_backend_test.go
+5
-1
ws_test.go
proxyd/integration_tests/ws_test.go
+5
-0
No files found.
.changeset/little-squids-invent.md
0 → 100644
View file @
406a4fce
---
'
@eth-optimism/proxyd'
:
patch
---
Unwrap single RPC batches
proxyd/backend.go
View file @
406a4fce
...
...
@@ -349,7 +349,17 @@ func (b *Backend) setOffline() {
}
func
(
b
*
Backend
)
doForward
(
ctx
context
.
Context
,
rpcReqs
[]
*
RPCReq
,
isBatch
bool
)
([]
*
RPCRes
,
error
)
{
body
:=
mustMarshalJSON
(
rpcReqs
)
isSingleElementBatch
:=
len
(
rpcReqs
)
==
1
// Single element batches are unwrapped before being sent
// since Alchemy handles single requests better than batches.
var
body
[]
byte
if
isSingleElementBatch
{
body
=
mustMarshalJSON
(
rpcReqs
[
0
])
}
else
{
body
=
mustMarshalJSON
(
rpcReqs
)
}
httpReq
,
err
:=
http
.
NewRequestWithContext
(
ctx
,
"POST"
,
b
.
rpcURL
,
bytes
.
NewReader
(
body
))
if
err
!=
nil
{
...
...
@@ -402,12 +412,22 @@ func (b *Backend) doForward(ctx context.Context, rpcReqs []*RPCReq, isBatch bool
}
var
res
[]
*
RPCRes
if
err
:=
json
.
Unmarshal
(
resB
,
&
res
);
err
!=
nil
{
// Infura may return a single JSON-RPC response if, for example, the batch contains a request for an unsupported method
if
responseIsNotBatched
(
resB
)
{
return
nil
,
ErrBackendUnexpectedJSONRPC
if
isSingleElementBatch
{
var
singleRes
RPCRes
if
err
:=
json
.
Unmarshal
(
resB
,
&
singleRes
);
err
!=
nil
{
return
nil
,
ErrBackendBadResponse
}
res
=
[]
*
RPCRes
{
&
singleRes
,
}
}
else
{
if
err
:=
json
.
Unmarshal
(
resB
,
&
res
);
err
!=
nil
{
// Infura may return a single JSON-RPC response if, for example, the batch contains a request for an unsupported method
if
responseIsNotBatched
(
resB
)
{
return
nil
,
ErrBackendUnexpectedJSONRPC
}
return
nil
,
ErrBackendBadResponse
}
return
nil
,
ErrBackendBadResponse
}
if
len
(
rpcReqs
)
!=
len
(
res
)
{
...
...
proxyd/integration_tests/mock_backend_test.go
View file @
406a4fce
...
...
@@ -35,8 +35,12 @@ func SingleResponseHandler(code int, response string) http.HandlerFunc {
}
func
BatchedResponseHandler
(
code
int
,
responses
...
string
)
http
.
HandlerFunc
{
// all proxyd upstream requests are batched
return
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
if
len
(
responses
)
==
1
{
SingleResponseHandler
(
code
,
responses
[
0
])(
w
,
r
)
return
}
var
body
string
body
+=
"["
for
i
,
response
:=
range
responses
{
...
...
proxyd/integration_tests/ws_test.go
View file @
406a4fce
...
...
@@ -44,11 +44,14 @@ func TestConcurrentWSPanic(t *testing.T) {
<-
readyCh
var
wg
sync
.
WaitGroup
wg
.
Add
(
2
)
// spam messages
go
func
()
{
for
{
select
{
case
<-
quitC
:
wg
.
Done
()
return
default
:
_
=
backendToProxyConn
.
WriteMessage
(
websocket
.
TextMessage
,
[]
byte
(
"garbage"
))
...
...
@@ -61,6 +64,7 @@ func TestConcurrentWSPanic(t *testing.T) {
for
{
select
{
case
<-
quitC
:
wg
.
Done
()
return
default
:
_
=
client
.
WriteMessage
(
websocket
.
TextMessage
,
[]
byte
(
"{
\"
id
\"
: 1,
\"
method
\"
:
\"
eth_foo
\"
,
\"
params
\"
: [
\"
newHeads
\"
]}"
))
...
...
@@ -72,6 +76,7 @@ func TestConcurrentWSPanic(t *testing.T) {
// concurrent write to websocket connection
time
.
Sleep
(
time
.
Second
)
close
(
quitC
)
wg
.
Wait
()
}
type
backendHandler
struct
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment