Skip to content

Commit d5741ed

Browse files
committed
feat: periodic fetch decoded blob data
1 parent 0e71dd5 commit d5741ed

File tree

1 file changed

+35
-19
lines changed

1 file changed

+35
-19
lines changed

explorer/lib/explorer/periodically.ex

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -81,46 +81,62 @@ defmodule Explorer.Periodically do
8181
read_from_block = max(0, latest_block_number - read_block_qty)
8282

8383
process_aggregated_proofs(read_from_block, latest_block_number)
84+
85+
{:noreply, state}
8486
end
8587

8688
def process_aggregated_proofs(from_block, to_block) do
8789
"Processing aggregated proofs" |> Logger.debug()
8890

89-
aggregated_proofs =
91+
events =
9092
AlignedProofAggregationService.get_aggregated_proof_event(%{
9193
from_block: from_block,
9294
to_block: to_block
9395
})
96+
97+
proofs =
98+
case events do
99+
{:ok, events} -> events
100+
{:error, reason} -> raise(reason)
101+
end
102+
103+
blob_data =
104+
proofs
94105
|> Enum.map(fn x ->
95-
Map.merge(
96-
x,
97-
%{
98-
blob_data: AlignedProofAggregationService.get_blob_data_from_versioned_hash(x)
99-
}
100-
)
106+
case AlignedProofAggregationService.get_blob_data_from_versioned_hash(x) do
107+
{:ok, data} -> data
108+
{:error, reason} -> raise("Error #{reason}")
109+
end
101110
end)
102111

103-
# Split the blob data in chunks of 32 to get the number of leaves (number of proofs) in the aggregated proof
104-
## TODO fix this parsing
105112
proofs_leaves =
106-
Enum.map(aggregated_proofs, fn x -> Enum.chunk_every(x.blob_data, 32) end)
113+
blob_data
114+
|> Enum.map(fn x ->
115+
AlignedProofAggregationService.decode_blob(
116+
to_charlist(String.replace_prefix(x, "0x", ""))
117+
)
118+
end)
107119

108120
# Store aggregated proofs to db
109-
aggregated_proofs
121+
proofs
110122
|> Enum.zip(proofs_leaves)
111-
|> Enum.map(fn {agg_proof, leaves} ->
112-
Map.merge(agg_proof, %{number_of_proofs: length(leaves)})
113-
|> Enum.each(fn x -> AggregatedProof.insert_or_update(x) end)
123+
|> Enum.each(fn {agg_proof, leaves} ->
124+
agg_proof
125+
|> Map.merge(%{number_of_proofs: length(leaves)})
126+
|> AggregatedProofs.insert_or_update()
114127
end)
115128

116129
# Store each individual proof
117-
aggregated_proofs
130+
proofs
118131
|> Enum.zip(proofs_leaves)
119-
|> Enum.map(fn {agg_proof, leaves} ->
120-
Enum.each(leaves, fn leaf ->
121-
AggregationModeProof.insert_proof(%{
132+
|> Enum.each(fn {agg_proof, leaves} ->
133+
leaves
134+
|> Enum.with_index()
135+
|> Enum.each(fn {leaf, index} ->
136+
AggregationModeProof.insert_or_update(%{
122137
aggregated_proof_number: agg_proof.number,
123-
proof_hash: leaf
138+
proof_hash: "0x" <> List.to_string(leaf),
139+
index: index
124140
})
125141
end)
126142
end)

0 commit comments

Comments
 (0)