fix: GetBlocks pagination off-by-one returning PerPage+1 results#330
Open
floflo777 wants to merge 1 commit intocanopy-network:mainfrom
Open
fix: GetBlocks pagination off-by-one returning PerPage+1 results#330floflo777 wants to merge 1 commit intocanopy-network:mainfrom
floflo777 wants to merge 1 commit intocanopy-network:mainfrom
Conversation
The GetBlocks callback was mutating page.PerPage inside the closure (page.PerPage += 1) to fetch one extra block for computing the Took duration metadata. This caused Load() to iterate PerPage+1 times instead of PerPage, corrupting Count and TotalPages in the response. Instead, create a separate PageParams with PerPage+1 before calling Load(), and use the original p.PerPage for the results guard and to restore correct pagination metadata afterwards. Fixes canopy-network#197
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #197
GetBlockswas returningPerPage + 1results instead ofPerPage. The root cause is thatpage.PerPage += 1inside theLoad()callback mutates the samePerPagefield thatLoad()uses to control iteration count, causing it to iterate one extra time beyond the requested page size. This also corruptsCountandTotalPagesin the response.Changes
PageParamswithPerPage + 1before callingLoad(), so the iteration limit is set upfront without mutation inside the callbackp.PerPageas the guard forresults = append(results, block)so exactlyPerPageblocks are returnedpage.PerPage += 1mutation from inside the callbackpage.Countandpage.TotalPagesafterLoad()using the originalPerPagevalueBefore
After
The extra block is still fetched (needed to compute
Tookduration for the last visible block), but it is no longer included in the results or pagination metadata.Test plan
go build ./store/...passesgo test ./store/... -vall 33 tests pass