From 76b7aabfe67aeb7acd74fd7dd41a2efa69b91849 Mon Sep 17 00:00:00 2001 From: Urix <43704209+uri-99@users.noreply.github.com> Date: Mon, 9 Dec 2024 18:37:46 -0300 Subject: [PATCH 1/2] feat: script to update operator names in telemetry --- Makefile | 4 ++ .../lib/scripts/fetch_operators_metadata.ex | 45 +++++++++++++++++++ telemetry_api/lib/telemetry_api/operators.ex | 2 +- .../scripts/fetch_operators_metadata.sh | 31 +++++++++++++ 4 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 telemetry_api/lib/scripts/fetch_operators_metadata.ex create mode 100755 telemetry_api/scripts/fetch_operators_metadata.sh diff --git a/Makefile b/Makefile index beb5b2a22..227a4a9dd 100644 --- a/Makefile +++ b/Makefile @@ -1116,6 +1116,10 @@ telemetry_compile_bls_verifier: @cd telemetry_api/priv && \ go build ../bls_verifier/bls_verify.go +telemetry_fetch_operators_metadata: + @cd telemetry_api && \ + ./scripts/fetch_operators_metadata.sh $(FROM_BLOCK) + setup_local_aligned_all: tmux kill-session -t aligned_layer || true tmux new-session -d -s aligned_layer diff --git a/telemetry_api/lib/scripts/fetch_operators_metadata.ex b/telemetry_api/lib/scripts/fetch_operators_metadata.ex new file mode 100644 index 000000000..55927834a --- /dev/null +++ b/telemetry_api/lib/scripts/fetch_operators_metadata.ex @@ -0,0 +1,45 @@ +defmodule Scripts.FetchOperatorsMetadata do + require Logger + alias TelemetryApi.ContractManagers.OperatorStateRetriever + alias TelemetryApi.Operators.Operator + alias TelemetryApi.Repo + + # This Script is to fetch operators metadata from the blockchain activity + # and insert/update them into the Ecto database + + def run(fromBlock) do + "Fetching old operators changes" |> Logger.debug() + update_operators_metadata(fromBlock) + + "Done" |> Logger.debug() + end + + def update_operators_metadata(fromBlock) do + with {:ok, operators} <- OperatorStateRetriever.get_operators() do + # Construct tuple {%Operator{}, op_data} + operators = Enum.map(operators, fn op_data -> + {Repo.get(Operator, op_data.address), op_data} + end) + + # Fetch metadata for all operators + operators = Enum.map(operators, fn {op, op_data} -> + case TelemetryApi.Operators.add_operator_metadata(op_data) do + {:ok, data} -> {:ok, {op, data}} + {:error, msg} -> {:error, msg} + end + end) + |> tap(&dbg/1) + # Filter status ok and map to {op, op_data} + |> Enum.filter(fn {status, _} -> status == :ok end) + |> Enum.map(fn {_, data} -> data end) + + dbg(operators) + + # Insert in db + Enum.map(operators, fn {op, op_data} -> + Operator.changeset(op, op_data) |> Repo.insert_or_update() + end) + end + :ok + end +end diff --git a/telemetry_api/lib/telemetry_api/operators.ex b/telemetry_api/lib/telemetry_api/operators.ex index c3b2864f9..2f1ad738d 100644 --- a/telemetry_api/lib/telemetry_api/operators.ex +++ b/telemetry_api/lib/telemetry_api/operators.ex @@ -116,7 +116,7 @@ defmodule TelemetryApi.Operators do # iex> add_operator_metadata(operator) # {:error, string} # - defp add_operator_metadata(op_data) do + def add_operator_metadata(op_data) do with {:ok, url} <- DelegationManager.get_operator_url(op_data.address), {:ok, metadata} <- TelemetryApi.Utils.fetch_json_data(url) do operator = %{ diff --git a/telemetry_api/scripts/fetch_operators_metadata.sh b/telemetry_api/scripts/fetch_operators_metadata.sh new file mode 100755 index 000000000..1e5980db3 --- /dev/null +++ b/telemetry_api/scripts/fetch_operators_metadata.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +source .env + +export ENVIRONMENT=$ENVIRONMENT +export RPC_URL=$RPC_URL +ELIXIR_HOSTNAME=$(elixir -e 'IO.puts(:inet.gethostname() |> elem(1))') +export ELIXIR_HOSTNAME=$ELIXIR_HOSTNAME +export ALIGNED_CONFIG_FILE=$ALIGNED_CONFIG_FILE +export OPERATOR_FETCHER_WAIT_TIME_MS=$OPERATOR_FETCHER_WAIT_TIME_MS + +if [ "$#" -eq 0 ]; then + echo "Error, No arguments provided." + echo "Try running the make target with FROM_BLOCK=" + exit 1 +elif [ "$#" -eq 1 ]; then + # argument provided, use it + FROM=$1 +else + echo "Please provide 1 arguments." + exit 1 +fi + +echo "Running fetch_operators_metadata.sh from block: $FROM" + +mix compile --force #force recompile to get the latest .env values + +echo "You will now connect to the Telemetry Node, make sure you run the following command inside it:" +echo "Scripts.FetchOperatorsMetadata.run($FROM)" + +iex --sname fetch_operators_metadata --remsh telemetry@$ELIXIR_HOSTNAME From 848248d1a5b2a8eca59ad639fcd4bbafc7d99f3b Mon Sep 17 00:00:00 2001 From: Urix <43704209+uri-99@users.noreply.github.com> Date: Tue, 10 Dec 2024 11:39:45 -0300 Subject: [PATCH 2/2] fix: remove unused param --- .../lib/scripts/fetch_operators_metadata.ex | 6 +++--- .../scripts/fetch_operators_metadata.sh | 16 +--------------- 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/telemetry_api/lib/scripts/fetch_operators_metadata.ex b/telemetry_api/lib/scripts/fetch_operators_metadata.ex index 55927834a..0dabe1f93 100644 --- a/telemetry_api/lib/scripts/fetch_operators_metadata.ex +++ b/telemetry_api/lib/scripts/fetch_operators_metadata.ex @@ -7,14 +7,14 @@ defmodule Scripts.FetchOperatorsMetadata do # This Script is to fetch operators metadata from the blockchain activity # and insert/update them into the Ecto database - def run(fromBlock) do + def run() do "Fetching old operators changes" |> Logger.debug() - update_operators_metadata(fromBlock) + update_operators_metadata() "Done" |> Logger.debug() end - def update_operators_metadata(fromBlock) do + def update_operators_metadata() do with {:ok, operators} <- OperatorStateRetriever.get_operators() do # Construct tuple {%Operator{}, op_data} operators = Enum.map(operators, fn op_data -> diff --git a/telemetry_api/scripts/fetch_operators_metadata.sh b/telemetry_api/scripts/fetch_operators_metadata.sh index 1e5980db3..e166a3aa1 100755 --- a/telemetry_api/scripts/fetch_operators_metadata.sh +++ b/telemetry_api/scripts/fetch_operators_metadata.sh @@ -9,23 +9,9 @@ export ELIXIR_HOSTNAME=$ELIXIR_HOSTNAME export ALIGNED_CONFIG_FILE=$ALIGNED_CONFIG_FILE export OPERATOR_FETCHER_WAIT_TIME_MS=$OPERATOR_FETCHER_WAIT_TIME_MS -if [ "$#" -eq 0 ]; then - echo "Error, No arguments provided." - echo "Try running the make target with FROM_BLOCK=" - exit 1 -elif [ "$#" -eq 1 ]; then - # argument provided, use it - FROM=$1 -else - echo "Please provide 1 arguments." - exit 1 -fi - -echo "Running fetch_operators_metadata.sh from block: $FROM" - mix compile --force #force recompile to get the latest .env values echo "You will now connect to the Telemetry Node, make sure you run the following command inside it:" -echo "Scripts.FetchOperatorsMetadata.run($FROM)" +echo "Scripts.FetchOperatorsMetadata.run()" iex --sname fetch_operators_metadata --remsh telemetry@$ELIXIR_HOSTNAME