commit b5fde2c62dd123ad5ccb8f52c0e48e2ed62752d0
parent 320e5e15501e03a3855bd9e1ef78b8f384e754bb
Author: lash <dev@holbrook.no>
Date: Wed, 3 Jul 2024 21:58:11 +0100
WIP Add literal proxy service
Diffstat:
5 files changed, 119 insertions(+), 14 deletions(-)
diff --git a/rpc/geth/service.go b/rpc/geth/service.go
@@ -2,14 +2,13 @@ package geth
import (
"context"
- "log"
-
- "github.com/ethereum/go-ethereum/core/types"
- "github.com/ethereum/go-ethereum/common"
+
+ "github.com/go-ethereum/ethereum/core/types"
"defalsify.org/go-eth-proxy/store"
)
+
type ProxyService struct {
store store.Store
}
@@ -21,12 +20,14 @@ func NewProxyService(store store.Store) (*ProxyService) {
}
func (p *ProxyService) GetTransactionByHash(ctx context.Context, hsh string) (*types.Transaction, error) {
- log.Printf("get tx hash %s", hsh)
- b := common.FromHex(hsh)
- tx, err := p.store.GetTransaction(b)
+ tx := &types.Transaction{}
+
+ 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
return tx, nil
}
diff --git a/rpc/literal.go b/rpc/literal.go
@@ -0,0 +1,66 @@
+package rpc
+
+import (
+ "encoding/json"
+ "fmt"
+ "io/ioutil"
+ "net/http"
+)
+
+type literalBackend struct {
+ svc *LiteralProxyService
+}
+
+func NewBackend(svc *LiteralProxyService) *literalBackend {
+ return &literalBackend {
+ svc: svc,
+ }
+
+}
+
+func inJson(b []byte) (*jsonRpcMsgFull, error) {
+ msg := &jsonRpcMsgFull{}
+ err := json.Unmarshal(b, msg)
+ return msg, err
+}
+
+func outJson(msg *jsonRpcResponse) []byte {
+ return []byte{}
+}
+
+func (l *literalBackend) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+ var err error
+
+ b, err := ioutil.ReadAll(r.Body)
+ if err != nil {
+ s := fmt.Sprintf("Status: %d Not valid jsonrpc request", http.StatusInternalServerError)
+ w.Write([]byte(s))
+ return
+ }
+
+ msg, err := inJson(b)
+ if err != nil {
+ s := fmt.Sprintf("Status: %d Not valid jsonrpc request", http.StatusBadRequest)
+ w.Write([]byte(s))
+ return
+ }
+
+ switch msg.Method {
+ case "eth_getTransactionByHash":
+ b, err = l.svc.GetTransactionByHash(r.Context(), msg.Params[0].(string))
+ default:
+ s := fmt.Sprintf("Status: %d Method not supported", http.StatusBadRequest)
+ w.Write([]byte(s))
+ return
+ }
+
+ if err != nil {
+ s := fmt.Sprintf("Status: %d Not found", http.StatusNotFound)
+ w.Write([]byte(s))
+ return
+ }
+
+ w.WriteHeader(http.StatusOK)
+ w.Write([]byte(b))
+
+}
diff --git a/rpc/rpc.go b/rpc/rpc.go
@@ -13,7 +13,11 @@ import (
type jsonRpcMsg struct {
Method string
+}
+type jsonRpcMsgFull struct {
+ Method string
+ Params []any
}
type jsonRpcError struct {
@@ -24,6 +28,10 @@ type jsonRpcResponse struct {
Error jsonRpcError
}
+type jsonRpcResponseFull struct {
+ Result any
+}
+
type ProxyServer struct {
Server http.Handler
uri *url.URL
@@ -177,3 +185,4 @@ func (s *ProxyServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
rrr := io.TeeReader(res.Body, w)
io.ReadAll(rrr)
}
+
diff --git a/rpc/service.go b/rpc/service.go
@@ -0,0 +1,35 @@
+package rpc
+
+import (
+ "context"
+
+ "github.com/ethereum/go-ethereum/common"
+
+ "defalsify.org/go-eth-proxy/store"
+)
+
+type LiteralProxyService struct {
+ store store.Store
+}
+
+func NewProxyService(store store.Store) (*LiteralProxyService) {
+ return &LiteralProxyService{
+ store: store,
+ }
+}
+
+
+func (p *LiteralProxyService) GetTransactionByHash(ctx context.Context, hsh string) ([]byte, error) {
+ b := common.FromHex(hsh)
+ tx, err := p.store.GetTransaction(b)
+ if err != nil {
+ return nil, err
+ }
+
+ b, err = tx.MarshalJSON()
+ if err != nil {
+ return nil, err
+ }
+
+ return b, nil
+}
diff --git a/store/lmdb/lmdb.go b/store/lmdb/lmdb.go
@@ -50,8 +50,8 @@ func NewStore(path string) (*LmdbStore, error) {
func (l *LmdbStore) GetTransaction(k []byte) (*types.Transaction, error) {
+ log.Printf("get tx hash %s", hsh)
var b []byte
- tx := &types.Transaction{}
kp := make([]byte, len(k) + 7)
copy(kp, []byte("tx/src/"))
@@ -71,12 +71,6 @@ func (l *LmdbStore) GetTransaction(k []byte) (*types.Transaction, error) {
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
}