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

Iterate all chains when fetching all Safes #2002

Merged
merged 7 commits into from
Oct 16, 2024
Merged

Iterate all chains when fetching all Safes #2002

merged 7 commits into from
Oct 16, 2024

Conversation

iamacook
Copy link
Member

@iamacook iamacook commented Oct 9, 2024

Summary

Resolves #2001

When getting all Safes by owner address, we fetch the chain configurations. We don't, however, take into account pagination.

This adds a new IChainsRepository['getAllChains'] method that fetches subsequent next pages and concatenates results, called in the getAllSafesByOwner method.

Changes

  • Add IChainsRepository['getAllChains'] implementation
  • Call getAllChains instead of getChains in getAllSafesByOwner
  • Add appropriate test coverage

@iamacook iamacook self-assigned this Oct 9, 2024
@iamacook iamacook requested a review from a team as a code owner October 9, 2024 07:59
const url = new URL(result.next);
const paginationData = PaginationData.fromLimitAndOffset(url);
offset ??= 0;
offset += paginationData.offset;
Copy link
Member

Choose a reason for hiding this comment

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

I think this is not correct. Considering for example 10 items per page (limit), it would work for the second page (offset was 0, + 10 = 10) and also for the third (previous offset is 10, + 10 = 20), but for the fourth it would take 20 as previous offset and the next URL would contain ?offset=30, right? So it would set 50 as new offset.

Maybe my math is wrong but I think we need to set the offset directly to the one extracted from the URL.

offset = paginationData.offset;

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. The offset is indeed "fixed". I change it in 16fcf76.


let offset: number | undefined;

while (true) {
Copy link
Member

Choose a reason for hiding this comment

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

I think we should be a bit more defensive here: while(true) seems scary, and we are relying on the fact the datasource (Config Service in this case) works as expected and does not have a bug affecting the offset value.

What do you think about having some kind of threshold to limit the max number of iterations in the loop? We could define a max number of Chain objects that can be handled in total and divide it by the default limit path param.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've set an "upper request limit" (adjustable by env. var.) so that only n amount of subsequent requests are made. We also now fetch the upper limit to hopefully also prevent subsequent requests. As of 6b4f5f7.

@iamacook iamacook marked this pull request as draft October 10, 2024 07:12
@iamacook iamacook marked this pull request as ready for review October 10, 2024 10:18
src/domain/chains/chains.repository.ts Show resolved Hide resolved

@Injectable()
export class ChainsRepository implements IChainsRepository {
private readonly maxLimit: number;
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Why is this named max? I mean, it seems the default limit we use for all the requests to get the chains. Maybe I'm wrong but I'd expect this to be something like a threshold but it is like a constant. Is there any reason to not use PaginationData.DEFAULT_LIMIT for this?

Copy link
Member Author

Choose a reason for hiding this comment

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

It mirrors the max_limit of the Chains pagination from the Config. Service.

I did consider changing the PaginationData.DEFAULT_LIMIT instead but that affects all pagination requests across the project. If we did, it would reduce the request by the Web though as they don't provide a limit in their request.

What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

I meant doing something like:

const result = await this.getChains(PaginationData.DEFAULT_LIMIT, offset);

I didn't mean to change the PaginationData.DEFAULT_LIMIT, that would indeed affect the overall service behavior. Sorry about the lack of clarity in my previous comment 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

The use of 40 was at the request of @PooyaRaki.

@PooyaRaki, what do you think about this?

Copy link
Contributor

Choose a reason for hiding this comment

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

Changing the default limit could be risky, as it would impact all endpoints. However, since the web isn't specifying any limits, for this particular case, I think it's better to set a maximum limit of for example 40 specifically for this endpoint.

Copy link
Member Author

Choose a reason for hiding this comment

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

Considering the above, I've moved the limit to a constant within the repository as I don't see it changing (for now). I left the sequential page requests in the configuration though.

What are your thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good to me! 🚀

Copy link
Contributor

@PooyaRaki PooyaRaki left a comment

Choose a reason for hiding this comment

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

LGTM 🚀

Copy link
Member

@hectorgomezv hectorgomezv left a comment

Choose a reason for hiding this comment

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

Great job!

@iamacook iamacook merged commit 36d6cfa into main Oct 16, 2024
18 checks passed
@iamacook iamacook deleted the iterate-chains branch October 16, 2024 11:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Iterate over chains when getting all Safes by owner
3 participants