go-eth-proxy

Transparent proxy server for eth-cache
Info | Log | Files | Refs

commit d57f85a0e69e6926b817a9c2935cb86fd6caf4f6
parent 452eef16a4413a1084dd32616b3aeb7a04d472c6
Author: lash <dev@holbrook.no>
Date:   Thu,  4 Jul 2024 16:35:47 +0100

Complete literal return for transaction

Diffstat:
Mrpc/geth/service.go | 6+++++-
Mrpc/literal.go | 3+--
Mrpc/rpc.go | 21++++-----------------
Mrpc/service.go | 24+++++++-----------------
Mstore/lmdb/lmdb.go | 3+--
5 files changed, 18 insertions(+), 39 deletions(-)

diff --git a/rpc/geth/service.go b/rpc/geth/service.go @@ -5,6 +5,7 @@ import ( "log" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/common" "defalsify.org/go-eth-proxy/store" ) @@ -23,7 +24,10 @@ func NewProxyService(store store.Store) (*ProxyService) { func (p *ProxyService) GetTransactionByHash(ctx context.Context, hsh string) (*types.Transaction, error) { tx := &types.Transaction{} - err := tx.UnmarshalJSON([]byte(hsh)) + b := common.FromHex(hsh) + r, err := p.store.GetTransaction(b) + + err = tx.UnmarshalJSON(r) if err != nil { return nil, err } diff --git a/rpc/literal.go b/rpc/literal.go @@ -47,7 +47,7 @@ func (l *literalBackend) ServeHTTP(w http.ResponseWriter, r *http.Request) { switch msg.Method { case "eth_getTransactionByHash": - b, err = l.svc.GetTransactionByHash(r.Context(), msg.Params[0].(string)) + b, err = l.svc.GetTransactionByHash(r.Context(), msg.Id, msg.Params[0].(string)) default: s := fmt.Sprintf("Status: %d Method not supported", http.StatusBadRequest) w.Write([]byte(s)) @@ -62,5 +62,4 @@ func (l *literalBackend) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) w.Write([]byte(b)) - } diff --git a/rpc/rpc.go b/rpc/rpc.go @@ -8,7 +8,6 @@ import ( "log" "net/http" "net/url" -// "strconv" ) type jsonRpcMsg struct { @@ -17,6 +16,7 @@ type jsonRpcMsg struct { type jsonRpcMsgFull struct { Method string + Id string Params []any } @@ -29,9 +29,9 @@ type jsonRpcResponse struct { } type jsonRpcResponseFull struct { - Jsonrpc string - Id string - Result any + Jsonrpc string `json:"jsonrpc"` + Id string `json:"id"` + Result any `json:"result"` } type ProxyServer struct { @@ -103,7 +103,6 @@ func NewProxyServer(backend http.Handler, remoteURI string) (*ProxyServer, error } func (s *ProxyServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { - //var rrr io.Reader msg := jsonRpcMsg{} b := make([]byte, r.ContentLength) c, err := io.ReadFull(r.Body, b) @@ -139,18 +138,7 @@ func (s *ProxyServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { 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 @@ -166,7 +154,6 @@ func (s *ProxyServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusBadGateway) return } - log.Printf("foo") client_req := &http.Request{} client_req.Method = "POST" diff --git a/rpc/service.go b/rpc/service.go @@ -1,10 +1,10 @@ package rpc import ( - "bytes" "context" - "encoding/json" - "log" + "fmt" + + "github.com/ethereum/go-ethereum/common" "defalsify.org/go-eth-proxy/store" ) @@ -20,26 +20,16 @@ func NewProxyService(store store.Store) (*LiteralProxyService) { } -func (p *LiteralProxyService) GetTransactionByHash(ctx context.Context, hsh string) ([]byte, error) { +func (p *LiteralProxyService) GetTransactionByHash(ctx context.Context, id string, hsh string) ([]byte, error) { var err error - b := []byte(hsh) + b := common.FromHex(hsh) b, err = p.store.GetTransaction(b) if err != nil { return nil, err } - log.Printf("gettin for %x %s", hsh, b) - j := &jsonRpcResponseFull{ - Jsonrpc: "2.0", - Id: "", - Result: string(b), - } - - b = []byte{} - r := bytes.NewBuffer(b) - o := json.NewEncoder(r) - o.Encode(j) + s := fmt.Sprintf("{\"jsonrpc\":\"2.0\",\"id\":\"%s\",\"result\":%s}", id, b) - return r.Bytes(), nil + return []byte(s), nil } diff --git a/store/lmdb/lmdb.go b/store/lmdb/lmdb.go @@ -48,7 +48,6 @@ func NewStore(path string) (*LmdbStore, error) { } -//func (l *LmdbStore) GetTransaction(k []byte) (*types.Transaction, error) { func (l *LmdbStore) GetTransaction(k []byte) ([]byte, error) { var b []byte @@ -66,10 +65,10 @@ func (l *LmdbStore) GetTransaction(k []byte) ([]byte, error) { copy(b, v) return nil }) + log.Printf("lmdb result: %s", b) if err != nil { return nil, err } - log.Printf("lmdn result: %s", b) return b, nil }