Conversation
…intBlockToJSON 1. remove redundant full BlockList serialization in printBlockList 2. serialize only block_header instead of entire block in printBlockToJSON 3. enhance testPrintBlockList to verify JSON structure after refactoring
📝 WalkthroughWalkthroughThe changes restructure the JSON output format for blocks in Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
📝 Coding Plan
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
framework/src/main/java/org/tron/core/services/http/Util.java (1)
100-100: Use a direct loop instead ofstream().forEachin this hot path.Since this PR is perf-focused, replacing
blocks.stream().forEach(...)with a plainforloop avoids lambda/stream overhead during serialization.♻️ Suggested change
- blocks.stream().forEach(block -> jsonArray.add(printBlockToJSON(block, selfType))); + for (Block block : blocks) { + jsonArray.add(printBlockToJSON(block, selfType)); + }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@framework/src/main/java/org/tron/core/services/http/Util.java` at line 100, Replace the hot-path call blocks.stream().forEach(block -> jsonArray.add(printBlockToJSON(block, selfType))) with a direct indexed/enhanced for loop to avoid stream/lambda overhead: iterate over the blocks collection with for (BlockType block : blocks) and call jsonArray.add(printBlockToJSON(block, selfType)) inside the loop; update the Util class where printBlockToJSON and jsonArray are used so behavior remains identical but without using stream().forEach.framework/src/test/java/org/tron/core/services/http/UtilMockTest.java (1)
57-69: Good structural assertions; consider validating header content, not only key presence.Right now the test can still pass if all entries have the same/mismatched header payload. Since fixtures are deterministic (heights 1 and 2), assert
block_header.raw_data.numberper index.✅ Stronger assertion example
for (int i = 0; i < blockArray.size(); i++) { JSONObject blockJson = blockArray.getJSONObject(i); Assert.assertTrue(blockJson.containsKey("blockID")); Assert.assertTrue(blockJson.containsKey("block_header")); Assert.assertFalse(blockJson.getString("blockID").isEmpty()); Assert.assertNotNull(blockJson.getJSONObject("block_header")); + JSONObject rawData = blockJson.getJSONObject("block_header").getJSONObject("raw_data"); + Assert.assertEquals(i + 1L, rawData.getLongValue("number")); }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@framework/src/test/java/org/tron/core/services/http/UtilMockTest.java` around lines 57 - 69, Update the assertions in UtilMockTest.java inside the loop that inspects blockArray (the blockJson checks) to validate header content: for each JSONObject blockJson, after confirming "block_header" presence, extract blockJson.getJSONObject("block_header").getJSONObject("raw_data").getLong("number") and assert it equals the expected block height for that index (e.g., 1 for i==0 and 2 for i==1) so the test verifies the deterministic fixture values rather than only key presence.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@framework/src/main/java/org/tron/core/services/http/Util.java`:
- Line 100: Replace the hot-path call blocks.stream().forEach(block ->
jsonArray.add(printBlockToJSON(block, selfType))) with a direct indexed/enhanced
for loop to avoid stream/lambda overhead: iterate over the blocks collection
with for (BlockType block : blocks) and call
jsonArray.add(printBlockToJSON(block, selfType)) inside the loop; update the
Util class where printBlockToJSON and jsonArray are used so behavior remains
identical but without using stream().forEach.
In `@framework/src/test/java/org/tron/core/services/http/UtilMockTest.java`:
- Around line 57-69: Update the assertions in UtilMockTest.java inside the loop
that inspects blockArray (the blockJson checks) to validate header content: for
each JSONObject blockJson, after confirming "block_header" presence, extract
blockJson.getJSONObject("block_header").getJSONObject("raw_data").getLong("number")
and assert it equals the expected block height for that index (e.g., 1 for i==0
and 2 for i==1) so the test verifies the deterministic fixture values rather
than only key presence.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: efb18bc4-6c01-495b-b161-de541dc23e3f
📒 Files selected for processing (2)
framework/src/main/java/org/tron/core/services/http/Util.javaframework/src/test/java/org/tron/core/services/http/UtilMockTest.java
What does this PR do?
Why are these changes required?
This PR has been tested by:
Follow up
Extra details
Summary by cubic
Optimizes block JSON serialization to reduce CPU work and payload size. Block lists are now built as minimal JSON, and individual blocks include only
blockID,block_header, andtransactionswhen present.printBlockList: stop serializing the fullBlockList; build{ "block": [...] }directly.printBlockToJSON: output"blockID"and a parsed"block_header"instead of the full block; include"transactions"only if present.UtilMockTest.testPrintBlockListto assert the new JSON structure and keys.Written for commit 840f97b. Summary will update on new commits.
Summary by CodeRabbit