Snap Server: sumListBytes fudge factor removed#9855
Snap Server: sumListBytes fudge factor removed#9855macfarla wants to merge 3 commits intohyperledger:mainfrom
Conversation
Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>
Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>
There was a problem hiding this comment.
Pull request overview
This PR removes the 10% fudge factor from response size calculations in the Snap protocol server, which was causing premature truncation of responses. The changes align the implementation with the snap protocol's soft limit behavior by ensuring responses include the element that first exceeds the size limit.
Changes:
- Removed the
* 11 / 10multiplier fromsumListBytes()that was overestimating response sizes - Modified bytecode collection logic to add elements before checking limits (add-then-check pattern)
- Updated test assertions to expect the correct number of elements without the fudge factor
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| SnapServer.java | Removed fudge factor from size calculation and changed bytecode loop to add-then-check pattern |
| SnapServerTest.java | Updated test assertions to expect correct element counts without fudge factor compensation |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (sumListBytes(codeBytes) > maxResponseBytes | ||
| || stopWatch.getTime() > ResponseSizePredicate.MAX_MILLIS_PER_REQUEST) { |
There was a problem hiding this comment.
The add-then-check pattern may cause the last added bytecode to be included in the response even if it should be excluded. After breaking from the loop, consider removing the last element if it caused the limit to be exceeded: codeBytes.remove(codeBytes.size() - 1) before the break.
| if (sumListBytes(codeBytes) > maxResponseBytes | |
| || stopWatch.getTime() > ResponseSizePredicate.MAX_MILLIS_PER_REQUEST) { | |
| final boolean exceededSizeLimit = sumListBytes(codeBytes) > maxResponseBytes; | |
| final boolean exceededTimeLimit = | |
| stopWatch.getTime() > ResponseSizePredicate.MAX_MILLIS_PER_REQUEST; | |
| if (exceededSizeLimit) { | |
| codeBytes.remove(codeBytes.size() - 1); | |
| } | |
| if (exceededSizeLimit || exceededTimeLimit) { |
| private static int sumListBytes(final List<Bytes> listOfBytes) { | ||
| // TODO: remove hack, 10% is a fudge factor to account for the overhead of rlp encoding | ||
| return listOfBytes.stream().map(Bytes::size).reduce((a, b) -> a + b).orElse(0) * 11 / 10; | ||
| return listOfBytes.stream().map(Bytes::size).reduce((a, b) -> a + b).orElse(0); |
There was a problem hiding this comment.
Using reduce((a, b) -> a + b) creates unnecessary lambda overhead. Replace with reduce(Integer::sum) or use mapToInt(Bytes::size).sum() for better performance.
| return listOfBytes.stream().map(Bytes::size).reduce((a, b) -> a + b).orElse(0); | |
| return listOfBytes.stream().mapToInt(Bytes::size).sum(); |
PR description
Resolves this TODO:
// TODO: remove hack, 10% is a fudge factor to account for the overhead of rlp encodinglimit behavior where the response should include the element that first exceeds the limit.
Fixed Issue(s)
Fixes one hive test:
under devp2p suite, snap - GetByteCodes
This is the failing test (before this fix)
Thanks for sending a pull request! Have you done the following?
doc-change-requiredlabel to this PR if updates are required.Locally, you can run these tests to catch failures early:
./gradlew spotlessApply./gradlew build./gradlew acceptanceTest./gradlew integrationTest./gradlew ethereum:referenceTests:referenceTests