go-eth-proxy

Unnamed repository; edit this file 'description' to name the repository.
Info | Log | Files | Refs

commit eed8c23464cf55ca6090c7f1eabf2734d3d12fd5
parent 6900aaae1de2543730432f1330ac07b0d93f8805
Author: lash <dev@holbrook.no>
Date:   Thu, 27 Jun 2024 01:40:08 +0100

Add lmdb retriever, connect to proxy server

Diffstat:
Mgo.mod | 5++++-
Aproxy/rpc.go | 3+++
Mproxy/service.go | 19+++++++++++++++++--
Mproxy/service_test.go | 29+++++++++++++++++++++++------
Astore/lmdb/lmdb.go | 81+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Astore/store.go | 10++++++++++
6 files changed, 138 insertions(+), 9 deletions(-)

diff --git a/go.mod b/go.mod @@ -2,7 +2,10 @@ module defalsify.org/go-eth-proxy go 1.22.4 -require github.com/ethereum/go-ethereum v1.14.5 +require ( + github.com/ethereum/go-ethereum v1.14.5 + github.com/ledgerwatch/lmdb-go v1.18.2 +) require ( github.com/Microsoft/go-winio v0.6.2 // indirect diff --git a/proxy/rpc.go b/proxy/rpc.go @@ -0,0 +1,3 @@ +package proxy + + diff --git a/proxy/service.go b/proxy/service.go @@ -4,12 +4,27 @@ import ( "context" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/common" + + "defalsify.org/go-eth-proxy/store" ) type ProxyService struct { + store store.Store } -func (p *ProxyService) GetTransaction(ctx context.Context, hsh string) (*types.Transaction, error) { - return nil, nil +func NewProxyService(store store.Store) (*ProxyService) { + return &ProxyService{ + store: store, + } } +func (p *ProxyService) GetTransaction(ctx context.Context, hsh string) (*types.Transaction, error) { + b := common.FromHex(hsh) + tx, err := p.store.GetTransaction(b) + if err != nil { + return nil, err + } + + return tx, nil +} diff --git a/proxy/service_test.go b/proxy/service_test.go @@ -1,30 +1,47 @@ package proxy import ( + "log" + "os" "testing" "github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/core/types" + + "defalsify.org/go-eth-proxy/store/lmdb" ) func TestProxyServerStart(t *testing.T) { var err error var tx types.Transaction + tx_test := "0x1c7770f04251de106344bc5e4c25a27143db9d40504045039d3d25c5b20b7740" - srv := rpc.NewServer() - svc := ProxyService{} - err = srv.RegisterName("eth", &svc) + dbpath, dbenv := os.LookupEnv("TEST_LMDB_DIR") + if !dbenv { + dbpath = "." + } + log.Printf("dbpath %s", dbpath) + + db, err := lmdb.NewStore(dbpath) if err != nil { t.Error(err) } + defer db.Close() + svc := NewProxyService(db) + + srv := rpc.NewServer() + err = srv.RegisterName("eth", svc) + if err != nil { + t.Fatal(err) + } client := rpc.DialInProc(srv) mods, err := client.SupportedModules() if err != nil { - t.Error(err) + t.Fatal(err) } t.Logf("mods %s", mods) - err = client.Call(&tx, "eth_getTransaction", "foo") + err = client.Call(&tx, "eth_getTransaction", tx_test) if err != nil { - t.Error(err) + t.Fatal(err) } } diff --git a/store/lmdb/lmdb.go b/store/lmdb/lmdb.go @@ -0,0 +1,81 @@ +package lmdb + +import ( + "log" + + "github.com/ethereum/go-ethereum/core/types" + "github.com/ledgerwatch/lmdb-go/lmdb" + + "defalsify.org/go-eth-proxy/store" +) + +type LmdbStore struct { + store store.Store + env *lmdb.Env + dbi lmdb.DBI +} + +func NewStore(path string) (*LmdbStore, error) { + var err error + + o := &LmdbStore{} + o.env, err = lmdb.NewEnv() + if err != nil { + return nil, err + } + err = o.env.SetMaxDBs(1) + if err != nil { + return nil, err + } + err = o.env.SetMapSize(1 << 30) + if err != nil { + return nil, err + } + err = o.env.Open(path, 0, 0644) + if err != nil { + return nil, err + } + err = o.env.Update(func(txn *lmdb.Txn) (error) { + var err error + o.dbi, err = txn.OpenRoot(0) + return err + }) + if err != nil { + return nil, err + } + return o, nil +} + + +func (l *LmdbStore) GetTransaction(k []byte) (*types.Transaction, error) { + var b []byte + tx := &types.Transaction{} + + kp := make([]byte, len(k) + 7) + copy(kp, []byte("tx/raw/")) + copy(kp[7:], k) + + err := l.env.View(func(txn *lmdb.Txn) (error) { + log.Printf("gettx %x %v", kp, txn) + v, err := txn.Get(l.dbi, kp) + if err != nil { + return err + } + b = make([]byte, len(v)) + copy(b, v) + return nil + }) + if err != nil { + return tx, err + } + err = tx.UnmarshalBinary(b) + if err != nil { + return nil, err + } + return tx, err +} + + +func (l *LmdbStore) Close() { + l.env.Close() +} diff --git a/store/store.go b/store/store.go @@ -0,0 +1,10 @@ +package store + +import ( + "github.com/ethereum/go-ethereum/core/types" +) + +type Store interface { + GetTransaction(b []byte) (*types.Transaction, error) + Close() +}