@@ -1441,6 +1441,147 @@ public void testGetBlockReceipts() {
14411441 Assert .assertEquals ("invalid block number" , overflowReceiptsEx .getMessage ());
14421442 }
14431443
1444+ /**
1445+ * Regression: querying eth_getBlockReceipts with an orphaned block hash must not splice the
1446+ * stale blockHash onto canonical (by-height) transactionHashes; it must return null.
1447+ */
1448+ @ Test
1449+ public void testGetBlockReceiptsByForkedHash () throws Exception {
1450+ // height stays below the test head (blockCapsule1 @ 10_000) so it never becomes "latest"
1451+ long height = 5_000L ;
1452+
1453+ // canonical block at `height` with one transfer tx
1454+ BlockCapsule canonicalBlock = new BlockCapsule (height , Sha256Hash .wrap (ByteString .copyFrom (
1455+ ByteArray .fromHexString (
1456+ "1111111111111111111111111111111111111111111111111111111111111111" ))), 1000000 ,
1457+ ByteString .copyFromUtf8 ("canonicalWitness" ));
1458+ TransferContract canonicalTransfer = TransferContract .newBuilder ().setAmount (11L )
1459+ .setOwnerAddress (ByteString .copyFrom ("0x0000000000000000000" .getBytes ()))
1460+ .setToAddress (ByteString .copyFrom (ByteArray .fromHexString (
1461+ Wallet .getAddressPreFixString () + "A389132D6639FBDA4FBC8B659264E6B7C90DB086" )))
1462+ .build ();
1463+ TransactionCapsule canonicalTx =
1464+ new TransactionCapsule (canonicalTransfer , ContractType .TransferContract );
1465+ canonicalTx .setBlockNum (height );
1466+ canonicalBlock .addTransaction (canonicalTx );
1467+
1468+ // stale (orphaned) block at the SAME height: different parent + different tx => different hash
1469+ BlockCapsule staleBlock = new BlockCapsule (height , Sha256Hash .wrap (ByteString .copyFrom (
1470+ ByteArray .fromHexString (
1471+ "2222222222222222222222222222222222222222222222222222222222222222" ))), 2000000 ,
1472+ ByteString .copyFromUtf8 ("staleWitness" ));
1473+ TransferContract staleTransfer = TransferContract .newBuilder ().setAmount (99L )
1474+ .setOwnerAddress (ByteString .copyFrom ("0x0000000000000000000" .getBytes ()))
1475+ .setToAddress (ByteString .copyFrom (ByteArray .fromHexString (
1476+ Wallet .getAddressPreFixString () + "ED738B3A0FE390EAA71B768B6D02CDBD18FB207B" )))
1477+ .build ();
1478+ TransactionCapsule staleTx =
1479+ new TransactionCapsule (staleTransfer , ContractType .TransferContract );
1480+ staleTx .setBlockNum (height );
1481+ staleBlock .addTransaction (staleTx );
1482+
1483+ // both blocks remain queryable by hash; the canonical chain points to canonicalBlock here
1484+ dbManager .getBlockStore ().put (canonicalBlock .getBlockId ().getBytes (), canonicalBlock );
1485+ dbManager .getBlockStore ().put (staleBlock .getBlockId ().getBytes (), staleBlock );
1486+ dbManager .getBlockIndexStore ().put (canonicalBlock .getBlockId ());
1487+
1488+ // height-keyed receipts hold the CANONICAL transaction info (overwrote the stale branch)
1489+ TransactionRetCapsule canonicalRet = new TransactionRetCapsule ();
1490+ canonicalBlock .getTransactions ().forEach (tx -> {
1491+ TransactionInfoCapsule info = new TransactionInfoCapsule ();
1492+ info .setId (tx .getTransactionId ().getBytes ());
1493+ info .setBlockNumber (height );
1494+ info .setBlockTimeStamp (canonicalBlock .getTimeStamp ());
1495+ canonicalRet .addTransactionInfo (info .getInstance ());
1496+ });
1497+ dbManager .getTransactionRetStore ().put (ByteArray .fromLong (height ), canonicalRet );
1498+
1499+ // preconditions: distinct blocks, equal tx counts (so the size-consistency guard passes)
1500+ Assert .assertNotEquals (canonicalBlock .getBlockId (), staleBlock .getBlockId ());
1501+ Assert .assertEquals (staleBlock .getTransactions ().size (),
1502+ canonicalRet .getInstance ().getTransactioninfoCount ());
1503+
1504+ // a non-canonical block hash must not return canonical-by-height receipts
1505+ List <TransactionReceipt > staleReceipts =
1506+ tronJsonRpc .getBlockReceipts (staleBlock .getBlockId ().toString ());
1507+ Assert .assertNull (staleReceipts );
1508+
1509+ // the canonical block at the same height is still served, with consistent block/tx hashes
1510+ List <TransactionReceipt > canonicalReceipts =
1511+ tronJsonRpc .getBlockReceipts (canonicalBlock .getBlockId ().toString ());
1512+ Assert .assertNotNull (canonicalReceipts );
1513+ Assert .assertEquals (1 , canonicalReceipts .size ());
1514+ Assert .assertEquals (ByteArray .toJsonHex (canonicalBlock .getBlockId ().getBytes ()),
1515+ canonicalReceipts .get (0 ).getBlockHash ());
1516+ Assert .assertEquals (ByteArray .toJsonHex (canonicalTx .getTransactionId ().getBytes ()),
1517+ canonicalReceipts .get (0 ).getTransactionHash ());
1518+ }
1519+
1520+ /**
1521+ * Regression: eth_getBlockByHash on an orphaned block hash must report gasUsed from the block's
1522+ * own transactions, never the canonical branch's gas indexed by height.
1523+ */
1524+ @ Test
1525+ public void testEthGetBlockByForkedHashGasUsed () throws Exception {
1526+ // height stays below the test head (blockCapsule1 @ 10_000) so it never becomes "latest"
1527+ long height = 5_001L ;
1528+
1529+ BlockCapsule canonicalBlock = new BlockCapsule (height , Sha256Hash .wrap (ByteString .copyFrom (
1530+ ByteArray .fromHexString (
1531+ "3333333333333333333333333333333333333333333333333333333333333333" ))), 1000000 ,
1532+ ByteString .copyFromUtf8 ("canonicalWitness2" ));
1533+ TransferContract canonicalTransfer = TransferContract .newBuilder ().setAmount (11L )
1534+ .setOwnerAddress (ByteString .copyFrom ("0x0000000000000000000" .getBytes ()))
1535+ .setToAddress (ByteString .copyFrom (ByteArray .fromHexString (
1536+ Wallet .getAddressPreFixString () + "A389132D6639FBDA4FBC8B659264E6B7C90DB086" )))
1537+ .build ();
1538+ TransactionCapsule canonicalTx =
1539+ new TransactionCapsule (canonicalTransfer , ContractType .TransferContract );
1540+ canonicalTx .setBlockNum (height );
1541+ canonicalBlock .addTransaction (canonicalTx );
1542+
1543+ // stale (orphaned) block at the SAME height: different parent + different tx => different hash
1544+ BlockCapsule staleBlock = new BlockCapsule (height , Sha256Hash .wrap (ByteString .copyFrom (
1545+ ByteArray .fromHexString (
1546+ "4444444444444444444444444444444444444444444444444444444444444444" ))), 2000000 ,
1547+ ByteString .copyFromUtf8 ("staleWitness2" ));
1548+ TransferContract staleTransfer = TransferContract .newBuilder ().setAmount (99L )
1549+ .setOwnerAddress (ByteString .copyFrom ("0x0000000000000000000" .getBytes ()))
1550+ .setToAddress (ByteString .copyFrom (ByteArray .fromHexString (
1551+ Wallet .getAddressPreFixString () + "ED738B3A0FE390EAA71B768B6D02CDBD18FB207B" )))
1552+ .build ();
1553+ TransactionCapsule staleTx =
1554+ new TransactionCapsule (staleTransfer , ContractType .TransferContract );
1555+ staleTx .setBlockNum (height );
1556+ staleBlock .addTransaction (staleTx );
1557+
1558+ dbManager .getBlockStore ().put (canonicalBlock .getBlockId ().getBytes (), canonicalBlock );
1559+ dbManager .getBlockStore ().put (staleBlock .getBlockId ().getBytes (), staleBlock );
1560+ dbManager .getBlockIndexStore ().put (canonicalBlock .getBlockId ());
1561+
1562+ // canonical height-keyed receipts carry a distinctive non-zero energy usage
1563+ long canonicalEnergy = 777L ;
1564+ TransactionRetCapsule canonicalRet = new TransactionRetCapsule ();
1565+ canonicalBlock .getTransactions ().forEach (tx -> {
1566+ Protocol .TransactionInfo info = Protocol .TransactionInfo .newBuilder ()
1567+ .setId (ByteString .copyFrom (tx .getTransactionId ().getBytes ()))
1568+ .setBlockNumber (height )
1569+ .setBlockTimeStamp (canonicalBlock .getTimeStamp ())
1570+ .setReceipt (Protocol .ResourceReceipt .newBuilder ()
1571+ .setEnergyUsageTotal (canonicalEnergy ).build ())
1572+ .build ();
1573+ canonicalRet .addTransactionInfo (info );
1574+ });
1575+ dbManager .getTransactionRetStore ().put (ByteArray .fromLong (height ), canonicalRet );
1576+
1577+ // querying the stale hash must NOT report canonical-by-height gas (777); the stale block has
1578+ // no retrievable receipt of its own, so its gasUsed is 0 -- never the canonical branch's value
1579+ BlockResult staleResult =
1580+ tronJsonRpc .ethGetBlockByHash (staleBlock .getBlockId ().toString (), true );
1581+ Assert .assertNotNull (staleResult );
1582+ Assert .assertEquals ("0x0" , staleResult .getGasUsed ());
1583+ }
1584+
14441585 @ Test
14451586 public void testBuildTransactionTransfer () {
14461587 // End-to-end smoke test for the buildTransaction JSON-RPC path:
0 commit comments