From a1318f869a0c6996e8b5cb8103721e0144ad71d3 Mon Sep 17 00:00:00 2001
From: Alex Sharov <AskAlexSharov@gmail.com>
Date: Fri, 11 Feb 2022 17:11:59 +0700
Subject: [PATCH] make services net-independent (#3481)

---
 cmd/rpcdaemon/cli/config.go                  | 17 +++++++++++------
 cmd/rpcdaemon/commands/eth_subscribe_test.go |  3 ++-
 cmd/rpcdaemon/services/eth_backend.go        |  4 ++--
 cmd/rpcdaemon/services/eth_mining.go         |  4 ++--
 cmd/rpcdaemon/services/eth_txpool.go         |  4 ++--
 go.mod                                       |  2 +-
 go.sum                                       |  4 ++--
 7 files changed, 22 insertions(+), 16 deletions(-)

diff --git a/cmd/rpcdaemon/cli/config.go b/cmd/rpcdaemon/cli/config.go
index 7f3dbf62fb..ae3760bdf6 100644
--- a/cmd/rpcdaemon/cli/config.go
+++ b/cmd/rpcdaemon/cli/config.go
@@ -14,6 +14,7 @@ import (
 	"github.com/ledgerwatch/erigon-lib/gointerfaces"
 	"github.com/ledgerwatch/erigon-lib/gointerfaces/grpcutil"
 	"github.com/ledgerwatch/erigon-lib/gointerfaces/remote"
+	"github.com/ledgerwatch/erigon-lib/gointerfaces/txpool"
 	"github.com/ledgerwatch/erigon-lib/kv"
 	"github.com/ledgerwatch/erigon-lib/kv/kvcache"
 	kv2 "github.com/ledgerwatch/erigon-lib/kv/mdbx"
@@ -236,7 +237,9 @@ func checkDbCompatibility(ctx context.Context, db kv.RoDB) error {
 	return nil
 }
 
-func RemoteServices(ctx context.Context, cfg Flags, logger log.Logger, rootCancel context.CancelFunc) (db kv.RoDB, borDb kv.RoDB, eth services.ApiBackend, txPool *services.TxPoolService, mining *services.MiningService, starknet *services.StarknetService, stateCache kvcache.Cache, blockReader interfaces.BlockAndTxnReader, err error) {
+// RemoteServices - use when RPCDaemon run as independent process. Still it can use --datadir flag to enable
+// `cfg.SingleNodeMode` (mode when it on 1 machine with Erigon)
+func RemoteServices(ctx context.Context, cfg Flags, logger log.Logger, rootCancel context.CancelFunc) (db kv.RoDB, borDb kv.RoDB, eth services.ApiBackend, txPool txpool.TxpoolClient, mining txpool.MiningClient, starknet *services.StarknetService, stateCache kvcache.Cache, blockReader interfaces.BlockAndTxnReader, err error) {
 	if !cfg.SingleNodeMode && cfg.PrivateApiAddr == "" {
 		return nil, nil, nil, nil, nil, nil, nil, nil, fmt.Errorf("either remote db or local db must be specified")
 	}
@@ -335,7 +338,7 @@ func RemoteServices(ctx context.Context, cfg Flags, logger log.Logger, rootCance
 	if !cfg.SingleNodeMode {
 		blockReader = snapshotsync.NewRemoteBlockReader(remote.NewETHBACKENDClient(conn))
 	}
-	remoteEth := services.NewRemoteBackend(conn, db, blockReader)
+	remoteEth := services.NewRemoteBackend(remote.NewETHBACKENDClient(conn), db, blockReader)
 	blockReader = remoteEth
 
 	txpoolConn := conn
@@ -346,8 +349,10 @@ func RemoteServices(ctx context.Context, cfg Flags, logger log.Logger, rootCance
 		}
 	}
 
-	mining = services.NewMiningService(txpoolConn)
-	txPool = services.NewTxPoolService(txpoolConn)
+	mining = txpool.NewMiningClient(txpoolConn)
+	miningService := services.NewMiningService(mining)
+	txPool = txpool.NewTxpoolClient(txpoolConn)
+	txPoolService := services.NewTxPoolService(txPool)
 	if db == nil {
 		db = remoteKv
 	}
@@ -359,10 +364,10 @@ func RemoteServices(ctx context.Context, cfg Flags, logger log.Logger, rootCance
 		if !remoteEth.EnsureVersionCompatibility() {
 			rootCancel()
 		}
-		if mining != nil && !mining.EnsureVersionCompatibility() {
+		if mining != nil && !miningService.EnsureVersionCompatibility() {
 			rootCancel()
 		}
-		if !txPool.EnsureVersionCompatibility() {
+		if !txPoolService.EnsureVersionCompatibility() {
 			rootCancel()
 		}
 	}()
diff --git a/cmd/rpcdaemon/commands/eth_subscribe_test.go b/cmd/rpcdaemon/commands/eth_subscribe_test.go
index 749d8f48bb..2eca8b2784 100644
--- a/cmd/rpcdaemon/commands/eth_subscribe_test.go
+++ b/cmd/rpcdaemon/commands/eth_subscribe_test.go
@@ -3,6 +3,7 @@ package commands
 import (
 	"testing"
 
+	"github.com/ledgerwatch/erigon-lib/gointerfaces/remote"
 	"github.com/ledgerwatch/erigon-lib/gointerfaces/sentry"
 	"github.com/ledgerwatch/erigon/cmd/rpcdaemon/filters"
 	"github.com/ledgerwatch/erigon/cmd/rpcdaemon/rpcdaemontest"
@@ -37,7 +38,7 @@ func TestEthSubscribe(t *testing.T) {
 	m.ReceiveWg.Wait() // Wait for all messages to be processed before we proceeed
 
 	ctx, conn := rpcdaemontest.CreateTestGrpcConn(t, m)
-	backend := services.NewRemoteBackend(conn, m.DB, snapshotsync.NewBlockReader())
+	backend := services.NewRemoteBackend(remote.NewETHBACKENDClient(conn), m.DB, snapshotsync.NewBlockReader())
 	ff := filters.New(ctx, backend, nil, nil)
 
 	newHeads := make(chan *types.Header)
diff --git a/cmd/rpcdaemon/services/eth_backend.go b/cmd/rpcdaemon/services/eth_backend.go
index 329de79782..891cefcf6e 100644
--- a/cmd/rpcdaemon/services/eth_backend.go
+++ b/cmd/rpcdaemon/services/eth_backend.go
@@ -47,9 +47,9 @@ type RemoteBackend struct {
 	blockReader      interfaces.BlockAndTxnReader
 }
 
-func NewRemoteBackend(cc grpc.ClientConnInterface, db kv.RoDB, blockReader interfaces.BlockAndTxnReader) *RemoteBackend {
+func NewRemoteBackend(client remote.ETHBACKENDClient, db kv.RoDB, blockReader interfaces.BlockAndTxnReader) *RemoteBackend {
 	return &RemoteBackend{
-		remoteEthBackend: remote.NewETHBACKENDClient(cc),
+		remoteEthBackend: client,
 		version:          gointerfaces.VersionFromProto(privateapi.EthBackendAPIVersion),
 		log:              log.New("remote_service", "eth_backend"),
 		db:               db,
diff --git a/cmd/rpcdaemon/services/eth_mining.go b/cmd/rpcdaemon/services/eth_mining.go
index 0a07cb77ff..a99890e144 100644
--- a/cmd/rpcdaemon/services/eth_mining.go
+++ b/cmd/rpcdaemon/services/eth_mining.go
@@ -18,9 +18,9 @@ type MiningService struct {
 	version gointerfaces.Version
 }
 
-func NewMiningService(cc grpc.ClientConnInterface) *MiningService {
+func NewMiningService(client txpool.MiningClient) *MiningService {
 	return &MiningService{
-		MiningClient: txpool.NewMiningClient(cc),
+		MiningClient: client,
 		version:      gointerfaces.VersionFromProto(privateapi.MiningAPIVersion),
 		log:          log.New("remote_service", "mining"),
 	}
diff --git a/cmd/rpcdaemon/services/eth_txpool.go b/cmd/rpcdaemon/services/eth_txpool.go
index 7d4753901a..7862a4c8e2 100644
--- a/cmd/rpcdaemon/services/eth_txpool.go
+++ b/cmd/rpcdaemon/services/eth_txpool.go
@@ -18,9 +18,9 @@ type TxPoolService struct {
 	version gointerfaces.Version
 }
 
-func NewTxPoolService(cc grpc.ClientConnInterface) *TxPoolService {
+func NewTxPoolService(client txpool.TxpoolClient) *TxPoolService {
 	return &TxPoolService{
-		TxpoolClient: txpool.NewTxpoolClient(cc),
+		TxpoolClient: client,
 		version:      gointerfaces.VersionFromProto(privateapi.TxPoolAPIVersion),
 		log:          log.New("remote_service", "tx_pool"),
 	}
diff --git a/go.mod b/go.mod
index 8bf8dd9540..1a87a45689 100644
--- a/go.mod
+++ b/go.mod
@@ -40,7 +40,7 @@ require (
 	github.com/json-iterator/go v1.1.12
 	github.com/julienschmidt/httprouter v1.3.0
 	github.com/kevinburke/go-bindata v3.21.0+incompatible
-	github.com/ledgerwatch/erigon-lib v0.0.0-20220210041724-1a4e61aba8fe
+	github.com/ledgerwatch/erigon-lib v0.0.0-20220211090027-9689e7046626
 	github.com/ledgerwatch/log/v3 v3.4.0
 	github.com/ledgerwatch/secp256k1 v1.0.0
 	github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
diff --git a/go.sum b/go.sum
index ced117cd07..3237beaf4b 100644
--- a/go.sum
+++ b/go.sum
@@ -632,8 +632,8 @@ github.com/kylelemons/godebug v0.0.0-20170224010052-a616ab194758 h1:0D5M2HQSGD3P
 github.com/kylelemons/godebug v0.0.0-20170224010052-a616ab194758/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k=
 github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c=
 github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8=
-github.com/ledgerwatch/erigon-lib v0.0.0-20220210041724-1a4e61aba8fe h1:JgTvZluIxHQEcGrUe8Fr0ZsGjso7jLW+P5vG1+TUb30=
-github.com/ledgerwatch/erigon-lib v0.0.0-20220210041724-1a4e61aba8fe/go.mod h1:NzHpr5GGDbLM/hYSU4BWB0INU6ZALzMzaqX4i03R6i0=
+github.com/ledgerwatch/erigon-lib v0.0.0-20220211090027-9689e7046626 h1:cJGqT2RxriqihCXjpLfUc98vBEoqy7HzLxKI4hD7gKA=
+github.com/ledgerwatch/erigon-lib v0.0.0-20220211090027-9689e7046626/go.mod h1:NzHpr5GGDbLM/hYSU4BWB0INU6ZALzMzaqX4i03R6i0=
 github.com/ledgerwatch/log/v3 v3.4.0 h1:SEIOcv5a2zkG3PmoT5jeTU9m/0nEUv0BJS5bzsjwKCI=
 github.com/ledgerwatch/log/v3 v3.4.0/go.mod h1:VXcz6Ssn6XEeU92dCMc39/g1F0OYAjw1Mt+dGP5DjXY=
 github.com/ledgerwatch/secp256k1 v1.0.0 h1:Usvz87YoTG0uePIV8woOof5cQnLXGYa162rFf3YnwaQ=
-- 
GitLab