Skip to content

Commit 1ba6581

Browse files
Merge 0fb9f21 into 73e0481
2 parents 73e0481 + 0fb9f21 commit 1ba6581

11 files changed

Lines changed: 164 additions & 47 deletions

File tree

core/config/base.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ type BaseConfig struct {
3333
EthRpcClientFallback eth.InstrumentedClient
3434
EthWsClient eth.InstrumentedClient
3535
EthWsClientFallback eth.InstrumentedClient
36+
EthRpcUrlFallback string
37+
EthWsUrlFallback string
3638
EigenMetricsIpPortAddress string
3739
ChainId *big.Int
3840
}
@@ -149,6 +151,8 @@ func NewBaseConfig(configFilePath string) *BaseConfig {
149151
EthRpcClientFallback: *ethRpcClientFallback,
150152
EthWsClient: *ethWsClient,
151153
EthWsClientFallback: *ethWsClientFallback,
154+
EthRpcUrlFallback: baseConfigFromYaml.EthRpcUrlFallback,
155+
EthWsUrlFallback: baseConfigFromYaml.EthWsUrlFallback,
152156
EigenMetricsIpPortAddress: baseConfigFromYaml.EigenMetricsIpPortAddress,
153157
ChainId: chainId,
154158
}

operator/cmd/actions/start.go

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,7 @@ var StartCommand = &cli.Command{
2626
Action: operatorMain,
2727
}
2828

