@@ -40,40 +40,84 @@ defmodule AlignedProofAggregationService do
4040
4141 case events do
4242 { :ok , [ ] } ->
43- [ ]
43+ { :ok , [ ] }
4444
4545 { :ok , list } ->
46- Enum . map ( list , fn x ->
47- data = x |> Map . get ( :data )
48- topics_raw = x |> Map . get ( :topics_raw )
49- block_number = x |> Map . get ( :block_number )
50- tx_hash = x |> Map . get ( :transaction_hash )
51-
52- {
53- :ok ,
54- % {
55- number: topics_raw |> Enum . at ( 1 ) ,
56- status: data |> Enum . at ( 0 ) ,
57- merkle_root: data |> Enum . at ( 1 ) ,
58- blob_versioned_hash: data |> Enum . at ( 2 ) ,
59- block_number: block_number ,
60- tx_hash: tx_hash
61- }
62- }
63- end )
46+ { :ok ,
47+ Enum . map ( list , fn x ->
48+ data = x |> Map . get ( :data )
49+ topics_raw = x |> Map . get ( :topics_raw )
50+ block_number = x |> Map . get ( :block_number )
51+ tx_hash = x |> Map . get ( :transaction_hash )
52+
53+ % {
54+ number:
55+ topics_raw
56+ |> Enum . at ( 1 )
57+ |> String . replace_prefix ( "0x" , "" )
58+ |> String . to_integer ( 16 ) ,
59+ status: data |> Enum . at ( 0 ) ,
60+ merkle_root: "0x" <> Base . encode16 ( data |> Enum . at ( 1 ) , case: :lower ) ,
61+ blob_versioned_hash: "0x" <> Base . encode16 ( data |> Enum . at ( 2 ) , case: :lower ) ,
62+ block_number: block_number ,
63+ tx_hash: tx_hash
64+ }
65+ end ) }
6466
6567 { :error , reason } ->
6668 raise ( "Error fetching events: #{ Map . get ( reason , "message" ) } " )
6769 end
6870 end
6971
7072 def get_blob_data_from_versioned_hash ( aggregated_proof ) do
71- case BeaconClient . fetch_blob_by_versioned_hash (
72- aggregated_proof . block_number ,
73+ { :ok , block } =
74+ Explorer.EthClient . get_block_by_number (
75+ Explorer.Utils . decimal_to_hex ( aggregated_proof . block_number )
76+ )
77+
78+ case Explorer.BeaconClient . fetch_blob_by_versioned_hash (
79+ Map . get ( block , "parentBeaconBlockRoot" ) ,
7380 aggregated_proof . blob_versioned_hash
7481 ) do
75- { :ok , data } -> data . blob
82+ { :ok , data } -> { :ok , Map . get ( data , " blob" ) }
7683 { :error , reason } -> { :error , reason }
7784 end
7885 end
86+
87+ def decode_blob ( blob_data ) , do: decode_blob ( blob_data , [ [ ] ] , 0 , 0 , 0 )
88+
89+ defp decode_blob ( [ ] , acc , _current_count , _total_count , _i ) , do: acc
90+
91+ defp decode_blob ( [ head | tail ] , acc , current_count , total_count , i ) do
92+ # Every 32 bytes there is a 00 for padding
93+ should_skip = rem ( total_count , 64 ) == 0
94+
95+ case should_skip do
96+ true ->
97+ [ head | tail ] = tail
98+ decode_blob ( tail , acc , current_count , total_count + 2 , i )
99+
100+ false ->
101+ acc = List . update_at ( acc , i , fn chunk -> chunk ++ [ head ] end )
102+
103+ case current_count + 1 < 64 do
104+ true ->
105+ decode_blob ( tail , acc , current_count + 1 , total_count + 1 , i )
106+
107+ # New hash encountered, this would be the index 0, so next iteration go to 1
108+ false ->
109+ # The iteration finishes when the acc is 0x00
110+ current_blob = Enum . at ( acc , i )
111+ is_all_zeroes = Enum . all? ( current_blob , fn x -> x == 48 end )
112+
113+ ## If the hash is all zeroed, then there are no more hashes in the blob
114+ 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
116+ Enum . drop ( acc , - 1 )
117+ else
118+ decode_blob ( tail , acc ++ [ [ ] ] , 0 , total_count + 1 , i + 1 )
119+ end
120+ end
121+ end
122+ end
79123end
0 commit comments