Skip to content

Commit

Permalink
feat: add HealthCheck method for Ethereum client
Browse files Browse the repository at this point in the history
  • Loading branch information
polsar88 committed Aug 17, 2024
1 parent 888ac69 commit 90b15e0
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,19 @@ go build -v ./...
go test -v ./...
```

Running all tests that match regexp `TestHealthCheckManager`:
```sh
go test -v -run TestHealthCheckManager ./...
```

To measure test code coverage, install the following tool and run the above `go test` command with the `-cover` flag:
```sh
go get golang.org/x/tools/cmd/cover
```

You can generate HTML coverage report and open it in your browser by running:
```sh
go test -coverprofile=cover.out ./... && go tool cover -html=cover.out
go test -v -coverprofile=cover.out ./... && go tool cover -html=cover.out
```

#### Linting
Expand Down
34 changes: 33 additions & 1 deletion internal/client/ethereum_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,44 @@ type NewHeadHandler struct {
OnError func(failure string)
}

type Client ethclient.Client

//go:generate mockery --output ../mocks --name EthClient --with-expecter
type EthClient interface {
SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (ethereum.Subscription, error)
HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)
PeerCount(ctx context.Context) (uint64, error)
SyncProgress(ctx context.Context) (*ethereum.SyncProgress, error)
HealthCheck(ctx context.Context, method string) (time.Duration, error)
}

func (c *Client) SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (ethereum.Subscription, error) {
return (*ethclient.Client)(c).SubscribeNewHead(ctx, ch)
}

func (c *Client) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
return (*ethclient.Client)(c).HeaderByNumber(ctx, number)
}

func (c *Client) PeerCount(ctx context.Context) (uint64, error) {
return (*ethclient.Client)(c).PeerCount(ctx)
}

func (c *Client) SyncProgress(ctx context.Context) (*ethereum.SyncProgress, error) {
return (*ethclient.Client)(c).SyncProgress(ctx)
}

// HealthCheck calls the specified RPC method using the given context and returns the duration
// of the call, as well as the error if one occurred. No arguments are passed to the RPC method.
//
// TODO(polsar): If the method expects one or more arguments, it will return an error that will
// be passed to the caller. We should detect this type of error and not return it, since the call
// has otherwise succeeded, which is the only thing the caller cares about.
func (c *Client) HealthCheck(ctx context.Context, method string) (time.Duration, error) {
start := time.Now()
err := (*ethclient.Client)(c).Client().CallContext(ctx, nil, method)

return time.Since(start), err
}

type EthClientGetter func(url string, credentials *config.BasicAuthConfig, additionalRequestHeaders *[]config.RequestHeaderConfig) (EthClient, error)
Expand All @@ -42,7 +74,7 @@ func NewEthClient(url string, credentials *config.BasicAuthConfig, additionalReq

setAdditionalRequestHeaders(rpcClient, additionalRequestHeaders)

return ethclient.NewClient(rpcClient), nil
return (*Client)(ethclient.NewClient(rpcClient)), nil
}

func getRPCClientWithAuthHeader(url string, credentials *config.BasicAuthConfig) (*rpc.Client, error) {
Expand Down
59 changes: 59 additions & 0 deletions internal/mocks/EthClient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 90b15e0

Please sign in to comment.