Skip to content

Commit 54281dd

Browse files
committed
refactor: get block header by hash
this prevents the situation where we would increase by one the slot from the parent but no block was proposed in the next slot
1 parent 2712e9e commit 54281dd

2 files changed

Lines changed: 30 additions & 16 deletions

File tree

explorer/lib/explorer/beacon_client.ex

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,6 @@ defmodule Explorer.BeaconClient do
44
# See https://eips.ethereum.org/EIPS/eip-4844#parameters
55
@versioned_hash_version_kzg 0x01
66

7-
def get_block_slot!(block_hash) do
8-
{:ok, beacon_block} = get_beacon_block_header_by_hash(block_hash)
9-
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
18-
197
def fetch_blob_by_versioned_hash!(slot, blob_versioned_hash) do
208
{:ok, blobs} = get_block_blobs(slot)
219
data = Map.get(blobs, "data")
@@ -35,14 +23,36 @@ defmodule Explorer.BeaconClient do
3523
"0x" <> Base.encode16(raw, case: :lower)
3624
end
3725

38-
def get_block_blobs(slot) do
39-
beacon_get("/eth/v1/beacon/blob_sidecars/#{slot}")
26+
def get_block_slot(beacon_block) do
27+
String.to_integer(
28+
beacon_block
29+
|> Map.get("data")
30+
|> Map.get("header")
31+
|> Map.get("message")
32+
|> Map.get("slot")
33+
)
4034
end
4135

42-
def get_beacon_block_header_by_hash(block_hash) do
36+
def get_block_header_by_hash(block_hash) do
4337
beacon_get("/eth/v1/beacon/headers/#{block_hash}")
4438
end
4539

40+
def get_block_header_by_parent_hash(parent_block_hash) do
41+
case beacon_get("/eth/v1/beacon/headers?parent_root=#{parent_block_hash}") do
42+
{:ok, header} ->
43+
data = header["data"] |> Enum.at(0)
44+
45+
{:ok, %{header | "data" => data}}
46+
47+
other ->
48+
other
49+
end
50+
end
51+
52+
def get_block_blobs(slot) do
53+
beacon_get("/eth/v1/beacon/blob_sidecars/#{slot}")
54+
end
55+
4656
def beacon_get(method) do
4757
headers = [{"Content-Type", "application/json"}]
4858
request = Finch.build(:get, "#{@beacon_url}#{method}", headers)

explorer/lib/explorer/contract_managers/aligned_proof_aggregation_service.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ defmodule AlignedProofAggregationService do
7676
)
7777

7878
parent_beacon_block_hash = Map.get(block, "parentBeaconBlockRoot")
79-
slot = Explorer.BeaconClient.get_block_slot!(parent_beacon_block_hash) + 1
79+
80+
{:ok, beacon_block} =
81+
Explorer.BeaconClient.get_block_header_by_parent_hash(parent_beacon_block_hash)
82+
83+
slot = Explorer.BeaconClient.get_block_slot(beacon_block)
8084

8185
data =
8286
Explorer.BeaconClient.fetch_blob_by_versioned_hash!(

0 commit comments

Comments
 (0)