Skip to content

Commit f95139f

Browse files
committed
refactor: more idiomatic elixir and made panics explicit
1 parent d5741ed commit f95139f

File tree

3 files changed

+47
-59
lines changed

3 files changed

+47
-59
lines changed

explorer/lib/explorer/beacon_client.ex

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
11
defmodule Explorer.BeaconClient do
22
require Logger
33
@beacon_url System.get_env("BEACON_CLIENT")
4-
@rpc_url System.get_env("RPC_URL")
54
# See https://eips.ethereum.org/EIPS/eip-4844#parameters
65
@versioned_hash_version_kzg 0x01
76

8-
def fetch_blob_by_versioned_hash(beacon_blob_hash, blob_versioned_hash) do
9-
{:ok, beacon_block} = get_beacon_block_header_by_hash(beacon_blob_hash)
7+
def get_block_slot!(block_hash) do
8+
{:ok, beacon_block} = get_beacon_block_header_by_hash(block_hash)
109

11-
slot =
12-
String.to_integer(
13-
Map.get(Map.get(Map.get(Map.get(beacon_block, "data"), "header"), "message"), "slot")
14-
)
15-
16-
case get_block_blobs(slot + 1) do
17-
{:ok, blobs} ->
18-
data = Map.get(blobs, "data")
19-
20-
blob =
21-
Enum.find(data, fn blob ->
22-
get_blob_versioned_hash(blob) == blob_versioned_hash
23-
end)
10+
String.to_integer(
11+
beacon_block
12+
|> Map.get("data")
13+
|> Map.get("header")
14+
|> Map.get("message")
15+
|> Map.get("slot")
16+
)
17+
end
2418

25-
{:ok, blob}
19+
def fetch_blob_by_versioned_hash!(slot, blob_versioned_hash) do
20+
{:ok, blobs} = get_block_blobs(slot)
21+
data = Map.get(blobs, "data")
2622

27-
{:error, reason} ->
28-
{:error, reason}
29-
end
23+
Enum.find(data, fn blob ->
24+
get_blob_versioned_hash(blob) == blob_versioned_hash
25+
end)
3026
end
3127

3228
def get_blob_versioned_hash(blob) do

explorer/lib/explorer/contract_managers/aligned_proof_aggregation_service.ex

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,36 +65,42 @@ defmodule AlignedProofAggregationService do
6565
end)}
6666

6767
{:error, reason} ->
68-
raise("Error fetching events: #{Map.get(reason, "message")}")
68+
{:error, reason}
6969
end
7070
end
7171

72-
def get_blob_data_from_versioned_hash(aggregated_proof) do
72+
def get_blob_data!(aggregated_proof) do
7373
{:ok, block} =
7474
Explorer.EthClient.get_block_by_number(
7575
Explorer.Utils.decimal_to_hex(aggregated_proof.block_number)
7676
)
7777

78-
case Explorer.BeaconClient.fetch_blob_by_versioned_hash(
79-
Map.get(block, "parentBeaconBlockRoot"),
80-
aggregated_proof.blob_versioned_hash
81-
) do
82-
{:ok, data} -> {:ok, Map.get(data, "blob")}
83-
{:error, reason} -> {:error, reason}
84-
end
78+
parent_beacon_block_hash = Map.get(block, "parentBeaconBlockRoot")
79+
slot = Explorer.BeaconClient.get_block_slot!(parent_beacon_block_hash) + 1
80+
81+
data =
82+
Explorer.BeaconClient.fetch_blob_by_versioned_hash!(
83+
slot,
84+
aggregated_proof.blob_versioned_hash
85+
)
86+
87+
Map.get(data, "blob")
8588
end
8689

90+
@doc """
91+
Decodes blob data represented as an ASCII charlist.
92+
"""
8793
def decode_blob(blob_data), do: decode_blob(blob_data, [[]], 0, 0, 0)
8894

8995
defp decode_blob([], acc, _current_count, _total_count, _i), do: acc
9096

9197
defp decode_blob([head | tail], acc, current_count, total_count, i) do
92-
# Every 32 bytes there is a 00 for padding
98+
# Every 64 characters (or 32 bytes) there is a 00 for padding
9399
should_skip = rem(total_count, 64) == 0
94100

