Skip to content

Commit

Permalink
Support blob_sidecars beacon endpoint
Browse files Browse the repository at this point in the history
Co-authored-by: Felipe Selmo <fselmo2@gmail.com>
  • Loading branch information
roysc and fselmo committed Aug 27, 2024
1 parent d1c0f29 commit 9e21be0
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 4 deletions.
45 changes: 45 additions & 0 deletions docs/web3.beacon.rst
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,51 @@ Methods
]
}
.. py:method:: Beacon.get_blob_sidecars(block_id, indices=[])
.. code-block:: python
>>> beacon.get_blob_sidecars(1, indices=[1])
{
"data": [
{
"index": "1",
"blob": ..., # ommitted
"kzg_commitment": "0x93247f2209abcacf57b75a51dafae777f9dd38bc7053d1af526f220a7489a6d3a2753e5f3e8b1cfe39b56f43611df74a",
"kzg_proof": "0x7FB0A12D11Ffe8A48c2fF80dCA17adbCC1da5F6aADaAEF2b338717dcDEECf6DaB9FD7C4e4265CfBc097cD31dCB19E836",
"signed_block_header": {
"message": {
"slot": "1",
"proposer_index": "1",
"parent_root": "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
"state_root": "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
"body_root": "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2"
},
"signature": "0x1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505cc411d61252fb6cb3fa0017b679f8bb2305b26a285fa2737f175668d0dff91cc1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505"
},
"kzg_commitment_inclusion_proof": [
"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2"
]
}
]
}
.. py:method:: Beacon.get_node_identity()
.. code-block:: python
Expand Down
1 change: 1 addition & 0 deletions newsfragments/3407.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add sync and async support for beacon ``/eth/v1/beacon/blob_sidecars`` endpoint.
13 changes: 13 additions & 0 deletions tests/beacon/test_async_beacon.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,16 @@ async def test_async_cl_node_get_version(async_beacon):
async def test_async_cl_node_get_syncing(async_beacon):
response = await async_beacon.get_syncing()
_assert_valid_response(response)


# Blob endpoint tests


@pytest.mark.asyncio
async def test_async_cl_node_get_blob_sidecars(async_beacon):
response = await async_beacon.get_blob_sidecars("head")
_assert_valid_response(response)

# test with indices
with_indices = await async_beacon.get_blob_sidecars("head", [0, 1])
_assert_valid_response(with_indices)
12 changes: 12 additions & 0 deletions tests/beacon/test_beacon.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,15 @@ def test_cl_node_get_version(beacon):
def test_cl_node_get_syncing(beacon):
response = beacon.get_syncing()
_assert_valid_response(response)


# Blob endpoint tests


def test_cl_node_get_blob_sidecars(beacon):
response = beacon.get_blob_sidecars("head")
_assert_valid_response(response)

# test with indices
with_indices = beacon.get_blob_sidecars("head", [0, 1])
_assert_valid_response(with_indices)
3 changes: 3 additions & 0 deletions web3/beacon/api_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
# rewards
GET_REWARDS = "/eth/v1/beacon/rewards/blocks/{0}"

# blobs
GET_BLOB_SIDECARS = "/eth/v1/beacon/blob_sidecars/{0}"

# light client
GET_LIGHT_CLIENT_BOOTSTRAP_STRUCTURE = "/eth/v1/beacon/light_client/bootstrap/{0}"
GET_LIGHT_CLIENT_UPDATES = "/eth/v1/beacon/light_client/updates"
Expand Down
20 changes: 18 additions & 2 deletions web3/beacon/async_beacon.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import (
Any,
Dict,
List,
Optional,
)

from eth_typing import (
Expand All @@ -17,6 +19,7 @@
GET_BEACON_HEADS,
GET_BEACON_STATE,
GET_BLINDED_BLOCKS,
GET_BLOB_SIDECARS,
GET_BLOCK,
GET_BLOCK_ATTESTATIONS,
GET_BLOCK_HEADER,
Expand Down Expand Up @@ -64,10 +67,12 @@ def __init__(
self.request_timeout = request_timeout
self._request_session_manager = HTTPSessionManager()

async def _async_make_get_request(self, endpoint_uri: str) -> Dict[str, Any]:
async def _async_make_get_request(
self, endpoint_uri: str, params: Optional[Dict[str, str]] = None
) -> Dict[str, Any]:
uri = URI(self.base_url + endpoint_uri)
return await self._request_session_manager.async_json_make_get_request(
uri, timeout=self.request_timeout
uri, params=params, timeout=self.request_timeout
)

# [ BEACON endpoints ]
Expand Down Expand Up @@ -220,3 +225,14 @@ async def get_version(self) -> Dict[str, Any]:

async def get_syncing(self) -> Dict[str, Any]:
return await self._async_make_get_request(GET_SYNCING)

# [ BLOB endpoints ]

async def get_blob_sidecars(
self, block_id: str, indices: Optional[List[int]] = None
) -> Dict[str, Any]:
indices_param = {"indices": ",".join(map(str, indices))} if indices else None
return await self._async_make_get_request(
GET_BLOB_SIDECARS.format(block_id),
params=indices_param,
)
20 changes: 18 additions & 2 deletions web3/beacon/beacon.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import (
Any,
Dict,
List,
Optional,
)

from eth_typing import (
Expand All @@ -17,6 +19,7 @@
GET_BEACON_HEADS,
GET_BEACON_STATE,
GET_BLINDED_BLOCKS,
GET_BLOB_SIDECARS,
GET_BLOCK,
GET_BLOCK_ATTESTATIONS,
GET_BLOCK_HEADER,
Expand Down Expand Up @@ -62,10 +65,12 @@ def __init__(
self.request_timeout = request_timeout
self._request_session_manager = HTTPSessionManager()

def _make_get_request(self, endpoint_url: str) -> Dict[str, Any]:
def _make_get_request(
self, endpoint_url: str, params: Optional[Dict[str, str]] = None
) -> Dict[str, Any]:
uri = URI(self.base_url + endpoint_url)
return self._request_session_manager.json_make_get_request(
uri, timeout=self.request_timeout
uri, params=params, timeout=self.request_timeout
)

# [ BEACON endpoints ]
Expand Down Expand Up @@ -206,3 +211,14 @@ def get_version(self) -> Dict[str, Any]:

def get_syncing(self) -> Dict[str, Any]:
return self._make_get_request(GET_SYNCING)

# [ BLOB endpoints ]

def get_blob_sidecars(
self, block_id: str, indices: Optional[List[int]] = None
) -> Dict[str, Any]:
indices_param = {"indices": ",".join(map(str, indices))} if indices else None
return self._make_get_request(
GET_BLOB_SIDECARS.format(block_id),
params=indices_param,
)

0 comments on commit 9e21be0

Please sign in to comment.