Skip to content

Commit 814db3e

Browse files
committed
feat: fetch aggregator from tx input signature for agg proofs
1 parent 56c30ce commit 814db3e

4 files changed

Lines changed: 47 additions & 6 deletions

File tree

explorer/lib/explorer/contract_managers/aligned_proof_aggregation_service.ex

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ defmodule AlignedProofAggregationService do
22
require Logger
33

44
@aligned_config_file System.get_env("ALIGNED_PROOF_AGG_CONFIG_FILE")
5+
@verifyRisc0_solidity_signature "0x015f8668"
6+
@verifySp1_solidity_signature "0x39c94cbf"
57

68
config_file_path =
79
case @aligned_config_file do
@@ -66,6 +68,24 @@ defmodule AlignedProofAggregationService do
6668
end
6769
end
6870

71+
# From a given aggregated proof event, it fetches the transaction
72+
# and returns the aggregator (:sp1, :risc0) based on the function signature
73+
def get_aggregator!(agg_proof) do
74+
tx_hash = agg_proof.tx_hash
75+
{:ok, tx} = Explorer.EthClient.get_transaction_by_hash(tx_hash)
76+
input = Map.get(tx, "input")
77+
# In solidity, the function signatures are the first 4 bytes of the input
78+
# Note: first two characters are the 0x
79+
function_signature = String.slice(input, 0..9)
80+
IO.inspect(function_signature, label: "SIGNATURE")
81+
82+
case function_signature do
83+
@verifyRisc0_solidity_signature -> :risc0
84+
@verifySp1_solidity_signature -> :sp1
85+
_ -> nil
86+
end
87+
end
88+
6989
def get_block_timestamp(block_number) do
7090
case Ethers.Utils.get_block_timestamp(block_number) do
7191
{:ok, timestamp} -> DateTime.from_unix!(timestamp)

explorer/lib/explorer/eth_client.ex

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ defmodule Explorer.EthClient do
66
eth_send("eth_getBlockByNumber", [block_number, false])
77
end
88

9+
def get_transaction_by_hash(tx_hash) do
10+
eth_send("eth_getTransactionByHash", [tx_hash])
11+
end
12+
913
defp eth_send(method, params, id \\ 1) do
1014
headers = [{"Content-Type", "application/json"}]
1115
body = Jason.encode!(%{jsonrpc: "2.0", method: method, params: params, id: id})
@@ -15,9 +19,14 @@ defmodule Explorer.EthClient do
1519
case response do
1620
{:ok, %Finch.Response{status: 200, body: body}} ->
1721
case Jason.decode(body) do
18-
{:ok, %{error: error} = _} -> {:error, error.message}
19-
{:ok, body} -> {:ok, Map.get(body, "result")}
20-
{:error, _} -> {:error, :invalid_json}
22+
{:ok, %{"error" => %{"message" => message}}} ->
23+
{:error, message}
24+
25+
{:ok, body} ->
26+
{:ok, Map.get(body, "result")}
27+
28+
{:error, _} ->
29+
{:error, :invalid_json}
2130
end
2231

2332
{:ok, %Finch.Response{status: status}} ->

explorer/lib/explorer/periodically.ex

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ defmodule Explorer.Periodically do
2424
:timer.send_interval(one_second * seconds_in_an_hour, :restakings)
2525

2626
# Fetch new aggregated proofs every 1 minute
27-
:timer.send_interval(one_second * 60, :aggregated_proofs)
27+
:timer.send_interval(one_second * 12, :aggregated_proofs)
2828
end
2929

3030
# Reads and process last blocks for operators and restaking changes
@@ -78,11 +78,11 @@ defmodule Explorer.Periodically do
7878
def handle_info(:aggregated_proofs, state) do
7979
# This task runs every hour
8080
# We read a bit more than 300 blocks (1hr) to make sure we don't lose any event
81-
read_block_qty = 310
81+
read_block_qty = 5000
8282
latest_block_number = AlignedLayerServiceManager.get_latest_block_number()
8383
read_from_block = max(0, latest_block_number - read_block_qty)
8484

85-
Task.start(fn -> process_aggregated_proofs(read_from_block, latest_block_number) end)
85+
Task.start(fn -> process_aggregated_proofs(3_734_200, 3_734_500) end)
8686

8787
{:noreply, state}
8888
end
@@ -113,8 +113,11 @@ defmodule Explorer.Periodically do
113113
proofs
114114
|> Enum.zip(proof_hashes)
115115
|> Enum.map(fn {agg_proof, hashes} ->
116+
aggregator = AlignedProofAggregationService.get_aggregator!(agg_proof)
117+
116118
agg_proof =
117119
agg_proof
120+
|> Map.merge(%{aggregator: aggregator})
118121
|> Map.merge(%{number_of_proofs: length(hashes)})
119122

120123
{:ok, %{id: id}} = AggregatedProofs.insert_or_update(agg_proof)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule Explorer.Repo.Migrations.AddAggregatorToAggProofs do
2+
use Ecto.Migration
3+
4+
def change do
5+
alter table(:aggregated_proofs) do
6+
add(:aggregator, :string, default: nil)
7+
end
8+
end
9+
end

0 commit comments

Comments
 (0)