go-eth-proxy

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

service.go (1745B)


      1 package rpc
      2 
      3 import (
      4 	"context"
      5 	"fmt"
      6 	"log"
      7 
      8 	"github.com/ethereum/go-ethereum/common"
      9 
     10 	"defalsify.org/go-eth-proxy/store"
     11 )
     12 
     13 type LiteralProxyService struct {
     14 	store store.Store
     15 }
     16 
     17 func NewProxyService(store store.Store) (*LiteralProxyService) {
     18 	return &LiteralProxyService{
     19 		store: store,
     20 	}
     21 }
     22 
     23 func wrapResult(id any, result []byte) ([]byte, error) {
     24 	var s string
     25 	var id_i int
     26 	id_s, ok := id.(string)
     27 	if ok {
     28 		s = fmt.Sprintf("{\"jsonrpc\":\"2.0\",\"id\":\"%s\",\"result\":%s}", id_s, result)
     29 	} else {
     30 		id_i, ok = id.(int)
     31 		if !ok {
     32 			return nil, fmt.Errorf("id not valid type")
     33 		}
     34 		s = fmt.Sprintf("{\"jsonrpc\":\"2.0\",\"id\":%d,\"result\":%s}", id_i, result)
     35 	}
     36 	return []byte(s), nil
     37 }
     38 
     39 func (p *LiteralProxyService) GetTransactionByHash(ctx context.Context, id any, hsh string) ([]byte, error) {
     40 	var err error
     41 
     42 	b := common.FromHex(hsh)
     43 	b, err = p.store.GetTransaction(b)
     44 	if err != nil {
     45 		return nil, err
     46 	}
     47 
     48 	return wrapResult(id, b)
     49 }
     50 
     51 func (p *LiteralProxyService) GetTransactionReceipt(ctx context.Context, id any, hsh string) ([]byte, error) {
     52 	var err error
     53 
     54 	b := common.FromHex(hsh)
     55 	b, err = p.store.GetTransactionReceipt(b)
     56 	if err != nil {
     57 		return nil, err
     58 	}
     59 
     60 	return wrapResult(id, b)
     61 }
     62 
     63 func (p *LiteralProxyService) GetBlockByHash(ctx context.Context, id any, hsh string) ([]byte, error) {
     64 	var err error
     65 
     66 	b := common.FromHex(hsh)
     67 	b, err = p.store.GetBlock(b)
     68 	if err != nil {
     69 		return nil, err
     70 	}
     71 
     72 	return wrapResult(id, b)
     73 }
     74 
     75 func (p *LiteralProxyService) GetBlockByNumber(ctx context.Context, id any, numhex string) ([]byte, error) {
     76 	b := common.FromHex(numhex)
     77 	b, err := p.store.GetBlockNumber(b)
     78 	log.Printf("result %v", b)
     79 	if err != nil {
     80 		return nil, err
     81 	}
     82 
     83 	return wrapResult(id, b)
     84 }