go-eth-proxy

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

commit 452eef16a4413a1084dd32616b3aeb7a04d472c6
parent b5fde2c62dd123ad5ccb8f52c0e48e2ed62752d0
Author: lash <dev@holbrook.no>
Date:   Wed,  3 Jul 2024 22:49:31 +0100

WIP Implement literal proxy

Diffstat:
Mcmd/geth_proxy/main.go | 11++++++++++-
Acmd/main.go | 46++++++++++++++++++++++++++++++++++++++++++++++
Mrpc/geth/service.go | 7+++----
Mrpc/rpc.go | 4++++
Mrpc/service.go | 26++++++++++++++++++--------
Mstore/lmdb/lmdb.go | 8++++----
Mstore/store.go | 7++-----
7 files changed, 87 insertions(+), 22 deletions(-)

diff --git a/cmd/geth_proxy/main.go b/cmd/geth_proxy/main.go @@ -8,12 +8,14 @@ import ( "strings" "defalsify.org/go-eth-proxy/rpc/geth" + "defalsify.org/go-eth-proxy/rpc" "defalsify.org/go-eth-proxy/store/lmdb" ) func main() { + log.SetOutput(os.Stderr) dbpath := flag.String("cachepath", ".", "Path to lmdb data") host := flag.String("host", "0.0.0.0", "Remote host") port := flag.String("port", "8545", "Remote path") @@ -31,8 +33,15 @@ func main() { log.Printf("%s", err) os.Exit(1) } + + prx, err := rpc.NewProxyServer(h, flag.Arg(0)) + if err != nil { + log.Printf("%s", err) + os.Exit(1) + } + srv := &http.Server{ - Handler: h, + Handler: prx, Addr: strings.Join([]string{*host, *port}, ":"), } err = srv.ListenAndServe() diff --git a/cmd/main.go b/cmd/main.go @@ -0,0 +1,46 @@ +package main + +import ( + "flag" + "log" + "os" + "net/http" + "strings" + + "defalsify.org/go-eth-proxy/rpc" + "defalsify.org/go-eth-proxy/store/lmdb" + +) + +func main() { + log.SetOutput(os.Stderr) + dbpath := flag.String("cachepath", ".", "Path to lmdb data") + host := flag.String("host", "0.0.0.0", "Remote host") + port := flag.String("port", "8545", "Remote path") + flag.Parse() + + db, err := lmdb.NewStore(*dbpath) + if err != nil { + log.Printf("%s", err) + os.Exit(1) + } + defer db.Close() + + svc := rpc.NewProxyService(db) + h := rpc.NewBackend(svc) //svc, flag.Arg(0)) + prx, err := rpc.NewProxyServer(h, flag.Arg(0)) + if err != nil { + log.Printf("%s", err) + os.Exit(1) + } + + srv := &http.Server{ + Handler: prx, + Addr: strings.Join([]string{*host, *port}, ":"), + } + err = srv.ListenAndServe() + if err != nil { + log.Printf("%s", err) + os.Exit(1) + } +} diff --git a/rpc/geth/service.go b/rpc/geth/service.go @@ -2,8 +2,9 @@ package geth import ( "context" + "log" - "github.com/go-ethereum/ethereum/core/types" + "github.com/ethereum/go-ethereum/core/types" "defalsify.org/go-eth-proxy/store" ) @@ -22,12 +23,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(b) + err := tx.UnmarshalJSON([]byte(hsh)) if err != nil { return nil, err } log.Printf("tx %s gasprice %u gas %u", tx.Type(), tx.GasPrice(), tx.Gas()) - return tx, err - return tx, nil } diff --git a/rpc/rpc.go b/rpc/rpc.go @@ -29,6 +29,8 @@ type jsonRpcResponse struct { } type jsonRpcResponseFull struct { + Jsonrpc string + Id string Result any } @@ -96,6 +98,7 @@ func NewProxyServer(backend http.Handler, remoteURI string) (*ProxyServer, error Server: backend, uri: uri, } + log.Printf("proxy server shadowing: %s", uri) return srv, nil } @@ -163,6 +166,7 @@ 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,9 +1,10 @@ package rpc import ( + "bytes" "context" - - "github.com/ethereum/go-ethereum/common" + "encoding/json" + "log" "defalsify.org/go-eth-proxy/store" ) @@ -20,16 +21,25 @@ func NewProxyService(store store.Store) (*LiteralProxyService) { func (p *LiteralProxyService) GetTransactionByHash(ctx context.Context, hsh string) ([]byte, error) { - b := common.FromHex(hsh) - tx, err := p.store.GetTransaction(b) + var err error + + b := []byte(hsh) + b, err = p.store.GetTransaction(b) if err != nil { return nil, err } - b, err = tx.MarshalJSON() - if err != nil { - return nil, err + log.Printf("gettin for %x %s", hsh, b) + j := &jsonRpcResponseFull{ + Jsonrpc: "2.0", + Id: "", + Result: string(b), } - return b, nil + b = []byte{} + r := bytes.NewBuffer(b) + o := json.NewEncoder(r) + o.Encode(j) + + return r.Bytes(), nil } diff --git a/store/lmdb/lmdb.go b/store/lmdb/lmdb.go @@ -3,7 +3,6 @@ package lmdb import ( "log" - "github.com/ethereum/go-ethereum/core/types" "github.com/ledgerwatch/lmdb-go/lmdb" "defalsify.org/go-eth-proxy/store" @@ -49,8 +48,8 @@ func NewStore(path string) (*LmdbStore, error) { } -func (l *LmdbStore) GetTransaction(k []byte) (*types.Transaction, error) { - log.Printf("get tx hash %s", hsh) +//func (l *LmdbStore) GetTransaction(k []byte) (*types.Transaction, error) { +func (l *LmdbStore) GetTransaction(k []byte) ([]byte, error) { var b []byte kp := make([]byte, len(k) + 7) @@ -68,9 +67,10 @@ func (l *LmdbStore) GetTransaction(k []byte) (*types.Transaction, error) { return nil }) if err != nil { - return tx, err + return nil, err } log.Printf("lmdn result: %s", b) + return b, nil } diff --git a/store/store.go b/store/store.go @@ -1,11 +1,8 @@ package store -import ( - "github.com/ethereum/go-ethereum/core/types" -) - type Store interface { - GetTransaction(b []byte) (*types.Transaction, error) + //GetTransaction(b []byte) (*types.Transaction, error) + GetTransaction(b []byte) ([]byte, error) Close() }