Skip to content

Commit f200e1f

Browse files
committed
test(api): verify visible flag in printBlockToJSON
Add two regression tests that pin the optimized printBlockToJSON path against the visible=true/false semantics of the prior implementation, which the field-by-field assembly now has to thread through JsonFormat explicitly. - testPrintBlockToJSONVisibleFalse: assert structural invariants (blockID, block_header, transactions) under visible=false, mirroring the existing visible=true cases. - testPrintBlockToJSONVisibleFlagAffectsAddressEncoding: build a block with a mainnet 0x41-prefixed witness_address and assert - blockID is identical under either flag (byte-level hash), - block_header JSON differs between the two (witness_address is re-encoded), and - visible=true renders witness_address as Base58 ("T...") while visible=false does not.
1 parent 3d41716 commit f200e1f

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,63 @@ public void testPrintBlockToJSONWithTransactions() {
108108
Assert.assertTrue(txJson.containsKey("raw_data"));
109109
}
110110

111+
@Test
112+
public void testPrintBlockToJSONVisibleFalse() {
113+
BlockCapsule blockCapsule = new BlockCapsule(1, Sha256Hash.ZERO_HASH,
114+
System.currentTimeMillis(), Sha256Hash.ZERO_HASH.getByteString());
115+
TransactionCapsule txCap = getTransactionCapsuleExample();
116+
blockCapsule.addTransaction(txCap);
117+
118+
JSONObject json = Util.printBlockToJSON(blockCapsule.getInstance(), false);
119+
Assert.assertTrue(json.containsKey("blockID"));
120+
Assert.assertTrue(json.containsKey("block_header"));
121+
Assert.assertTrue(json.containsKey("transactions"));
122+
JSONObject blockHeader = json.getJSONObject("block_header");
123+
Assert.assertNotNull(blockHeader);
124+
Assert.assertTrue(blockHeader.containsKey("raw_data"));
125+
126+
JSONArray txArray = json.getJSONArray("transactions");
127+
Assert.assertEquals(1, txArray.size());
128+
JSONObject txJson = txArray.getJSONObject(0);
129+
Assert.assertTrue(txJson.containsKey("txID"));
130+
Assert.assertTrue(txJson.containsKey("raw_data"));
131+
}
132+
133+
@Test
134+
public void testPrintBlockToJSONVisibleFlagAffectsAddressEncoding() {
135+
// Pins the optimized printBlockToJSON against the prior behavior: the
136+
// visible flag must still thread through to JsonFormat so address-bearing
137+
// fields switch encoding while byte-identity fields stay stable.
138+
ByteString witnessAddress = ByteString.copyFrom(
139+
ByteArray.fromHexString("41548794500882809695a8a687866e76d4271a1abc"));
140+
BlockCapsule blockCapsule = new BlockCapsule(1, Sha256Hash.ZERO_HASH,
141+
System.currentTimeMillis(), witnessAddress);
142+
143+
JSONObject visible = Util.printBlockToJSON(blockCapsule.getInstance(), true);
144+
JSONObject hidden = Util.printBlockToJSON(blockCapsule.getInstance(), false);
145+
146+
// blockID is derived from raw bytes; identical under either flag.
147+
Assert.assertEquals(visible.getString("blockID"), hidden.getString("blockID"));
148+
149+
// Overall block_header must differ because witness_address is re-encoded.
150+
String headerVisible = visible.getJSONObject("block_header").toJSONString();
151+
String headerHidden = hidden.getJSONObject("block_header").toJSONString();
152+
Assert.assertNotEquals(headerVisible, headerHidden);
153+
154+
// visible=true renders a mainnet address as Base58 starting with 'T'.
155+
String witnessVisible = visible.getJSONObject("block_header")
156+
.getJSONObject("raw_data").getString("witness_address");
157+
Assert.assertNotNull(witnessVisible);
158+
Assert.assertTrue("visible=true witness_address should be Base58 ('T...'), got: "
159+
+ witnessVisible, witnessVisible.startsWith("T"));
160+
161+
// visible=false keeps witness_address in raw (non-Base58) form.
162+
String witnessHidden = hidden.getJSONObject("block_header")
163+
.getJSONObject("raw_data").getString("witness_address");
164+
Assert.assertNotNull(witnessHidden);
165+
Assert.assertNotEquals(witnessVisible, witnessHidden);
166+
}
167+
111168
@Test
112169
public void testPrintTransactionList() {
113170
TransactionCapsule transactionCapsule = getTransactionCapsuleExample();

0 commit comments

Comments
 (0)