Skip to content

Commit 840f97b

Browse files
committed
perf(api): optimize block JSON serialization in printBlockList and printBlockToJSON
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
1 parent 5044c82 commit 840f97b

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

framework/src/main/java/org/tron/core/services/http/Util.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public static String printErrorMsg(Exception e) {
9595

9696
public static String printBlockList(BlockList list, boolean selfType) {
9797
List<Block> blocks = list.getBlockList();
98-
JSONObject jsonObject = JSONObject.parseObject(JsonFormat.printToString(list, selfType));
98+
JSONObject jsonObject = new JSONObject();
9999
JSONArray jsonArray = new JSONArray();
100100
blocks.stream().forEach(block -> jsonArray.add(printBlockToJSON(block, selfType)));
101101
jsonObject.put("block", jsonArray);
@@ -110,8 +110,10 @@ public static String printBlock(Block block, boolean selfType) {
110110
public static JSONObject printBlockToJSON(Block block, boolean selfType) {
111111
BlockCapsule blockCapsule = new BlockCapsule(block);
112112
String blockID = ByteArray.toHexString(blockCapsule.getBlockId().getBytes());
113-
JSONObject jsonObject = JSONObject.parseObject(JsonFormat.printToString(block, selfType));
113+
JSONObject jsonObject = new JSONObject();
114114
jsonObject.put("blockID", blockID);
115+
jsonObject.put("block_header",
116+
JSONObject.parseObject(JsonFormat.printToString(block.getBlockHeader(), selfType)));
115117
if (!blockCapsule.getTransactions().isEmpty()) {
116118
jsonObject.put("transactions",
117119
printTransactionListToJSON(blockCapsule.getTransactions(), selfType));

framework/src/test/java/org/tron/core/services/http/UtilMockTest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.tron.core.services.http;
22

3+
import com.alibaba.fastjson.JSONArray;
34
import com.alibaba.fastjson.JSONObject;
45
import com.google.protobuf.ByteString;
56

@@ -44,14 +45,28 @@ public void testPrintTransactionFee() {
4445
public void testPrintBlockList() {
4546
BlockCapsule blockCapsule1 = new BlockCapsule(1, Sha256Hash.ZERO_HASH,
4647
System.currentTimeMillis(), Sha256Hash.ZERO_HASH.getByteString());
47-
BlockCapsule blockCapsule2 = new BlockCapsule(1, Sha256Hash.ZERO_HASH,
48+
BlockCapsule blockCapsule2 = new BlockCapsule(2, Sha256Hash.ZERO_HASH,
4849
System.currentTimeMillis(), Sha256Hash.ZERO_HASH.getByteString());
4950
GrpcAPI.BlockList list = GrpcAPI.BlockList.newBuilder()
5051
.addBlock(blockCapsule1.getInstance())
5152
.addBlock(blockCapsule2.getInstance())
5253
.build();
5354
String out = Util.printBlockList(list, true);
5455
Assert.assertNotNull(out);
56+
57+
JSONObject json = JSONObject.parseObject(out);
58+
Assert.assertTrue(json.containsKey("block"));
59+
JSONArray blockArray = json.getJSONArray("block");
60+
Assert.assertEquals(2, blockArray.size());
61+
62+
// verify each block has correct structure
63+
for (int i = 0; i < blockArray.size(); i++) {
64+
JSONObject blockJson = blockArray.getJSONObject(i);
65+
Assert.assertTrue(blockJson.containsKey("blockID"));
66+
Assert.assertTrue(blockJson.containsKey("block_header"));
67+
Assert.assertFalse(blockJson.getString("blockID").isEmpty());
68+
Assert.assertNotNull(blockJson.getJSONObject("block_header"));
69+
}
5570
}
5671

5772
@Test

0 commit comments

Comments
 (0)