29-
func operatorMain(ctx *cli.Context) error {
30-
operatorConfigFilePath := ctx.String("config")
31-
operatorConfig := config.NewOperatorConfig(operatorConfigFilePath)
32-
err := sdkutils.ReadYamlConfig(operatorConfigFilePath, &operatorConfig)
33-
if err != nil {
34-
return err
35-
}
36-
37-
operator, err := operator.NewOperatorFromConfig(*operatorConfig)
38-
if err != nil {
39-
return err
40-
}
41-
29+
func updateTelemetryService(operator *operator.Operator, ctx *cli.Context, operatorConfig *config.OperatorConfig) error {
4230
// hash version
4331
hash := sha3.NewLegacyKeccak256()
4432
hash.Write([]byte(ctx.App.Version))
@@ -51,11 +39,32 @@ func operatorMain(ctx *cli.Context) error {
5139
if err != nil {
5240
return err
5341
}
42+
ethRpcUrl, err := baseUrlOnly(operatorConfig.BaseConfig.EthRpcUrl)
43+
if err != nil {
44+
return err
45+
}
46+
ethRpcUrlFallback, err := baseUrlOnly(operatorConfig.BaseConfig.EthRpcUrlFallback)
47+
if err != nil {
48+
return err
49+
}
50+
ethWsUrl, err := baseUrlOnly(operatorConfig.BaseConfig.EthWsUrl)
51+
if err != nil {
52+
return err
53+
}
54+
ethWsUrlFallback, err := baseUrlOnly(operatorConfig.BaseConfig.EthWsUrlFallback)
55+
if err != nil {
56+
return err
57+
}
5458

5559
body := map[string]interface{}{
56-
"version": ctx.App.Version,
57-
"signature": signature,
60+
"version": ctx.App.Version,
61+
"signature": signature,
62+
"eth_rpc_url": ethRpcUrl,
63+
"eth_rpc_url_fallback": ethRpcUrlFallback,
64+
"eth_ws_url": ethWsUrl,
65+
"eth_ws_url_fallback": ethWsUrlFallback,
5866
}
67+
5968
bodyBuffer := new(bytes.Buffer)
6069

6170
bodyReader := json.NewEncoder(bodyBuffer)
@@ -68,15 +77,35 @@ func operatorMain(ctx *cli.Context) error {
6877
endpoint := operatorConfig.Operator.OperatorTrackerIpPortAddress + "/versions"
6978
operator.Logger.Info("Sending version to operator tracker server: ", "endpoint", endpoint)
7079

71-
res, err := http.Post(endpoint, "application/json",
72-
bodyBuffer)
80+
res, err := http.Post(endpoint, "application/json", bodyBuffer)
7381
if err != nil {
7482
// Dont prevent operator from starting if operator tracker server is down
7583
operator.Logger.Warn("Error sending version to metrics server: ", "err", err)
7684
} else if res.StatusCode != http.StatusCreated && res.StatusCode != http.StatusNoContent {
7785
operator.Logger.Warn("Error sending version to operator tracker server: ", "status_code", res.StatusCode)
7886
}
7987

88+
return nil
89+
}
90+
91+
func operatorMain(ctx *cli.Context) error {
92+
operatorConfigFilePath := ctx.String("config")
93+
operatorConfig := config.NewOperatorConfig(operatorConfigFilePath)
94+
err := sdkutils.ReadYamlConfig(operatorConfigFilePath, &operatorConfig)
95+
if err != nil {
96+
return err
97+
}
98+
99+
operator, err := operator.NewOperatorFromConfig(*operatorConfig)
100+
if err != nil {
101+
return err
102+
}
103+
104+
err = updateTelemetryService(operator, ctx, operatorConfig)
105+
if err != nil {
106+
return err
107+
}
108+
80109
operator.Logger.Info("Operator starting...")
81110
err = operator.Start(context.Background())
82111
if err != nil {

operator/cmd/actions/utils.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package actions
2+
3+
import (
4+
"errors"
5+
"regexp"
6+
)
7+
8+
func baseUrlOnly(url string) (string, error) {
9+
// Removes the protocol and api key part from any url formated like so:
10+
// "<protocol>://<base_url>/<api_key>"
11+
regex := regexp.MustCompile(`^[a-z]+://([^/]+)`)
12+
match := regex.FindStringSubmatch(url)
13+
if len(match) > 1 {
14+
return match[1], nil
15+
}
16+
return "", errors.New("Url did not match the expected format <protocol>://<base_url>/<api_key>")
17+
}

operator/cmd/actions/utils_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package actions
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestBaseUrlOnlyHappyPath(t *testing.T) {
8+
// Format "<protocol>://<base_url>/<api_key>"
9+
10+
urls := [...][2]string{
11+
{"http://localhost:8545/asdfoij2a7831has89%342jddav98j2748", "localhost:8545"},
12+
{"ws://test.com/23r2f98hkjva0udhvi1j%342jddav98j2748", "test.com"},
13+
{"http://localhost:8545", "localhost:8545"},
14+
}
15+
16+
for _, pair := range urls {
17+
url := pair[0]
18+
expectedBaseUrl := pair[1]
19+
20+
baseUrl, err := baseUrlOnly(url)
21+
22+
if err != nil {
23+
t.Errorf("Unexpected error for URL %s: %v", url, err)
24+
}
25+
26+
if baseUrl != expectedBaseUrl {
27+
t.Errorf("Expected base URL %s, got %s for URL %s", expectedBaseUrl, baseUrl, url)
28+
}
29+
}
30+
}
31+
32+
func TestBaseUrlOnlyFailureCases(t *testing.T) {
33+
34+
urls := [...]string{
35+
"localhost:8545/asdfoij2a7831has89%342jddav98j2748",
36+
"this-is-all-wrong",
37+
}
38+
39+
for _, url := range urls {
40+
baseUrl, err := baseUrlOnly(url)
41+
42+
if err == nil {
43+
t.Errorf("An error was expected, but received %s", baseUrl)
44+
}
45+
}
46+
}

telemetry_api/lib/telemetry_api/operators.ex

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -116,21 +116,16 @@ defmodule TelemetryApi.Operators do
116116
117117
## Examples
118118
119-
iex> update_operator_version(%{field: value})
119+
iex> update_operator(%{field: value})
120120
{:ok, %Ecto.Changeset{}}
121121
122122
iex> update_operator_version(%{field: bad_value})
123123
{:error, "Some status", "Some message"}
124124
125125
"""
126-
def update_operator_version(%{"version" => version, "signature" => signature}) do
126+
def update_operator(version, signature, changes) do
127127
with {:ok, address} <- SignatureVerifier.recover_address(version, signature) do
128128
address = "0x" <> address
129-
# We only want to allow changes on version
130-
changes = %{
131-
version: version
132-
}
133-
134129
case Repo.get(Operator, address) do
135130
nil ->
136131
{:error, :bad_request,
@@ -142,24 +137,6 @@ defmodule TelemetryApi.Operators do
142137
end
143138
end
144139

145-
@doc """
146-
Updates a operator.
147-
148-
## Examples
149-
150-
iex> update_operator(operator, %{field: new_value})
151-
{:ok, %Operator{}}
152-
153-
iex> update_operator(operator, %{field: bad_value})
154-
{:error, %Ecto.Changeset{}}
155-
156-
"""
157-
def update_operator(%Operator{} = operator, attrs) do
158-
operator
159-
|> Operator.changeset(attrs)
160-
|> Repo.update()
161-
end
162-
163140
@doc """
164141
Deletes a operator.
165142

telemetry_api/lib/telemetry_api/operators/operator.ex

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,28 @@ defmodule TelemetryApi.Operators.Operator do
88
field :stake, :string
99
field :name, :string
1010
field :version, :string
11+
field :eth_rpc_url, :string
12+
field :eth_rpc_url_fallback, :string
13+
field :eth_ws_url, :string
14+
field :eth_ws_url_fallback, :string
1115

1216
timestamps(type: :utc_datetime)
1317
end
1418

1519
@doc false
1620
def changeset(operator, attrs) do
1721
operator
18-
|> cast(attrs, [:address, :id, :stake, :name, :version])
22+
|> cast(attrs, [
23+
:address,
24+
:id,
25+
:stake,
26+
:name,
27+
:version,
28+
:eth_rpc_url,
29+
:eth_rpc_url_fallback,
30+
:eth_ws_url,
31+
:eth_ws_url_fallback
32+
])
1933
|> validate_required([:address, :id, :name, :stake])
2034
end
2135
end

telemetry_api/lib/telemetry_api/utils.ex

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
defmodule TelemetryApi.Utils do
2+
use TelemetryApiWeb, :controller
3+
24
@moduledoc """
35
Some utility functions
46
"""
@@ -50,4 +52,15 @@ defmodule TelemetryApi.Utils do
5052
{:error, error_message}
5153
end
5254
end
55+
56+
57+
@doc """
58+
Returns json encoded error using http
59+
"""
60+
def return_error(conn, message) do
61+
conn
62+
|> put_status(:bad_request)
63+
|> put_resp_content_type("application/json")
64+
|> send_resp(:bad_request, Jason.encode!(%{error: message}))
65+
end
5366
end

telemetry_api/lib/telemetry_api_web/controllers/operator_controller.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ defmodule TelemetryApiWeb.OperatorController do
22
use TelemetryApiWeb, :controller
33

44
alias TelemetryApi.Operators
5+
alias TelemetryApi.Utils
56
alias TelemetryApi.Operators.Operator
67

78
action_fallback(TelemetryApiWeb.FallbackController)
@@ -11,8 +12,8 @@ defmodule TelemetryApiWeb.OperatorController do
1112
render(conn, :index, operators: operators)
1213
end
1314

14-
def create_or_update(conn, operator_params) do
15-
with {:ok, %Operator{} = operator} <- Operators.update_operator_version(operator_params) do
15+
def create_or_update(conn, %{"version" => version, "signature" => signature} = attrs) do
16+
with {:ok, %Operator{} = operator} <- Operators.update_operator(version, signature, attrs) do
1617
conn
1718
|> put_status(:created)
1819
|> put_resp_header("location", ~p"/api/operators/#{operator}")

telemetry_api/lib/telemetry_api_web/controllers/operator_json.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ defmodule TelemetryApiWeb.OperatorJSON do
2121
id: operator.id,
2222
stake: operator.stake,
2323
name: operator.name,
24-
version: operator.version
24+
version: operator.version,
25+
eth_rpc_url: operator.eth_rpc_url,
26+
eth_rpc_url_fallback: operator.eth_rpc_url_fallback,
27+
eth_ws_url: operator.eth_ws_url,
28+
eth_ws_url_fallback: operator.eth_ws_url_fallback
2529
}
2630
end
2731
end

0 commit comments

Comments
 (0)