88 "net/http"
99 "os"
1010 "os/exec"
11- "strconv"
1211 "strings"
1312 "time"
1413
@@ -18,9 +17,11 @@ import (
1817 "github.com/ethereum/go-ethereum/rpc"
1918 "github.com/pkg/errors"
2019
20+ "github.com/base/base-bench/runner/benchmark/portmanager"
2121 "github.com/base/base-bench/runner/clients/common"
2222 "github.com/base/base-bench/runner/clients/types"
2323 "github.com/base/base-bench/runner/config"
24+ "github.com/base/base-bench/runner/metrics"
2425 "github.com/ethereum/go-ethereum/ethclient"
2526)
2627
@@ -34,18 +35,30 @@ type GethClient struct {
3435 authClient client.RPC
3536 process * exec.Cmd
3637
38+ ports portmanager.PortManager
39+ metricsPort uint64
40+ rpcPort uint64
41+ authRPCPort uint64
42+
3743 stdout io.WriteCloser
3844 stderr io.WriteCloser
45+
46+ metricsCollector metrics.Collector
3947}
4048
4149// NewGethClient creates a new client for geth.
42- func NewGethClient (logger log.Logger , options * config.InternalClientOptions ) types.ExecutionClient {
50+ func NewGethClient (logger log.Logger , options * config.InternalClientOptions , ports portmanager. PortManager ) types.ExecutionClient {
4351 return & GethClient {
4452 logger : logger ,
4553 options : options ,
54+ ports : ports ,
4655 }
4756}
4857
58+ func (g * GethClient ) MetricsCollector () metrics.Collector {
59+ return g .metricsCollector
60+ }
61+
4962// Run runs the geth client with the given runtime config.
5063func (g * GethClient ) Run (ctx context.Context , cfg * types.RuntimeConfig ) error {
5164
@@ -78,17 +91,20 @@ func (g *GethClient) Run(ctx context.Context, cfg *types.RuntimeConfig) error {
7891 }
7992 }
8093
81-
8294 args = make ([]string , 0 )
8395 args = append (args , "--datadir" , g .options .DataDirPath )
8496 args = append (args , "--http" )
8597
98+ g .rpcPort = g .ports .AcquirePort ("geth" , portmanager .ELPortPurpose )
99+ g .authRPCPort = g .ports .AcquirePort ("geth" , portmanager .AuthELPortPurpose )
100+ g .metricsPort = g .ports .AcquirePort ("geth" , portmanager .ELMetricsPortPurpose )
101+
86102 // TODO: allocate these dynamically eventually
87- args = append (args , "--http.port" , strconv . Itoa ( g . options . GethHttpPort ))
88- args = append (args , "--authrpc.port" , strconv . Itoa ( g . options . GethAuthRpcPort ))
103+ args = append (args , "--http.port" , fmt . Sprintf ( "%d" , g . rpcPort ))
104+ args = append (args , "--authrpc.port" , fmt . Sprintf ( "%d" , g . authRPCPort ))
89105 args = append (args , "--metrics" )
90106 args = append (args , "--metrics.addr" , "localhost" )
91- args = append (args , "--metrics.port" , strconv . Itoa ( g . options . GethMetricsPort ))
107+ args = append (args , "--metrics.port" , fmt . Sprintf ( "%d" , g . metricsPort ))
92108
93109 // Set mempool size to 100x default
94110 args = append (args , "--txpool.globalslots" , "10000000" )
@@ -136,7 +152,7 @@ func (g *GethClient) Run(ctx context.Context, cfg *types.RuntimeConfig) error {
136152 return err
137153 }
138154
139- g .clientURL = fmt .Sprintf ("http://127.0.0.1:%d" , g .options . GethHttpPort )
155+ g .clientURL = fmt .Sprintf ("http://127.0.0.1:%d" , g .rpcPort )
140156 rpcClient , err := rpc .DialOptions (ctx , g .clientURL , rpc .WithHTTPClient (& http.Client {
141157 Timeout : 30 * time .Second ,
142158 }))
@@ -145,13 +161,14 @@ func (g *GethClient) Run(ctx context.Context, cfg *types.RuntimeConfig) error {
145161 }
146162
147163 g .client = ethclient .NewClient (rpcClient )
164+ g .metricsCollector = newMetricsCollector (g .logger , g .client , int (g .metricsPort ))
148165
149166 err = common .WaitForRPC (ctx , g .client )
150167 if err != nil {
151168 return errors .Wrap (err , "geth rpc failed to start" )
152169 }
153170
154- l2Node , err := client .NewRPC (ctx , g .logger , fmt .Sprintf ("http://127.0.0.1:%d" , g .options . GethAuthRpcPort ), client .WithGethRPCOptions (rpc .WithHTTPAuth (node .NewJWTAuth (jwtSecret ))), client .WithCallTimeout (30 * time .Second ))
171+ l2Node , err := client .NewRPC (ctx , g .logger , fmt .Sprintf ("http://127.0.0.1:%d" , g .authRPCPort ), client .WithGethRPCOptions (rpc .WithHTTPAuth (node .NewJWTAuth (jwtSecret ))), client .WithCallTimeout (30 * time .Second ))
155172 if err != nil {
156173 return err
157174 }
@@ -181,6 +198,10 @@ func (g *GethClient) Stop() {
181198 _ = g .stdout .Close ()
182199 _ = g .stderr .Close ()
183200
201+ g .ports .ReleasePort (g .rpcPort )
202+ g .ports .ReleasePort (g .authRPCPort )
203+ g .ports .ReleasePort (g .metricsPort )
204+
184205 g .stdout = nil
185206 g .stderr = nil
186207 g .process = nil
@@ -202,5 +223,5 @@ func (g *GethClient) AuthClient() client.RPC {
202223}
203224
204225func (r * GethClient ) MetricsPort () int {
205- return r . options . GethMetricsPort
226+ return int ( r . metricsPort )
206227}
0 commit comments