Skip to content

Comments

Snap Server: sumListBytes fudge factor removed#9855

Open
macfarla wants to merge 3 commits intohyperledger:mainfrom
macfarla:hive-more
Open

Snap Server: sumListBytes fudge factor removed#9855
macfarla wants to merge 3 commits intohyperledger:mainfrom
macfarla:hive-more

Conversation

@macfarla
Copy link
Contributor

PR description

  1. sumListBytes fudge factor removal (line ~701): Removed the * 11 / 10 multiplier that was overestimating response sizes, causing premature truncation of bytecodes and trie nodes responses.
    Resolves this TODO:
    // TODO: remove hack, 10% is a fudge factor to account for the overhead of rlp encoding
  2. constructGetBytecodesResponse add-then-check (lines ~479-486): Changed the bytecodes loop from checking size limits BEFORE adding each code to adding first, THEN checking. This matches the snap protocol's soft
    limit 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)

 -- Test 8
 This test requests the same code hash multiple times. The server should deliver it multiple times.
   request:
       hashes: [005277275713c3dca04ab6c7e47c80280f192d40dbc42d4798ae13efafb012aa 005277275713c3dca04ab6c7e47c80280f192d40dbc42d4798ae13efafb012aa 005277275713c3dca04ab6c7e47c80280f192d40dbc42d4798ae13efafb012aa 005277275713c3dca04ab6c7e47c80280f192d40dbc42d4798ae13efafb012aa]
       responseBytes: 1000
 0. 0x66ce4e8e12a5403828e3fb3176b429cb926ef9dc29fd04c1b3c13ed2787d98d63eae4e449535fef8fff684d6f73d890e306ee348ada8a418981c28d496bb7be350dfc491ab5c757ee78cfa27228f3a47882447741e862da671b4cb5afd51d95370d52b43b3e1f9a31ab6163a901e55133bd37da50c470c7ad07e6be9a4e139f4309f8175bdcf9b0c39a91eee31067537b925ed2b384eabdf80116866cc3ab80ddb57df9c1c4f68e2fa98244b87e0d27e04c99093c63e7983b367307c46863d3f3fc3eafd6666b4115f284754aee9ae5ecd0e309cfccdd979fc5507fff0213446ac2be92b45f4e420d0da2f410ca74328fa136fa304300cb98f3bfe380f244449
 1. 0x66ce4e8e12a5403828e3fb3176b429cb926ef9dc29fd04c1b3c13ed2787d98d63eae4e449535fef8fff684d6f73d890e306ee348ada8a418981c28d496bb7be350dfc491ab5c757ee78cfa27228f3a47882447741e862da671b4cb5afd51d95370d52b43b3e1f9a31ab6163a901e55133bd37da50c470c7ad07e6be9a4e139f4309f8175bdcf9b0c39a91eee31067537b925ed2b384eabdf80116866cc3ab80ddb57df9c1c4f68e2fa98244b87e0d27e04c99093c63e7983b367307c46863d3f3fc3eafd6666b4115f284754aee9ae5ecd0e309cfccdd979fc5507fff0213446ac2be92b45f4e420d0da2f410ca74328fa136fa304300cb98f3bfe380f244449
 2. 0x66ce4e8e12a5403828e3fb3176b429cb926ef9dc29fd04c1b3c13ed2787d98d63eae4e449535fef8fff684d6f73d890e306ee348ada8a418981c28d496bb7be350dfc491ab5c757ee78cfa27228f3a47882447741e862da671b4cb5afd51d95370d52b43b3e1f9a31ab6163a901e55133bd37da50c470c7ad07e6be9a4e139f4309f8175bdcf9b0c39a91eee31067537b925ed2b384eabdf80116866cc3ab80ddb57df9c1c4f68e2fa98244b87e0d27e04c99093c63e7983b367307c46863d3f3fc3eafd6666b4115f284754aee9ae5ecd0e309cfccdd979fc5507fff0213446ac2be92b45f4e420d0da2f410ca74328fa136fa304300cb98f3bfe380f244449
 failed: expected 4 bytecodes, got 3

Thanks for sending a pull request! Have you done the following?

  • Checked out our contribution guidelines?
  • Considered documentation and added the doc-change-required label to this PR if updates are required.
  • Considered the changelog and included an update if required.
  • For database changes (e.g. KeyValueSegmentIdentifier) considered compatibility and performed forwards and backwards compatibility tests

Locally, you can run these tests to catch failures early:

  • spotless: ./gradlew spotlessApply
  • unit tests: ./gradlew build
  • acceptance tests: ./gradlew acceptanceTest
  • integration tests: ./gradlew integrationTest
  • reference tests: ./gradlew ethereum:referenceTests:referenceTests
  • hive tests: Engine or other RPCs modified?

Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>
Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>
Copilot AI review requested due to automatic review settings February 20, 2026 03:12
@macfarla macfarla added the hive relating to hive tests label Feb 20, 2026
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 / 10 multiplier from sumListBytes() 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.

Comment on lines +482 to +483
if (sumListBytes(codeBytes) > maxResponseBytes
|| stopWatch.getTime() > ResponseSizePredicate.MAX_MILLIS_PER_REQUEST) {
Copy link

Copilot AI Feb 20, 2026

Choose a reason for hiding this comment

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

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.

Suggested change
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) {

Copilot uses AI. Check for mistakes.
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);
Copy link

Copilot AI Feb 20, 2026

Choose a reason for hiding this comment

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

Using reduce((a, b) -> a + b) creates unnecessary lambda overhead. Replace with reduce(Integer::sum) or use mapToInt(Bytes::size).sum() for better performance.

Suggested change
return listOfBytes.stream().map(Bytes::size).reduce((a, b) -> a + b).orElse(0);
return listOfBytes.stream().mapToInt(Bytes::size).sum();

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hive relating to hive tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant