go-eth-proxy

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

commit aaf37609b603e33bc6b66980cc71f0614dab704d
parent e4331cc2950a0352008bd60da3c748b6656d5943
Author: lash <dev@holbrook.no>
Date:   Thu,  4 Jul 2024 17:52:36 +0100

Fix block resolve, add receipt getter

Diffstat:
Mrpc/literal.go | 7+++++++
Mrpc/rpc.go | 1+
Mrpc/service.go | 44++++++++++++++++++++++++++++++++++++++++++--
Mstore/lmdb/lmdb.go | 19++++++++++---------
Mstore/store.go | 4+++-
5 files changed, 63 insertions(+), 12 deletions(-)

diff --git a/rpc/literal.go b/rpc/literal.go @@ -45,9 +45,16 @@ func (l *literalBackend) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } + // TODO: make sure getblockbynumber is hex input switch msg.Method { case "eth_getTransactionByHash": b, err = l.svc.GetTransactionByHash(r.Context(), msg.Id, msg.Params[0].(string)) + case "eth_getTransactionReceipt": + b, err = l.svc.GetTransactionReceipt(r.Context(), msg.Id, msg.Params[0].(string)) + case "eth_getBlockByHash": + b, err = l.svc.GetBlockByHash(r.Context(), msg.Id, msg.Params[0].(string)) + case "eth_getBlockByNumber": + b, err = l.svc.GetBlockByNumber(r.Context(), msg.Id, msg.Params[0].(string)) default: s := fmt.Sprintf("Status: %d Method not supported", http.StatusBadRequest) w.Write([]byte(s)) diff --git a/rpc/rpc.go b/rpc/rpc.go @@ -126,6 +126,7 @@ func (s *ProxyServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { for _, k := range([]string{ "eth_getTransactionByHash", + "eth_getTransactionReceipt", "eth_getBlockByNumber", "eth_getBlockByHash", }) { diff --git a/rpc/service.go b/rpc/service.go @@ -19,6 +19,10 @@ func NewProxyService(store store.Store) (*LiteralProxyService) { } } +func wrapResult(id string, result []byte) []byte { + s := fmt.Sprintf("{\"jsonrpc\":\"2.0\",\"id\":\"%s\",\"result\":%s}", id, result) + return []byte(s) +} func (p *LiteralProxyService) GetTransactionByHash(ctx context.Context, id string, hsh string) ([]byte, error) { var err error @@ -29,7 +33,43 @@ func (p *LiteralProxyService) GetTransactionByHash(ctx context.Context, id strin return nil, err } - s := fmt.Sprintf("{\"jsonrpc\":\"2.0\",\"id\":\"%s\",\"result\":%s}", id, b) + r := wrapResult(id, b) + return r, nil +} + +func (p *LiteralProxyService) GetTransactionReceipt(ctx context.Context, id string, hsh string) ([]byte, error) { + var err error + + b := common.FromHex(hsh) + b, err = p.store.GetTransactionReceipt(b) + if err != nil { + return nil, err + } + + r := wrapResult(id, b) + return r, nil +} + +func (p *LiteralProxyService) GetBlockByHash(ctx context.Context, id string, hsh string) ([]byte, error) { + var err error + + b := common.FromHex(hsh) + b, err = p.store.GetBlock(b) + if err != nil { + return nil, err + } + + r := wrapResult(id, b) + return r, nil +} + +func (p *LiteralProxyService) GetBlockByNumber(ctx context.Context, id string, numhex string) ([]byte, error) { + b := common.FromHex(numhex) + b, err := p.store.GetBlockNumber(b) + if err != nil { + return nil, err + } - return []byte(s), nil + r := wrapResult(id, b) + return r, nil } diff --git a/store/lmdb/lmdb.go b/store/lmdb/lmdb.go @@ -1,7 +1,6 @@ package lmdb import ( - "encoding/binary" "log" "github.com/ledgerwatch/lmdb-go/lmdb" @@ -78,20 +77,22 @@ func (l *LmdbStore) GetTransaction(k []byte) ([]byte, error) { return l.get("tx/src/", k) } -func (l *LmdbStore) GetBlockNumber(n uint64) ([]byte, error) { - var err error +func (l *LmdbStore) GetBlockNumber(n []byte) ([]byte, error) { b := make([]byte, 8) - binary.BigEndian.PutUint64(b, n) - - b, err = l.get("block/num/", b) + copy(b[8-len(n):], n) + k, err := l.get("block/num/", b) if err != nil { return nil, err } - return l.get("block/hash/", b) + return l.get("block/src/", k) +} + +func (l *LmdbStore) GetBlock(k []byte) ([]byte, error) { + return l.get("block/src/", k) } -func (l *LmdbStore) GetBlockHash(k []byte) ([]byte, error) { - return l.get("block/hash/", k) +func (l *LmdbStore) GetTransactionReceipt(k []byte) ([]byte, error) { + return l.get("rcpt/src/", k) } func (l *LmdbStore) Close() { diff --git a/store/store.go b/store/store.go @@ -2,6 +2,8 @@ package store type Store interface { GetTransaction(b []byte) ([]byte, error) + GetBlock(b []byte) ([]byte, error) + GetBlockNumber(n []byte) ([]byte, error) + GetTransactionReceipt(b []byte) ([]byte, error) Close() } -