95101
case should_skip do
96102
true ->
97-
[head | tail] = tail
103+
[_head | tail] = tail
98104
decode_blob(tail, acc, current_count, total_count + 2, i)
99105

100106
false ->
@@ -104,15 +110,14 @@ defmodule AlignedProofAggregationService do
104110
true ->
105111
decode_blob(tail, acc, current_count + 1, total_count + 1, i)
106112

107-
# New hash encountered, this would be the index 0, so next iteration go to 1
108113
false ->
109-
# The iteration finishes when the acc is 0x00
110114
current_blob = Enum.at(acc, i)
115+
# 48 is 0 in ascii
111116
is_all_zeroes = Enum.all?(current_blob, fn x -> x == 48 end)
112117

113118
## If the hash is all zeroed, then there are no more hashes in the blob
114119
if is_all_zeroes do
115-
# Drop last element as it is made all out of zeroes and it is the one we use to stop decoding
120+
# Drop last limiter zeroed element
116121
Enum.drop(acc, -1)
117122
else
118123
decode_blob(tail, acc ++ [[]], 0, total_count + 1, i + 1)

explorer/lib/explorer/periodically.ex

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -74,42 +74,29 @@ defmodule Explorer.Periodically do
7474
end
7575

7676
def handle_info(:aggregated_proofs, state) do
77-
# This runs every 1hr, so reading 300 means going back exactly one hour
78-
# We add a few blocks more to make sure we don't lose anything
7977
read_block_qty = 310
8078
latest_block_number = AlignedLayerServiceManager.get_latest_block_number()
8179
read_from_block = max(0, latest_block_number - read_block_qty)
8280

83-
process_aggregated_proofs(read_from_block, latest_block_number)
81+
Task.start(fn -> process_aggregated_proofs(read_from_block, latest_block_number) end)
8482

8583
{:noreply, state}
8684
end
8785

8886
def process_aggregated_proofs(from_block, to_block) do
8987
"Processing aggregated proofs" |> Logger.debug()
9088

91-
events =
89+
{:ok, proofs} =
9290
AlignedProofAggregationService.get_aggregated_proof_event(%{
9391
from_block: from_block,
9492
to_block: to_block
9593
})
9694

97-
proofs =
98-
case events do
99-
{:ok, events} -> events
100-
{:error, reason} -> raise(reason)
101-
end
102-
10395
blob_data =
10496
proofs
105-
|> Enum.map(fn x ->
106-
case AlignedProofAggregationService.get_blob_data_from_versioned_hash(x) do
107-
{:ok, data} -> data
108-
{:error, reason} -> raise("Error #{reason}")
109-
end
110-
end)
97+
|> Enum.map(&AlignedProofAggregationService.get_blob_data!/1)
11198

112-
proofs_leaves =
99+
proof_hashes =
113100
blob_data
114101
|> Enum.map(fn x ->
115102
AlignedProofAggregationService.decode_blob(
@@ -119,23 +106,23 @@ defmodule Explorer.Periodically do
119106

120107
# Store aggregated proofs to db
121108
proofs
122-
|> Enum.zip(proofs_leaves)
123-
|> Enum.each(fn {agg_proof, leaves} ->
109+
|> Enum.zip(proof_hashes)
110+
|> Enum.each(fn {agg_proof, hashes} ->
124111
agg_proof
125-
|> Map.merge(%{number_of_proofs: length(leaves)})
112+
|> Map.merge(%{number_of_proofs: length(hashes)})
126113
|> AggregatedProofs.insert_or_update()
127114
end)
128115

129116
# Store each individual proof
130117
proofs
131-
|> Enum.zip(proofs_leaves)
132-
|> Enum.each(fn {agg_proof, leaves} ->
133-
leaves
118+
|> Enum.zip(proof_hashes)
119+
|> Enum.each(fn {agg_proof, hashes} ->
120+
hashes
134121
|> Enum.with_index()
135-
|> Enum.each(fn {leaf, index} ->
122+
|> Enum.each(fn {hash, index} ->
136123
AggregationModeProof.insert_or_update(%{
137124
aggregated_proof_number: agg_proof.number,
138-
proof_hash: "0x" <> List.to_string(leaf),
125+
proof_hash: "0x" <> List.to_string(hash),
139126
index: index
140127
})
141128
end)

0 commit comments

Comments
 (0)