go-eth-proxy

Unnamed repository; edit this file 'description' to name the repository.
Info | Log | Files | Refs

commit 081d6d437cf328bd7ea8e95fb0cd2a8d33f1dce9
parent b79acb89533373e41477aa70ec8634d1a060dd21
Author: lash <dev@holbrook.no>
Date:   Sat, 29 Jun 2024 14:21:13 +0100

Defer to remote if not found

Diffstat:
Mproxy/rpc.go | 100+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
Mproxy/service_test.go | 1+
Mstore/lmdb/lmdb.go | 4++++
3 files changed, 95 insertions(+), 10 deletions(-)

diff --git a/proxy/rpc.go b/proxy/rpc.go @@ -7,22 +7,76 @@ import ( "log" "net/http" "net/url" +// "strconv" "github.com/ethereum/go-ethereum/rpc" ) type jsonRpcMsg struct { - Jsonrpc string - Id string Method string - Params []any } + +type jsonRpcError struct { + Code int +} + +type jsonRpcResponse struct { + Error jsonRpcError +} + type ProxyServer struct { *rpc.Server uri *url.URL } +type proxyWriter struct { + header map[string][]string + status int + data *bytes.Buffer + afterHeader bool +} + + +func (p *proxyWriter) Header() http.Header { + return p.header +} + +func (p *proxyWriter) Write(b []byte) (int, error) { + log.Printf("proxyserver %s", b) + return p.data.Write(b) +} + +func (p *proxyWriter) WriteHeader(status int) { + p.status = status + +} + +func (p *proxyWriter) Copy(w http.ResponseWriter) (int, error) { + c := 0 + l := p.data.Len() + b := p.data.Bytes() + for ;c < l; { + r, err := w.Write(b[c:]) + if err != nil { + return 0, err + } + c += r + } + return c, nil +} + +func newProxyWriter() *proxyWriter { + b := make([]byte, 0, 1024) + p := &proxyWriter{ + header: make(map[string][]string), + data: bytes.NewBuffer(b), + } + return p +} + + + func NewProxyServer(svc *ProxyService, remoteURI string) (*ProxyServer, error) { var uri *url.URL var err error @@ -45,7 +99,7 @@ func NewProxyServer(svc *ProxyService, remoteURI string) (*ProxyServer, error) { } func (s *ProxyServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { - var rr io.Reader + //var rrr io.Reader msg := jsonRpcMsg{} b := make([]byte, r.ContentLength) c, err := io.ReadFull(r.Body, b) @@ -64,16 +118,42 @@ func (s *ProxyServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - rr = bytes.NewReader(b) + rr := bytes.NewReader(b) r.Body = io.NopCloser(rr) for _, k := range([]string{ "eth_getTransactionByHash", }) { + rw := newProxyWriter() if msg.Method == k { - log.Printf("match %s", msg.Method) - s.Server.ServeHTTP(w, r) - return + s.Server.ServeHTTP(rw, r) + rsp := jsonRpcResponse{} + err = json.Unmarshal(b, &rsp) + if (err != nil) { + log.Printf("%s", err) + r.Body.Close() + w.WriteHeader(http.StatusInternalServerError) + return + } + //hd := rw.Header() + //statusHd := hd["status"] + //log.Printf("got status %s from proxy", statusHd) + //if len(statusHd) > 0 && statusHd[0][:1] == "2" { + if rsp.Error.Code == 0 { +// statusCode, err := strconv.Atoi(hd["Status"][0]) +// if err != nil { +// r.Body.Close() +// w.WriteHeader(http.StatusInternalServerError) +// return +// } +// rw.WriteHeader(statusCode) + rw.WriteHeader(http.StatusOK) + rw.Copy(w) + return + } + + log.Printf("not found in proxy: %s", k) + rr.Seek(0, io.SeekStart) } } @@ -101,6 +181,6 @@ func (s *ProxyServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { log.Printf("%s", v) } w.WriteHeader(res.StatusCode) - rr = io.TeeReader(res.Body, w) - io.ReadAll(rr) + rrr := io.TeeReader(res.Body, w) + io.ReadAll(rrr) } diff --git a/proxy/service_test.go b/proxy/service_test.go @@ -15,6 +15,7 @@ func TestProxyServerStart(t *testing.T) { var err error var tx types.Transaction tx_test := "0x60891c813816bb378ee8af428c5eb53b0479c980307d265e4abe39b4efd02e1d" + //tx_test := "0xffee674f0467b59ce6dd455e9256f95f41118a5bf15a3cedf3d20b5fcbb787a1" dbpath, dbenv := os.LookupEnv("TEST_LMDB_DIR") if !dbenv { diff --git a/store/lmdb/lmdb.go b/store/lmdb/lmdb.go @@ -15,9 +15,11 @@ type LmdbStore struct { dbi lmdb.DBI } +/// TODO: not create func NewStore(path string) (*LmdbStore, error) { var err error + log.Printf("lmdb store path: %s", path) o := &LmdbStore{} o.env, err = lmdb.NewEnv() if err != nil { @@ -68,10 +70,12 @@ func (l *LmdbStore) GetTransaction(k []byte) (*types.Transaction, error) { if err != nil { return tx, err } + log.Printf("lmdn result: %s", b) err = tx.UnmarshalJSON(b) if err != nil { return nil, err } + log.Printf("tx %s gasprice %u gas %u", tx.Type(), tx.GasPrice(), tx.Gas()) return tx, err }