Skip to content

Commit 2ebf48c

Browse files
committed
Add eth_call patch to address geth change
ethereum/go-ethereum#28078 changed geth to use "input" instead of "data", which causes issues with multiple chain RPC endpoints. Temporarily revert that for those chains.
1 parent dada102 commit 2ebf48c

File tree

14 files changed

+130
-44
lines changed

14 files changed

+130
-44
lines changed

endpointproxy/acalaproxy.go renamed to endpointproxy/acala.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package endpointproxy
33
import (
44
"bytes"
55
"encoding/json"
6-
"io/ioutil"
6+
"io"
77
"net/http"
88
"net/http/httputil"
99
"net/url"
@@ -39,7 +39,7 @@ func (h *AcalaProxy) modifyAcalaRequest(req *http.Request) {
3939
req.URL.Scheme = h.acalaTargetUrl.Scheme
4040
req.URL.Host = h.acalaTargetUrl.Host
4141
req.Host = h.acalaTargetUrl.Host
42-
reqStr, err := ioutil.ReadAll(req.Body)
42+
reqStr, err := io.ReadAll(req.Body)
4343
if err != nil {
4444
log.Errorf("invalid acala request err:%s", err.Error())
4545
return
@@ -58,6 +58,6 @@ func (h *AcalaProxy) modifyAcalaRequest(req *http.Request) {
5858
log.Errorf("fail to marshal this new acala req, raw:%s, err:%s", string(newMsg), marshalErr.Error())
5959
return
6060
}
61-
req.Body = ioutil.NopCloser(bytes.NewReader(newMsg))
61+
req.Body = io.NopCloser(bytes.NewReader(newMsg))
6262
req.ContentLength = int64(len(newMsg))
6363
}

endpointproxy/astarproxy.go renamed to endpointproxy/astar.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package endpointproxy
33
import (
44
"bytes"
55
"encoding/json"
6-
"io/ioutil"
6+
"io"
77
"net/http"
88
"net/http/httputil"
99
"net/url"
@@ -39,7 +39,7 @@ func (h *AstarProxy) modifyAstarRequest(req *http.Request) {
3939
req.URL.Scheme = h.astarTargetUrl.Scheme
4040
req.URL.Host = h.astarTargetUrl.Host
4141
req.Host = h.astarTargetUrl.Host
42-
reqStr, err := ioutil.ReadAll(req.Body)
42+
reqStr, err := io.ReadAll(req.Body)
4343
if err != nil {
4444
log.Errorf("invalid astar request err:%s", err.Error())
4545
return
@@ -58,6 +58,6 @@ func (h *AstarProxy) modifyAstarRequest(req *http.Request) {
5858
log.Errorf("fail to marshal this new astar req, raw:%s, err:%s", string(newMsg), marshalErr.Error())
5959
return
6060
}
61-
req.Body = ioutil.NopCloser(bytes.NewReader(newMsg))
61+
req.Body = io.NopCloser(bytes.NewReader(newMsg))
6262
req.ContentLength = int64(len(newMsg))
6363
}

endpointproxy/celoproxy.go renamed to endpointproxy/celo.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"bytes"
55
"compress/gzip"
66
"encoding/json"
7-
"io/ioutil"
7+
"io"
88
"net/http"
99
"net/http/httputil"
1010
"net/url"
@@ -47,7 +47,7 @@ func (c *CeloProxy) modifyCeloRequest(req *http.Request) {
4747
req.URL.Scheme = c.celoTargetUrl.Scheme
4848
req.URL.Host = c.celoTargetUrl.Host
4949
req.Host = c.celoTargetUrl.Host
50-
reqStr, err := ioutil.ReadAll(req.Body)
50+
reqStr, err := io.ReadAll(req.Body)
5151
if err != nil {
5252
log.Warnf("invalid celo request err:%s", err.Error())
5353
return
@@ -58,7 +58,7 @@ func (c *CeloProxy) modifyCeloRequest(req *http.Request) {
5858
return
5959
}
6060
req.Header.Set(celoHeaderRpcMethod, msg.Method)
61-
req.Body = ioutil.NopCloser(bytes.NewReader(reqStr))
61+
req.Body = io.NopCloser(bytes.NewReader(reqStr))
6262
}
6363

6464
func modifyCeloResponse() func(*http.Response) error {
@@ -68,7 +68,7 @@ func modifyCeloResponse() func(*http.Response) error {
6868
if err != nil {
6969
return err
7070
}
71-
originData, err := ioutil.ReadAll(gzipReader)
71+
originData, err := io.ReadAll(gzipReader)
7272
if err != nil {
7373
return err
7474
}
@@ -105,7 +105,7 @@ func modifyCeloResponse() func(*http.Response) error {
105105
if err = gz.Close(); err != nil {
106106
return err
107107
}
108-
resp.Body = ioutil.NopCloser(bytes.NewReader(b.Bytes()))
108+
resp.Body = io.NopCloser(bytes.NewReader(b.Bytes()))
109109
resp.ContentLength = int64(len(b.Bytes()))
110110
}
111111
return nil

endpointproxy/cloverproxy.go renamed to endpointproxy/clover.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package endpointproxy
33
import (
44
"bytes"
55
"encoding/json"
6-
"io/ioutil"
6+
"io"
77
"net/http"
88
"net/http/httputil"
99
"net/url"
@@ -39,7 +39,7 @@ func (h *CloverProxy) modifyCloverRequest(req *http.Request) {
3939
req.URL.Scheme = h.cloverTargetUrl.Scheme
4040
req.URL.Host = h.cloverTargetUrl.Host
4141
req.Host = h.cloverTargetUrl.Host
42-
reqStr, err := ioutil.ReadAll(req.Body)
42+
reqStr, err := io.ReadAll(req.Body)
4343
if err != nil {
4444
log.Errorf("invalid clover request err:%s", err.Error())
4545
return
@@ -58,6 +58,6 @@ func (h *CloverProxy) modifyCloverRequest(req *http.Request) {
5858
log.Errorf("fail to marshal this new clover req, raw:%s, err:%s", string(newMsg), marshalErr.Error())
5959
return
6060
}
61-
req.Body = ioutil.NopCloser(bytes.NewReader(newMsg))
61+
req.Body = io.NopCloser(bytes.NewReader(newMsg))
6262
req.ContentLength = int64(len(newMsg))
6363
}

endpointproxy/confluxproxy.go renamed to endpointproxy/conflux.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package endpointproxy
33
import (
44
"bytes"
55
"encoding/json"
6-
"io/ioutil"
6+
"io"
77
"net/http"
88
"net/http/httputil"
99
"net/url"
@@ -39,7 +39,7 @@ func (h *ConfluxProxy) modifyConfluxRequest(req *http.Request) {
3939
req.URL.Scheme = h.confluxTargetUrl.Scheme
4040
req.URL.Host = h.confluxTargetUrl.Host
4141
req.Host = h.confluxTargetUrl.Host
42-
reqStr, err := ioutil.ReadAll(req.Body)
42+
reqStr, err := io.ReadAll(req.Body)
4343
if err != nil {
4444
log.Errorf("invalid conflux request err:%s", err.Error())
4545
return
@@ -55,13 +55,14 @@ func (h *ConfluxProxy) modifyConfluxRequest(req *http.Request) {
5555
}
5656
if msg.Method == MethodEthCall {
5757
newParams := strings.Replace(string(msg.Params), ",\"from\":\"0x0000000000000000000000000000000000000000\"", "", 1)
58+
newParams = strings.Replace(newParams, ",\"input\":", "\"data\":", 1)
5859
msg.Params = []byte(newParams)
5960
}
6061
newMsg, marshalErr := json.Marshal(msg)
6162
if marshalErr != nil {
6263
log.Errorf("fail to marshal this new conflux req, raw:%s, err:%s", string(newMsg), marshalErr.Error())
6364
return
6465
}
65-
req.Body = ioutil.NopCloser(bytes.NewReader(newMsg))
66+
req.Body = io.NopCloser(bytes.NewReader(newMsg))
6667
req.ContentLength = int64(len(newMsg))
6768
}

endpointproxy/crabproxy.go renamed to endpointproxy/crab.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package endpointproxy
33
import (
44
"bytes"
55
"encoding/json"
6-
"io/ioutil"
6+
"io"
77
"net/http"
88
"net/http/httputil"
99
"net/url"
@@ -39,7 +39,7 @@ func (h *CrabProxy) modifyCrabRequest(req *http.Request) {
3939
req.URL.Scheme = h.crabTargetUrl.Scheme
4040
req.URL.Host = h.crabTargetUrl.Host
4141
req.Host = h.crabTargetUrl.Host
42-
reqStr, err := ioutil.ReadAll(req.Body)
42+
reqStr, err := io.ReadAll(req.Body)
4343
if err != nil {
4444
log.Errorf("invalid crab request err:%s", err.Error())
4545
return
@@ -58,6 +58,6 @@ func (h *CrabProxy) modifyCrabRequest(req *http.Request) {
5858
log.Errorf("fail to marshal this new crab req, raw:%s, err:%s", string(newMsg), marshalErr.Error())
5959
return
6060
}
61-
req.Body = ioutil.NopCloser(bytes.NewReader(newMsg))
61+
req.Body = io.NopCloser(bytes.NewReader(newMsg))
6262
req.ContentLength = int64(len(newMsg))
6363
}

endpointproxy/ethcall.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package endpointproxy
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"io"
7+
"net/http"
8+
"net/http/httputil"
9+
"net/url"
10+
"strings"
11+
12+
"github.com/celer-network/goutils/log"
13+
)
14+
15+
type EthCallProxy struct {
16+
ethCallTargetUrl *url.URL
17+
}
18+
19+
// NewProxy takes target host and creates a reverse proxy
20+
func (h *EthCallProxy) startEthCallProxy(targetHost string, port int, chainId uint64) error {
21+
var err error
22+
h.ethCallTargetUrl, err = url.Parse(targetHost)
23+
if err != nil {
24+
return err
25+
}
26+
p := httputil.NewSingleHostReverseProxy(h.ethCallTargetUrl)
27+
originalDirector := p.Director
28+
p.Director = func(req *http.Request) {
29+
originalDirector(req)
30+
h.modifyEthCallRequest(req)
31+
}
32+
mux := http.NewServeMux()
33+
mux.HandleFunc("/", proxyRequestHandler(p))
34+
go startCustomProxyByPort(port, mux, chainId, targetHost)
35+
return nil
36+
}
37+
38+
func (h *EthCallProxy) modifyEthCallRequest(req *http.Request) {
39+
req.URL.Scheme = h.ethCallTargetUrl.Scheme
40+
req.URL.Host = h.ethCallTargetUrl.Host
41+
req.Host = h.ethCallTargetUrl.Host
42+
reqStr, err := io.ReadAll(req.Body)
43+
if err != nil {
44+
log.Errorf("invalid eth_call request err:%s", err.Error())
45+
return
46+
}
47+
msg := &jsonrpcMessage{}
48+
if err = json.Unmarshal(reqStr, msg); err != nil {
49+
log.Errorf("fail to unmarshal this eth_call req body err:%s", err.Error())
50+
return
51+
}
52+
if msg.Method == MethodEthCall {
53+
newParams := strings.Replace(string(msg.Params), ",\"input\":", "\"data\":", 1)
54+
msg.Params = []byte(newParams)
55+
}
56+
newMsg, marshalErr := json.Marshal(msg)
57+
if marshalErr != nil {
58+
log.Errorf("fail to marshal this new eth_call req, raw:%s, err:%s", string(newMsg), marshalErr.Error())
59+
return
60+
}
61+
req.Body = io.NopCloser(bytes.NewReader(newMsg))
62+
req.ContentLength = int64(len(newMsg))
63+
}

endpointproxy/godwokenproxy.go renamed to endpointproxy/godwoken.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package endpointproxy
33
import (
44
"bytes"
55
"encoding/json"
6-
"io/ioutil"
6+
"io"
77
"net/http"
88
"net/http/httputil"
99
"net/url"
@@ -39,7 +39,7 @@ func (h *GodwokenProxy) modifyGodwokenRequest(req *http.Request) {
3939
req.URL.Scheme = h.godwokenTargetUrl.Scheme
4040
req.URL.Host = h.godwokenTargetUrl.Host
4141
req.Host = h.godwokenTargetUrl.Host
42-
reqStr, err := ioutil.ReadAll(req.Body)
42+
reqStr, err := io.ReadAll(req.Body)
4343
if err != nil {
4444
log.Errorf("invalid godwoken request err:%s", err.Error())
4545
return
@@ -51,13 +51,14 @@ func (h *GodwokenProxy) modifyGodwokenRequest(req *http.Request) {
5151
}
5252
if msg.Method == MethodEthCall {
5353
newParams := strings.Replace(string(msg.Params), ",\"from\":\"0x0000000000000000000000000000000000000000\"", "", 1)
54+
newParams = strings.Replace(newParams, ",\"input\":", "\"data\":", 1)
5455
msg.Params = []byte(newParams)
5556
}
5657
newMsg, marshalErr := json.Marshal(msg)
5758
if marshalErr != nil {
5859
log.Errorf("fail to marshal this new godwoken req, raw:%s, err:%s", string(newMsg), marshalErr.Error())
5960
return
6061
}
61-
req.Body = ioutil.NopCloser(bytes.NewReader(newMsg))
62+
req.Body = io.NopCloser(bytes.NewReader(newMsg))
6263
req.ContentLength = int64(len(newMsg))
6364
}

endpointproxy/harmonyproxy.go renamed to endpointproxy/harmony.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package endpointproxy
33
import (
44
"bytes"
55
"encoding/json"
6-
"io/ioutil"
6+
"io"
77
"net/http"
88
"net/http/httputil"
99
"net/url"
@@ -39,7 +39,7 @@ func (h *HarmonyProxy) modifyHarmonyRequest(req *http.Request) {
3939
req.URL.Scheme = h.harmonyTargetUrl.Scheme
4040
req.URL.Host = h.harmonyTargetUrl.Host
4141
req.Host = h.harmonyTargetUrl.Host
42-
reqStr, err := ioutil.ReadAll(req.Body)
42+
reqStr, err := io.ReadAll(req.Body)
4343
if err != nil {
4444
log.Errorf("invalid harmony request err:%s", err.Error())
4545
return
@@ -53,11 +53,15 @@ func (h *HarmonyProxy) modifyHarmonyRequest(req *http.Request) {
5353
newParams := strings.Replace(string(msg.Params), "\"pending\"", "\"latest\"", 1)
5454
msg.Params = []byte(newParams)
5555
}
56+
if msg.Method == MethodEthCall {
57+
newParams := strings.Replace(string(msg.Params), ",\"input\":", "\"data\":", 1)
58+
msg.Params = []byte(newParams)
59+
}
5660
newMsg, marshalErr := json.Marshal(msg)
5761
if marshalErr != nil {
5862
log.Errorf("fail to marshal this new harmony req, raw:%s, err:%s", string(newMsg), marshalErr.Error())
5963
return
6064
}
61-
req.Body = ioutil.NopCloser(bytes.NewReader(newMsg))
65+
req.Body = io.NopCloser(bytes.NewReader(newMsg))
6266
req.ContentLength = int64(len(newMsg))
6367
}

endpointproxy/ontologyproxy.go renamed to endpointproxy/ontology.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package endpointproxy
33
import (
44
"bytes"
55
"encoding/json"
6-
"io/ioutil"
6+
"io"
77
"net/http"
88
"net/http/httputil"
99
"net/url"
@@ -45,7 +45,7 @@ func (c *OntologyProxy) modifyOntologyRequest(req *http.Request) {
4545
req.URL.Scheme = c.ontologyTargetUrl.Scheme
4646
req.URL.Host = c.ontologyTargetUrl.Host
4747
req.Host = c.ontologyTargetUrl.Host
48-
reqStr, err := ioutil.ReadAll(req.Body)
48+
reqStr, err := io.ReadAll(req.Body)
4949
if err != nil {
5050
log.Warnf("invalid ontology request err:%s", err.Error())
5151
return
@@ -55,19 +55,23 @@ func (c *OntologyProxy) modifyOntologyRequest(req *http.Request) {
5555
log.Warnf("fail to unmarshal this ontology req body err:%s", err.Error())
5656
return
5757
}
58+
if msg.Method == MethodEthCall {
59+
newParams := strings.Replace(string(msg.Params), ",\"input\":", "\"data\":", 1)
60+
msg.Params = []byte(newParams)
61+
}
5862
req.Header.Set(ontologyHeaderRpcMethod, msg.Method)
59-
req.Body = ioutil.NopCloser(bytes.NewReader(reqStr))
63+
req.Body = io.NopCloser(bytes.NewReader(reqStr))
6064
}
6165

6266
func modifyOntologyResponse() func(*http.Response) error {
6367
return func(resp *http.Response) error {
6468
if resp.Request != nil && resp.Request.Header.Get(ontologyHeaderRpcMethod) == MethodEthGetBlockByNumber {
65-
originData, err := ioutil.ReadAll(resp.Body)
69+
originData, err := io.ReadAll(resp.Body)
6670
if err != nil {
6771
return err
6872
}
6973
newData := strings.Replace(string(originData), "\"stateRoot\":\"0x\"", "\"stateRoot\":\"0x0000000000000000000000000000000000000000000000000000000000000000\"", 1)
70-
resp.Body = ioutil.NopCloser(bytes.NewReader([]byte(newData)))
74+
resp.Body = io.NopCloser(bytes.NewReader([]byte(newData)))
7175
resp.ContentLength = int64(len([]byte(newData)))
7276
resp.Header.Set("Content-Length", strconv.Itoa(len(newData)))
7377
}

0 commit comments

Comments
 (0)