Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Changes to BlobSidecars Endpoint #4455

Merged
merged 13 commits into from
Jul 21, 2023

Conversation

Gua00va
Copy link
Contributor

@Gua00va Gua00va commented Jul 1, 2023

Issue Addressed

Addresses Issue #4317
Update the getBlobSidecars endpoint as of ethereum/beacon-APIs#286.

  • path changed from beacon/blobs to beacon/blob_sidecars
  • new indices query parameter

@Gua00va Gua00va marked this pull request as draft July 1, 2023 07:29
@realbigsean
Copy link
Member

Hey @Gua00va , I cherry-picked the renaming changes into here because it sounds like there's a team relying on the rename (blobscan?). Thank you for the fix!

@Gua00va Gua00va marked this pull request as ready for review July 14, 2023 19:26
@Gua00va
Copy link
Contributor Author

Gua00va commented Jul 14, 2023

Hey @Gua00va , I cherry-picked the renaming changes into here because it sounds like there's a team relying on the rename (blobscan?). Thank you for the fix!

Hey @realbigsean, only the rename has been merged, right? The query part is yet to be added.

@Gua00va Gua00va changed the title [WIP] Add Changes to BlobSidecars Endpoint Add Changes to BlobSidecars Endpoint Jul 18, 2023
@Gua00va
Copy link
Contributor Author

Gua00va commented Jul 20, 2023

Hey! @jimmygchen, I have added the required changes. But I think my work lacks code quality. Do you have any suggestions to improve it?

let mut list = Vec::new();
for i in vec.iter() {
list.push(blob_sidecar_list.get(*i as usize).unwrap().clone());
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few suggestions:
1 . Blobs are quite big so we'd want to avoid cloning as much as possible. Instead of iterating using iter, you could use .into_iter (which moves and consumes the original values), and then use .filter to retain the requested indices, and finally collect the blob sidecar(s) into a vec using .collect.
2. Always avoid using unwrap in production code, as it can panic and cause Lighthouse to crash. Generally we handle the error by either logging or returning an error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this current logic, if user sends 1,1,1 as inputs, it would result in the endpoint returning 3 blob sidecars. But I think it you go with using the above approach and iterate using blob_sidecar_list.into_iter(), this shouldn't be an issue.

A small bonus would be to move this filtering logic into a function in block_id.rs, to keep this already huge file from growing further.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback !

@jimmygchen jimmygchen added waiting-on-author The reviewer has suggested changes and awaits thier implementation. HTTP-API deneb labels Jul 20, 2023
@Gua00va Gua00va requested a review from jimmygchen July 20, 2023 16:31
// None => {
// blob_sidecar_list
// },
// };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove commented code.

Just a minor comment on naming of blob_sidecar_list_indexed, I think it's slightly confusing because it's just a list (potentially filtered), maybe blob_sidecar_list_filtered or something similar would be clearer?

Some(vec) => {
let list = blob_sidecar_list.into_iter()
.enumerate()
.filter(|(index, _)| vec.contains(index))
Copy link
Member

@jimmygchen jimmygchen Jul 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be more accurate to filter using the blob_sidecar.index instead, in case if it's somehow not the same order in the list. This means we can also get rid of .enumerate and map.

.map(|(_, blob)| blob)
.collect();
let indexed_list = BlobSidecarList::new(list);
indexed_list.unwrap()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although this is an unexpected scenario, we should always avoid using unwrap in production code. In this case it's probably best to return a server error, something like:

BlobSidecarList::new(list)
                    .map_err(|e| warp_utils::reject::custom_server_error(format!("{e:?}")))?

The map_err function maps the ssz Error type into the required warp::Rejection error type, and the ? allow the error to be automatically propagated / returned.

let blob_sidecar_list_indexed = match indices.indices {
Some(vec) => {
let list = blob_sidecar_list.into_iter()
.enumerate()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code formatting seems a bit off here and will fail our CI tests, you can fix it by running

cargo fmt --all

You may also want to run clippy to ensure the changes passes our CI. Some guidelines here in case you haven't seen it:
https://lighthouse-book.sigmaprime.io/contributing.html?highlight=fmt#rust

We'll kick off a CI workflow once this is fixed!

@jimmygchen
Copy link
Member

Hey @Gua00va, thanks for the quick fixes! 🙏 I've left a few more comments.

@Gua00va Gua00va requested a review from jimmygchen July 21, 2023 04:39
Comment on lines 283 to 285
let filtered_list = BlobSidecarList::new(list)
.map_err(|e| warp_utils::reject::custom_server_error(format!("{:?}", e)));
filtered_list.unwrap()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The .unwrap call can be removed and return the error to the caller using ?

Suggested change
let filtered_list = BlobSidecarList::new(list)
.map_err(|e| warp_utils::reject::custom_server_error(format!("{:?}", e)));
filtered_list.unwrap()
BlobSidecarList::new(list)
.map_err(|e| warp_utils::reject::custom_server_error(format!("{:?}", e)))?

@@ -648,6 +648,12 @@ pub struct ValidatorBalancesQuery {
pub id: Option<Vec<ValidatorId>>,
}

#[derive(Clone, Deserialize)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth adding a deny_unknown_fields attribute here for consistency with other endpoints and return error if user provided some unknown parameters.

Suggested change
#[derive(Clone, Deserialize)]
#[derive(Clone, Deserialize)]
#[serde(deny_unknown_fields)]

Copy link
Member

@jimmygchen jimmygchen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, we're close! Just two small comments and we should be good 👍

@jimmygchen jimmygchen added ready-for-review The code is ready for review and removed waiting-on-author The reviewer has suggested changes and awaits thier implementation. labels Jul 21, 2023
Copy link
Member

@realbigsean realbigsean left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
deneb HTTP-API ready-for-review The code is ready for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants